-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpointer_lock.script
More file actions
78 lines (64 loc) · 2.04 KB
/
pointer_lock.script
File metadata and controls
78 lines (64 loc) · 2.04 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
local pointer = require("pointer_lock.pointer")
go.property("acquire_input_focus", true)
go.property("click_action_id", hash("touch"))
go.property("unlock_action_id", hash("key_esc"))
--- HTML5 only.
-- For web platforms, you cannot request mouse lock outside of “user interactions”,
-- so to get that to work we should use the html5.set_interaction_listener(callback)
-- function to request the mouse lock for web platforms.
local function lock_mouse_on_interaction(self)
if not pointer.locked then
window.set_mouse_lock(true)
end
end
function init(self)
if self.acquire_input_focus then
msg.post(".", "acquire_input_focus")
end
pointer.locked = window.get_mouse_lock()
if html5 then
html5.set_interaction_listener(lock_mouse_on_interaction)
end
end
function final(self)
if html5 then
html5.set_interaction_listener(nil)
end
end
function update(self, dt)
if html5 then
-- If user presses "esc" to exit pointer lock then web browsers don't fire "input" event.
-- That's why we have to check the pointer lock status in the update loop.
pointer.update_lock_state()
end
end
function on_message(self, message_id, message, sender)
end
function on_input(self, action_id, action)
if html5 then
--
-- HTML5
--
pointer.update_lock_state()
if pointer.locked and action_id == self.unlock_action_id and action.released then
window.set_mouse_lock(false)
return true
end
else
--
-- Desktop
--
if action_id == self.unlock_action_id and action.released then
local skip_events = pointer.locked
pointer.locked = false
window.set_mouse_lock(false)
return skip_events
elseif action_id == self.click_action_id and action.released then
if not pointer.locked then
pointer.locked = true
window.set_mouse_lock(true)
return true
end
end
end
end