From 280b986455c9cf086181a00406783c1f4d1e7633 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Tue, 7 Nov 2023 21:07:34 +0100 Subject: [PATCH] add 3D containers --- content/main.tscn | 2 +- content/ui/device/device.tscn | 2 +- content/ui/entity/entity.tscn | 2 +- content/ui/menu/container3d.gd | 15 ++++++++++++ content/ui/menu/grid.gd | 42 ++++++++++++++++++++++++++++++++++ content/ui/menu/menu.gd | 28 ++++------------------- content/ui/menu/menu.tscn | 9 ++++++-- 7 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 content/ui/menu/container3d.gd create mode 100644 content/ui/menu/grid.gd diff --git a/content/main.tscn b/content/main.tscn index 1da5ecb..9ba0098 100644 --- a/content/main.tscn +++ b/content/main.tscn @@ -42,7 +42,7 @@ pose = &"aim" mesh = SubResource("BoxMesh_ir3co") [node name="Menu" parent="XROrigin3D/XRControllerLeft" instance=ExtResource("3_1tbp3")] -transform = Transform3D(-4.37114e-08, 0, -1, -0.707107, 0.707107, 3.09086e-08, 0.707107, 0.707107, -3.09086e-08, 0.183517, 0, -0.0534939) +transform = Transform3D(1, -0.000382732, -0.000120985, 8.65898e-05, 0.5, -0.866025, 0.000391948, 0.866025, 0.5, 0.0669508, 0.0876772, -0.101157) [node name="XRControllerRight" type="XRController3D" parent="XROrigin3D"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.488349, 0.559219, -0.2988) diff --git a/content/ui/device/device.tscn b/content/ui/device/device.tscn index 3c8ea3a..242b496 100644 --- a/content/ui/device/device.tscn +++ b/content/ui/device/device.tscn @@ -18,6 +18,6 @@ mesh = SubResource("BoxMesh_aa3i4") shape = SubResource("BoxShape3D_28fjq") [node name="Label" type="Label3D" parent="."] -transform = Transform3D(-2.18557e-09, -0.05, -2.18557e-09, 0, -2.18557e-09, 0.05, -0.05, 2.18557e-09, 9.55343e-17, 0, 0.00918245, 0) +transform = Transform3D(0.05, 0, 0, 0, -2.18557e-09, 0.05, 0, -0.05, -2.18557e-09, 0, 0.00918245, 0) text = "Text" autowrap_mode = 3 diff --git a/content/ui/entity/entity.tscn b/content/ui/entity/entity.tscn index a80a77d..cb1fc8c 100644 --- a/content/ui/entity/entity.tscn +++ b/content/ui/entity/entity.tscn @@ -18,6 +18,6 @@ mesh = SubResource("BoxMesh_aa3i4") shape = SubResource("BoxShape3D_28fjq") [node name="Label" type="Label3D" parent="."] -transform = Transform3D(-2.18557e-09, -0.05, -2.18557e-09, 0, -2.18557e-09, 0.05, -0.05, 2.18557e-09, 9.55343e-17, 0, 0.00918245, 0) +transform = Transform3D(0.05, 0, 0, 0, -2.18557e-09, 0.05, 0, -0.05, -2.18557e-09, 0, 0.00918245, 0) text = "Text" autowrap_mode = 3 diff --git a/content/ui/menu/container3d.gd b/content/ui/menu/container3d.gd new file mode 100644 index 0000000..4818f29 --- /dev/null +++ b/content/ui/menu/container3d.gd @@ -0,0 +1,15 @@ +extends Node3D +class_name Container3D + +@export var size := Vector3(1.0, 1.0, 1.0) : + set(value): + size = value + _update_container() + +@export var padding: Vector4 = Vector4(0, 0, 0, 0) : + set(value): + padding = value + _update_container() + +func _update_container(): + pass \ No newline at end of file diff --git a/content/ui/menu/grid.gd b/content/ui/menu/grid.gd new file mode 100644 index 0000000..3bff0b0 --- /dev/null +++ b/content/ui/menu/grid.gd @@ -0,0 +1,42 @@ +@tool +extends Container3D +class_name GridContainer3D + + +@export var columns := 5 : + set(value): + columns = value + _update_container() + +@export var rows := 1 : + set(value): + rows = value + _update_container() + +@export var depth_gap := 1.0 : + set(value): + depth_gap = value + _update_container() + +func _ready(): + _update_container() + +func get_gaps() -> Vector3: + return Vector3( + (float(size.x) / (columns - 1 )) if columns != 1 else 0.0, + (float(size.y) / (rows - 1)) if rows != 1 else 0.0, + depth_gap + ) + + +func _update_container(): + var i := 0 + var gaps := get_gaps() + + for child in get_children(): + var x := (i % columns) * gaps.x + var y := ((i / columns) % rows) * gaps.y + var z := (i / (columns * rows)) * gaps.z + + child.set_position(Vector3(x, -y, z)) + i += 1 \ No newline at end of file diff --git a/content/ui/menu/menu.gd b/content/ui/menu/menu.gd index 0c16034..05621c2 100644 --- a/content/ui/menu/menu.gd +++ b/content/ui/menu/menu.gd @@ -6,7 +6,7 @@ const Switch = preload("res://content/entities/switch/switch.tscn") const Light = preload("res://content/entities/light/light.tscn") const Sensor = preload("res://content/entities/sensor/sensor.tscn") -@onready var devices_node = $Devices +@onready var devices_node: GridContainer3D = $Devices var devices var selected_device = null @@ -16,30 +16,18 @@ func _ready(): render_devices() func render_devices(): - var x = 0 - var y = 0 - for device in devices: var info = device.values()[0] var device_instance = Device.instantiate() - device_instance.set_position(Vector3(y * 0.08, 0, -x * 0.08)) device_instance.click.connect(_on_device_click) device_instance.id = device.keys()[0] - devices_node.add_child(device_instance) - device_instance.set_device_name(info["name"]) - - x += 1 - if x % 5 == 0: - x = 0 - y += 1 + + devices_node._update_container() func render_entities(): - var x = 0 - var y = 0 - var info for device in devices: @@ -54,17 +42,11 @@ func render_entities(): for entity in entities: var entity_instance = Entity.instantiate() - entity_instance.set_position(Vector3(y * 0.08, 0, -x * 0.08)) entity_instance.click.connect(_on_entity_click) - devices_node.add_child(entity_instance) - entity_instance.set_entity_name(entity) - - x += 1 - if x % 5 == 0: - x = 0 - y += 1 + + devices_node._update_container() func _on_device_click(device_id): selected_device = device_id diff --git a/content/ui/menu/menu.tscn b/content/ui/menu/menu.tscn index 928c938..09916c4 100644 --- a/content/ui/menu/menu.tscn +++ b/content/ui/menu/menu.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=3 uid="uid://c3kdssrmv84kv"] +[gd_scene load_steps=5 format=3 uid="uid://c3kdssrmv84kv"] [ext_resource type="Script" path="res://content/ui/menu/menu.gd" id="1_ng4u3"] [ext_resource type="Material" uid="uid://bertj8bp8b5l1" path="res://assets/materials/interface.tres" id="2_nsukb"] +[ext_resource type="Script" path="res://content/ui/menu/grid.gd" id="3_35a5r"] [sub_resource type="PlaneMesh" id="PlaneMesh_6t3dn"] material = ExtResource("2_nsukb") @@ -11,7 +12,11 @@ size = Vector2(0.3, 0.3) script = ExtResource("1_ng4u3") [node name="Background" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.15, 0, 0.15) mesh = SubResource("PlaneMesh_6t3dn") [node name="Devices" type="Node3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.149223, 0, 0.150667) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.03, 0, 0.03) +script = ExtResource("3_35a5r") +depth_gap = 0.06 +size = Vector3(0.24, 0.1, 0.1)