-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosgui_impl_glfw.cpp
More file actions
247 lines (221 loc) · 10.5 KB
/
Copy pathosgui_impl_glfw.cpp
File metadata and controls
247 lines (221 loc) · 10.5 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
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
#include "osgui_impl_glfw.h"
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <string.h>
static GLFWwindow* g_Window = 0;
static double g_Time = 0.0;
static OG_GlfwFontBuilder g_FontBuilder = 0;
static void* g_FontUser = 0;
static float g_Dpi = 1.0f;
static bool g_InstalledCallbacks = false;
static unsigned char* g_OwnedFontPixels = 0;
static GLFWkeyfun g_PrevKey = 0;
static GLFWcharfun g_PrevChar = 0;
static GLFWscrollfun g_PrevScroll = 0;
static GLFWmousebuttonfun g_PrevMouseButton = 0;
static GLFWcursorposfun g_PrevCursorPos = 0;
static GLFWwindowfocusfun g_PrevWindowFocus = 0;
static og::Key OG_ImplGlfw_MapKey(int key) {
if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
return (og::Key)((int)og::Key_A + key - GLFW_KEY_A);
if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
return (og::Key)((int)og::Key_0 + key - GLFW_KEY_0);
if (key >= GLFW_KEY_F1 && key <= GLFW_KEY_F12)
return (og::Key)((int)og::Key_F1 + key - GLFW_KEY_F1);
switch (key) {
case GLFW_KEY_TAB: return og::Key_Tab;
case GLFW_KEY_ENTER: case GLFW_KEY_KP_ENTER: return og::Key_Enter;
case GLFW_KEY_ESCAPE: return og::Key_Escape;
case GLFW_KEY_SPACE: return og::Key_Space;
case GLFW_KEY_BACKSPACE: return og::Key_Backspace;
case GLFW_KEY_DELETE: return og::Key_Delete;
case GLFW_KEY_INSERT: return og::Key_Insert;
case GLFW_KEY_LEFT: return og::Key_LeftArrow;
case GLFW_KEY_UP: return og::Key_UpArrow;
case GLFW_KEY_RIGHT: return og::Key_RightArrow;
case GLFW_KEY_DOWN: return og::Key_DownArrow;
case GLFW_KEY_PAGE_UP: return og::Key_PageUp;
case GLFW_KEY_PAGE_DOWN: return og::Key_PageDown;
case GLFW_KEY_HOME: return og::Key_Home;
case GLFW_KEY_END: return og::Key_End;
case GLFW_KEY_LEFT_SHIFT: case GLFW_KEY_RIGHT_SHIFT: return og::Key_Shift;
case GLFW_KEY_LEFT_CONTROL: case GLFW_KEY_RIGHT_CONTROL: return og::Key_Ctrl;
case GLFW_KEY_LEFT_ALT: case GLFW_KEY_RIGHT_ALT: return og::Key_Alt;
default: return og::Key_None;
}
}
static bool OG_ImplGlfw_ModifierStillDown(GLFWwindow* window, int key) {
if (key == GLFW_KEY_LEFT_SHIFT) return glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
if (key == GLFW_KEY_RIGHT_SHIFT) return glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
if (key == GLFW_KEY_LEFT_CONTROL) return glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS;
if (key == GLFW_KEY_RIGHT_CONTROL) return glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS;
if (key == GLFW_KEY_LEFT_ALT) return glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS;
if (key == GLFW_KEY_RIGHT_ALT) return glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS;
return false;
}
static void OG_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (og::GetCurrentContext()) {
og::Key mapped = OG_ImplGlfw_MapKey(key);
if (mapped != og::Key_None) {
bool down = action != GLFW_RELEASE;
if (!down && OG_ImplGlfw_ModifierStillDown(window, key)) down = true;
og::GetIO().AddKeyEvent(mapped, down);
}
}
if (g_PrevKey) g_PrevKey(window, key, scancode, action, mods);
}
static void OG_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int codepoint) {
if (og::GetCurrentContext()) og::GetIO().AddInputCharacter(codepoint);
if (g_PrevChar) g_PrevChar(window, codepoint);
}
static void OG_ImplGlfw_ScrollCallback(GLFWwindow* window, double x, double y) {
if (og::GetCurrentContext()) og::GetIO().AddMouseWheelEvent((float)x, (float)y);
if (g_PrevScroll) g_PrevScroll(window, x, y);
}
static void OG_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
if (og::GetCurrentContext() && button >= 0 && button < 5)
og::GetIO().AddMouseButtonEvent(button, action != GLFW_RELEASE);
if (g_PrevMouseButton) g_PrevMouseButton(window, button, action, mods);
}
static void OG_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) {
if (og::GetCurrentContext()) og::GetIO().AddMousePosEvent((float)x, (float)y);
if (g_PrevCursorPos) g_PrevCursorPos(window, x, y);
}
static void OG_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) {
if (og::GetCurrentContext()) og::GetIO().AddFocusEvent(focused != 0);
if (g_PrevWindowFocus) g_PrevWindowFocus(window, focused);
}
static const char* OG_ImplGlfw_GetClipboard(void*) {
if (!g_Window) return "";
const char* text = glfwGetClipboardString(g_Window);
return text ? text : "";
}
static void OG_ImplGlfw_SetClipboard(void*, const char* text) {
if (g_Window) glfwSetClipboardString(g_Window, text ? text : "");
}
void OG_ImplGlfw_SetFontBuilder(OG_GlfwFontBuilder builder, void* user) {
g_FontBuilder = builder;
g_FontUser = user;
}
bool OG_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks) {
if (!window || g_Window || !og::GetCurrentContext()) return false;
g_Window = window;
g_Time = glfwGetTime();
og::IO& io = og::GetIO();
io.get_clipboard_text = OG_ImplGlfw_GetClipboard;
io.set_clipboard_text = OG_ImplGlfw_SetClipboard;
io.clipboard_user_data = 0;
io.backend_platform_name = "osgui_impl_glfw";
io.backend_flags |= og::BackendFlags_HasClipboard;
io.app_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
if (install_callbacks) {
g_PrevKey = glfwSetKeyCallback(window, OG_ImplGlfw_KeyCallback);
g_PrevChar = glfwSetCharCallback(window, OG_ImplGlfw_CharCallback);
g_PrevScroll = glfwSetScrollCallback(window, OG_ImplGlfw_ScrollCallback);
g_PrevMouseButton = glfwSetMouseButtonCallback(window, OG_ImplGlfw_MouseButtonCallback);
g_PrevCursorPos = glfwSetCursorPosCallback(window, OG_ImplGlfw_CursorPosCallback);
g_PrevWindowFocus = glfwSetWindowFocusCallback(window, OG_ImplGlfw_WindowFocusCallback);
g_InstalledCallbacks = true;
}
return true;
}
void OG_ImplGlfw_Shutdown() {
if (g_Window && g_InstalledCallbacks) {
// Restore even null callbacks. Conditional restoration left dangling
// OSGui callbacks whenever the application originally had no handler.
glfwSetKeyCallback(g_Window, g_PrevKey);
glfwSetCharCallback(g_Window, g_PrevChar);
glfwSetScrollCallback(g_Window, g_PrevScroll);
glfwSetMouseButtonCallback(g_Window, g_PrevMouseButton);
glfwSetCursorPosCallback(g_Window, g_PrevCursorPos);
glfwSetWindowFocusCallback(g_Window, g_PrevWindowFocus);
}
unsigned char* owned_pixels = g_OwnedFontPixels;
if (og::GetCurrentContext()) {
og::IO& io = og::GetIO();
io.AddFocusEvent(false);
og::FontAtlas& atlas = og::GetFontAtlas();
if (owned_pixels && atlas.pixels == owned_pixels) {
atlas.pixels = 0;
atlas.width = atlas.height = 0;
atlas.line_height = atlas.ascent = 0.0f;
atlas.white_uv = og::Vec2();
atlas.tex_id = 0;
atlas.glyph_map.clear();
for (int i = 0; i < 128; ++i) atlas.glyph_valid[i] = false;
}
if (io.get_clipboard_text == OG_ImplGlfw_GetClipboard) io.get_clipboard_text = 0;
if (io.set_clipboard_text == OG_ImplGlfw_SetClipboard) io.set_clipboard_text = 0;
if (io.backend_platform_name && strcmp(io.backend_platform_name, "osgui_impl_glfw") == 0)
io.backend_platform_name = 0;
io.backend_flags &= ~og::BackendFlags_HasClipboard;
}
if (owned_pixels) free(owned_pixels);
g_OwnedFontPixels = 0;
g_Window = 0;
g_Time = 0.0;
g_Dpi = 1.0f;
g_InstalledCallbacks = false;
g_PrevKey = 0; g_PrevChar = 0; g_PrevScroll = 0;
g_PrevMouseButton = 0; g_PrevCursorPos = 0; g_PrevWindowFocus = 0;
}
static bool OG_ImplGlfw_RebuildFont(float dpi) {
if (!g_FontBuilder || !og::GetCurrentContext()) return false;
og::FontAtlas candidate;
candidate.pixels = 0;
candidate.width = candidate.height = 0;
candidate.line_height = candidate.ascent = 0.0f;
candidate.tex_id = 0;
for (int i = 0; i < 128; ++i) candidate.glyph_valid[i] = false;
bool built = g_FontBuilder(candidate, dpi, g_FontUser);
if (!built || !candidate.pixels || candidate.width <= 0 || candidate.height <= 0 || candidate.line_height <= 0.0f) {
if (candidate.pixels && candidate.pixels != g_OwnedFontPixels) free(candidate.pixels);
return false;
}
og::FontAtlas& atlas = og::GetFontAtlas();
if (g_OwnedFontPixels && g_OwnedFontPixels != candidate.pixels) free(g_OwnedFontPixels);
g_OwnedFontPixels = candidate.pixels;
atlas = candidate;
atlas.tex_id = 0;
return true;
}
void OG_ImplGlfw_NewFrame() {
if (!g_Window || !og::GetCurrentContext()) return;
og::IO& io = og::GetIO();
const bool focused = glfwGetWindowAttrib(g_Window, GLFW_FOCUSED) != 0;
if (io.app_focused != focused) io.AddFocusEvent(focused);
int width = 0, height = 0, framebuffer_width = 0, framebuffer_height = 0;
glfwGetWindowSize(g_Window, &width, &height);
glfwGetFramebufferSize(g_Window, &framebuffer_width, &framebuffer_height);
io.display_size = og::Vec2((float)width, (float)height);
io.framebuffer_scale = og::Vec2(width > 0 ? (float)framebuffer_width / width : 1.0f,
height > 0 ? (float)framebuffer_height / height : 1.0f);
float x_scale = 1.0f, y_scale = 1.0f;
glfwGetWindowContentScale(g_Window, &x_scale, &y_scale);
float dpi = x_scale > 0.0f ? x_scale : 1.0f;
if (dpi < 0.5f) dpi = 0.5f;
if (dpi > 3.0f) dpi = 3.0f;
if (dpi != g_Dpi || !og::GetFontAtlas().pixels) {
const bool needs_font = !og::GetFontAtlas().pixels;
const bool font_ready = g_FontBuilder ? OG_ImplGlfw_RebuildFont(dpi) : !needs_font;
if (font_ready) {
g_Dpi = dpi;
io.dpi_scale = dpi;
og::SetUIScale(dpi);
}
}
double now = glfwGetTime();
io.delta_time = g_Time > 0.0 ? (float)(now - g_Time) : 1.0f / 60.0f;
if (io.delta_time <= 0.0f) io.delta_time = 1.0f / 60.0f;
g_Time = now;
// Polling complements callbacks when callbacks are not installed and also
// repairs state after event loss while a window is being dragged.
double mouse_x = 0.0, mouse_y = 0.0;
glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
if (io.mouse_pos.x != (float)mouse_x || io.mouse_pos.y != (float)mouse_y)
io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
for (int button = 0; button < 5; ++button) {
bool down = glfwGetMouseButton(g_Window, button) == GLFW_PRESS;
if (io.mouse_down[button] != down) io.AddMouseButtonEvent(button, down);
}
}