-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow.lua
More file actions
413 lines (362 loc) · 10.2 KB
/
Copy pathwindow.lua
File metadata and controls
413 lines (362 loc) · 10.2 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
local ffi = require("ffi")
local glfw = require("ffi.ffi_glfw")
local _window = nil
local scroll_delta_x, scroll_delta_y = 0, 0 -- frame offset deltas
local M = {}
function M.create(config)
-- keep references to ffi callbacks so they aren't garbage collected
local CB_KEEP = {}
-- Safety calling
local function make_callback(ctype, func, ret_default)
local function wrapper(...)
local ok, res = xpcall(func, debug.traceback, ...)
if not ok then
io.stderr:write("\n[ffi-callback error]\n", res, "\n\n")
return ret_default -- nil for void, 0 for int-returners
end
return res
end
local cb = ffi.cast(ctype, wrapper)
CB_KEEP[#CB_KEEP+1] = cb
return cb
end
glfw.SetErrorCallback(
make_callback(
"GLFWerrorfun",
function(err, desc)
print(ffi.string(desc))
end
)
)
if (glfw.Init() == 0) then
error("glfw.Init() failed")
end
-- Configuration
local x = config and tonumber(config.x) or 0
local y = config and tonumber(config.y) or 0
local width = config and tonumber(config.width) or 800
local height = config and tonumber(config.height) or 600
local fullscreen = config and config.fullscreen == true or false
local monitor = glfw.GetPrimaryMonitor()
local mode = glfw.GetVideoMode(monitor)
glfw.WindowHint(glfw.CLIENT_API, glfw.NO_API)
glfw.WindowHint(glfw.DOUBLEBUFFER, 1)
glfw.WindowHint(glfw.RED_BITS, mode.redBits)
glfw.WindowHint(glfw.GREEN_BITS, mode.greenBits)
glfw.WindowHint(glfw.BLUE_BITS, mode.blueBits)
glfw.WindowHint(glfw.ALPHA_BITS, glfw.DONT_CARE)
glfw.WindowHint(glfw.DEPTH_BITS, glfw.DONT_CARE)
glfw.WindowHint(glfw.STENCIL_BITS, 8)
glfw.WindowHint(glfw.REFRESH_RATE, mode.refreshRate)
if (ffi.os == "OSX") then
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 2)
glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1)
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
end
local wnd = nil
if fullscreen then
wnd = glfw.CreateWindow(width, height, "", monitor, nil)
else
wnd = glfw.CreateWindow(width, height, "", nil, nil)
glfw.SetWindowPos(wnd, (mode.width - width) / 2, (mode.height - height) / 2)
end
if wnd == nil then
glfw.Terminate()
error("Glfw window creation failed")
end
_window = wnd
M.callback_send("window_creation", wnd, width, height) -- If using OpenGL instead of BGFX remember to call glfw.MakeContextCurrent(wnd) here
glfw.SetWindowPosCallback(
wnd,
make_callback(
"GLFWwindowposfun",
function(_, x, y)
M.callback_send("window_position", x, y)
end
)
)
glfw.SetWindowSizeCallback(
wnd,
make_callback(
"GLFWwindowsizefun",
function(_, w, h)
M.callback_send("window_size", w, h)
end
)
)
glfw.SetWindowCloseCallback(
wnd,
make_callback(
"GLFWwindowclosefun",
function(_)
M.callback_send("window_close")
end
)
)
glfw.SetWindowRefreshCallback(
wnd,
make_callback(
"GLFWwindowrefreshfun",
function(_)
M.callback_send("window_refresh")
end
)
)
glfw.SetWindowFocusCallback(
wnd,
make_callback(
"GLFWwindowfocusfun",
function(_, focused)
M.callback_send("window_focus", focused)
end
)
)
glfw.SetWindowIconifyCallback(
wnd,
make_callback(
"GLFWwindowiconifyfun",
function(_, iconified)
M.callback_send("window_iconify", iconified)
end
)
)
glfw.SetWindowMaximizeCallback(
wnd,
make_callback(
"GLFWwindowmaximizefun",
function(_, maximized)
M.callback_send("window_maximized", maximized)
end
)
)
glfw.SetFramebufferSizeCallback(
wnd,
make_callback(
"GLFWframebuffersizefun",
function(_, w, h)
M.callback_send("window_framebuffer_size", w, h)
end
)
)
glfw.SetWindowContentScaleCallback(
wnd,
make_callback(
"GLFWwindowcontentscalefun",
function(_, sx, sy)
M.callback_send("window_content_scale", sx, sy)
end
)
)
glfw.SetKeyCallback(
wnd,
make_callback(
"GLFWkeyfun",
function(_, key, sc, action, mods)
M.callback_send("window_set_key", key, sc, action, mods) -- https://www.glfw.org/docs/latest/group__keys.html
end
)
)
glfw.SetCharCallback(
wnd,
make_callback(
"GLFWcharfun",
function(_, codepoint)
M.callback_send("window_set_char", codepoint)
end
)
)
glfw.SetCharModsCallback(
wnd,
make_callback(
"GLFWcharmodsfun",
function(_, codepoint, mods)
M.callback_send("window_set_char_mods", codepoint, mods)
end
)
)
glfw.SetMouseButtonCallback(
wnd,
make_callback(
"GLFWmousebuttonfun",
function(_, button, action, mods)
M.callback_send("mouse_button", button, action, mods)
end
)
)
glfw.SetCursorPosCallback(
wnd,
make_callback(
"GLFWcursorposfun",
function(_, x, y)
M.callback_send("mouse_position", x, y)
end
)
)
glfw.SetCursorEnterCallback(
wnd,
make_callback(
"GLFWcursorenterfun",
function(_, entered)
M.callback_send("mouse_enter", entered)
end
)
)
glfw.SetScrollCallback(
wnd,
make_callback(
"GLFWscrollfun",
function(_, xoff, yoff)
scroll_delta_x = scroll_delta_x + xoff
scroll_delta_y = scroll_delta_y + yoff
M.callback_send("mouse_scroll", scroll_delta_x, scroll_delta_y, xoff, yoff)
end
)
)
glfw.SetDropCallback(
wnd,
make_callback(
"GLFWdropfun",
function(_, count, paths)
local t = {}
for i = 0, count - 1 do
table.insert(t, ffi.string(paths[i]))
end
M.callback_send("file_drop", t, count)
end
)
)
return wnd
end
function M.update(dt)
glfw.PollEvents()
M.callback_send("window_update", _window, dt)
scroll_delta_x = 0
scroll_delta_y = 0
end
-- Get or Set window title
function M.title(str)
if (str) then
glfw.SetWindowTitle(_window, str)
return str
end
return glfw.GetWindowTitle(_window)
end
-- Get or Set window width and height
function M.size(w, h)
local _w, _h = ffi.new("int[1]"), ffi.new("int[1]")
glfw.GetWindowSize(_window, _w, _h)
if (w or h) then
w = w or _w[0]
h = h or _h[0]
glfw.SetWindowSize(_window, w, h)
return w, h
end
return _w[0], _h[0]
end
-- Get or Set window position
function M.position(x, y)
local _x, _y = glfw.GetWindowPos(_window)
if (x or y) then
x = x or _x
y = y or _y
glfw.SetWindowPos(_window, x, y)
return x, y
end
return _x, _y
end
-- Get framebuffer width and height
function M.framebufferSize()
local width = ffi.new("int[1]")
local height = ffi.new("int[1]")
glfw.GetFramebufferSize(_window, width, height)
return width[0], height[0]
end
function M.show()
glfw.ShowWindow(_window)
M.callback_send("window_show", _window)
end
function M.hide()
M.callback_send("window_hide", _window)
glfw.HideWindow(_window)
end
function M.time()
return glfw.GetTime()
end
function M.shouldClose()
return glfw.WindowShouldClose(_window) == glfw.TRUE
end
function M.destroy()
if (_window ~= nil) then
M.callback_send("window_destroy", _window)
glfw.DestroyWindow(_window)
glfw.Terminate()
_window = nil
end
end
M.callbacks = {}
-- Create a callback that will execute lowest priority order first
function M.callback_register(name, func_or_userdata, prior)
if not (M.callbacks) then
return -- intentionally removed all callbacks
end
assert(func_or_userdata ~= nil)
if not (M.callbacks[name]) then
M.callbacks[name] = {}
end
if (type(func_or_userdata) == "function") then
M.callbacks[name][func_or_userdata] = {["functor"] = function(...)
func_or_userdata(...)
end, ["prior"] = prior or 0}
elseif (func_or_userdata[name]) then
M.callbacks[name][func_or_userdata] = {["functor"] = function(...)
func_or_userdata[name](func_or_userdata, ...)
end, ["prior"] = prior or 0}
end
end
function M.callback_remove(name, func_or_userdata)
if (M.callbacks and M.callbacks[name]) then
M.callbacks[name][func_or_userdata] = nil
end
end
local function order(t, a, b)
return t[a].prior > t[b].prior
end
local function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do
keys[#keys + 1] = k
end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(
keys,
function(a, b)
return order(t, a, b)
end
)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
-- Execute all registered functors for a specific event name
function M.callback_send(name, ...)
if (M.callbacks and M.callbacks[name]) then
for func_or_userdata, v in spairs(M.callbacks[name], order) do
if (v.functor) then
v.functor(...)
end
end
end
end
return M