summaryrefslogtreecommitdiffstats
path: root/tools/platform/window.h
blob: 654f0daab48aa844fed47b672e927347f08b8c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// window.h
#pragma once

#include "core/slang-basic.h"
#include "core/slang-func-ptr.h"
#include "platform-api.h"
#include "slang-com-ptr.h"

namespace platform
{

enum class KeyCode : uint32_t
{
    None = 0,
    Left = 0x25,
    Up = 0x26,
    Down = 0x28,
    Right = 0x27,
    Escape = 0x1B,
    Return = 0x0D,
    Space = 0x20,
    Shift = 0x10,
    Ctrl = 0x11,
    Alt = 0x12,
    Backspace = 0x08,
    Delete = 0x2E,
    Home = 0x24,
    End = 0x23,
    PageUp = 0x21,
    PageDown = 0x22,
    Insert = 0x2D,
    Tab = 0x09,
    A = 0x41,
    B = 0x42,
    C = 0x43,
    D = 0x44,
    E = 0x45,
    F = 0x46,
    G = 0x47,
    H = 0x48,
    I = 0x49,
    J = 0x4A,
    K = 0x4B,
    L = 0x4C,
    M = 0x4D,
    N = 0x4E,
    O = 0x4F,
    P = 0x50,
    Q = 0x51,
    R = 0x52,
    S = 0x53,
    T = 0x54,
    U = 0x55,
    V = 0x56,
    W = 0x57,
    X = 0x58,
    Y = 0x59,
    Z = 0x5A,
    Semicolon = 0xBA,
    Comma = 0xBC,
    Dot = 0xBE,
    Slash = 0xBF,
    Quote = 0xDE,
    LBracket = 0xDB,
    RBracket = 0xDD,
    Backslash = 0xDC,
    Minus = 0xBD,
    Plus = 0xBB,
    Tilde = 0xC0,
    Key0 = 0x30,
    Key1 = 0x31,
    Key2 = 0x32,
    Key3 = 0x33,
    Key4 = 0x34,
    Key5 = 0x35,
    Key6 = 0x36,
    Key7 = 0x37,
    Key8 = 0x38,
    Key9 = 0x39,
    F1 = 0x70,
    F2 = 0x71,
    F3 = 0x72,
    F4 = 0x73,
    F5 = 0x74,
    F6 = 0x75,
    F7 = 0x76,
    F8 = 0x77,
    F9 = 0x78,
    F10 = 0x79,
    F11 = 0x7A,
    F12 = 0x7B,
};

struct WindowHandle
{
    enum class Type
    {
        Unknown,
        Win32Handle,
        NSWindowHandle,
        XLibHandle,
    };
    Type type;
    intptr_t handleValues[2];
    static WindowHandle fromHwnd(void* hwnd)
    {
        WindowHandle handle = {};
        handle.type = WindowHandle::Type::Win32Handle;
        handle.handleValues[0] = (intptr_t)(hwnd);
        return handle;
    }
    static WindowHandle fromNSWindow(void* nswindow)
    {
        WindowHandle handle = {};
        handle.type = WindowHandle::Type::NSWindowHandle;
        handle.handleValues[0] = (intptr_t)(nswindow);
        return handle;
    }
    static WindowHandle fromXWindow(void* xdisplay, uint32_t xwindow)
    {
        WindowHandle handle = {};
        handle.type = WindowHandle::Type::XLibHandle;
        handle.handleValues[0] = (intptr_t)(xdisplay);
        handle.handleValues[1] = xwindow;
        return handle;
    }
    template<typename T>
    T convert()
    {
        T result;
        result.type = (decltype(result.type))type;
        result.handleValues[0] = handleValues[0];
        result.handleValues[1] = handleValues[1];
        return result;
    }
};

struct ButtonState
{
    enum Enum
    {
        None = 0,
        LeftButton = 1,
        RightButton = 2,
        MiddleButton = 4,
        Shift = 8,
        Control = 16,
        Alt = 32
    };
};

struct KeyEventArgs
{
    KeyCode key;
    wchar_t keyChar; // For KeyPress event
    ButtonState::Enum buttons;
    bool cancelEvent;
};

struct MouseEventArgs
{
    int x, y;
    int delta;
    ButtonState::Enum buttons;
};

struct Rect
{
    int x, y;
    int width, height;
};

enum class WindowStyle
{
    Default,
    FixedSize,
};

struct WindowDesc
{
    char const* title = nullptr;
    int width = 0;
    int height = 0;
    WindowStyle style = WindowStyle::Default;
};

class Window : public Slang::RefObject
{
public:
    struct Events
    {
        Slang::Action<> mainLoop;
        Slang::Action<> sizeChanged;
        Slang::Action<> focus;
        Slang::Action<> lostFocus;
        Slang::Action<KeyEventArgs&> keyDown;
        Slang::Action<KeyEventArgs&> keyUp;
        Slang::Action<KeyEventArgs&> keyPress;
        Slang::Action<MouseEventArgs> mouseMove;
        Slang::Action<MouseEventArgs> mouseUp;
        Slang::Action<MouseEventArgs> mouseDown;
        Slang::Action<MouseEventArgs> mouseWheel;
    };

    Events events;

    virtual void setClientSize(uint32_t width, uint32_t height) = 0;
    virtual Rect getClientRect() = 0;
    virtual void centerScreen() = 0;
    virtual void close() = 0;
    virtual bool getFocused() = 0;
    virtual bool getVisible() = 0;
    virtual WindowHandle getNativeHandle() = 0;
    virtual void setText(Slang::String text) = 0;
    virtual void show() = 0;
    virtual void hide() = 0;
    virtual int getCurrentDpi() = 0;
};

class Application
{
public:
    static SLANG_PLATFORM_API Window* createWindow(const WindowDesc& desc);
    static SLANG_PLATFORM_API void init();
    static SLANG_PLATFORM_API void run(Window* mainWindow, bool waitForEvents = false);
    static SLANG_PLATFORM_API void quit();
    static SLANG_PLATFORM_API void doEvents();
    static SLANG_PLATFORM_API void dispose();
};

} // namespace platform

#ifdef _WIN32

#ifdef _MSC_VER
#ifdef _DEBUG
#define GFX_DUMP_LEAK _CrtDumpMemoryLeaks();
#endif
#endif

#endif

#ifndef GFX_DUMP_LEAK
#define GFX_DUMP_LEAK
#endif

#define PLATFORM_UI_MAIN(APPLICATION_ENTRY)      \
    int exampleMain(int argc, char** argv)       \
    {                                            \
        platform::Application::init();           \
        auto rs = APPLICATION_ENTRY(argc, argv); \
        platform::Application::dispose();        \
        GFX_DUMP_LEAK                            \
        return rs;                               \
    }