diff --git a/content/entities/camera/camera.gd b/content/entities/camera/camera.gd new file mode 100644 index 0000000..49b254d --- /dev/null +++ b/content/entities/camera/camera.gd @@ -0,0 +1,61 @@ +extends StaticBody3D + +@export var entity_id = "camera.bedroomspeaker" +@export var view_width = 0.15 + +@onready var view = $View +@onready var http_request = $HTTPRequest + + +# Called when the node enters the scene tree for the first time. +func _ready(): + var stateInfo = await HomeApi.get_state(entity_id) + + set_state(stateInfo) + + await HomeApi.watch_state(entity_id, func(new_state): + set_state(new_state) + ) + +func set_state(stateInfo): + if stateInfo == null: + view.texture = null + return + + var state = stateInfo["state"] + + if state == "unavailable": + view.texture = null + return + + if stateInfo["attributes"].has("entity_picture"): + load_image(stateInfo["attributes"]["entity_picture"]) + + +func load_image(url: String): + http_request.request("http://192.168.33.33:8123" + url) + + var result = await http_request.request_completed + + if result[0] != HTTPRequest.RESULT_SUCCESS: + print("Error loading image: ", result[0], " ", result[1]) + return + + var image = Image.new() + var error = image.load_png_from_buffer(result[3]) + + var pixel_size = view_width / image.get_size().x + + if error != OK: + print("Error loading image: ", error) + return + + var texture = ImageTexture.create_from_image(image) + view.texture = texture + view.pixel_size = pixel_size + +func _save(): + return { + "transform": transform, + "entity_id": entity_id + } diff --git a/content/entities/camera/camera.tscn b/content/entities/camera/camera.tscn new file mode 100644 index 0000000..df3a325 --- /dev/null +++ b/content/entities/camera/camera.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=5 format=3 uid="uid://b0nq4wjfckxsa"] + +[ext_resource type="Script" path="res://content/entities/camera/camera.gd" id="1_htxq3"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="2_e2u6o"] +[ext_resource type="Script" path="res://content/functions/occludable.gd" id="3_jheyx"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_te0pn"] +size = Vector3(0.15, 0.15, 0.01) + +[node name="Camera" type="StaticBody3D" groups=["entity"]] +script = ExtResource("1_htxq3") + +[node name="View" type="Sprite3D" parent="."] + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +shape = SubResource("BoxShape3D_te0pn") + +[node name="HTTPRequest" type="HTTPRequest" parent="."] + +[node name="Movable" type="Node" parent="."] +script = ExtResource("2_e2u6o") + +[node name="Occludable" type="Node" parent="."] +script = ExtResource("3_jheyx") diff --git a/content/ui/menu/edit/entity_creator.gd b/content/ui/menu/edit/entity_creator.gd index 9d4e5f1..240bf91 100644 --- a/content/ui/menu/edit/entity_creator.gd +++ b/content/ui/menu/edit/entity_creator.gd @@ -4,6 +4,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") const MediaPlayer = preload("res://content/entities/media_player/media_player.tscn") +const Camera = preload("res://content/entities/camera/camera.tscn") static func create_entity(type: String, id: String): var entity = null @@ -17,6 +18,8 @@ static func create_entity(type: String, id: String): entity = Sensor.instantiate() "media_player": entity = MediaPlayer.instantiate() + "camera": + entity = Camera.instantiate() _: return null