69 lines
1.8 KiB
GDScript
69 lines
1.8 KiB
GDScript
class_name PalettePanel
|
|
extends VBoxContainer
|
|
## Catalog-sourced object palette (docs/architecture/editor.md §7.2
|
|
## `PalettePanel`). Lists every id in an `EditorObjectCatalog` and emits
|
|
## which one the player picked; `InGameEditor` remembers it as the pending
|
|
## `catalog_id` for the next `ObjectPlaceTool` placement.
|
|
|
|
signal object_selected(catalog_id: String)
|
|
|
|
var _list: ItemList
|
|
var _catalog: EditorObjectCatalog = null
|
|
var _ids: Array[String] = []
|
|
|
|
|
|
func _init() -> void:
|
|
name = "PalettePanel"
|
|
size_flags_vertical = Control.SIZE_EXPAND_FILL
|
|
|
|
|
|
func _ready() -> void:
|
|
var header := Label.new()
|
|
header.text = "Palette"
|
|
header.add_theme_font_size_override("font_size", 15)
|
|
header.add_theme_color_override("font_color", Color(1, 0.95, 0.4))
|
|
add_child(header)
|
|
|
|
var hint := Label.new()
|
|
hint.text = "Click an item to place on the grid:"
|
|
hint.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
hint.add_theme_font_size_override("font_size", 11)
|
|
hint.add_theme_color_override("font_color", Color(0.7, 0.7, 0.7))
|
|
add_child(hint)
|
|
|
|
_list = ItemList.new()
|
|
_list.size_flags_vertical = Control.SIZE_EXPAND_FILL
|
|
_list.custom_minimum_size = Vector2(0, 180)
|
|
_list.item_selected.connect(_on_item_selected)
|
|
add_child(_list)
|
|
|
|
|
|
func set_catalog(catalog: EditorObjectCatalog) -> void:
|
|
_catalog = catalog
|
|
_refresh()
|
|
|
|
|
|
func _refresh() -> void:
|
|
_list.clear()
|
|
_ids.clear()
|
|
if _catalog == null or not _catalog.is_loaded():
|
|
return
|
|
for id in _catalog.get_all_ids():
|
|
var entry := _catalog.resolve(id)
|
|
var label: String = entry.get("display_name", id)
|
|
_list.add_item(label)
|
|
_ids.append(id)
|
|
|
|
|
|
func _on_item_selected(index: int) -> void:
|
|
if index < 0 or index >= _ids.size():
|
|
return
|
|
object_selected.emit(_ids[index])
|
|
|
|
|
|
func get_selected_catalog_id() -> String:
|
|
var selected: PackedInt32Array = _list.get_selected_items()
|
|
if selected.is_empty():
|
|
return ""
|
|
return _ids[selected[0]]
|