class_name PortLayer extends Control ## Draws pipe ends on top of the cards: an inlet on each card and an outlet on ## each result. In Build 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 var main: Main var _grab: Array = [] # [{pos, tid, eid}] outlets that can start a pipe var _linking := false var _ghost_from := Vector2.ZERO var _ghost_to := Vector2.ZERO func _init() -> void: mouse_filter = MOUSE_FILTER_STOP mouse_default_cursor_shape = CURSOR_CROSS # Controls only draw inside their own rect, so cover the whole world. size = Vector2(BoardView.WORLD_SIZE, BoardView.WORLD_SIZE) func _process(_delta: float) -> void: queue_redraw() func _has_point(p: Vector2) -> bool: if _linking: return true if main == null or main.mode != Main.Mode.BUILD: return false for g in _grab: if p.distance_to(g["pos"]) <= GRAB_RADIUS: return true return false func _draw() -> void: _grab.clear() var build := main.mode == Main.Mode.BUILD var mouse := get_local_mouse_position() for t in main.data.tables: var card := main.card_for(t.id) if card == null or not card.is_inside_tree(): continue _port(card.inlet_point(), Style.COPPER, Style.COPPER_DARK, 1.0) for e in t.entries: if not card.rows.has(e.id): continue var a := card.anchor_for(e.id) if a.is_empty() or not a["visible"]: continue var on := not e.pipes.is_empty() if not build and not on: continue var pos: Vector2 = a["pos"] var fill := Style.COPPER if on else Style.CARD var ring := Style.COPPER_DARK var s := 1.0 if on and main.is_entry_lit(t.id, e.id): fill = Style.BRASS ring = Style.BRASS_DARK if build and pos.distance_to(mouse) <= GRAB_RADIUS and not _linking: fill = Style.BRASS s = 1.2 _port(pos, fill, ring, s) if build: _grab.append({"pos": pos, "tid": t.id, "eid": e.id}) if card.more_row != null: var m := card.anchor_for("") var on_hidden := card.hidden_has_pipes() if not m.is_empty() and (on_hidden or build): _port(m["pos"], Style.COPPER if on_hidden else Style.CARD, Style.COPPER_DARK, 1.0) if _linking: _draw_ghost(PipeLayer.curve(_ghost_from, _ghost_to)) func _port(pos: Vector2, fill: Color, ring: Color, s: float) -> void: draw_circle(pos, 8.0 * s, ring) draw_circle(pos, 5.0 * s, fill) func _draw_ghost(pts: PackedVector2Array) -> void: var carry := 0.0 for i in range(1, pts.size()): var a := pts[i - 1] var b := pts[i] var seg := a.distance_to(b) var d := carry while d < seg: draw_circle(a.lerp(b, d / seg), 2.5, Style.BRASS) d += 11.0 carry = d - seg func _gui_input(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton 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: _linking = true _ghost_from = g["pos"] _ghost_to = mb.position main.start_link(g["tid"], g["eid"]) accept_event() return elif _linking: _linking = false main.finish_link() accept_event() return var mm := ev as InputEventMouseMotion if mm and _linking: _ghost_to = mm.position main.update_link() accept_event()