-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgui.cpp
290 lines (235 loc) · 7.61 KB
/
gui.cpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "precomp.h"
#include "gui.h"
#include "logger.h"
#include "chrono.h"
#include "icon.h"
#include <lodepng/picopng.h>
#include <map>
namespace {
std::map<GLFWwindow*, gui::Handle*> handles;
void ErrorCallback(int error, const char* description)
{
logger::Error("UI %d: %s", error, description);
}
void WindowSizeCallback(GLFWwindow* window, int width, int height)
{
gui::Handle* handle = handles[window];
handle->width = width;
handle->height = height;
handle->sizeChangeCb(handle, width, height);
}
void DropCallback(GLFWwindow* window, int count, const char** paths)
{
gui::Handle* handle = handles[window];
if( count > 1 || count == 0 )
{
return;
}
handle->fileDropCb(handle, paths[0]);
}
void ToggleFullScreen(GLFWwindow* window)
{
gui::Handle* handle = handles[window];
if( gui::IsFullScreen(handle) )
{
glfwSetWindowMonitor( handle->window, nullptr, handle->posx, handle->posy, handle->backupWidth, handle->backupHeight, 0 );
}
else
{
// backup window position and window size
glfwGetWindowPos( handle->window, &handle->posx, &handle->posy );
glfwGetWindowSize( handle->window, &handle->backupWidth, &handle->backupHeight );
// get reolution of monitor
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// switch to full screen
glfwSetWindowMonitor( handle->window, monitor, 0, 0, mode->width, mode->height, 0 );
}
}
void ToggleSubtitle(GLFWwindow* window)
{
gui::Handle* handle = handles[window];
handle->subtitleCb(handle);
}
void MouseDoubleClick(GLFWwindow* window)
{
ToggleFullScreen(window);
}
void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
gui::Handle* handle = handles[window];
if( button == GLFW_MOUSE_BUTTON_LEFT )
{
if( action == GLFW_PRESS )
{
handle->mouseButtonPress = true;
handle->mouseButtonShift = mods & GLFW_MOD_SHIFT;
if( handle->mouseButtonShift )
{
const double seekPercent
= static_cast<double>(handle->posx) / static_cast<double>(handle->width);
logger::Debug("Seeking %f %%", seekPercent);
handle->seekCb(handle, seekPercent);
}
}
else if( action == GLFW_RELEASE )
{
handle->mouseButtonPress = false;
handle->mouseButtonShift = mods & GLFW_MOD_SHIFT;
// double click handling
if( handle->mouseReleaseTimeUs == 0 )
{
handle->mouseReleaseTimeUs = chrono::Now();
}
else
{
uint64_t diffUs = chrono::Now() - handle->mouseReleaseTimeUs;
double diffMs = chrono::Milliseconds(diffUs);
if(diffMs >10.0 && diffMs < 200.0)
{
MouseDoubleClick(window);
}
handle->mouseReleaseTimeUs = 0;
}
}
}
}
void CursorPositionCallback(GLFWwindow* window, double xpos, double ypos)
{
gui::Handle* handle = handles[window];
handle->posx = static_cast<int32_t>(xpos);
handle->posy = static_cast<int32_t>(ypos);
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
gui::Handle* handle = handles[window];
if(action == GLFW_PRESS)
{
switch(key)
{
case GLFW_KEY_F:
ToggleFullScreen(window);
break;
case GLFW_KEY_S:
ToggleSubtitle(window);
break;
case GLFW_KEY_SPACE:
handle->pauseCb(handle);
break;
}
}
}
}
namespace gui
{
Result Init()
{
Result result;
if(!glfwInit() )
{
result = Result(false, std::string("gwfw failed to init"));
return result;
}
glfwSetErrorCallback(ErrorCallback);
return result;
}
Result Create(Handle*& handle)
{
Result result;
handle = new Handle();
return result;
}
Result OpenWindow(Handle* handle, uint32_t width, uint32_t height)
{
Result result;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
handle->window = glfwCreateWindow(width, height, "grumpy", nullptr, nullptr);
if(!handle->window)
{
result = Result(false, std::string("gwfw failed to open window"));
return result;
}
handle->width = width;
handle->height = height;
glfwSetKeyCallback(handle->window, KeyCallback);
glfwSetMouseButtonCallback(handle->window, MouseButtonCallback);
glfwSetCursorPosCallback(handle->window, CursorPositionCallback);
glfwSetDropCallback(handle->window, DropCallback);
glfwMakeContextCurrent(handle->window);
std::vector<unsigned char> imageBuffer;
unsigned long iconWidth;
unsigned long iconHeight;
int outcome = decodePNG(imageBuffer, iconWidth, iconHeight, icon_png, icon_png_len);
if( outcome == 0 )
{
GLFWimage image;
image.width = iconWidth;
image.height = iconHeight;
image.pixels = &imageBuffer[0];
glfwSetWindowIcon(handle->window, 1, &image);
}
handles[handle->window] = handle;
return result;
}
void SetTitle(Handle* handle, const char* title)
{
glfwSetWindowTitle(handle->window, title);
}
void SetWindowSizeChangeCallback(Handle* handle, WindowSizeChangeCb cb)
{
handle->sizeChangeCb = cb;
glfwSetWindowSizeCallback(handle->window, WindowSizeCallback);
}
void SetFileDropCallback(Handle* handle, FileDropCb cb)
{
handle->fileDropCb = cb;
}
void SetSeekCallback(Handle* handle, SeekCb seekCb)
{
handle->seekCb = seekCb;
}
void SetPauseCallback(Handle* handle, PauseCb pauseCb)
{
handle->pauseCb = pauseCb;
}
void SetSubtitleCallback(Handle* handle, SubtitleCb cb)
{
handle->subtitleCb = cb;
}
void SetWindowSize(Handle* handle, uint32_t width, uint32_t height)
{
glfwSetWindowSize(handle->window, width, height);
}
bool IsFullScreen(Handle* handle)
{
return glfwGetWindowMonitor(handle->window) != nullptr;
}
void SwapBuffers(Handle* handle)
{
glfwSwapBuffers(handle->window);
}
void ShowWindow(Handle* handle)
{
glfwShowWindow(handle->window);
}
void Hideindow(Handle* handle)
{
glfwHideWindow(handle->window);
}
bool ShouldClose(Handle* handle)
{
return glfwGetKey(handle->window, GLFW_KEY_ESCAPE) || glfwWindowShouldClose(handle->window);
}
void PollEvents()
{
glfwPollEvents();
}
void Destroy()
{
glfwTerminate();
}
}