Skip to content

Commit

Permalink
Post cursor messages to attached game object as well
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Feb 16, 2018
1 parent 062392d commit 6569234
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
15 changes: 15 additions & 0 deletions examples/cursor/controller.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function on_message(self, message_id, message, sender)
if message_id == hash("cursor_over") then
print("Cursor over", message.id)
elseif message_id == hash("cursor_out") then
print("Cursor out", message.id)
elseif message_id == hash("pressed") then
print("Pressed", message.id)
elseif message_id == hash("released") then
print("Released", message.id)
elseif message_id == hash("drag_start") then
print("Drag started", message.id)
elseif message_id == hash("drag_end") then
print("Drag ended", message.id)
end
end
15 changes: 15 additions & 0 deletions examples/cursor/cursor.collection
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ embedded_instances {
" type: PROPERTY_TYPE_BOOLEAN\n"
" }\n"
"}\n"
"components {\n"
" id: \"controller\"\n"
" component: \"/examples/cursor/controller.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collisionobject\"\n"
" type: \"collisionobject\"\n"
Expand Down
5 changes: 3 additions & 2 deletions in/cursor.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ The script has the following properties:
* ```drag``` - (boolean) If the cursor should be able to drag game objects
* ```drag_threshold``` - (number) Distance the cursor has to move from a pressed object before it's considered dragged
* ```acquire_input_focus``` - (boolean) Check if the script should acquire input and handle input events itself
* ```notify_own_gameobject``` - (boolean) Check if cursor messages should be sent not only to the interacting game object but also to the game object this script is attached to.

You can let the cursor react to input in several ways:

* Enable the ```acquire_input_focus``` property. This will make the script automatically respond to input events
* Pass ```input``` messages. This will feed input events from an external source. This is useful if the app uses a camera solution or render script where screen coordinates doesn't translate to world coordinates and where conversion is required (using a screen_to_world function or similar).

The script will generate messages to game objects for the following situations:
The script will generate messages to game objects the cursor is interacting with and to the game object the cursor script is attached to for the following situations:

* ```cursor_over``` - The cursors moves over the game object
* ```cursor_out``` - The cursor moves out from the game object
* ```pressed``` - When pressing the game object
* ```released``` - When releasing the game object
* ```drag_start``` - When starting to drag the game object
* ``drag_end`` - When no longer dragging the game object
* ```drag_end``` - When no longer dragging the game object
22 changes: 14 additions & 8 deletions in/cursor.script
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go.property("action_id", hash("touch"))
go.property("drag", true)
go.property("drag_threshold", 20)
go.property("acquire_input_focus", false)
go.property("notify_own_gameobject", true)

local COLLISION_RESPONSE = hash("collision_response")
local INPUT = hash("input")
Expand Down Expand Up @@ -32,47 +33,52 @@ function init(self)
self.state = {
pressed = false,
dragging = false,
dragged_id = nil,
dragged_pos = nil,
over_id = nil,
}
end

local function notify_event(self, game_object_id, message_id)
msg.post(game_object_id, message_id)
if self.notify_own_gameobject then
msg.post(".", message_id, { id = game_object_id })
end
end

function update(self, dt)
local pos = go.get_position()

if self.released and self.state.pressed then
if self.state.dragging then
self.state.dragging = false
msg.post(self.state.pressed_id, "drag_end")
notify_event(self, self.state.pressed_id, "drag_end")
end
msg.post(self.state.pressed_id, "released")
notify_event(self, self.state.pressed_id, "released")
self.state.pressed = false
self.state.pressed_id = nil
end

if self.state.over_id and self.state.over_id ~= self.collision_id then
msg.post(self.state.over_id, "cursor_out")
notify_event(self, self.state.over_id, "cursor_out")
self.state.over_id = nil
end

if self.collision_id and self.state.over_id ~= self.collision_id then
self.state.over_id = self.collision_id
msg.post(self.state.over_id, "cursor_over")
notify_event(self, self.state.over_id, "cursor_over")
end

if self.pressed and not self.state.pressed and self.state.over_id then
self.state.pressed = true
self.state.pressed_id = self.state.over_id
self.state.pressed_pos = go.get_position()
msg.post(self.state.pressed_id, "pressed")
notify_event(self, self.state.pressed_id, "pressed")
end

if self.drag and self.state.pressed and not self.state.dragging then
local distance = vmath.length(pos - self.state.pressed_pos)
if distance >= self.drag_threshold then
self.state.dragging = true
msg.post(self.state.pressed_id, "drag_start")
notify_event(self, self.state.pressed_id, "drag_start")
end
end

Expand Down

0 comments on commit 6569234

Please sign in to comment.