Skip to content

Commit

Permalink
fix slider and throttle/debounce functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitwel committed Jun 12, 2024
1 parent d712c81 commit 92a19eb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
1 change: 0 additions & 1 deletion app/content/entities/light/light.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ focus_point = NodePath("../Settings")
[node name="Slider" parent="." instance=ExtResource("6_mhjlm")]
transform = Transform3D(-4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0, 1, 0.08, 0, 0)
max = 100.0
value = 0.0
step = 1.0

[node name="ColorWheel" parent="." instance=ExtResource("5_qj75k")]
Expand Down
6 changes: 3 additions & 3 deletions app/content/system/miniature/mini_wall_shader.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ void fragment() {
if (pos.x <= border) {
blend = max(blend, 1.0 - pos.x / border);
}

if (pos.x >= aspect_ratio.x - border) {
blend = max(blend, (pos.x - (aspect_ratio.x - border)) / border);
}

if (pos.y <= border) {
blend = max(blend, 1.0 - pos.y / border);
}

if (pos.y >= aspect_ratio.y - border) {
blend = max(blend, (pos.y - (aspect_ratio.y - border)) / border);
}

if (smooth_border) {
ALBEDO = mix(ALBEDO, mix(ALBEDO, edge_color.rgb, edge_color.a), blend * blend);
} else if(blend > 0.0) {
Expand Down
3 changes: 2 additions & 1 deletion app/content/ui/components/slider/slider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ var throttled_value_changed = ProcessTools.throttle_bouce(func(value: float):

@export var value: float = 0.2:
set(new_value):
var old_value = value
value = roundi(clamp(new_value, min, max) / step) * step

if !is_inside_tree(): return

label.text = str(value) + " " + label_unit
if new_value != value: throttled_value_changed.call(value)
if new_value != old_value: throttled_value_changed.call(value)
_update_slider()

@export var step: float = 0.01
Expand Down
7 changes: 5 additions & 2 deletions app/content/ui/components/slider/slider.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ size = Vector3(0.15, 0.02, 0.01)

[node name="Slider" type="Node3D"]
script = ExtResource("1_ylune")
max = 0.0
value = 0.0
step = 0.0
show_label = true
size = Vector3(0.15, 0.02, 0.01)

Expand All @@ -72,13 +75,13 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.08, 0, 0.01)
pixel_size = 0.001
render_priority = 30
outline_render_priority = 29
text = "0.2 "
text = "-0 "
font_size = 10
outline_size = 4
horizontal_alignment = 0

[node name="Knob" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.039, 0, 0.012)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, nan, 0, 0.012)
material_override = SubResource("ShaderMaterial_mv0la")
mesh = SubResource("QuadMesh_vw14o")
skeleton = NodePath("../Body")
Expand Down
16 changes: 9 additions & 7 deletions app/lib/globals/process_tools.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ func debounce(callback: Callable, delay: int) -> Callable:
var timer = Timer.new()
timer.one_shot = true
timer.autostart = false
var args = []
add_child(timer)

timer.timeout.connect(func():
var args=timer.get_meta("args")
callback.callv(args)
args=[]
timer.set_meta("args", null)
)

# TODO: Implement variadic arguments when available https://github.com/godotengine/godot/pull/82808
var wrapper = func(arg1=null, arg2=null, arg3=null, arg4=null, arg5=null, arg6=null, arg7=null, arg8=null, arg9=null):
args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9].slice(0, callback.get_argument_count())
var args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9].slice(0, callback.get_argument_count())
timer.set_meta("args", args)
timer.stop()
timer.start(delay / 1000.0)

Expand All @@ -40,25 +41,26 @@ func throttle_bouce(callback: Callable, delay: int) -> Callable:
var timer = Timer.new()
timer.one_shot = true
timer.autostart = false
var args = null
add_child(timer)

timer.timeout.connect(func():
var args=timer.get_meta("args")
if args == null:
return

callback.callv(args)
args=null
timer.set_meta("args", null)
)

# TODO: Implement variadic arguments when available https://github.com/godotengine/godot/pull/82808
var wrapper = func(arg1=null, arg2=null, arg3=null, arg4=null, arg5=null, arg6=null, arg7=null, arg8=null, arg9=null):
args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9].slice(0, callback.get_argument_count())
var args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9].slice(0, callback.get_argument_count())
timer.set_meta("args", args)

if timer.is_stopped():
callback.callv(args)
timer.start(delay / 1000.0)
args = null
timer.set_meta("args", null)

return wrapper

Expand Down
9 changes: 5 additions & 4 deletions app/lib/home_apis/hass_ws/connection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ signal on_packed_received(packet: Dictionary)

signal _try_connect(success: bool)

const LOG_MESSAGES := false
const LOG_SENDING := true
const LOG_RECEIVING := false

var socket := WebSocketPeer.new()
var packet_callbacks := CallbackMap.new()
Expand Down Expand Up @@ -112,7 +113,7 @@ func _process(_delta):
on_disconnect.emit()

func handle_packet(packet: Dictionary):
if LOG_MESSAGES: print("Received packet: %s" % str(packet).substr(0, 1000))
if LOG_RECEIVING: print("Received packet: %s" % str(packet).substr(0, 1000))

on_packed_received.emit(packet)

Expand Down Expand Up @@ -168,15 +169,15 @@ func send_request_packet(packet: Dictionary, ignore_initial:=false):
return await promise.settled

func send_raw(packet: PackedByteArray):
if LOG_MESSAGES: print("Sending binary: %s" % packet.hex_encode())
if LOG_SENDING: print("Sending binary: %s" % packet.hex_encode())
socket.send(packet)

func send_packet(packet: Dictionary, with_id:=false):
if with_id:
packet.id = id
id += 1

if LOG_MESSAGES: print("Sending packet: %s" % _encode_packet(packet))
if LOG_SENDING: print("Sending packet: %s" % _encode_packet(packet))
socket.send_text(_encode_packet(packet))

func _decode_packet(packet: PackedByteArray):
Expand Down

0 comments on commit 92a19eb

Please sign in to comment.