Skip to content

Commit

Permalink
fix renamed hub invalidates port references
Browse files Browse the repository at this point in the history
fixes #163
  • Loading branch information
Novakasa committed Sep 30, 2023
1 parent 34de27c commit edb63f4
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

## [Unreleased]

### Added

### Fixed

- fixed Switches and Crossing motors invalidated port when renaming a hub (https://github.com/Novakasa/brickrail/issues/163)

### Changed

## [v1.0.0-alpha.5]

### Added
Expand Down
121 changes: 121 additions & 0 deletions brickrail-gui/brickrail-layouts/test-hub-rename.brl
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"version": "1.0.0",
"devices": {
"trains": [

],
"controllers": [
{
"name": "city-blue",
"devices": {
"0": "switch_motor",
"1": null,
"2": null,
"3": null
}
}
]
},
"layout": {
"tracks": [
{
"l_idx": 0,
"x_idx": 3,
"y_idx": 2,
"connections": {
"E": [
"center",
"right"
],
"W": [
"center"
]
},
"switches": {
"E": {
"motor1": {
"controller": "city-blue",
"port": 0,
"storage": {
"0": 100,
"1": 600,
"2": 0
}
}
}
}
},
{
"l_idx": 0,
"x_idx": 2,
"y_idx": 2,
"connections": {
"E": [
"center"
],
"W": [

]
}
},
{
"l_idx": 0,
"x_idx": 5,
"y_idx": 2,
"connections": {
"E": [

],
"W": [
"center"
]
}
},
{
"l_idx": 0,
"x_idx": 4,
"y_idx": 2,
"connections": {
"E": [
"center"
],
"W": [
"center"
]
}
},
{
"l_idx": 0,
"x_idx": 4,
"y_idx": 2,
"connections": {
"S": [
"left"
],
"W": [
"center"
]
}
},
{
"l_idx": 0,
"x_idx": 4,
"y_idx": 3,
"connections": {
"N": [
"left"
],
"E": [

]
}
}
],
"blocks": [

],
"trains": [

]
}
}
6 changes: 6 additions & 0 deletions brickrail-gui/devices/layout_devices/crossing_motor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ func set_hub(p_hub):
if hub != null:
hub.disconnect("responsiveness_changed", self, "_on_hub_responsiveness_changed")
hub.disconnect("runtime_data_received", self, "_on_hub_runtime_data_received")
hub.disconnect("name_changed", self, "_on_hub_name_changed")
hub = p_hub
if hub != null:
hub.connect("responsiveness_changed", self, "_on_hub_responsiveness_changed")
hub.connect("runtime_data_received", self, "_on_hub_runtime_data_received")
hub.connect("name_changed", self, "_on_hub_name_changed")

func set_position(p_position):
if position == p_position:
Expand All @@ -66,6 +68,10 @@ func _on_hub_runtime_data_received(_data):
func _on_hub_responsiveness_changed(_responsiveness):
pass

func _on_hub_name_changed(old_name, new_name):
assert(old_name == controllername)
controllername = new_name

func serialize():
var struct = {}
struct["type"] = device_type
Expand Down
6 changes: 6 additions & 0 deletions brickrail-gui/devices/layout_devices/switch_motor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func set_hub(p_hub):
if hub != null:
hub.disconnect("responsiveness_changed", self, "_on_hub_responsiveness_changed")
hub.disconnect("runtime_data_received", self, "_on_hub_runtime_data_received")
hub.disconnect("name_changed", self, "_on_hub_name_changed")
hub = p_hub
if hub != null:
hub.connect("responsiveness_changed", self, "_on_hub_responsiveness_changed")
hub.connect("runtime_data_received", self, "_on_hub_runtime_data_received")
hub.connect("name_changed", self, "_on_hub_name_changed")

func _on_hub_runtime_data_received(data):
if data[0] == DATA_SWITCH_CONFIRM:
Expand Down Expand Up @@ -92,6 +94,10 @@ func _on_hub_responsiveness_changed(value):
else:
set_unresponsive()

func _on_hub_name_changed(old_name, new_name):
assert(old_name == controllername)
controllername = new_name

func setup_on_hub():
# hub.rpc("add_switch", [port])
pass
Expand Down
2 changes: 1 addition & 1 deletion brickrail-gui/gui/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ tab_align = 0
use_hidden_tabs_for_min_size = true

[node name="Trains" type="ScrollContainer" parent="VBoxContainer2/HSplitContainer/VSplitContainer/VBoxContainer/TabContainer"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 5.0
Expand Down Expand Up @@ -139,6 +138,7 @@ size_flags_horizontal = 3
size_flags_vertical = 3

[node name="Settings" type="VBoxContainer" parent="VBoxContainer2/HSplitContainer/VSplitContainer/VBoxContainer/TabContainer"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 5.0
Expand Down
8 changes: 7 additions & 1 deletion brickrail-gui/layout/layout_devices/port_selector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ func setup_options():
controller_option.set_items(Devices.layout_controllers.keys(), Devices.layout_controllers.keys())

var port_option: Selector = $VBoxContainer/GridContainer/PortOption

if not controllername in Devices.layout_controllers.keys():
# the controller was probably renamed
# temp fix, on reselect valid controller should be updated
controllername = null

controller_option.select_meta(controllername)

if controllername == null:
port_option.set_items([], [])
return


var controller: LayoutController = Devices.layout_controllers[controllername]
var num_ports = len(controller.devices)
Expand Down

0 comments on commit edb63f4

Please sign in to comment.