From f66cd9a71b7910db9b9d4b9f2327f66eb86695f4 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Tue, 8 Oct 2019 22:17:33 +0200 Subject: [PATCH] Added preview icons and filter to library pane --- addons/material_maker/library.gd | 99 ++++++++++++++------------ addons/material_maker/library.tscn | 39 ++++++++++ addons/material_maker/library_tree.gd | 24 +++++++ addons/material_maker/main_window.tscn | 13 ++-- project.godot | 10 +-- 5 files changed, 129 insertions(+), 56 deletions(-) create mode 100644 addons/material_maker/library.tscn create mode 100644 addons/material_maker/library_tree.gd diff --git a/addons/material_maker/library.gd b/addons/material_maker/library.gd index cd5259f..b197362 100644 --- a/addons/material_maker/library.gd +++ b/addons/material_maker/library.gd @@ -1,58 +1,64 @@ tool -extends Tree +extends VBoxContainer -func get_drag_data(position): - var selected_item = get_selected() - if selected_item != null: - var data = selected_item.get_metadata(0) - if data == null: - return null - var preview - if data.has("icon") && data.has("library"): - var filename = data.library.left(data.library.rfind("."))+"/"+data.icon+".png" - preview = TextureRect.new() - preview.texture = ImageTexture.new() - preview.texture.load(filename) - elif data.has("type") and data.type == "uniform": - preview = ColorRect.new() - preview.rect_size = Vector2(32, 32) - if data.has("color"): - preview.color = Color(data.color.r, data.color.g, data.color.b, data.color.a) - else: - preview = Label.new() - preview.text = data.tree_item - set_drag_preview(preview) - return data - return null +var libraries = [] + +onready var tree = $Tree func _ready(): - var root = create_item() + tree.set_column_expand(0, true) + tree.set_column_expand(1, false) + tree.set_column_min_width(1, 32) var lib_path = OS.get_executable_path().get_base_dir()+"/library/base.json" if !add_library(lib_path): add_library("res://addons/material_maker/library/base.json") add_library("user://library/user.json") + update_tree() -func add_library(filename): - var root = get_root() +func add_library(file_name : String, filter : String = ""): + var root = tree.get_root() var file = File.new() - if file.open(filename, File.READ) != OK: + if file.open(file_name, File.READ) != OK: return false var lib = parse_json(file.get_as_text()) file.close() - if lib != null && lib.has("lib"): + if lib != null and lib.has("lib"): for m in lib.lib: - m.library = filename - add_item(m, m.tree_item) + m.library = file_name + libraries.push_back(lib.lib) return true return false -func add_item(item, item_name, item_parent = null): +func update_tree(filter : String = ""): + filter = filter.to_lower() + tree.clear() + var root = tree.create_item() + for l in libraries: + for m in l: + if filter == "" or m.tree_item.to_lower().find(filter) != -1: + add_item(m, m.tree_item, get_preview_texture(m), null, filter != "") + +func get_preview_texture(data : Dictionary): + if data.has("icon") and data.has("library"): + var image_path = data.library.left(data.library.rfind("."))+"/"+data.icon+".png" + var t : ImageTexture + if image_path.left(6) == "res://": + return load(image_path) + else: + t = ImageTexture.new() + var image : Image = Image.new() + image.load(image_path) + t.create_from_image(image) + return t + return null + +func add_item(item, item_name, item_icon = null, item_parent = null, force_expand = false): if item_parent == null: item.tree_item = item_name - item_parent = get_root() + item_parent = tree.get_root() var slash_position = item_name.find("/") if slash_position == -1: - var new_item = null + var new_item : TreeItem = null var c = item_parent.get_children() while c != null: if c.get_text(0) == item_name: @@ -60,13 +66,16 @@ func add_item(item, item_name, item_parent = null): break c = c.get_next() if new_item == null: - new_item = create_item(item_parent) + new_item = tree.create_item(item_parent) new_item.set_text(0, item_name) - new_item.collapsed = true + new_item.collapsed = !force_expand + if item_icon != null: + new_item.set_icon(1, item_icon) + new_item.set_icon_max_width(1, 32) if item.has("type") || item.has("nodes"): new_item.set_metadata(0, item) if item.has("collapsed"): - new_item.collapsed = item.collapsed + new_item.collapsed = item.collapsed and !force_expand return new_item else: var prefix = item_name.left(slash_position) @@ -79,18 +88,18 @@ func add_item(item, item_name, item_parent = null): break c = c.get_next() if new_parent == null: - new_parent = create_item(item_parent) - new_parent.collapsed = true + new_parent = tree.create_item(item_parent) + new_parent.collapsed = !force_expand new_parent.set_text(0, prefix) - return add_item(item, suffix, new_parent) + return add_item(item, suffix, item_icon, new_parent, force_expand) -func serialize_library(array, library_name, item = null): +func serialize_library(array, library_name = null, item = null): if item == null: - item = get_root() + item = tree.get_root() item = item.get_children() while item != null: var m = item.get_metadata(0) - if m != null && m.has("library") and m.library == library_name: + if m != null and (library_name == null or (m.has("library") and m.library == library_name)): array.append(m) serialize_library(array, library_name, item) item = item.get_next() @@ -98,8 +107,10 @@ func serialize_library(array, library_name, item = null): func save_library(library_name, item = null): var array = [] serialize_library(array, library_name) - print("Saving library "+library_name) var file = File.new() if file.open(library_name, File.WRITE) == OK: file.store_string(to_json({lib=array})) file.close() + +func _on_Filter_text_changed(filter): + update_tree(filter) diff --git a/addons/material_maker/library.tscn b/addons/material_maker/library.tscn new file mode 100644 index 0000000..9c333b3 --- /dev/null +++ b/addons/material_maker/library.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/material_maker/library.gd" type="Script" id=1] +[ext_resource path="res://addons/material_maker/library_tree.gd" type="Script" id=2] + +[node name="Library" type="VBoxContainer"] +margin_right = 352.0 +margin_bottom = 423.0 +script = ExtResource( 1 ) + +[node name="Tree" type="Tree" parent="."] +margin_right = 352.0 +margin_bottom = 395.0 +rect_min_size = Vector2( 100, 100 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 +columns = 2 +hide_root = true +select_mode = 1 +script = ExtResource( 2 ) + +[node name="HBoxContainer" type="HBoxContainer" parent="."] +margin_top = 399.0 +margin_right = 352.0 +margin_bottom = 423.0 +size_flags_horizontal = 3 + +[node name="FilterLabel" type="Label" parent="HBoxContainer"] +margin_top = 5.0 +margin_right = 37.0 +margin_bottom = 19.0 +text = "Filter:" + +[node name="Filter" type="LineEdit" parent="HBoxContainer"] +margin_left = 41.0 +margin_right = 352.0 +margin_bottom = 24.0 +size_flags_horizontal = 3 +[connection signal="text_changed" from="HBoxContainer/Filter" to="." method="_on_Filter_text_changed"] diff --git a/addons/material_maker/library_tree.gd b/addons/material_maker/library_tree.gd new file mode 100644 index 0000000..6319edf --- /dev/null +++ b/addons/material_maker/library_tree.gd @@ -0,0 +1,24 @@ +extends Tree + +func get_drag_data(position): + var selected_item = get_selected() + if selected_item != null: + var data = selected_item.get_metadata(0) + if data == null: + return null + var preview + var preview_texture = get_parent().get_preview_texture(data) + if preview_texture != null: + preview = TextureRect.new() + preview.texture = preview_texture + elif data.has("type") and data.type == "uniform": + preview = ColorRect.new() + preview.rect_size = Vector2(32, 32) + if data.has("color"): + preview.color = Color(data.color.r, data.color.g, data.color.b, data.color.a) + else: + preview = Label.new() + preview.text = data.tree_item + set_drag_preview(preview) + return data + return null diff --git a/addons/material_maker/main_window.tscn b/addons/material_maker/main_window.tscn index f2e496a..237882e 100644 --- a/addons/material_maker/main_window.tscn +++ b/addons/material_maker/main_window.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=7 format=2] [ext_resource path="res://addons/material_maker/main_window.gd" type="Script" id=1] -[ext_resource path="res://addons/material_maker/library.gd" type="Script" id=2] +[ext_resource path="res://addons/material_maker/library.tscn" type="PackedScene" id=2] [ext_resource path="res://addons/material_maker/preview.tscn" type="PackedScene" id=3] [ext_resource path="res://addons/material_maker/widgets/tabs.gd" type="Script" id=4] [ext_resource path="res://addons/material_maker/engine/renderer.tscn" type="PackedScene" id=5] @@ -63,20 +63,17 @@ margin_bottom = 696.0 size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="Library" type="Tree" parent="VBoxContainer/HBoxContainer/VBoxContainer"] +[node name="Library" parent="VBoxContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 2 )] margin_right = 314.0 -margin_bottom = 342.0 -rect_min_size = Vector2( 100, 100 ) -size_flags_horizontal = 3 +margin_bottom = 411.0 size_flags_vertical = 3 -hide_root = true -script = ExtResource( 2 ) +size_flags_stretch_ratio = 1.5 [node name="Preview" parent="VBoxContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 3 )] anchor_left = 0.0 anchor_right = 0.0 margin_left = 0.0 -margin_top = 354.0 +margin_top = 423.0 margin_right = 314.0 margin_bottom = 696.0 diff --git a/project.godot b/project.godot index ae19cfd..1e52dd0 100644 --- a/project.godot +++ b/project.godot @@ -89,6 +89,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://addons/material_maker/widgets/gradient_editor.gd" }, { +"base": "GraphEdit", +"class": "MMGraphEdit", +"language": "GDScript", +"path": "res://addons/material_maker/graph_edit.gd" +}, { "base": "GraphNode", "class": "MMGraphNodeGeneric", "language": "GDScript", @@ -126,6 +131,7 @@ _global_script_class_icons={ "MMGenTexture": "", "MMGradient": "", "MMGradientEditor": "", +"MMGraphEdit": "", "MMGraphNodeGeneric": "", "MMGraphNodeRemote": "", "MMNodeLink": "", @@ -156,10 +162,6 @@ gdscript/warnings/unsafe_call_argument=true window/size/width=1280 window/size/height=720 -[editor_plugins] - -enabled=PoolStringArray( "material_maker" ) - [logging] file_logging/enable_file_logging=true