diff --git a/ui/port_layer.gd b/ui/port_layer.gd index ac24393..10454f9 100644 --- a/ui/port_layer.gd +++ b/ui/port_layer.gd @@ -5,10 +5,15 @@ extends Control ## mode, dragging from an outlet lays a new pipe. ## Only the ports themselves take mouse input; everything else passes through. -const GRAB_RADIUS := 10.0 +## Screen pixels: the most a dot's grab area grows to, and the gap kept +## between the grab areas of two close dots. +const GRAB_RADIUS_MAX := MainEditing.LINK_DROP_RADIUS +const GRAB_GAP := 2.0 var main: Main -var _grab: Array = [] # [{pos, tid, eid}] outlets that can start a pipe +var _grab: Array = [] # [{pos, tid, eid, reach}] outlets that can start a pipe (reach: grab radius, world units) +## Where the dot under the mouse sits (null for none). +var _hot_pos: Variant = null var _linking := false var _ghost_from := Vector2.ZERO var _ghost_to := Vector2.ZERO @@ -17,6 +22,7 @@ var _drawn_rev := -1 var _drawn_hover := -1 var _drawn_mode := -1 var _drawn_far := false +var _drawn_zoom := 0.0 ## [stack index, rect] of each visible card, for hiding ports under other cards. var _stack: Array = [] @@ -34,8 +40,9 @@ func _process(_delta: float) -> void: return var hover := _hovered_grab() var far := main.board.cards.far - if _linking or main.board.geometry_rev != _drawn_rev or hover != _drawn_hover or main.mode != _drawn_mode or far != _drawn_far: + if _linking or main.board.geometry_rev != _drawn_rev or hover != _drawn_hover or main.mode != _drawn_mode or far != _drawn_far or main.board.zoom != _drawn_zoom: _drawn_far = far + _drawn_zoom = main.board.zoom _drawn_rev = main.board.geometry_rev _drawn_hover = hover _drawn_mode = main.mode @@ -48,7 +55,7 @@ func _hovered_grab() -> int: return -1 var mouse := get_local_mouse_position() for i in _grab.size(): - if mouse.distance_to(_grab[i]["pos"]) <= GRAB_RADIUS: + if mouse.distance_to(_grab[i]["pos"]) <= _grab[i]["reach"]: return i return -1 @@ -59,12 +66,15 @@ func _has_point(p: Vector2) -> bool: if main == null or main.mode != Main.Mode.BUILD: return false for g in _grab: - if p.distance_to(g["pos"]) <= GRAB_RADIUS: + if p.distance_to(g["pos"]) <= g["reach"]: return true return false func _draw() -> void: + # The dot under the mouse (found with last frame's grab areas). + var hover_index := _hovered_grab() + _hot_pos = _grab[hover_index]["pos"] if hover_index >= 0 else null _grab.clear() # Zoomed far out (BoardView.FAR_ZOOM) the ports would be specks. if main.board.cards.far and not _linking: @@ -101,7 +111,7 @@ func _draw() -> void: if on and main.is_entry_lit(t.id, e.id): fill = Style.ACCENT ring = Style.ACCENT_DARK - if build and pos.distance_to(mouse) <= GRAB_RADIUS and not _linking: + if build and _hot_pos == pos and not _linking: fill = Style.ACCENT s = 1.2 _port(pos, fill, ring, s) @@ -126,10 +136,33 @@ func _draw() -> void: _port(Vector2(p), Style.ACCENT, Style.ACCENT_DARK, 1.0) else: _port(Vector2(p), Style.THREAD, Style.THREAD_DARK, 1.0) + _size_grab_areas() if _linking: _draw_ghost(PipeLayer.curve(_ghost_from, _ghost_to, _ghost_down)) +## Each dot's grab radius: up to GRAB_RADIUS_MAX on screen, less where another +## dot is near, so neighbouring areas stay GRAB_GAP screen pixels apart. +func _size_grab_areas() -> void: + var zoom := main.board.zoom + var cell := 2.0 * GRAB_RADIUS_MAX / zoom # only dots this close can matter + var buckets := {} + for g: Dictionary in _grab: + var key := Vector2i((g["pos"] as Vector2) / cell) + if not buckets.has(key): + buckets[key] = [] + (buckets[key] as Array).append(g) + for g: Dictionary in _grab: + var key := Vector2i((g["pos"] as Vector2) / cell) + var nearest := cell + for dx in range(-1, 2): + for dy in range(-1, 2): + for other: Dictionary in buckets.get(key + Vector2i(dx, dy), []): + if not is_same(other, g): + nearest = minf(nearest, (g["pos"] as Vector2).distance_to(other["pos"])) + g["reach"] = clampf(nearest / 2.0 - GRAB_GAP / 2.0 / zoom, 4.0 / zoom, GRAB_RADIUS_MAX / zoom) + + ## The port on a card's bottom edge for card threads (CardThreads): in Build ## always (drag from it to add one), in Play only when the card has some. func _outlet_port(t: RollTable, card: TableCard, build: bool, mouse: Vector2, below: int) -> void: @@ -144,7 +177,7 @@ func _outlet_port(t: RollTable, card: TableCard, build: bool, mouse: Vector2, be if main.lit_pipes.has(Roller.pipe_key(t.id, RollTable.OUTLET_ID, to)): fill = Style.ACCENT ring = Style.ACCENT_DARK - if build and pos.distance_to(mouse) <= GRAB_RADIUS and not _linking: + if build and _hot_pos == pos and not _linking: fill = Style.ACCENT s = 1.2 _port(pos, fill, ring, s) @@ -192,7 +225,7 @@ func _gui_input(ev: InputEvent) -> void: if mb and mb.button_index == MOUSE_BUTTON_LEFT: if mb.pressed: for g in _grab: - if mb.position.distance_to(g["pos"]) <= GRAB_RADIUS: + if mb.position.distance_to(g["pos"]) <= g["reach"]: _linking = true _ghost_from = g["pos"] _ghost_down = g["eid"] == RollTable.OUTLET_ID