add simple camera support

This commit is contained in:
Nitwel 2023-12-09 23:47:17 +01:00
parent fd4ff6d388
commit c1e2ccf162
3 changed files with 90 additions and 0 deletions

View File

@ -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
}

View File

@ -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")

View File

@ -4,6 +4,7 @@ const Switch = preload("res://content/entities/switch/switch.tscn")
const Light = preload("res://content/entities/light/light.tscn") const Light = preload("res://content/entities/light/light.tscn")
const Sensor = preload("res://content/entities/sensor/sensor.tscn") const Sensor = preload("res://content/entities/sensor/sensor.tscn")
const MediaPlayer = preload("res://content/entities/media_player/media_player.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): static func create_entity(type: String, id: String):
var entity = null var entity = null
@ -17,6 +18,8 @@ static func create_entity(type: String, id: String):
entity = Sensor.instantiate() entity = Sensor.instantiate()
"media_player": "media_player":
entity = MediaPlayer.instantiate() entity = MediaPlayer.instantiate()
"camera":
entity = Camera.instantiate()
_: _:
return null return null