Skip to content

Commit 1b8e3c1

Browse files
committed
Added support for resetting cursor state
Use this to completely reset the state of a cursor, for instance on window focus loss. Fixes #23
1 parent f8a00f6 commit 1b8e3c1

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

examples/cursor/controller.script

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ function init(self)
77
end
88

99
cursor.listen(msg.url("#cursor"), cursor.DRAG_START, function(message_id, message)
10-
print("drag")
10+
print("Drag start")
1111
-- prevent dragging of blue aliens
1212
if message.group == hash("blue") then
13-
print("prevent")
13+
print("Prevent drag on blue")
1414
return true
1515
end
1616
end)

in/cursor.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ M.DRAG = hash("drag")
88
M.DRAG_END = hash("drag_end")
99
M.DRAG_START = hash("drag_start")
1010
M.CLICKED = hash("clicked")
11+
M.RESET = hash("reset")
1112

1213
M.START_DRAGGING = hash("start_dragging")
1314

@@ -54,4 +55,9 @@ function M.final()
5455
listeners[url_to_key()] = nil
5556
end
5657

58+
function M.reset(cursor_url)
59+
cursor_url = cursor_url or msg.url()
60+
msg.post(cursor_url, M.RESET)
61+
end
62+
5763
return M

in/cursor.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,32 @@ You can listen for events generated by the cursor script and block execution of
5555
### cursor.listen(cursor_script_url, cursor_event, callback_fn)
5656
Set up a listener for a specific cursor event from a specific cursor script.
5757

58-
* **cursor_script_url** (url) - URL to the script you wish to listen to
58+
* **cursor_script_url** (url|string) - URL to the script you wish to listen to
5959
* **cursor_event** (hash) - The cursor message to listen for (see above)
6060
* **callback_fn** (function) - Function to call. Signature (`message_id`, `message`). Return true from the function to block execution of a `cursor.OVER`, `cursor.PRESSED` or `cursor.DRAG_START` event.
6161

6262
Example:
6363

6464
```
65-
local cursor = require "in.cursor"
66-
67-
cursor.listen(msg.url("#cursor"), cursor.DRAG_START, function(message_id, message)
68-
-- prevent dragging of blue aliens
69-
if message.group == hash("blue") then
70-
return true -- return true to block the event
71-
end
72-
end)
65+
local cursor = require "in.cursor"
66+
67+
cursor.listen("#cursor", cursor.DRAG_START, function(message_id, message)
68+
-- prevent dragging of blue aliens
69+
if message.group == hash("blue") then
70+
return true -- return true to block the event
71+
end
72+
end)
7373
```
74+
75+
## Resetting cursor state
76+
It is recommended to reset the cursor state when the screen is minimized and/or has lost focus. This can be done by sending a message to the cursor script:
77+
78+
```
79+
local cursor = require "in.cursor"
80+
81+
msg.post("#cursor", cursor.RESET)
82+
83+
-- or
84+
85+
cursor.reset("#cursor")
86+
```

in/cursor.script

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,7 @@ local function clear_over_state(self)
7474
self.state.over_group = nil
7575
end
7676

77-
function init(self)
78-
cursor.init()
79-
if self.acquire_input_focus then
80-
msg.post(".", "acquire_input_focus")
81-
end
82-
83-
self.mobile = is_mobile()
84-
77+
local function reset_state(self)
8578
self.collision_id = nil
8679
self.collision_group = nil
8780
self.pressed = false
@@ -96,6 +89,17 @@ function init(self)
9689
}
9790
end
9891

92+
function init(self)
93+
cursor.init()
94+
if self.acquire_input_focus then
95+
msg.post(".", "acquire_input_focus")
96+
end
97+
98+
self.mobile = is_mobile()
99+
100+
reset_state(self)
101+
end
102+
99103
function final(self)
100104
cursor.final()
101105
end
@@ -318,6 +322,8 @@ function on_message(self, message_id, message, sender)
318322
end
319323
elseif message_id == cursor.START_DRAGGING then
320324
start_dragging(self, message.id, message.mode or cursor.DRAG_MODE_FREE)
325+
elseif message_id == cursor.RESET then
326+
reset_state(self)
321327
elseif message_id == INPUT then
322328
handle_input(self, message.action_id, message.action)
323329
end

0 commit comments

Comments
 (0)