Legal docs draft and Client refactor
This commit is contained in:
@@ -0,0 +1,366 @@
|
||||
class_name BoardInputHandler
|
||||
extends Node
|
||||
|
||||
# ── Signals ───────────────────────────────────────────────────────────────────
|
||||
|
||||
signal board_clicked(screen_pos: Vector2) # tap/click on board (no drag)
|
||||
signal board_held(coord: Vector2i) # long press on tile (hold timer fired)
|
||||
signal board_double_clicked(screen_pos: Vector2) # double-click on unit
|
||||
signal board_orbit(delta: Vector2) # camera orbit delta
|
||||
signal board_pan(delta: Vector2, cam_distance: float) # camera pan delta
|
||||
signal board_zoom(amount: float) # scroll/pinch zoom
|
||||
signal hand_scroll(direction: int) # -1 = left, +1 = right
|
||||
signal deselect_pending() # left-click outside hand while card selected
|
||||
|
||||
# ── Touch / mouse state ───────────────────────────────────────────────────────
|
||||
|
||||
var last_mouse: Vector2 = Vector2.ZERO
|
||||
var drag_active: bool = false
|
||||
var press_was_on_board: bool = false
|
||||
var pan_active: bool = false
|
||||
var orbit_active: bool = false
|
||||
var touch_points: Dictionary = {}
|
||||
var pinch_dist: float = 0.0
|
||||
var was_multi_touch: bool = false
|
||||
var touch_drag_active: bool = false
|
||||
var touch_drag_total: float = 0.0
|
||||
var touch_start_pos: Vector2 = Vector2.ZERO
|
||||
var touch_cam_orbit: bool = false
|
||||
var touch_cam_pan: bool = false
|
||||
var touch_cam_zoom: bool = false
|
||||
var gesture_move_accum: float = 0.0
|
||||
var gesture_finger_count: int = 0
|
||||
|
||||
# ── Internal deselect state ───────────────────────────────────────────────────
|
||||
|
||||
var _deselect_pending: bool = false
|
||||
var _hand_scroll_time: float = 0.0
|
||||
|
||||
# ── References set by parent ──────────────────────────────────────────────────
|
||||
|
||||
# Timers
|
||||
var hold_timer: Timer = null
|
||||
var cam_gesture_timer: Timer = null
|
||||
|
||||
# UI reference — used to detect hand-area wheel scrolls / deselect guard
|
||||
var hand_area: PanelContainer = null
|
||||
|
||||
# Camera rig — used for pan/orbit basis calculations
|
||||
var camera_rig: Node3D = null
|
||||
|
||||
# Camera distance — kept in sync by camera_controller via property
|
||||
var cam_distance: float = 14.0
|
||||
|
||||
# ── Mode flags set by selection_controller ────────────────────────────────────
|
||||
|
||||
# true when a card is selected in hand
|
||||
var card_mode_active: bool = false
|
||||
# true when targeting for a card ability
|
||||
var ability_card_active: bool = false
|
||||
|
||||
# ── Held-coord (exposed so hold_timer signal handler in parent can read it) ───
|
||||
|
||||
var held_coord: Vector2i = Vector2i(-1, -1)
|
||||
|
||||
# ── Callbacks set by parent for raycast queries ───────────────────────────────
|
||||
# Assign Callables pointing at game_screen's _mouse_to_tile_coord and _mouse_to_unit_id.
|
||||
# Signature: func(screen_pos: Vector2) -> Vector2i
|
||||
var fn_mouse_to_tile_coord: Callable = Callable()
|
||||
# Signature: func(screen_pos: Vector2) -> String
|
||||
var fn_mouse_to_unit_id: Callable = Callable()
|
||||
|
||||
# ── Camera-button toggles (called by game_screen from button signals) ─────────
|
||||
|
||||
func on_cam_orbit_btn_toggled(pressed: bool) -> void:
|
||||
touch_cam_orbit = pressed
|
||||
if pressed:
|
||||
touch_cam_pan = false
|
||||
touch_cam_zoom = false
|
||||
|
||||
func on_cam_pan_btn_toggled(pressed: bool) -> void:
|
||||
touch_cam_pan = pressed
|
||||
if pressed:
|
||||
touch_cam_orbit = false
|
||||
touch_cam_zoom = false
|
||||
|
||||
func on_cam_zoom_btn_toggled(pressed: bool) -> void:
|
||||
touch_cam_zoom = pressed
|
||||
if pressed:
|
||||
touch_cam_orbit = false
|
||||
touch_cam_pan = false
|
||||
|
||||
# ── Gesture timer callback (called by cam_gesture_timer.timeout) ──────────────
|
||||
|
||||
func on_cam_gesture_timer_fired() -> void:
|
||||
if Settings.touch_camera_mode != "gesture":
|
||||
return
|
||||
match gesture_finger_count:
|
||||
1:
|
||||
touch_cam_orbit = true
|
||||
touch_cam_pan = false
|
||||
touch_cam_zoom = false
|
||||
2:
|
||||
touch_cam_zoom = true
|
||||
touch_cam_orbit = false
|
||||
touch_cam_pan = false
|
||||
3:
|
||||
touch_cam_pan = true
|
||||
touch_cam_orbit = false
|
||||
touch_cam_zoom = false
|
||||
# The user deliberately held — mark as drag so release won't fire a board click.
|
||||
touch_drag_active = true
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
|
||||
# ── Deactivate all touch camera modes ────────────────────────────────────────
|
||||
|
||||
func deactivate_touch_cam_modes() -> void:
|
||||
touch_cam_orbit = false
|
||||
touch_cam_pan = false
|
||||
touch_cam_zoom = false
|
||||
|
||||
# ── _input: intercepts every event first (handles deselect guard) ─────────────
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
var mb: InputEventMouseButton = event as InputEventMouseButton
|
||||
if mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed and not mb.double_click:
|
||||
var is_card_active: bool = card_mode_active or ability_card_active
|
||||
if is_card_active:
|
||||
if hand_area != null and not hand_area.get_global_rect().has_point(mb.position):
|
||||
_deselect_pending = true
|
||||
_do_deselect_if_pending.call_deferred()
|
||||
|
||||
# ── Deselect-if-pending (called deferred so board click can cancel it first) ──
|
||||
|
||||
func _do_deselect_if_pending() -> void:
|
||||
if _deselect_pending:
|
||||
_deselect_pending = false
|
||||
var is_card_active: bool = card_mode_active or ability_card_active
|
||||
if is_card_active:
|
||||
emit_signal("deselect_pending")
|
||||
|
||||
# ── _unhandled_input: board-level input, not consumed by UI ──────────────────
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
var mb: InputEventMouseButton = event as InputEventMouseButton
|
||||
# Left-click reaching here is a board click — cancel any pending deselect
|
||||
# so _do_deselect_if_pending won't wipe selection before the card is played.
|
||||
if mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed:
|
||||
_deselect_pending = false
|
||||
if mb.button_index == MOUSE_BUTTON_WHEEL_UP:
|
||||
if hand_area != null and hand_area.get_global_rect().has_point(mb.position):
|
||||
var now: float = Time.get_ticks_msec() / 1000.0
|
||||
if now - _hand_scroll_time >= 0.15:
|
||||
_hand_scroll_time = now
|
||||
hand_scroll.emit(-1)
|
||||
return
|
||||
board_zoom.emit(1.5)
|
||||
return
|
||||
elif mb.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
||||
if hand_area != null and hand_area.get_global_rect().has_point(mb.position):
|
||||
var now: float = Time.get_ticks_msec() / 1000.0
|
||||
if now - _hand_scroll_time >= 0.15:
|
||||
_hand_scroll_time = now
|
||||
hand_scroll.emit(1)
|
||||
return
|
||||
board_zoom.emit(-1.5)
|
||||
return
|
||||
elif mb.button_index == MOUSE_BUTTON_MIDDLE:
|
||||
pan_active = mb.pressed
|
||||
last_mouse = mb.position
|
||||
elif mb.button_index == MOUSE_BUTTON_RIGHT:
|
||||
orbit_active = mb.pressed
|
||||
last_mouse = mb.position
|
||||
elif mb.button_index == MOUSE_BUTTON_LEFT:
|
||||
if mb.pressed:
|
||||
# Double-click on a battlefield unit → show full card detail popup.
|
||||
if mb.double_click:
|
||||
var dbl_unit_id: String = ""
|
||||
if fn_mouse_to_unit_id.is_valid():
|
||||
dbl_unit_id = fn_mouse_to_unit_id.call(mb.position)
|
||||
if not dbl_unit_id.is_empty():
|
||||
board_double_clicked.emit(mb.position)
|
||||
return
|
||||
last_mouse = mb.position
|
||||
drag_active = false
|
||||
press_was_on_board = true # unhandled press = board area, not a UI card
|
||||
var coord: Vector2i = Vector2i(-1, -1)
|
||||
if fn_mouse_to_tile_coord.is_valid():
|
||||
coord = fn_mouse_to_tile_coord.call(mb.position)
|
||||
if coord.x >= 0:
|
||||
held_coord = coord
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.start()
|
||||
else:
|
||||
held_coord = Vector2i(-1, -1)
|
||||
else:
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
if not drag_active and not touch_drag_active and press_was_on_board:
|
||||
board_clicked.emit(mb.position)
|
||||
drag_active = false
|
||||
touch_drag_active = false
|
||||
touch_drag_total = 0.0
|
||||
press_was_on_board = false
|
||||
|
||||
elif event is InputEventMouseMotion:
|
||||
var mm: InputEventMouseMotion = event as InputEventMouseMotion
|
||||
var delta: Vector2 = mm.position - last_mouse
|
||||
last_mouse = mm.position
|
||||
# On some mobile browsers, pinch emits synthetic mouse-motion/left-drag
|
||||
# alongside touch/gesture events. Ignore mouse-orbit while touch is active.
|
||||
# Right-mouse drag orbits camera on desktop; left-click is reserved for board actions.
|
||||
if orbit_active and touch_points.is_empty() and not was_multi_touch:
|
||||
board_orbit.emit(delta)
|
||||
if pan_active:
|
||||
# Normalise sign convention: receiver applies right = basis.x * d.x, fwd = fwd_vec * d.y
|
||||
# Mouse: original used +delta.x right, -delta.y forward.
|
||||
board_pan.emit(Vector2(delta.x, -delta.y), cam_distance)
|
||||
|
||||
elif event is InputEventScreenTouch:
|
||||
var st: InputEventScreenTouch = event as InputEventScreenTouch
|
||||
if st.pressed:
|
||||
touch_points[st.index] = st.position
|
||||
if touch_points.size() == 1:
|
||||
# Single-touch start: arm tap + hold (touch right-click) behavior.
|
||||
touch_drag_active = false
|
||||
touch_drag_total = 0.0
|
||||
touch_start_pos = st.position
|
||||
press_was_on_board = true
|
||||
last_mouse = st.position
|
||||
var touch_coord: Vector2i = Vector2i(-1, -1)
|
||||
if fn_mouse_to_tile_coord.is_valid():
|
||||
touch_coord = fn_mouse_to_tile_coord.call(st.position)
|
||||
if touch_coord.x >= 0:
|
||||
held_coord = touch_coord
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.start()
|
||||
else:
|
||||
held_coord = Vector2i(-1, -1)
|
||||
elif touch_points.size() >= 2:
|
||||
pinch_dist = _touch_pair_dist()
|
||||
last_mouse = _touch_pair_mid()
|
||||
was_multi_touch = true
|
||||
held_coord = Vector2i(-1, -1)
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
# Gesture mode: (re)start timer whenever finger count changes.
|
||||
if Settings.touch_camera_mode == "gesture":
|
||||
gesture_finger_count = touch_points.size()
|
||||
gesture_move_accum = 0.0
|
||||
if is_instance_valid(cam_gesture_timer):
|
||||
cam_gesture_timer.start()
|
||||
else:
|
||||
var released_pos: Vector2 = st.position
|
||||
var had_single_touch: bool = (touch_points.size() == 1 and touch_points.has(st.index))
|
||||
touch_points.erase(st.index)
|
||||
if had_single_touch and not was_multi_touch and press_was_on_board:
|
||||
# Use start-to-end distance, not cumulative, to detect taps.
|
||||
# Cumulative distance mis-classifies natural finger jitter (A→B→A) as a drag.
|
||||
var tap_dist: float = released_pos.distance_to(touch_start_pos)
|
||||
if tap_dist < 50.0 and not touch_drag_active:
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
board_clicked.emit(released_pos)
|
||||
if touch_points.is_empty():
|
||||
was_multi_touch = false
|
||||
touch_drag_active = false
|
||||
touch_drag_total = 0.0
|
||||
touch_start_pos = Vector2.ZERO
|
||||
press_was_on_board = false
|
||||
held_coord = Vector2i(-1, -1)
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
if is_instance_valid(cam_gesture_timer):
|
||||
cam_gesture_timer.stop()
|
||||
# Gesture mode: deactivate camera mode when all fingers lift.
|
||||
if Settings.touch_camera_mode == "gesture":
|
||||
deactivate_touch_cam_modes()
|
||||
gesture_move_accum = 0.0
|
||||
gesture_finger_count = 0
|
||||
|
||||
elif event is InputEventScreenDrag:
|
||||
var sd: InputEventScreenDrag = event as InputEventScreenDrag
|
||||
touch_points[sd.index] = sd.position
|
||||
|
||||
# Cancel gesture timer if fingers move significantly before the hold activates.
|
||||
if Settings.touch_camera_mode == "gesture":
|
||||
gesture_move_accum += sd.relative.length()
|
||||
if gesture_move_accum > 50.0:
|
||||
if is_instance_valid(cam_gesture_timer):
|
||||
cam_gesture_timer.stop()
|
||||
|
||||
if touch_points.size() == 1 and not was_multi_touch:
|
||||
touch_drag_total += sd.relative.length()
|
||||
if touch_drag_total > 36.0:
|
||||
if is_instance_valid(hold_timer):
|
||||
hold_timer.stop()
|
||||
# Only suppress the tap click when a camera action will actually happen.
|
||||
# Without this, a normal swipe with no camera mode active silently eats the touch.
|
||||
if touch_cam_orbit or touch_cam_pan or touch_cam_zoom:
|
||||
touch_drag_active = true
|
||||
if touch_drag_active:
|
||||
if touch_cam_orbit:
|
||||
board_orbit.emit(sd.relative)
|
||||
elif touch_cam_pan:
|
||||
# Negate x to match original sign convention (pan right moves camera right).
|
||||
var pan_delta_touch: Vector2 = Vector2(-sd.relative.x, sd.relative.y)
|
||||
board_pan.emit(pan_delta_touch, cam_distance)
|
||||
elif touch_cam_zoom:
|
||||
board_zoom.emit(sd.relative.y * -0.05)
|
||||
elif Settings.touch_camera_mode == "gesture" and touch_points.size() == 3 and touch_cam_pan:
|
||||
# 3-finger pan (gesture mode): only process the leading finger to avoid triple-apply.
|
||||
var keys3: Array = touch_points.keys()
|
||||
keys3.sort()
|
||||
if sd.index == keys3[0]:
|
||||
var pan_delta3: Vector2 = Vector2(-sd.relative.x, sd.relative.y)
|
||||
board_pan.emit(pan_delta3, cam_distance)
|
||||
elif Settings.touch_camera_mode == "gesture" and touch_points.size() >= 2:
|
||||
var keys: Array = touch_points.keys()
|
||||
var pos_a: Vector2 = touch_points[keys[0]]
|
||||
var pos_b: Vector2 = touch_points[keys[1]]
|
||||
var mid: Vector2 = (pos_a + pos_b) * 0.5
|
||||
var nd: float = pos_a.distance_to(pos_b)
|
||||
board_zoom.emit((nd - pinch_dist) * 0.03)
|
||||
pinch_dist = nd
|
||||
var cdelta: Vector2 = mid - last_mouse
|
||||
last_mouse = mid
|
||||
var pan_delta2: Vector2 = Vector2(-cdelta.x, cdelta.y)
|
||||
board_pan.emit(pan_delta2, cam_distance)
|
||||
|
||||
# ── Browser high-level gesture events (Chromium / WebKit) ──────────────────
|
||||
# Browsers sometimes emit these *instead of* dual ScreenDrag events.
|
||||
# factor > 1.0 = spreading (zoom in), < 1.0 = pinching (zoom out).
|
||||
elif event is InputEventMagnifyGesture:
|
||||
if Settings.touch_camera_mode == "gesture":
|
||||
var mg: InputEventMagnifyGesture = event as InputEventMagnifyGesture
|
||||
board_zoom.emit((mg.factor - 1.0) * 8.0)
|
||||
|
||||
elif event is InputEventPanGesture:
|
||||
if Settings.touch_camera_mode != "gesture":
|
||||
return
|
||||
# On touch devices, treat pan-gesture as two-finger pan only.
|
||||
# Some browsers emit PanGesture during single-finger movement;
|
||||
# ignore that so single-finger remains orbit.
|
||||
if touch_points.size() == 1 and not was_multi_touch:
|
||||
return
|
||||
var pg: InputEventPanGesture = event as InputEventPanGesture
|
||||
var pan_delta_pg: Vector2 = Vector2(-pg.delta.x, pg.delta.y)
|
||||
board_pan.emit(pan_delta_pg, cam_distance)
|
||||
|
||||
# ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
func _touch_pair_dist() -> float:
|
||||
var keys: Array = touch_points.keys()
|
||||
if keys.size() < 2:
|
||||
return 0.0
|
||||
return (touch_points[keys[0]] as Vector2).distance_to(touch_points[keys[1]])
|
||||
|
||||
|
||||
func _touch_pair_mid() -> Vector2:
|
||||
var keys: Array = touch_points.keys()
|
||||
if keys.size() < 2:
|
||||
return Vector2.ZERO
|
||||
return ((touch_points[keys[0]] as Vector2) + (touch_points[keys[1]] as Vector2)) * 0.5
|
||||
@@ -0,0 +1 @@
|
||||
uid://cqs166tf6n0s0
|
||||
Reference in New Issue
Block a user