-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwindow.cpp
More file actions
177 lines (166 loc) · 4.46 KB
/
window.cpp
File metadata and controls
177 lines (166 loc) · 4.46 KB
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
#include "window.h"
#include <SDL2/SDL.h>
#include "imgui/imgui.h"
#include <SDL2/SDL_syswm.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <commdlg.h>
#endif
Window::Window()
{
_sw = SDL_CreateWindow("XXL Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _width=1280, _height=720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
}
static int igMouseIndex(int sdl) {
switch (sdl) {
case SDL_BUTTON_LEFT: return 0;
case SDL_BUTTON_RIGHT: return 1;
case SDL_BUTTON_MIDDLE: return 2;
}
return -1;
}
static ImGuiKey SdlToImGuiKey(SDL_Keycode code)
{
switch (code) {
case SDLK_TAB: return ImGuiKey_Tab;
case SDLK_LEFT: return ImGuiKey_LeftArrow;
case SDLK_RIGHT: return ImGuiKey_RightArrow;
case SDLK_UP: return ImGuiKey_UpArrow;
case SDLK_DOWN: return ImGuiKey_DownArrow;
case SDLK_PAGEUP: return ImGuiKey_PageUp;
case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
case SDLK_HOME: return ImGuiKey_Home;
case SDLK_END: return ImGuiKey_End;
case SDLK_DELETE: return ImGuiKey_Delete;
case SDLK_BACKSPACE: return ImGuiKey_Backspace;
case SDLK_SPACE: return ImGuiKey_Space;
case SDLK_RETURN: return ImGuiKey_Enter;
case SDLK_ESCAPE: return ImGuiKey_Escape;
case SDLK_a: return ImGuiKey_A;
case SDLK_c: return ImGuiKey_C;
case SDLK_v: return ImGuiKey_V;
case SDLK_x: return ImGuiKey_X;
case SDLK_y: return ImGuiKey_Y;
case SDLK_z: return ImGuiKey_Z;
}
return ImGuiKey_None;
}
static void HandleImGui(const SDL_Event &event)
{
ImGuiIO &igio = ImGui::GetIO();
switch (event.type) {
case SDL_MOUSEMOTION:
igio.MousePos = ImVec2((float)event.motion.x, (float)event.motion.y);
break;
case SDL_MOUSEBUTTONDOWN: {
int x = igMouseIndex(event.button.button);
if (x != -1)
igio.MouseDown[x] = true;
break;
}
case SDL_MOUSEBUTTONUP: {
int x = igMouseIndex(event.button.button);
if (x != -1)
igio.MouseDown[x] = false;
break;
}
case SDL_MOUSEWHEEL:
igio.MouseWheel += event.wheel.y;
break;
case SDL_KEYDOWN:
case SDL_KEYUP: {
auto kmod = event.key.keysym.mod;
igio.AddKeyEvent(ImGuiKey_ModCtrl, kmod & KMOD_CTRL);
igio.AddKeyEvent(ImGuiKey_ModShift, kmod & KMOD_SHIFT);
igio.AddKeyEvent(ImGuiKey_ModAlt, kmod & KMOD_ALT);
igio.AddKeyEvent(SdlToImGuiKey(event.key.keysym.sym), event.type == SDL_KEYDOWN);
break;
}
case SDL_TEXTINPUT:
igio.AddInputCharactersUTF8(event.text.text);
break;
}
}
void * Window::getNativeWindow()
{
SDL_SysWMinfo syswm;
SDL_VERSION(&syswm.version);
SDL_GetWindowWMInfo(getSDLWindow(), &syswm);
HWND hWindow = syswm.info.win.window;
return (void*)hWindow;
}
void Window::handle()
{
SDL_Event event;
bool imguion = ImGui::GetCurrentContext() != nullptr;
for (int i = 0; i < SDL_NUM_SCANCODES; i++)
_keyPressed[i] = false;
_mouseWheel = 0;
for (bool &b : _mousePressed)
b = false;
bool igWantsMouse = false, igWantsKeyboard = false;
if (imguion) {
ImGuiIO &io = ImGui::GetIO();
igWantsMouse = io.WantCaptureMouse;
igWantsKeyboard = io.WantCaptureKeyboard;
}
SDL_Keymod kmod = SDL_GetModState();
_ctrlPressed = kmod & KMOD_CTRL;
_shiftPressed = kmod & KMOD_SHIFT;
_altPressed = kmod & KMOD_ALT;
while (SDL_PollEvent(&event)) {
if (imguion)
HandleImGui(event);
switch (event.type) {
case SDL_QUIT:
_quit = true;
break;
case SDL_KEYDOWN:
if (igWantsKeyboard) break;
_keyDown[event.key.keysym.scancode] = true;
_keyPressed[event.key.keysym.scancode] = true;
break;
case SDL_KEYUP:
_keyDown[event.key.keysym.scancode] = false;
break;
case SDL_WINDOWEVENT:
//printf("window event!!!\n");
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
_width = event.window.data1;
_height = event.window.data2;
//printf("Resized! %i %i\n", _width, _height);
//if (g_gfxRenderer)
// g_gfxRenderer->Reset();
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (!igWantsMouse)
_mouseDown[event.button.button] = true;
break;
case SDL_MOUSEBUTTONUP:
_mouseDown[event.button.button] = false;
if (!igWantsMouse)
_mousePressed[event.button.button] = true;
break;
case SDL_MOUSEMOTION:
_mouseX = event.motion.x;
_mouseY = event.motion.y;
break;
case SDL_MOUSEWHEEL:
if (!igWantsMouse)
_mouseWheel += event.wheel.y;
//printf("mouse wheel %i\n", g_mouseWheel);
break;
}
}
// Pause when the window is minimized, until it is visible again.
while (SDL_GetWindowFlags(_sw) & SDL_WINDOW_MINIMIZED) {
SDL_WaitEvent(&event);
if (event.type == SDL_QUIT) {
_quit = true;
break;
}
}
}