Move MorseState into autoload

Before this every instance of the MorseBanner had its own state, but as
we want to use the state also for other purposes, it doesn't make sense
to keep it in each instance. Therefore we now have an autoload that
keeps the state as a singleton. The MorseBanner now gets its state from
the autoload, so no more communication is needed between the main part
and the MorseBanner.
This commit is contained in:
Sebastian Lohff 2025-02-02 18:36:32 +01:00
parent 49d2656335
commit ca3b8dba62
5 changed files with 30 additions and 35 deletions

20
autoloads/morse_state.gd Normal file
View File

@ -0,0 +1,20 @@
extends Node
var states: Array[int] = []
var curr_state := false
var last_change: int = 0
var start_time := 0
func reset() -> void:
last_change = Time.get_ticks_msec()
start_time = last_change
states = []
func set_state(state: bool) -> void:
if state == curr_state:
return
curr_state = state
var now := Time.get_ticks_msec()
states.push_front(now - last_change)
last_change = now

View File

@ -16,6 +16,10 @@ config/features=PackedStringArray("4.3", "Mobile")
run/max_fps=120
config/icon="res://icon.svg"
[autoload]
MorseState="*res://autoloads/morse_state.gd"
[rendering]
renderer/rendering_method="mobile"

View File

@ -11,6 +11,3 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_475pl")
[connection signal="reset_buffer" from="." to="." method="_on_reset_buffer"]
[connection signal="set_state" from="." to="." method="_on_set_state"]

View File

@ -29,9 +29,7 @@ func _ready():
print(OS.get_connected_midi_inputs())
func set_morse_state(state: bool):
%MorseBanner.set_state.emit(state)
%MorseBannerMedium.set_state.emit(state)
%MorseBannerSlow.set_state.emit(state)
MorseState.set_state(state)
morse_state = state
if state:
%ColorRect.color = Color(0, 128, 0)

View File

@ -1,22 +1,13 @@
extends Control
signal set_state(state: bool)
signal reset_buffer()
var color_on := Color(0, 128, 0)
var color_off := Color(0, 0, 0)
var curr_state := false
var last_delta := 0.0
@export_range(0.1, 120.0) var display_sec := 5.0
@export var stretch_display := false
var start_time := 0
var last_state_change_time: int = 0
var morse_step_perc := 0.45
var morse_states: Array[int] = []
func _ready():
_on_reset_buffer()
func _draw_morse_rect(x: float, width: float, state: bool):
var rect := Rect2(max(x, 0.0), morse_step_perc * size.y, width, (0.5 - morse_step_perc) * size.y)
@ -26,17 +17,17 @@ func _draw():
# black background
draw_rect(Rect2(0.0, 0.0, size.x, size.y), Color.BLACK)
var morse_on := curr_state
var first_time := Time.get_ticks_msec() - last_state_change_time
var morse_on := MorseState.curr_state
var first_time := Time.get_ticks_msec() - MorseState.last_change
var curr_x := float(size.x)
var px_per_s := 0.0
if not stretch_display:
px_per_s = size.x / display_sec
else:
px_per_s = size.x / (Time.get_ticks_msec() - start_time) * 1000.0
px_per_s = size.x / (Time.get_ticks_msec() - MorseState.start_time) * 1000.0
for duration in [first_time] + morse_states:
for duration in [first_time] + MorseState.states:
var rect_width: float = min(duration / 1000.0 * px_per_s, curr_x)
curr_x -= rect_width
if morse_on:
@ -49,18 +40,3 @@ func _draw():
func _process(_delta):
last_delta += _delta
queue_redraw()
func _on_set_state(state: bool) -> void:
if state == curr_state:
return
curr_state = state
var now := Time.get_ticks_msec()
morse_states.push_front(now - last_state_change_time)
last_state_change_time = now
func _on_reset_buffer() -> void:
last_state_change_time = Time.get_ticks_msec()
start_time = last_state_change_time
morse_states = []