diff --git a/lib/cyberarm_engine/game_state.rb b/lib/cyberarm_engine/game_state.rb index cce233d..ecb985a 100644 --- a/lib/cyberarm_engine/game_state.rb +++ b/lib/cyberarm_engine/game_state.rb @@ -53,6 +53,18 @@ module CyberarmEngine def gamepad_disconnected(index) end + def touch_began(touch) + end + + def touch_moved(touch) + end + + def touch_ended(touch) + end + + def touch_cancelled(touch) + end + def gain_focus end diff --git a/lib/cyberarm_engine/ui/gui_state.rb b/lib/cyberarm_engine/ui/gui_state.rb index 17ce0d1..d1e0520 100644 --- a/lib/cyberarm_engine/ui/gui_state.rb +++ b/lib/cyberarm_engine/ui/gui_state.rb @@ -33,6 +33,9 @@ module CyberarmEngine @pending_element_recalculate_requests = [] @hit_elements = [] + @touch_down_position = {} + @min_touch_drag_distance = 50 + @needs_repaint = false @menu = nil @@ -227,6 +230,31 @@ module CyberarmEngine @page&.update end + def touch_began(touch) + @touch_down_position[touch.id] = touch + end + + def touch_moved(touch) + # TODO: Support scrolling containers and dragging elements + end + + def touch_ended(touch) + touch_down_pos = @touch_down_position[touch.id] + distance = Vector.new(touch_down_pos.x, touch_down_pos.y).distance(Vector.new(touch.x, touch.y)) + + # emulate mouse click + if distance < @min_touch_drag_distance + button_down(Gosu::MS_LEFT) + button_up(Gosu::MS_LEFT) + end + + @touch_down_position.delete(touch.id) + end + + def touch_cancelled(touch) + @touch_down_position.delete(touch.id) + end + def button_down(id) super diff --git a/lib/cyberarm_engine/window.rb b/lib/cyberarm_engine/window.rb index 0809773..fd23f05 100644 --- a/lib/cyberarm_engine/window.rb +++ b/lib/cyberarm_engine/window.rb @@ -143,6 +143,22 @@ module CyberarmEngine current_state&.gamepad_disconnected(index) end + def touch_began(touch) + current_state&.touch_began(touch) + end + + def touch_moved(touch) + current_state&.touch_moved(touch) + end + + def touch_ended(touch) + current_state&.touch_ended(touch) + end + + def touch_cancelled(touch) + current_state&.touch_cancelled(touch) + end + def gain_focus @has_focus = true