game/user_interface/worker_prompt.gd

57 lines
1.1 KiB
GDScript3
Raw Normal View History

class_name WorkerPrompt extends Control
##
2023-02-01 01:05:48 +01:00
## The work being performed by the prompt has ended.
##
signal prompt_completed()
##
2023-02-01 01:05:48 +01:00
## The prompt has been started.
##
signal prompted()
var _worker_thread := Thread.new()
##
2023-02-01 01:05:48 +01:00
## Used to display a message from the prompt or [code]null[/code] to not display anything.
##
@export
var label: Label = null
##
2023-02-01 01:05:48 +01:00
## Used to display the progress of the prompt or [code]null[/code] to not display anything.
##
@export
var progress: Range = null
##
2023-02-01 01:05:48 +01:00
## Starts the prompt, emitting [signal prompted].
##
2023-02-01 01:05:48 +01:00
## [signal prompt_completed] is emitted when the work being performed by the prompt has ended.
##
func prompt(display_message: String, steps: Array) -> void:
LocalPlayer.override_controls(hidden)
show()
prompted.emit()
if label != null:
label.text = display_message
_worker_thread.start(func () -> void:
var count := steps.size()
var total := float(count)
for i in count:
steps[i].call()
if progress != null:
progress.value = i / total)
while _worker_thread.is_alive():
await get_tree().process_frame
_worker_thread.wait_to_finish()
prompt_completed.emit()
hide()