2024-01-17 07:31:39 +01:00
|
|
|
# (c) 2023-present Eroax
|
|
|
|
# (c) 2023-present Yagich
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
extends VBoxContainer
|
|
|
|
class_name VariableViewer
|
|
|
|
@onready var variable_tree: Tree = %VariableTree
|
2024-01-17 10:45:38 +01:00
|
|
|
@onready var types_popup: PopupMenu = %TypesPopup
|
2024-01-17 07:31:39 +01:00
|
|
|
var root: TreeItem
|
|
|
|
|
2024-01-18 08:17:26 +01:00
|
|
|
var _old_name: String
|
|
|
|
|
|
|
|
signal top_field_edited(old_name: String, new_name: String, new_value: Variant)
|
|
|
|
|
2024-01-17 10:45:38 +01:00
|
|
|
|
2024-01-17 07:31:39 +01:00
|
|
|
func _ready() -> void:
|
|
|
|
variable_tree.set_column_title(0, "Name")
|
|
|
|
variable_tree.set_column_title(1, "Value")
|
|
|
|
variable_tree.set_column_title(2, "Type")
|
|
|
|
variable_tree.set_column_expand(1, true)
|
|
|
|
variable_tree.set_column_expand_ratio(0, 15)
|
|
|
|
variable_tree.set_column_expand_ratio(1, 70)
|
|
|
|
variable_tree.set_column_expand_ratio(2, 15)
|
|
|
|
root = variable_tree.create_item()
|
|
|
|
rebuild_variable_tree()
|
2024-01-17 10:45:38 +01:00
|
|
|
|
|
|
|
variable_tree.item_activated.connect(
|
|
|
|
func():
|
|
|
|
var column := variable_tree.get_selected_column()
|
|
|
|
var item := variable_tree.get_selected()
|
2024-01-18 08:17:26 +01:00
|
|
|
|
|
|
|
# do nothing if this is an array and user is trying to edit index
|
|
|
|
# (TODO: proper reordering)
|
|
|
|
if column == 0 && item.get_meta(&"indexed"):
|
|
|
|
return
|
|
|
|
|
2024-01-17 10:45:38 +01:00
|
|
|
if column < 2:
|
2024-01-18 08:17:26 +01:00
|
|
|
_old_name = item.get_text(0)
|
|
|
|
|
2024-01-17 10:45:38 +01:00
|
|
|
variable_tree.edit_selected(true)
|
|
|
|
else:
|
|
|
|
var pos := get_global_mouse_position()
|
|
|
|
var r := Rect2i(Vector2i(pos), Vector2i(0, 100))
|
|
|
|
|
|
|
|
for i in types_popup.get_item_count():
|
|
|
|
types_popup.set_item_checked(i, false)
|
|
|
|
|
|
|
|
types_popup.set_item_checked(item.get_metadata(2), true)
|
|
|
|
types_popup.popup(r)
|
|
|
|
)
|
|
|
|
|
2024-01-18 08:17:26 +01:00
|
|
|
variable_tree.item_edited.connect(
|
|
|
|
func():
|
|
|
|
var item := variable_tree.get_selected()
|
|
|
|
var new_name := item.get_text(0)
|
|
|
|
var new_value
|
|
|
|
var type: DeckType.Types = item.get_metadata(2)
|
|
|
|
|
|
|
|
match type:
|
|
|
|
DeckType.Types.STRING:
|
|
|
|
new_value = item.get_text(1)
|
|
|
|
DeckType.Types.BOOL:
|
|
|
|
new_value = item.is_checked(1)
|
|
|
|
DeckType.Types.NUMERIC:
|
|
|
|
new_value = item.get_range(1)
|
|
|
|
_:
|
|
|
|
new_value = item.get_meta(&"value")
|
|
|
|
|
|
|
|
item.set_meta(&"value", new_value)
|
|
|
|
|
|
|
|
if item.get_meta(&"container", -1) is int:
|
|
|
|
top_field_edited.emit(_old_name, new_name, new_value)
|
|
|
|
else:
|
|
|
|
var container = item.get_meta(&"container")
|
|
|
|
|
|
|
|
# array
|
|
|
|
if item.get_meta(&"indexed", false):
|
|
|
|
var index := int(new_name)
|
|
|
|
(container as Array)[index] = new_value
|
|
|
|
# dictionary
|
|
|
|
else:
|
|
|
|
var key := new_name
|
|
|
|
(container as Dictionary).erase(_old_name)
|
|
|
|
(container as Dictionary)[key] = new_value
|
|
|
|
)
|
|
|
|
|
2024-01-17 10:45:38 +01:00
|
|
|
for i in DeckType.Types.size():
|
|
|
|
types_popup.add_radio_check_item(DeckType.type_str(i))
|
2024-01-17 07:31:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
func rebuild_variable_tree(data: Dictionary = {}) -> void:
|
|
|
|
#variable_tree.clear() # godot will raw dog a nullptr later if we clear the whole tree
|
|
|
|
for i in root.get_children():
|
|
|
|
i.free()
|
|
|
|
|
|
|
|
for i in data:
|
2024-01-17 08:55:54 +01:00
|
|
|
add_item(i, data[i])
|
|
|
|
|
|
|
|
|
2024-01-18 08:17:26 +01:00
|
|
|
func add_item(item_name: String, item_value: Variant, parent: TreeItem = root, container: Variant = -1, indexed: bool = false) -> TreeItem:
|
|
|
|
# the container parameter is -1 instead of null because Object#get_metadata() logs an error
|
|
|
|
# if the default parameter is null
|
2024-01-17 08:55:54 +01:00
|
|
|
var item := variable_tree.create_item(parent)
|
|
|
|
item.set_text(0, item_name)
|
|
|
|
var type: DeckType.Types = DeckType.INVERSE_GODOT_TYPES_MAP[typeof(item_value)]
|
2024-01-17 10:45:23 +01:00
|
|
|
|
|
|
|
match type:
|
|
|
|
DeckType.Types.NUMERIC:
|
|
|
|
item.set_cell_mode(1, TreeItem.CELL_MODE_RANGE)
|
|
|
|
item.set_range(1, item_value)
|
|
|
|
item.set_range_config(1, -9999, 9999, 0.0001)
|
2024-01-18 08:17:26 +01:00
|
|
|
DeckType.Types.BOOL:
|
|
|
|
item.set_cell_mode(1, TreeItem.CELL_MODE_CHECK)
|
|
|
|
item.set_checked(1, item_value)
|
2024-01-17 10:45:23 +01:00
|
|
|
_:
|
|
|
|
item.set_text(1, str(item_value))
|
|
|
|
|
2024-01-18 08:17:26 +01:00
|
|
|
item.set_meta(&"container", container)
|
|
|
|
item.set_meta(&"indexed", indexed)
|
|
|
|
item.set_meta(&"value", item_value)
|
|
|
|
|
2024-01-17 08:55:54 +01:00
|
|
|
item.set_text(2, DeckType.type_str(type))
|
2024-01-17 10:45:38 +01:00
|
|
|
item.set_metadata(2, type)
|
2024-01-17 08:55:54 +01:00
|
|
|
if item_value is Dictionary:
|
|
|
|
for i in item_value:
|
2024-01-18 08:17:26 +01:00
|
|
|
add_item(i, item_value[i], item, item_value)
|
2024-01-17 08:55:54 +01:00
|
|
|
if item_value is Array:
|
|
|
|
for i in (item_value as Array).size():
|
2024-01-18 08:17:26 +01:00
|
|
|
add_item(str(i), item_value[i], item, item_value, true)
|
2024-01-17 08:55:54 +01:00
|
|
|
item.collapsed = true
|
2024-01-18 08:17:26 +01:00
|
|
|
|
2024-01-17 08:55:54 +01:00
|
|
|
return item
|