File size: 5,833 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 | @tool
class_name PushButton
extends Node3D
## Text label displayed on the button.
@export var text: String = "STOP":
set(value):
text = value
if _text:
_text.text = text
## When true, button stays pressed until clicked again (toggle mode).
@export var toggle: bool = false:
set(value):
toggle = value
if not toggle:
pressed = false
## When true, output is inverted (false when pressed).
@export var normally_closed: bool = false:
set(value):
normally_closed = value
_update_output()
## Current pressed state of the button.
@export var pressed: bool = false:
set(value):
pressed = value
_update_output()
if not toggle and pressed:
_reset_pushbutton()
var tween := create_tween()
tween.tween_property(_button_mesh, "position", Vector3(0, 0, _button_pressed_z_pos), 0.035)
tween.tween_interval(0.2)
tween.tween_property(_button_mesh, "position", Vector3.ZERO, 0.02)
elif _button_mesh:
if pressed:
_button_mesh.position = Vector3(0, 0, _button_pressed_z_pos)
else:
_button_mesh.position = Vector3.ZERO
## Final output signal after applying normally_closed logic (read-only).
@export var output: bool = false:
set(value):
if _pushbutton_tag.is_ready() and value != output:
_pushbutton_tag.write_bit(value)
output = value
## When true, the button illuminates (lamp indicator).
@export var lamp: bool = false:
set(value):
lamp = value
if _button_mesh:
_ensure_unique_material()
var mat := _button_mesh.get_surface_override_material(0)
if value:
mat.emission_energy_multiplier = 1.0
else:
mat.emission_energy_multiplier = 0.0
## Color of the button and its lamp emission.
@export var button_color: Color = Color.RED:
set(value):
button_color = value
if _button_mesh:
_ensure_unique_material()
var mat := _button_mesh.get_surface_override_material(0)
mat.albedo_color = value
mat.emission = value
var _button_pressed_z_pos: float = -0.04
var _pushbutton_tag := OIPCommsTag.new()
var _lamp_tag := OIPCommsTag.new()
var _material_made_unique: bool = false
@onready var _text: Label3D = $Text
@onready var _button_mesh: MeshInstance3D = $Meshes/Button
@export_category("Communications")
## Enable communication with external PLC/control systems.
@export var enable_comms: bool = false
@export var pushbutton_tag_group_name: String
## The tag group for writing button output state.
@export_custom(0, "tag_group_enum") var pushbutton_tag_groups: String:
set(value):
pushbutton_tag_group_name = value
pushbutton_tag_groups = value
## The tag name for the button output in the selected tag group.[br]Datatype: [code]BOOL[/code][br][br]Format varies by protocol:[br][b]EIP:[/b] CIP tag names[br][b]Modbus:[/b] prefix+number (e.g. [code]co0[/code])[br][b]OPC UA:[/b] full NodeId (e.g. [code]ns=2;s=MyVariable[/code] or [code]ns=2;i=12345[/code]).
@export var pushbutton_tag_name: String = ""
@export var lamp_tag_group_name: String
## The tag group for reading lamp control signals.
@export_custom(0, "tag_group_enum") var lamp_tag_groups: String:
set(value):
lamp_tag_group_name = value
lamp_tag_groups = value
## The tag name for the lamp control in the selected tag group.[br]Datatype: [code]BOOL[/code][br][br]Format varies by protocol:[br][b]EIP:[/b] CIP tag names[br][b]Modbus:[/b] prefix+number (e.g. [code]co0[/code])[br][b]OPC UA:[/b] full NodeId (e.g. [code]ns=2;s=MyVariable[/code] or [code]ns=2;i=12345[/code]).
@export var lamp_tag_name: String = ""
func _validate_property(property: Dictionary) -> void:
if property.name == "output":
property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY
elif not OIPCommsSetup.validate_tag_property(property, "pushbutton_tag_group_name", "pushbutton_tag_groups", "pushbutton_tag_name"):
OIPCommsSetup.validate_tag_property(property, "lamp_tag_group_name", "lamp_tag_groups", "lamp_tag_name")
func _enter_tree() -> void:
pushbutton_tag_group_name = OIPCommsSetup.default_tag_group(pushbutton_tag_group_name)
lamp_tag_group_name = OIPCommsSetup.default_tag_group(lamp_tag_group_name)
EditorInterface.simulation_started.connect(_on_simulation_started)
OIPCommsSetup.connect_comms(self, _tag_group_initialized, _tag_group_polled)
func _ready() -> void:
_text.text = text
button_color = button_color
lamp = lamp
func _ensure_unique_material() -> void:
if not _button_mesh or _material_made_unique:
return
var current_override := _button_mesh.get_surface_override_material(0)
if current_override:
var unique_mat := current_override.duplicate() as Material
_button_mesh.set_surface_override_material(0, unique_mat)
else:
var mat: StandardMaterial3D = _button_mesh.mesh.surface_get_material(0).duplicate()
_button_mesh.set_surface_override_material(0, mat)
_material_made_unique = true
func _exit_tree() -> void:
EditorInterface.simulation_started.disconnect(_on_simulation_started)
OIPCommsSetup.disconnect_comms(self, _tag_group_initialized, _tag_group_polled)
func use() -> void:
pressed = not pressed
func _reset_pushbutton() -> void:
await get_tree().create_timer(0.3).timeout
pressed = false
func _update_output() -> void:
var new_output := pressed
if normally_closed:
new_output = !pressed
output = new_output
func _on_simulation_started() -> void:
if enable_comms:
_pushbutton_tag.register(pushbutton_tag_group_name, pushbutton_tag_name)
_lamp_tag.register(lamp_tag_group_name, lamp_tag_name)
func _tag_group_initialized(tag_group_name_param: String) -> void:
if _pushbutton_tag.on_group_initialized(tag_group_name_param):
_pushbutton_tag.write_bit(output)
_lamp_tag.on_group_initialized(tag_group_name_param)
func _tag_group_polled(tag_group_name_param: String) -> void:
if not enable_comms:
return
if _lamp_tag.matches_group(tag_group_name_param):
lamp = _lamp_tag.read_bit()
|