From 0d6c862c6d0e4fa319024447ff235ceebc67068d Mon Sep 17 00:00:00 2001 From: Nitwel Date: Thu, 21 Mar 2024 13:29:10 +0100 Subject: [PATCH] fix reactivity bugs --- app/content/system/assist/chat.gd | 32 ++++----- app/content/system/house/align_reference.gd | 20 +++--- app/content/system/house/room/room.gd | 10 ++- app/content/ui/components/button/button.gd | 71 +++++++------------ app/content/ui/components/button/button.tscn | 71 ++----------------- app/content/ui/components/input/input.gd | 23 +++--- .../components/notification/notification.gd | 13 ++-- app/content/ui/components/slider/slider.gd | 30 ++++---- app/content/ui/components/tabs/tabs.gd | 25 ++++--- app/content/ui/menu/room/room_menu.tscn | 5 +- app/content/ui/menu/room/views/rooms.gd | 7 +- app/content/ui/menu/room/views/rooms.tscn | 3 +- app/lib/utils/proxy_group.gd | 3 +- app/lib/utils/update_attributes.gd | 5 ++ 14 files changed, 127 insertions(+), 191 deletions(-) create mode 100644 app/lib/utils/update_attributes.gd diff --git a/app/content/system/assist/chat.gd b/app/content/system/assist/chat.gd index a7969e1..be46121 100644 --- a/app/content/system/assist/chat.gd +++ b/app/content/system/assist/chat.gd @@ -12,27 +12,27 @@ const FontTools = preload ("res://lib/utils/font_tools.gd") @export var text := "Hello, World!": set(value): text = value - if !is_node_ready(): await ready + if !is_inside_tree(): return + + label.text = text + var text_width = FontTools.get_font_size(label).x - label.text = value - update() + var offset = (text_width - base_width) / 0.2 + + offset = max(0.0, offset) + + chat.set_bone_pose_position(0, Vector3(0, offset, 0)) + chat_flipped.set_bone_pose_position(1, Vector3(0, -offset, 0)) @export var flip: bool = false: set(value): flip = value - if !is_node_ready(): await ready - - model.visible = !value - model_flipped.visible = value + if !is_inside_tree(): return + + model.visible = !flip + model_flipped.visible = flip const base_width = 0.8 * 0.2 -func update(): - var text_width = FontTools.get_font_size(label).x - - var offset = (text_width - base_width) / 0.2 - - offset = max(0.0, offset) - - chat.set_bone_pose_position(0, Vector3(0, offset, 0)) - chat_flipped.set_bone_pose_position(1, Vector3(0, -offset, 0)) \ No newline at end of file +func _ready(): + Update.props(self, ["text", "flip"]) diff --git a/app/content/system/house/align_reference.gd b/app/content/system/house/align_reference.gd index 4773c5a..0118f58 100644 --- a/app/content/system/house/align_reference.gd +++ b/app/content/system/house/align_reference.gd @@ -9,14 +9,14 @@ extends Node3D set(value): disabled = value - if !is_node_ready(): await ready - - corner1.get_node("CollisionShape3D").disabled = value - corner2.get_node("CollisionShape3D").disabled = value - edge.get_node("CollisionShape3D").disabled = value - corner1.visible = !value - corner2.visible = !value - edge.visible = !value + if !is_inside_tree(): return + + corner1.get_node("CollisionShape3D").disabled = disabled + corner2.get_node("CollisionShape3D").disabled = disabled + edge.get_node("CollisionShape3D").disabled = disabled + corner1.visible = !disabled + corner2.visible = !disabled + edge.visible = !disabled func _ready(): update_initial_positions() @@ -29,7 +29,7 @@ func _ready(): edge.align_to_corners(corner1.global_position, corner2.global_position) corner2.look_at(corner1.global_position, Vector3.UP) - corner2.rotate(Vector3.UP, deg_to_rad(-90)) + corner2.rotate(Vector3.UP, deg_to_rad( - 90)) ) corner2.get_node("Movable").restrict_movement = func(new_position): @@ -52,7 +52,7 @@ func update_align_reference(): corner2.global_position = Store.house.align_position2 corner2.look_at(corner1.global_position, Vector3.UP) - corner2.rotate(Vector3.UP, deg_to_rad(-90)) + corner2.rotate(Vector3.UP, deg_to_rad( - 90)) edge.align_to_corners(corner1.global_position, corner2.global_position) diff --git a/app/content/system/house/room/room.gd b/app/content/system/house/room/room.gd index a39f61e..7c76b8f 100644 --- a/app/content/system/house/room/room.gd +++ b/app/content/system/house/room/room.gd @@ -14,13 +14,17 @@ extends Node3D var editable: bool = false: set(value): editable = value - if !is_node_ready(): await ready - - if value: + + if !is_inside_tree(): return + + if editable: state_machine.change_to("Edit") else: state_machine.change_to("View") +func _ready(): + Update.props(self, ["editable"]) + func has_point(point: Vector3) -> bool: return get_aabb().has_point(point) diff --git a/app/content/ui/components/button/button.gd b/app/content/ui/components/button/button.gd index 8827d3e..219af49 100644 --- a/app/content/ui/components/button/button.gd +++ b/app/content/ui/components/button/button.gd @@ -10,6 +10,7 @@ const IconFont = preload ("res://assets/icons/icons.tres") const ECHO_WAIT_INITIAL = 0.5 const ECHO_WAIT_REPEAT = 0.1 +@onready var body: StaticBody3D = $Body @onready var label_node: Label3D = $Body/Label @onready var finger_area: Area3D = $FingerArea @@ -24,19 +25,21 @@ const ECHO_WAIT_REPEAT = 0.1 @export var font_size: int = 10: set(value): font_size = value - if !is_node_ready(): await ready - label_node.font_size = value + if !is_inside_tree()||icon: return + label_node.font_size = font_size + @export var label: String = "": set(value): label = value - if !is_node_ready(): await ready - label_node.text = value + if !is_inside_tree(): return + label_node.text = label + @export var icon: bool = false: set(value): icon = value - if !is_node_ready(): await ready - - if value: + if !is_inside_tree(): return + + if icon: label_node.font = IconFont label_node.font_size = 36 label_node.width = 1000 @@ -46,42 +49,26 @@ const ECHO_WAIT_REPEAT = 0.1 label_node.font_size = font_size label_node.width = 50 label_node.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART - + @export var toggleable: bool = false @export var disabled: bool = false @export var echo: bool = false @export var initial_active: bool = false -var external_value: Proxy = null: - set(value): - external_value = value - if !is_node_ready(): await ready - - if value != null: - value.on_set.connect(func(_value): - update_animation() - ) var active: bool = false: - get: - if external_value != null: - return external_value.value - return active set(value): - if !is_node_ready(): await ready - - if external_value != null: - external_value.value = value - else: - active = value - update_animation() - -@onready var animation_player: AnimationPlayer = $AnimationPlayer + active = value + if !is_inside_tree(): return + update_animation(1.0 if active else 0.0) + var echo_timer: Timer = null func _ready(): if initial_active: active = true + Update.props(self, ["active", "external_value", "icon", "label", "font_size"]) + if echo: echo_timer = Timer.new() echo_timer.wait_time = ECHO_WAIT_INITIAL @@ -96,16 +83,12 @@ func _ready(): add_child(echo_timer) -func update_animation(): - var length = animation_player.get_animation("down").length +func update_animation(value: float): + var tween = create_tween() + tween.set_parallel(true) - if animation_player.current_animation == "": - return - - if active&&animation_player.current_animation_position != length: - animation_player.play("down") - elif !active&&animation_player.current_animation_position != 0: - animation_player.play_backwards("down") + tween.tween_property(body, "scale:y", lerpf(1.0, 0.5, value), 0.2) + tween.tween_property(body, "position:y", lerpf(0.01, 0.005, value), 0.2) func _on_press_down(event): if disabled: @@ -146,10 +129,7 @@ func _on_press_up(event): func _on_touch_enter(event: EventTouch): if event.target != finger_area: return - - animation_player.stop() - animation_player.speed_scale = 0 - animation_player.current_animation = "down" + AudioPlayer.play_effect("click") _touch_change(event) @@ -157,9 +137,6 @@ func _on_touch_move(event: EventTouch): _touch_change(event) func _on_touch_leave(_event: EventTouch): - animation_player.stop() - animation_player.speed_scale = 1 - if toggleable: active = !active if active: @@ -188,7 +165,7 @@ func _touch_change(event: EventTouch): elif active&&percent >= 1: on_button_up.emit() - animation_player.seek(percent * animation_player.current_animation_length, true) + update_animation(percent) if toggleable: return diff --git a/app/content/ui/components/button/button.tscn b/app/content/ui/components/button/button.tscn index c8565bd..d7dee7b 100644 --- a/app/content/ui/components/button/button.tscn +++ b/app/content/ui/components/button/button.tscn @@ -1,70 +1,12 @@ -[gd_scene load_steps=11 format=3 uid="uid://bsjqdvkt0u87c"] +[gd_scene load_steps=8 format=3 uid="uid://bsjqdvkt0u87c"] [ext_resource type="Script" path="res://content/ui/components/button/button.gd" id="1_74x7g"] [ext_resource type="ArrayMesh" uid="uid://iv4lk77axlk4" path="res://assets/immersive_home/button.obj" id="2_cve3l"] [ext_resource type="Material" uid="uid://bujy3egn1oqac" path="res://assets/materials/pri-500.material" id="2_wx7av"] [ext_resource type="Script" path="res://content/system/hands/touch_area.gd" id="4_b27lx"] -[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_o4j7g"] -points = PackedVector3Array(-0.025, -0.01, -0.025, -0.025, 0.01, -0.025, 0.025, -0.01, -0.025, -0.025, -0.01, 0.025, -0.025, 0.01, 0.025, 0.025, 0.01, -0.025, 0.025, -0.01, 0.025, 0.025, 0.01, 0.025) - -[sub_resource type="Animation" id="Animation_gvfrg"] -length = 0.001 -tracks/0/type = "bezier" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("Body:scale:y") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"handle_modes": PackedInt32Array(0), -"points": PackedFloat32Array(1, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0) -} -tracks/1/type = "bezier" -tracks/1/imported = false -tracks/1/enabled = true -tracks/1/path = NodePath("Body:position:y") -tracks/1/interp = 1 -tracks/1/loop_wrap = true -tracks/1/keys = { -"handle_modes": PackedInt32Array(0), -"points": PackedFloat32Array(0.01, -0.25, 0, 0.25, 0), -"times": PackedFloat32Array(0) -} - -[sub_resource type="Animation" id="Animation_iu2ed"] -resource_name = "down" -length = 0.2 -step = 0.01 -tracks/0/type = "bezier" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("Body:scale:y") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(1, -0.25, 0, 0.12, -0.00966179, 0.500747, -0.13, 0.00874269, 0.25, 0), -"times": PackedFloat32Array(0, 0.2) -} -tracks/1/type = "bezier" -tracks/1/imported = false -tracks/1/enabled = true -tracks/1/path = NodePath("Body:position:y") -tracks/1/interp = 1 -tracks/1/loop_wrap = true -tracks/1/keys = { -"handle_modes": PackedInt32Array(0, 0), -"points": PackedFloat32Array(0.0101447, -0.25, 0, 0.12, 0.000134136, 0.0051993, -0.12, 0.000145131, 0.25, 0), -"times": PackedFloat32Array(0, 0.2) -} - -[sub_resource type="AnimationLibrary" id="AnimationLibrary_sbgno"] -_data = { -"RESET": SubResource("Animation_gvfrg"), -"down": SubResource("Animation_iu2ed") -} +[sub_resource type="BoxShape3D" id="BoxShape3D_xwopm"] +size = Vector3(0.05, 0.02, 0.05) [sub_resource type="BoxShape3D" id="BoxShape3D_bqjii"] size = Vector3(0.0501598, 0.0390937, 0.0501598) @@ -88,7 +30,7 @@ mesh = ExtResource("2_cve3l") skeleton = NodePath("../..") [node name="CollisionShape3D" type="CollisionShape3D" parent="Body"] -shape = SubResource("ConvexPolygonShape3D_o4j7g") +shape = SubResource("BoxShape3D_xwopm") [node name="Label" type="Label3D" parent="Body"] transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0.0107199, 0) @@ -99,11 +41,6 @@ outline_size = 0 autowrap_mode = 3 width = 50.0 -[node name="AnimationPlayer" type="AnimationPlayer" parent="."] -libraries = { -"": SubResource("AnimationLibrary_sbgno") -} - [node name="FingerArea" type="Area3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0101447, 0) collision_layer = 4 diff --git a/app/content/ui/components/input/input.gd b/app/content/ui/components/input/input.gd index 7bed645..19c454e 100644 --- a/app/content/ui/components/input/input.gd +++ b/app/content/ui/components/input/input.gd @@ -14,27 +14,26 @@ var text_handler = preload ("res://content/ui/components/input/text_handler.gd") set(value): width = value text_handler.width = value + if !is_inside_tree(): return - if !is_node_ready(): await ready - - mesh_box.mesh.size.x = value - collision.shape.size.x = value - label.position.x = -value / 2 + 0.002 - + mesh_box.mesh.size.x = width + collision.shape.size.x = width + label.position.x = -width / 2 + 0.002 + @export var text: String: set(value): text = value - var focused = Engine.is_editor_hint() == false&&EventSystem.is_focused(self) == false - if !is_node_ready(): await ready + if !is_inside_tree(): return - text_handler.set_text(value, focused) + var focused = Engine.is_editor_hint() == false&&EventSystem.is_focused(self) == false + text_handler.set_text(text, focused) label.text = text_handler.get_display_text() @export var disabled: bool = false: set(value): disabled = value - if !is_node_ready(): await ready - + if !is_inside_tree(): return + if disabled: label.modulate = Color(0.7, 0.7, 0.7) add_to_group("ui_focus_skip") @@ -49,6 +48,8 @@ var keyboard_input: bool = false var input_plane = Plane(Vector3.UP, Vector3.ZERO) func _ready(): + Update.props(self, ["text", "disabled", "width"]) + text_handler.label = label if Engine.is_editor_hint(): diff --git a/app/content/ui/components/notification/notification.gd b/app/content/ui/components/notification/notification.gd index 38ddd61..6e1a16e 100644 --- a/app/content/ui/components/notification/notification.gd +++ b/app/content/ui/components/notification/notification.gd @@ -12,15 +12,18 @@ extends Node3D @export var type: EventNotify.Type = EventNotify.Type.INFO: set(value): type = value - if !is_node_ready(): await ready - icon_label.text = _type_to_string(value) + if !is_inside_tree(): return + icon_label.text = _type_to_string(type) + @export var text: String = "": set(value): text = value - if !is_node_ready(): await ready - label.text = value + if !is_inside_tree(): return + label.text = text func _ready(): + Update.props(self, ["type", "text"]) + button.on_button_down.connect(fade_out) fade_in() @@ -29,7 +32,7 @@ func _ready(): ) func fade_in(): - if !is_node_ready(): await ready + if !is_inside_tree(): await ready animation_player.play("fade_in") diff --git a/app/content/ui/components/slider/slider.gd b/app/content/ui/components/slider/slider.gd index 981ddb7..435d79c 100644 --- a/app/content/ui/components/slider/slider.gd +++ b/app/content/ui/components/slider/slider.gd @@ -6,22 +6,26 @@ class_name Slider3D set(new_value): min = new_value if value < min: value = min - if !is_node_ready(): await ready + if !is_inside_tree(): return + _update_slider() @export var max: float = 1.0: set(new_value): max = new_value if value > max: value = max - if !is_node_ready(): await ready + if !is_inside_tree(): return + _update_slider() + @export var value: float = 0.2: set(new_value): value = roundi(clamp(new_value, min, max) / step) * step - if !is_node_ready(): await ready + if !is_inside_tree(): return + label.text = str(value) + " " + label_unit - on_value_changed.emit(value) + if new_value != value: on_value_changed.emit(value) _update_slider() @export var step: float = 0.01 @@ -29,29 +33,29 @@ class_name Slider3D @export var show_label: bool = false: set(value): show_label = value - if !is_node_ready(): await ready + if !is_inside_tree(): return label.visible = show_label @export var label_unit: String = "": set(new_value): label_unit = new_value - if !is_node_ready(): await ready + if !is_inside_tree(): return label.text = str(value) + " " + label_unit @export var size: Vector3 = Vector3(0.2, 0.01, 0.02): # Warning, units are in cm set(value): size = value - if !is_node_ready(): await ready + if !is_inside_tree(): return _update_shape() @export var cutout_border: float = 0.02: set(value): cutout_border = value - if !is_node_ready(): await ready + if !is_inside_tree(): return _update_shape() @export var cutout_depth: float = 0.05: set(value): cutout_depth = value - if !is_node_ready(): await ready + if !is_inside_tree(): return _update_shape() @onready var outside_rod: CSGBox3D = $Rod/Outside @@ -68,6 +72,9 @@ signal on_value_changed(value: float) var move_plane: Plane func _ready(): + Update.props(self, ["value", "show_label", "label_unit"]) + + _update_slider() _update_shape() move_plane = Plane(Vector3.UP, Vector3(0, size.y / 200, 0)) @@ -80,7 +87,7 @@ func _on_press_move(event: EventPointer): func _get_slider_min_max(): var cutout_radius = (size.z - cutout_border * 2) / 2 - return Vector2(-size.x / 2 + cutout_border + cutout_radius, size.x / 2 - cutout_border - cutout_radius) / 100 + return Vector2( - size.x / 2 + cutout_border + cutout_radius, size.x / 2 - cutout_border - cutout_radius) / 100 func _handle_press(event: EventPointer): var ray_pos = event.ray.global_position @@ -101,7 +108,6 @@ func _handle_press(event: EventPointer): var click_percent = inverse_lerp(min_max.x, min_max.y, pos_x) value = lerp(min, max, click_percent) - _update_slider() func _update_slider(): var min_max = _get_slider_min_max() @@ -133,7 +139,7 @@ func _update_shape(): cutout_end_right.height = cutout_depth cutout_end_left.position = Vector3( - -cutout_box.size.x / 2, + - cutout_box.size.x / 2, 0, 0 ) diff --git a/app/content/ui/components/tabs/tabs.gd b/app/content/ui/components/tabs/tabs.gd index f8e88e6..c31150d 100644 --- a/app/content/ui/components/tabs/tabs.gd +++ b/app/content/ui/components/tabs/tabs.gd @@ -3,26 +3,29 @@ class_name Tabs3D signal on_select(selected: int) -var selected: Node3D +var selected: Node3D: + set(value): + if selected == value: + return + + if selected != null: + selected.active = false + + selected = value + selected.active = true + on_select.emit(selected.get_index()) @export var initial_selected: Node3D -var proxy_group = ProxyGroup.new() func _ready(): if initial_selected != null: selected = initial_selected - on_select.emit(selected.get_index()) for option in get_children(): if option is Button3D == false: continue - var proxy = proxy_group.proxy(func(): - return selected == option - , func(value: bool): - if value == true: - selected = option - on_select.emit(selected.get_index()) + option.on_button_down.connect(func(): + selected=option ) - - option.external_value = proxy + option.toggleable = true diff --git a/app/content/ui/menu/room/room_menu.tscn b/app/content/ui/menu/room/room_menu.tscn index 4dd5d60..81085b2 100644 --- a/app/content/ui/menu/room/room_menu.tscn +++ b/app/content/ui/menu/room/room_menu.tscn @@ -26,11 +26,11 @@ script = ExtResource("5_ddrep") initial_selected = NodePath("Overview") [node name="Overview" parent="Interface/Tabs3D" instance=ExtResource("4_cghmp")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.03, 0, -0.02) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.03, 0, 0.02) label = "Overview" [node name="Rooms" parent="Interface/Tabs3D" instance=ExtResource("4_cghmp")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.09, 0, -0.02) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.09, 0, 0.02) label = "Rooms" [node name="TabsContent3D" type="Node3D" parent="Interface" node_paths=PackedStringArray("tabs")] @@ -38,5 +38,6 @@ script = ExtResource("6_ba00g") tabs = NodePath("../Tabs3D") [node name="Overview" parent="Interface/TabsContent3D" instance=ExtResource("6_206ad")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.04) [node name="Rooms" parent="Interface/TabsContent3D" instance=ExtResource("7_2f8e0")] diff --git a/app/content/ui/menu/room/views/rooms.gd b/app/content/ui/menu/room/views/rooms.gd index 90a1ea4..298e13c 100644 --- a/app/content/ui/menu/room/views/rooms.gd +++ b/app/content/ui/menu/room/views/rooms.gd @@ -104,7 +104,8 @@ func _on_click(event: EventPointer): func _generate_room_map(): var rooms = Store.house.rooms - var target_size = Vector2(0.3, 0.24) + var target_size = Vector2(0.2, 0.24) + var target_offset = Vector2(0, 0.05) for old_room in rooms_map.get_children(): old_room.queue_free() @@ -154,7 +155,7 @@ func _generate_room_map(): var target_scale = target_size / current_size var scale_value = min(target_scale.x, target_scale.y) - rooms_map.position.x = -current_min.x * scale_value - rooms_map.position.z = -current_min.y * scale_value + rooms_map.position.x = -current_min.x * scale_value + target_offset.x + rooms_map.position.z = -current_min.y * scale_value + target_offset.y rooms_map.scale = Vector3(scale_value, scale_value, scale_value) diff --git a/app/content/ui/menu/room/views/rooms.tscn b/app/content/ui/menu/room/views/rooms.tscn index 3987264..c215f74 100644 --- a/app/content/ui/menu/room/views/rooms.tscn +++ b/app/content/ui/menu/room/views/rooms.tscn @@ -8,7 +8,7 @@ script = ExtResource("1_3a1oa") [node name="Rooms" type="Node3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.15, 0.01, 0.12) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.15, 0.01, 0.17) [node name="Button" parent="." instance=ExtResource("1_y3lty")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.27, 0, 0.27) @@ -17,6 +17,5 @@ icon = true [node name="Input" parent="." instance=ExtResource("2_hstw7")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.12, 0.005, 0.27) -width = 0.15 text = "Room 1" disabled = true diff --git a/app/lib/utils/proxy_group.gd b/app/lib/utils/proxy_group.gd index d4c58c2..bf042d9 100644 --- a/app/lib/utils/proxy_group.gd +++ b/app/lib/utils/proxy_group.gd @@ -11,10 +11,9 @@ func proxy(_get: Callable, _set: Callable): for p in proxies: if p != _proxy: - p.on_set.emit(value) + p.on_set.emit(p.value) ) proxies.append(_proxy) return _proxy - \ No newline at end of file diff --git a/app/lib/utils/update_attributes.gd b/app/lib/utils/update_attributes.gd new file mode 100644 index 0000000..1d24fa0 --- /dev/null +++ b/app/lib/utils/update_attributes.gd @@ -0,0 +1,5 @@ +class_name Update + +static func props(node: Node, prop_names=[]): + for prop in prop_names: + node.set(prop, node.get(prop))