Refactor multi-resource UI and transfers
This commit is contained in:
parent
af5e08e5a1
commit
8231e165ff
20 changed files with 648 additions and 435 deletions
161
ResourceTransferList.gd
Normal file
161
ResourceTransferList.gd
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
class_name ResourceTransferList
|
||||
extends Control
|
||||
|
||||
# Two-sided cargo control. Resources are rows and locations stay fixed:
|
||||
# fleet on the left, the current building on the right. Horizontal arrows
|
||||
# make the result of a press visible before the player commits to it.
|
||||
|
||||
signal transfer_requested(resource_type: String, to_building: bool)
|
||||
|
||||
const HEADER_HEIGHT: float = 58.0
|
||||
const FLEET_COLOR: Color = Color(1.0, 0.78, 0.25)
|
||||
const LOCAL_COLOR: Color = Color(0.45, 0.9, 0.55)
|
||||
const RULE_COLOR: Color = Color(0.18, 0.38, 0.52)
|
||||
|
||||
var building_id: int = 0
|
||||
var _destination_heading: Label
|
||||
var _rows: Dictionary = {}
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var fleet_heading := _make_label("FLEET HOLD", 22, FLEET_COLOR)
|
||||
fleet_heading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(fleet_heading)
|
||||
_destination_heading = _make_label("THIS BUILDING", 22, LOCAL_COLOR)
|
||||
_destination_heading.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(_destination_heading)
|
||||
|
||||
for resource_type in GameState.RESOURCE_TYPES:
|
||||
var colour: Color = GameState.resource_color(resource_type)
|
||||
var glyph := ResourceGlyph.new()
|
||||
glyph.resource_type = resource_type
|
||||
add_child(glyph)
|
||||
var name_label := _make_label(GameState.resource_name(resource_type).to_upper(), 25, colour)
|
||||
name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(name_label)
|
||||
var fleet_amount := _make_label("0", 42, FLEET_COLOR)
|
||||
fleet_amount.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(fleet_amount)
|
||||
var local_amount := _make_label("0", 42, LOCAL_COLOR)
|
||||
local_amount.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(local_amount)
|
||||
|
||||
var unload := IconButton.new()
|
||||
unload.name = "%sToBuildingButton" % GameState.resource_name(resource_type)
|
||||
unload.icon = IconButton.Icon.RIGHT_ARROW
|
||||
unload.glow_color = colour
|
||||
unload.pressed.connect(_request_transfer.bind(resource_type, true))
|
||||
add_child(unload)
|
||||
var unload_label := _make_label("UNLOAD ALL →", 20, colour)
|
||||
unload_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(unload_label)
|
||||
|
||||
var load := IconButton.new()
|
||||
load.name = "%sToFleetButton" % GameState.resource_name(resource_type)
|
||||
load.icon = IconButton.Icon.LEFT_ARROW
|
||||
load.glow_color = colour
|
||||
load.pressed.connect(_request_transfer.bind(resource_type, false))
|
||||
add_child(load)
|
||||
var load_label := _make_label("← LOAD ALL", 20, colour)
|
||||
load_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
add_child(load_label)
|
||||
|
||||
_rows[resource_type] = {
|
||||
"glyph": glyph, "name": name_label,
|
||||
"fleet_amount": fleet_amount, "local_amount": local_amount,
|
||||
"unload": unload, "unload_label": unload_label,
|
||||
"load": load, "load_label": load_label,
|
||||
}
|
||||
|
||||
resized.connect(_layout)
|
||||
_layout()
|
||||
refresh()
|
||||
|
||||
|
||||
func _make_label(text_value: String, font_size: int, colour: Color) -> Label:
|
||||
var label := Label.new()
|
||||
label.text = text_value
|
||||
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||||
label.add_theme_font_size_override("font_size", font_size)
|
||||
label.add_theme_color_override("font_color", colour)
|
||||
label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
return label
|
||||
|
||||
|
||||
func _request_transfer(resource_type: String, to_building: bool) -> void:
|
||||
transfer_requested.emit(resource_type, to_building)
|
||||
|
||||
|
||||
func set_destination(id: int, heading: String) -> void:
|
||||
building_id = id
|
||||
if _destination_heading != null:
|
||||
_destination_heading.text = heading.to_upper()
|
||||
refresh()
|
||||
|
||||
|
||||
func refresh() -> void:
|
||||
for resource_type in GameState.RESOURCE_TYPES:
|
||||
var row: Dictionary = _rows.get(resource_type, {})
|
||||
if row.is_empty():
|
||||
continue
|
||||
var fleet_amount: int = GameState.fleet_resource(resource_type)
|
||||
var local_amount: int = GameState.building_resource(building_id, resource_type)
|
||||
row.fleet_amount.text = str(fleet_amount)
|
||||
row.local_amount.text = str(local_amount)
|
||||
row.unload.enabled = fleet_amount > 0
|
||||
row.load.enabled = local_amount > 0
|
||||
row.unload.queue_redraw()
|
||||
row.load.queue_redraw()
|
||||
|
||||
|
||||
func _layout() -> void:
|
||||
if _destination_heading == null:
|
||||
return
|
||||
var side_width: float = size.x * 0.36
|
||||
var centre_width: float = size.x - side_width * 2.0
|
||||
var right_x: float = side_width + centre_width
|
||||
var fleet_heading: Label = get_child(0)
|
||||
fleet_heading.position = Vector2(0.0, 0.0)
|
||||
fleet_heading.size = Vector2(side_width, HEADER_HEIGHT)
|
||||
_destination_heading.position = Vector2(right_x, 0.0)
|
||||
_destination_heading.size = Vector2(side_width, HEADER_HEIGHT)
|
||||
|
||||
var row_height: float = (size.y - HEADER_HEIGHT) / maxf(1.0, float(_rows.size()))
|
||||
var row_index: int = 0
|
||||
for resource_type in GameState.RESOURCE_TYPES:
|
||||
var row: Dictionary = _rows[resource_type]
|
||||
var y: float = HEADER_HEIGHT + float(row_index) * row_height
|
||||
var glyph_size: float = minf(58.0, row_height * 0.22)
|
||||
row.glyph.position = Vector2(size.x * 0.5 - glyph_size * 0.5, y + 12.0)
|
||||
row.glyph.size = Vector2(glyph_size, glyph_size)
|
||||
row.name.position = Vector2(side_width, y + 12.0 + glyph_size)
|
||||
row.name.size = Vector2(centre_width, 38.0)
|
||||
|
||||
row.fleet_amount.position = Vector2(0.0, y + 12.0)
|
||||
row.fleet_amount.size = Vector2(side_width, 62.0)
|
||||
row.local_amount.position = Vector2(right_x, y + 12.0)
|
||||
row.local_amount.size = Vector2(side_width, 62.0)
|
||||
|
||||
var button_size: float = minf(118.0, row_height * 0.42)
|
||||
var button_y: float = y + 78.0
|
||||
row.unload.position = Vector2(side_width * 0.5 - button_size * 0.5, button_y)
|
||||
row.unload.size = Vector2(button_size, button_size)
|
||||
row.load.position = Vector2(right_x + side_width * 0.5 - button_size * 0.5, button_y)
|
||||
row.load.size = Vector2(button_size, button_size)
|
||||
var caption_y: float = button_y + button_size + 2.0
|
||||
row.unload_label.position = Vector2(0.0, caption_y)
|
||||
row.unload_label.size = Vector2(side_width, 36.0)
|
||||
row.load_label.position = Vector2(right_x, caption_y)
|
||||
row.load_label.size = Vector2(side_width, 36.0)
|
||||
row_index += 1
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
var row_height: float = (size.y - HEADER_HEIGHT) / maxf(1.0, float(_rows.size()))
|
||||
NeonDraw.draw_glowing_line(self, Vector2(0.0, HEADER_HEIGHT), Vector2(size.x, HEADER_HEIGHT),
|
||||
RULE_COLOR, 0.5, 0.7)
|
||||
for i in range(1, _rows.size()):
|
||||
var y: float = HEADER_HEIGHT + float(i) * row_height
|
||||
NeonDraw.draw_glowing_line(self, Vector2(0.0, y), Vector2(size.x, y),
|
||||
RULE_COLOR, 0.4, 0.6)
|
||||
Loading…
Reference in a new issue