From 726aab33549af4dbc27e76a687feec8e5404f61b Mon Sep 17 00:00:00 2001 From: Olof Pettersson Date: Wed, 22 Oct 2025 17:55:33 +0200 Subject: [PATCH] OnClick version of sockets --- .../components/hardware/system_station.tscn | 1 + .../ship/computer/panels/frame/panel_frame.gd | 65 ++++++++++--------- scenes/ship/computer/wiring/socket.gd | 18 ++--- scenes/ship/computer/wiring/socket.tscn | 21 +++--- 4 files changed, 49 insertions(+), 56 deletions(-) diff --git a/scenes/ship/components/hardware/system_station.tscn b/scenes/ship/components/hardware/system_station.tscn index b45f793..939fe4c 100644 --- a/scenes/ship/components/hardware/system_station.tscn +++ b/scenes/ship/components/hardware/system_station.tscn @@ -8,6 +8,7 @@ [node name="Station" type="Node2D"] physics_interpolation_mode = 2 script = ExtResource("1_8usqu") +databank_installations = null mass = 1.0 [node name="InteractionArea" type="Area2D" parent="."] diff --git a/scenes/ship/computer/panels/frame/panel_frame.gd b/scenes/ship/computer/panels/frame/panel_frame.gd index 37b465e..53f7402 100644 --- a/scenes/ship/computer/panels/frame/panel_frame.gd +++ b/scenes/ship/computer/panels/frame/panel_frame.gd @@ -36,7 +36,9 @@ func _init() -> void: # It tells the layout system the smallest size this container can be. func _get_minimum_size() -> Vector2: # The minimum size is simply the grid dimensions multiplied by the pixel size. - return Vector2(columns * Constants.UI_GRID_SIZE, rows * Constants.UI_GRID_SIZE) + var front_size = Vector2(columns * Constants.UI_GRID_SIZE, rows * Constants.UI_GRID_SIZE) + var back_size = Vector2(front_size.x, front_size.y + databanks_container.size.y) if is_instance_valid(databanks_container) else front_size + return front_size if not wiring_mode else Vector2.ZERO func _notification(what: int) -> void: if what == NOTIFICATION_SORT_CHILDREN: @@ -116,6 +118,8 @@ func toggle_wiring_mode(): for panel in installed_panels: panel.set_wiring_mode(wiring_mode) + for socket in panel.all_sockets: + socket.socket_selected.connect(on_socket_pressed) if wiring_mode: _build_databanks(databanks) @@ -150,6 +154,7 @@ class InstalledDatabank: var socket = SocketScene.instantiate() inputs_container.add_child(socket) socket.initialize(socket_name, Socket.SocketType.INPUT) + all_sockets.append(socket) # Populate Output Sockets @@ -159,11 +164,28 @@ class InstalledDatabank: socket.initialize(socket_name, Socket.SocketType.OUTPUT) all_sockets.append(socket) +func on_socket_pressed(socket): + print(socket) + + if socket: + current_state = WiringState.DRAGGING_WIRE + + if not start_socket: + # start new wire + start_socket = socket + + # Add start point to wire points + active_wire_points.append(start_socket.get_global_rect().get_center() - get_global_position()) + elif start_socket and socket.socket_type != start_socket.socket_type: + end_socket = socket + _save_new_connection() + _reset_wiring_state() + func _build_databanks(dbs_to_install: Array[Databank]): if not is_instance_valid(databanks_container): databanks_container = GridContainer.new() databanks_container.columns = columns - databanks_container.add_theme_constant_override("h_separation", Constants.UI_GRID_SIZE * 3) + databanks_container.add_theme_constant_override("h_separation", Constants.UI_GRID_SIZE * 1) databanks_container.add_theme_constant_override("v_separation", Constants.UI_GRID_SIZE + 16) add_child(databanks_container) @@ -177,30 +199,18 @@ func _build_databanks(dbs_to_install: Array[Databank]): installed_databank.databank_ref = to_install databanks_container.add_child(installed_databank) installed_databank._populate_sockets() + for socket: Socket in installed_databank.all_sockets: + socket.socket_selected.connect(on_socket_pressed) func _gui_input(event: InputEvent): if event is InputEventMouseButton: - # --- Start or End a Wire --- - if event.button_index == MOUSE_BUTTON_LEFT: - if event.is_pressed(): - - var socket = _get_socket_at_pos(event.position) - if socket: - current_state = WiringState.DRAGGING_WIRE - if not start_socket: - # start new wire - start_socket = socket - # Add start point to wire points - active_wire_points.append(start_socket.icon.get_global_rect().get_center() - get_global_position()) - elif start_socket and socket.socket_type != start_socket.socket_type: - end_socket = socket - _save_new_connection() - _reset_wiring_state() - elif current_state == WiringState.DRAGGING_WIRE: - # Add intermediate point - active_wire_points.append(get_local_mouse_position()) - - elif event.button_index == MOUSE_BUTTON_RIGHT: + # --- Add wire point to grid --- + if event.button_index == MOUSE_BUTTON_LEFT and current_state == WiringState.DRAGGING_WIRE: + # Add intermediate point + active_wire_points.append(get_local_mouse_position()) + + # --- Remove last placed wire node --- + if event.button_index == MOUSE_BUTTON_RIGHT: # Pop Last Point active_wire_points.remove_at(active_wire_points.size() - 1) @@ -250,7 +260,7 @@ func _save_new_connection(): new_connection.output_socket_name = end_socket.socket_name - var end_pos = end_socket.icon.get_global_rect().get_center() - get_global_position() + var end_pos = end_socket.get_global_rect().get_center() - get_global_position() active_wire_points.append(end_pos) new_connection.path_points = active_wire_points @@ -265,10 +275,3 @@ func _reset_wiring_state(): end_socket = null active_wire_points.clear() queue_redraw() - -func _get_socket_at_pos(global_pos: Vector2) -> Socket: - for panel in installed_panels: - for socket in panel.all_sockets: - if is_instance_valid(socket) and socket.icon.get_global_rect().has_point(global_pos): - return socket - return null diff --git a/scenes/ship/computer/wiring/socket.gd b/scenes/ship/computer/wiring/socket.gd index c6c34dd..68fc0ed 100644 --- a/scenes/ship/computer/wiring/socket.gd +++ b/scenes/ship/computer/wiring/socket.gd @@ -1,16 +1,15 @@ # Socket.gd -extends Control +extends TextureButton class_name Socket +signal socket_selected(socket: Socket) + enum SocketType { INPUT, OUTPUT } enum SocketDataTypes { FLOAT, INT, STRING, EXEC } -@onready var icon: ColorRect = $Icon -@onready var label: Label = $Label - var socket_name: String var socket_type: SocketType @@ -19,12 +18,7 @@ func initialize(s_name: String, s_type: SocketType): socket_name = s_name socket_type = s_type - icon.tooltip_text = socket_name + tooltip_text = socket_name - if socket_type == SocketType.INPUT: - icon.color = Color.DODGER_BLUE - label.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT - else: # OUTPUT - icon.color = Color.ORANGE - # For output sockets, we can right-align the text to make it look neat. - label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT +func _pressed() -> void: + socket_selected.emit(self) diff --git a/scenes/ship/computer/wiring/socket.tscn b/scenes/ship/computer/wiring/socket.tscn index 1752454..820c91c 100644 --- a/scenes/ship/computer/wiring/socket.tscn +++ b/scenes/ship/computer/wiring/socket.tscn @@ -1,17 +1,12 @@ -[gd_scene load_steps=2 format=3 uid="uid://8glctqud5ioq"] +[gd_scene load_steps=3 format=3 uid="uid://8glctqud5ioq"] [ext_resource type="Script" uid="uid://d3d6tgy757f43" path="res://scenes/ship/computer/wiring/socket.gd" id="1_ubhg7"] -[node name="Socket" type="HBoxContainer"] -offset_right = 5.0 -offset_bottom = 23.0 +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_ubhg7"] +size = Vector2(24, 24) + +[node name="Socket" type="TextureButton"] +offset_right = 53.0 +offset_bottom = 48.0 +texture_normal = SubResource("PlaceholderTexture2D_ubhg7") script = ExtResource("1_ubhg7") - -[node name="Icon" type="ColorRect" parent="."] -custom_minimum_size = Vector2(48, 48) -layout_mode = 2 -mouse_filter = 1 -color = Color(0.2, 0.235294, 0.796078, 1) - -[node name="Label" type="Label" parent="."] -layout_mode = 2