File size: 9,537 Bytes
d4e5991 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | @tool
class_name StackLight
extends Node3D
const STEP: float = 0.048
const _TOP_MESH_INITIAL_Y_POS: float = 0.087
## Number of light segments in the stack (1-10).
var segments: int = 1:
set(value):
var new_value: int = clamp(value, 1, 8)
if new_value == segments:
return
segments = new_value
if is_inside_tree():
_data.set_segments(segments)
var current_child_count := _segments_container.get_child_count()
var difference := segments - current_child_count
if difference > 0:
_spawn_segments(difference)
elif difference < 0:
_remove_segments(-difference)
_init_segments()
# If segments were removed, mask the light value
var new_light_value := light_value & ((1 << segments) - 1)
if new_light_value != light_value:
light_value = new_light_value
_update_segment_visuals()
if _top_mesh:
_top_mesh.position = Vector3(0, _TOP_MESH_INITIAL_Y_POS + (STEP * (segments - 1)), 0)
notify_property_list_changed()
## Bitmask controlling which segments are lit (bit 0 = segment 1, etc.).
var light_value: int = 0:
set(value):
var new_val: int = value % (1 << segments)
if new_val == light_value:
return
light_value = new_val
_update_segment_visuals()
notify_property_list_changed()
## Enable communication with external PLC/control systems.
var enable_comms: bool = true
## Internal storage for the selected tag group name.
@export var tag_group_name: String
## The tag group for reading light values from external systems.
var _tag_groups: String:
set(value):
tag_group_name = value
_tag_groups = value
## The tag name for the light value in the selected tag group.[br]Datatype: [code]BYTE[/code] (8-bit)[br][br]Format varies by protocol:[br][b]EIP:[/b] CIP tag names[br][b]Modbus:[/b] prefix+number (e.g. [code]hr0[/code])[br][b]OPC UA:[/b] full NodeId (e.g. [code]ns=2;s=MyVariable[/code] or [code]ns=2;i=12345[/code]).
var tag_name: String = ""
var _segment_scene: PackedScene = load("res://src/StackLight/StackSegment.tscn")
var _data: StackLightData = load("res://src/StackLight/StackLightData.tres")
var _prev_scale: Vector3
var _top_mesh_initial_y_pos: float
var _segment_initial_y_pos: float
var _tag := OIPCommsTag.new()
@onready var _segments_container: Node3D = get_node("Mid/Segments")
@onready var _top_mesh: MeshInstance3D = get_node("Mid/Top")
@onready var _bottom_mesh: MeshInstance3D = get_node("Bottom")
@onready var _mid_mesh: MeshInstance3D = get_node("Mid")
func _get(property: StringName) -> Variant:
if not is_inside_tree() or not _segments_container:
return null
for i in range(segments):
if property == "Light " + str(i + 1):
var segment := _segments_container.get_child(i)
return segment.segment_data
return null
func _validate_property(property: Dictionary) -> void:
if property.name == "tag_group_name":
property.usage = PROPERTY_USAGE_STORAGE
func _get_property_list() -> Array:
var properties: Array = []
properties.append({
"name": "light_value",
"type": TYPE_INT,
"usage": PROPERTY_USAGE_DEFAULT
})
properties.append({
"name": "segments",
"type": TYPE_INT,
"usage": PROPERTY_USAGE_DEFAULT
})
properties.append({
"name": "_data",
"type": TYPE_OBJECT,
"usage": PROPERTY_USAGE_NO_EDITOR
})
for i in range(segments - 1, -1, -1):
properties.append({
"name": "Light " + str(i + 1),
"class_name": "StackSegmentData",
"type": TYPE_OBJECT,
"usage": PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY
})
properties.append({
"name": "Communications",
"type": TYPE_NIL,
"usage": PROPERTY_USAGE_CATEGORY
})
properties.append({
"name": "enable_comms",
"type": TYPE_BOOL,
"usage": PROPERTY_USAGE_DEFAULT if OIPComms.get_enable_comms() else PROPERTY_USAGE_STORAGE
})
properties.append({
"name": "tag_groups",
"type": 0,
"usage": PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NO_INSTANCE_STATE if OIPComms.get_enable_comms() else PROPERTY_USAGE_NONE,
"hint": 0,
"hint_string": "tag_group_enum"
})
properties.append({
"name": "tag_name",
"type": TYPE_STRING,
"usage": PROPERTY_USAGE_DEFAULT if OIPComms.get_enable_comms() else PROPERTY_USAGE_STORAGE
})
return properties
func _enter_tree() -> void:
EditorInterface.simulation_started.connect(_on_simulation_started)
tag_group_name = OIPCommsSetup.default_tag_group(tag_group_name)
OIPCommsSetup.connect_comms(self, Callable(), _tag_group_polled)
func _ready() -> void:
set_notify_transform(true)
_data = _data.duplicate(true)
_data.init_segments(segments)
var current_child_count := _segments_container.get_child_count()
var difference := segments - current_child_count
if difference > 0:
_spawn_segments(difference)
elif difference < 0:
for i in range(-difference):
var child_to_remove_index := current_child_count - 1 - i
if child_to_remove_index >= 0:
var child_node := _segments_container.get_child(child_to_remove_index)
child_node.queue_free()
else:
pass
if _segments_container.get_child_count() > 0:
_segment_initial_y_pos = _segments_container.get_child(0).position.y
else:
_segment_initial_y_pos = 0.0
_fix_segment_positions()
_init_segments()
_top_mesh.position = Vector3(0, _TOP_MESH_INITIAL_Y_POS + (STEP * (max(0, segments - 1))), 0)
_update_segment_visuals()
_prev_scale = scale
_rescale()
func _exit_tree() -> void:
EditorInterface.simulation_started.disconnect(_on_simulation_started)
OIPCommsSetup.disconnect_comms(self, Callable(), _tag_group_polled)
if _segments_container:
for i in range(_segments_container.get_child_count()):
var segment_node := _segments_container.get_child(i)
if segment_node.active_state_changed.is_connected(_on_segment_state_changed):
segment_node.active_state_changed.disconnect(_on_segment_state_changed)
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSFORM_CHANGED:
if scale != _prev_scale:
_rescale()
_prev_scale = scale
func use() -> void:
light_value += 1
func _rescale() -> void:
scale = Vector3(scale.x, scale.y, scale.x)
_bottom_mesh.scale = Vector3(1, 1 / scale.y, 1)
_mid_mesh.scale = Vector3(1, (1 / scale.y) * scale.x, 1)
func _init_segments() -> void:
for i in range(segments):
if i >= _segments_container.get_child_count():
printerr("Mismatch between segments count and child nodes during InitSegments")
break
var segment_node := _segments_container.get_child(i)
segment_node.segment_data = _data.segment_datas[i]
segment_node.index = i
if not segment_node.active_state_changed.is_connected(_on_segment_state_changed):
segment_node.active_state_changed.connect(_on_segment_state_changed)
_increase_collision_shape()
func _spawn_segments(count: int) -> void:
if not is_inside_tree():
return
var start_index := _segments_container.get_child_count()
for i in range(count):
var segment := _segment_scene.instantiate() as Node3D
_segments_container.add_child(segment, true)
var current_index := start_index + i
segment.index = current_index
segment.position = Vector3(0, _segment_initial_y_pos + (STEP * current_index), 0)
if current_index < _data.segment_datas.size():
segment.segment_data = _data.segment_datas[current_index]
else:
printerr("Not enough data segments available when spawning segment %d" % current_index)
segment.active_state_changed.connect(_on_segment_state_changed)
func _remove_segments(count: int) -> void:
if not is_inside_tree():
return
var current_child_count := _segments_container.get_child_count()
for i in range(count):
var child_index := current_child_count - 1 - i
if child_index < 0:
break
var segment_node := _segments_container.get_child(child_index)
if segment_node.active_state_changed.is_connected(_on_segment_state_changed):
segment_node.active_state_changed.disconnect(_on_segment_state_changed)
segment_node.queue_free()
func _fix_segment_positions() -> void:
if not is_inside_tree():
return
for i in range(_segments_container.get_child_count()):
var segment_node := _segments_container.get_child(i)
segment_node.position = Vector3(0, _segment_initial_y_pos + (STEP * i), 0)
func _increase_collision_shape() -> void:
var collision_shape := $StaticBody3D/CollisionShape3D
if collision_shape:
var new_scale_y := 1.45 + (0.3 * segments)
collision_shape.shape.size.y = new_scale_y
collision_shape.position.y = -0.148 + (0.16 * (segments - 1))
func _update_segment_visuals() -> void:
if not is_inside_tree():
return
for i in range(segments):
if i < _data.segment_datas.size():
var segment_data: StackSegmentData = _data.segment_datas[i]
var is_active := (light_value >> i) & 1 == 1
if segment_data.active != is_active:
segment_data.active = is_active
else:
if i < _segments_container.get_child_count():
var segment_node := _segments_container.get_child(i) as StackSegment
segment_node._set_active(false)
func _on_segment_state_changed(index: int, active: bool) -> void:
if index < 0 or index >= segments:
printerr("Received state change for invalid index: ", index)
return
var current_bit := (1 << index)
var newlight_value: int
if active:
newlight_value = light_value | current_bit
else:
newlight_value = light_value & ~current_bit
self.light_value = newlight_value
func _on_simulation_started() -> void:
if enable_comms:
_tag.register(tag_group_name, tag_name)
func _tag_group_polled(tag_group_name_param: String) -> void:
if not enable_comms or not _tag.matches_group(tag_group_name_param):
return
light_value = _tag.read_uint8()
|