From 8ea665f7549ec37057933c949741eb1b2d9928b0 Mon Sep 17 00:00:00 2001 From: don-tnowe <67479453+don-tnowe@users.noreply.github.com> Date: Fri, 23 Jun 2023 18:07:33 +0300 Subject: [PATCH] Improve display for Resource arrays and Dicts Closes #27 --- .../typed_cells/cell_editor_array.gd | 14 +++++-- .../typed_cells/cell_editor_dict.gd | 37 +++++++++++++++++++ .../typed_cells/cell_editor_enum_array.gd | 9 +++-- example/Random Upgrades/upgrade_data.gd | 2 + .../upgrades/mastery_magic.tres | 6 ++- .../upgrades/mastery_strength.tres | 6 ++- 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 addons/resources_spreadsheet_view/typed_cells/cell_editor_dict.gd diff --git a/addons/resources_spreadsheet_view/typed_cells/cell_editor_array.gd b/addons/resources_spreadsheet_view/typed_cells/cell_editor_array.gd index 6b9f93c..061541b 100644 --- a/addons/resources_spreadsheet_view/typed_cells/cell_editor_array.gd +++ b/addons/resources_spreadsheet_view/typed_cells/cell_editor_array.gd @@ -2,6 +2,7 @@ extends SheetsCellEditor const SettingsGrid := preload("res://addons/resources_spreadsheet_view/settings_grid.gd") + func can_edit_value(value, type, property_hint, column_index) -> bool: return type == TYPE_PACKED_STRING_ARRAY or type == TYPE_ARRAY @@ -25,17 +26,24 @@ func set_value(node : Control, value): else: children[i].visible = true - _write_value_to_child(value[i], column_hints, children[i], colored) + _write_value_to_child(value[i], value[i], column_hints, children[i], colored) -func _write_value_to_child(value, hint_arr : PackedStringArray, child : Label, colored : bool): +func _write_value_to_child(value, key, hint_arr : PackedStringArray, child : Label, colored : bool): + if value is Resource: + value = _resource_to_string(value) + child.text = str(value) child.self_modulate = ( Color.WHITE if !colored else - Color(str(value).hash()) + Color(0.25, 0.25, 0.25, 1.0) + Color(str(key).hash()) + Color(0.25, 0.25, 0.25, 1.0) ) +func _resource_to_string(res : Resource): + return res.resource_name if res.resource_name != "" else res.resource_path.get_file() + + func is_text(): return false diff --git a/addons/resources_spreadsheet_view/typed_cells/cell_editor_dict.gd b/addons/resources_spreadsheet_view/typed_cells/cell_editor_dict.gd new file mode 100644 index 0000000..08f1cf9 --- /dev/null +++ b/addons/resources_spreadsheet_view/typed_cells/cell_editor_dict.gd @@ -0,0 +1,37 @@ +extends "res://addons/resources_spreadsheet_view/typed_cells/cell_editor_array.gd" + + +func can_edit_value(value, type, property_hint, column_index) -> bool: + return type == TYPE_DICTIONARY + + +func create_cell(caller : Control) -> Control: + return load(CELL_SCENE_DIR + "array.tscn").instantiate() + + +func set_value(node : Control, value): + var children = node.get_node("Box").get_children() + node.custom_minimum_size.x = ProjectSettings.get_setting(SettingsGrid.SETTING_PREFIX + "array_min_width") + var colored = ProjectSettings.get_setting(SettingsGrid.SETTING_PREFIX + "color_arrays") + while children.size() < value.size(): + children.append(Label.new()) + node.get_node("Box").add_child(children[children.size() - 1]) + + var column_hints = hint_strings_array[node.get_index() % hint_strings_array.size()] + var values : Array = value.values() + var keys : Array = value.keys() + + for i in children.size(): + if i >= values.size(): + children[i].visible = false + + else: + children[i].visible = true + if values[i] is Resource: values[i] = _resource_to_string(values[i]) + if keys[i] is Resource: keys[i] = _resource_to_string(keys[i]) + + _write_value_to_child("%s ◆ %s" % [keys[i], values[i]], keys[i], column_hints, children[i], colored) + + +func is_text(): + return false diff --git a/addons/resources_spreadsheet_view/typed_cells/cell_editor_enum_array.gd b/addons/resources_spreadsheet_view/typed_cells/cell_editor_enum_array.gd index 54f3702..7e141fc 100644 --- a/addons/resources_spreadsheet_view/typed_cells/cell_editor_enum_array.gd +++ b/addons/resources_spreadsheet_view/typed_cells/cell_editor_enum_array.gd @@ -12,11 +12,14 @@ func can_edit_value(value, type, property_hint, column_index) -> bool: return hint_strings_array[column_index][0].begins_with("2/2:") -func _write_value_to_child(value, hint_arr : PackedStringArray, child : Label, colored : bool): +func _write_value_to_child(value, key, hint_arr : PackedStringArray, child : Label, colored : bool): + var value_str : String if value == 0: # Enum array hints have "2/3:" before list. var found := hint_arr[0].find(":") + 1 - super._write_value_to_child(hint_arr[0].substr(hint_arr[0].find(":") + 1), hint_arr, child, colored) + value_str = hint_arr[0].substr(hint_arr[0].find(":") + 1) else: - super._write_value_to_child(hint_arr[value], hint_arr, child, colored) + value_str = hint_arr[value] + + super(value_str, value_str, hint_arr, child, colored) diff --git a/example/Random Upgrades/upgrade_data.gd b/example/Random Upgrades/upgrade_data.gd index 72d4aa6..115b876 100644 --- a/example/Random Upgrades/upgrade_data.gd +++ b/example/Random Upgrades/upgrade_data.gd @@ -1,4 +1,5 @@ @tool +class_name UpgradeData extends Resource enum Attributes { @@ -16,6 +17,7 @@ enum Attributes { @export var attributes : Array[Attributes] @export var icon : Texture @export var custom_scene : PackedScene +@export var prerequisites : Array[UpgradeData] @export var color2 := Color.WHITE @export var base_weight := 10.0 @export var is_notable := false diff --git a/example/Random Upgrades/upgrades/mastery_magic.tres b/example/Random Upgrades/upgrades/mastery_magic.tres index c5349ea..49c2058 100644 --- a/example/Random Upgrades/upgrades/mastery_magic.tres +++ b/example/Random Upgrades/upgrades/mastery_magic.tres @@ -1,7 +1,10 @@ -[gd_resource type="Resource" load_steps=3 format=3 uid="uid://2totlbwc40e8"] +[gd_resource type="Resource" script_class="UpgradeData" load_steps=6 format=3 uid="uid://2totlbwc40e8"] [ext_resource type="Texture2D" uid="uid://djjpa8lluue1g" path="res://example/Random Upgrades/icons/all_icons/all_icons_5.tres" id="1_ardng"] [ext_resource type="Script" path="res://example/Random Upgrades/upgrade_data.gd" id="2"] +[ext_resource type="Resource" uid="uid://rjxr4qtfc6qd" path="res://example/Random Upgrades/upgrades/weapon_fireball.tres" id="2_485ad"] +[ext_resource type="Resource" uid="uid://djqq1lqaevth5" path="res://example/Random Upgrades/upgrades/weapon_blizzard.tres" id="3_xnxxn"] +[ext_resource type="Resource" uid="uid://b0kdw067vtgvn" path="res://example/Random Upgrades/upgrades/weapon_lightning.tres" id="4_kn0kg"] [resource] resource_name = "Mastery: Magic" @@ -12,6 +15,7 @@ tags = Array[String](["magic", "mastery"]) type = 2 attributes = Array[int]([1, 1, 4]) icon = ExtResource("1_ardng") +prerequisites = Array[ExtResource("2")]([ExtResource("2_485ad"), ExtResource("3_xnxxn"), ExtResource("4_kn0kg")]) color2 = Color(0.407843, 0.192157, 0.827451, 1) base_weight = 1.0 is_notable = true diff --git a/example/Random Upgrades/upgrades/mastery_strength.tres b/example/Random Upgrades/upgrades/mastery_strength.tres index 02aa566..d9a14d3 100644 --- a/example/Random Upgrades/upgrades/mastery_strength.tres +++ b/example/Random Upgrades/upgrades/mastery_strength.tres @@ -1,7 +1,10 @@ -[gd_resource type="Resource" load_steps=3 format=3 uid="uid://d1suh8iai43st"] +[gd_resource type="Resource" script_class="UpgradeData" load_steps=6 format=3 uid="uid://d1suh8iai43st"] [ext_resource type="Texture2D" uid="uid://dfpwu4ra3fxgx" path="res://example/Random Upgrades/icons/all_icons/all_icons_2.tres" id="1_442ey"] [ext_resource type="Script" path="res://example/Random Upgrades/upgrade_data.gd" id="2"] +[ext_resource type="Resource" uid="uid://cux4x0qopwiqk" path="res://example/Random Upgrades/upgrades/weapon_dagger.tres" id="2_kb33p"] +[ext_resource type="Resource" uid="uid://b78jqcpgef2ud" path="res://example/Random Upgrades/upgrades/weapon_axe.tres" id="3_tt25f"] +[ext_resource type="Resource" uid="uid://xl0yx8uq6bfp" path="res://example/Random Upgrades/upgrades/weapon_spear.tres" id="4_kpsn8"] [resource] resource_name = "Mastery: Strength" @@ -12,6 +15,7 @@ tags = Array[String](["strength", "mastery"]) type = 2 attributes = Array[int]([0, 0, 4]) icon = ExtResource("1_442ey") +prerequisites = Array[ExtResource("2")]([ExtResource("2_kb33p"), ExtResource("3_tt25f"), ExtResource("4_kpsn8")]) color2 = Color(0.992157, 0.941176, 0.2, 1) base_weight = 1.0 is_notable = true