Files
owniverse/ui/actions/actions.gd
T

81 lines
2.7 KiB
GDScript

extends HBoxContainer
class_name OwniverseAction
signal action_pressed(page_id, amount: float)
@onready var action_labels: Array = [
$ActionControl0/Label,
$ActionControl1/Label,
$ActionControl2/Label,
$ActionControl3/Label,
$ActionControl4/Label,
]
var page_id: int = -1
var current_item_id = ""
var action_values: Dictionary = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
}
func set_actions(item_id: String, value: float, preview: Callable = Callable()) -> void:
current_item_id = item_id
var requests: Array = _get_distribution(value)
var unique_requests: Array = []
var displayed: Array = []
var labels: Dictionary = {}
# Prefer the largest request (including MAX) when previews or rounded
# scientific labels coincide. Keep the request paired with its preview.
for i in range(requests.size() - 1, -1, -1):
if requests[i] <= 0.0: continue
var amount: float = preview.call(requests[i]) if preview.is_valid() else requests[i]
if not is_finite(amount) or amount < 1.0 or amount != floorf(amount): continue
var text: String = _format_action_value(amount)
if displayed.has(amount) or labels.has(text): continue
labels[text] = true
unique_requests.push_front(requests[i])
displayed.push_front(amount)
while unique_requests.size() < 5:
unique_requests.append(0.0)
displayed.append(0.0)
_set_action_values(unique_requests, displayed)
func _get_distribution(value: float) -> Array:
if value < 1.0 or not is_finite(value):
return [0.0, 0.0, 0.0, 0.0, 0.0]
value = floorf(value)
var distribution: Array = []
var power: float = 1.0
while power < value:
distribution.append(power)
if distribution.size() > 4: distribution.pop_front()
power *= 10.0
distribution.append(value)
while distribution.size() < 5: distribution.append(0.0)
return distribution
func _set_action_values(values: Array, displayed: Array) -> void:
for ind in range(0, 5):
action_values[ind] = values[ind]
# Keep the original request for execution; show the exact planned output.
var text: String = _format_action_value(displayed[ind])
if action_labels[ind].text != text:
action_labels[ind].text = text
action_labels[ind].get_parent().get_node("TextureButton").disabled = values[ind] <= 0.0
func _format_action_value(value: float) -> String:
if value < 1.0 or not is_finite(value) or value != floorf(value):
return ""
return Global.format_number(value)
func _on_action_button_pressed(button_id: int) -> void:
if action_values.get(button_id, 0.0) > 0.0:
action_pressed.emit(page_id, action_values[button_id])