mirror of
https://github.com/Relintai/material-maker.git
synced 2024-11-13 06:27:18 +01:00
Added preview icons and filter to library pane
This commit is contained in:
parent
ad2de4a52e
commit
f66cd9a71b
@ -1,58 +1,64 @@
|
|||||||
tool
|
tool
|
||||||
extends Tree
|
extends VBoxContainer
|
||||||
|
|
||||||
func get_drag_data(position):
|
var libraries = []
|
||||||
var selected_item = get_selected()
|
|
||||||
if selected_item != null:
|
onready var tree = $Tree
|
||||||
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
|
|
||||||
|
|
||||||
func _ready():
|
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"
|
var lib_path = OS.get_executable_path().get_base_dir()+"/library/base.json"
|
||||||
if !add_library(lib_path):
|
if !add_library(lib_path):
|
||||||
add_library("res://addons/material_maker/library/base.json")
|
add_library("res://addons/material_maker/library/base.json")
|
||||||
add_library("user://library/user.json")
|
add_library("user://library/user.json")
|
||||||
|
update_tree()
|
||||||
|
|
||||||
func add_library(filename):
|
func add_library(file_name : String, filter : String = ""):
|
||||||
var root = get_root()
|
var root = tree.get_root()
|
||||||
var file = File.new()
|
var file = File.new()
|
||||||
if file.open(filename, File.READ) != OK:
|
if file.open(file_name, File.READ) != OK:
|
||||||
return false
|
return false
|
||||||
var lib = parse_json(file.get_as_text())
|
var lib = parse_json(file.get_as_text())
|
||||||
file.close()
|
file.close()
|
||||||
if lib != null && lib.has("lib"):
|
if lib != null and lib.has("lib"):
|
||||||
for m in lib.lib:
|
for m in lib.lib:
|
||||||
m.library = filename
|
m.library = file_name
|
||||||
add_item(m, m.tree_item)
|
libraries.push_back(lib.lib)
|
||||||
return true
|
return true
|
||||||
return false
|
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:
|
if item_parent == null:
|
||||||
item.tree_item = item_name
|
item.tree_item = item_name
|
||||||
item_parent = get_root()
|
item_parent = tree.get_root()
|
||||||
var slash_position = item_name.find("/")
|
var slash_position = item_name.find("/")
|
||||||
if slash_position == -1:
|
if slash_position == -1:
|
||||||
var new_item = null
|
var new_item : TreeItem = null
|
||||||
var c = item_parent.get_children()
|
var c = item_parent.get_children()
|
||||||
while c != null:
|
while c != null:
|
||||||
if c.get_text(0) == item_name:
|
if c.get_text(0) == item_name:
|
||||||
@ -60,13 +66,16 @@ func add_item(item, item_name, item_parent = null):
|
|||||||
break
|
break
|
||||||
c = c.get_next()
|
c = c.get_next()
|
||||||
if new_item == null:
|
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.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"):
|
if item.has("type") || item.has("nodes"):
|
||||||
new_item.set_metadata(0, item)
|
new_item.set_metadata(0, item)
|
||||||
if item.has("collapsed"):
|
if item.has("collapsed"):
|
||||||
new_item.collapsed = item.collapsed
|
new_item.collapsed = item.collapsed and !force_expand
|
||||||
return new_item
|
return new_item
|
||||||
else:
|
else:
|
||||||
var prefix = item_name.left(slash_position)
|
var prefix = item_name.left(slash_position)
|
||||||
@ -79,18 +88,18 @@ func add_item(item, item_name, item_parent = null):
|
|||||||
break
|
break
|
||||||
c = c.get_next()
|
c = c.get_next()
|
||||||
if new_parent == null:
|
if new_parent == null:
|
||||||
new_parent = create_item(item_parent)
|
new_parent = tree.create_item(item_parent)
|
||||||
new_parent.collapsed = true
|
new_parent.collapsed = !force_expand
|
||||||
new_parent.set_text(0, prefix)
|
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:
|
if item == null:
|
||||||
item = get_root()
|
item = tree.get_root()
|
||||||
item = item.get_children()
|
item = item.get_children()
|
||||||
while item != null:
|
while item != null:
|
||||||
var m = item.get_metadata(0)
|
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)
|
array.append(m)
|
||||||
serialize_library(array, library_name, item)
|
serialize_library(array, library_name, item)
|
||||||
item = item.get_next()
|
item = item.get_next()
|
||||||
@ -98,8 +107,10 @@ func serialize_library(array, library_name, item = null):
|
|||||||
func save_library(library_name, item = null):
|
func save_library(library_name, item = null):
|
||||||
var array = []
|
var array = []
|
||||||
serialize_library(array, library_name)
|
serialize_library(array, library_name)
|
||||||
print("Saving library "+library_name)
|
|
||||||
var file = File.new()
|
var file = File.new()
|
||||||
if file.open(library_name, File.WRITE) == OK:
|
if file.open(library_name, File.WRITE) == OK:
|
||||||
file.store_string(to_json({lib=array}))
|
file.store_string(to_json({lib=array}))
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
func _on_Filter_text_changed(filter):
|
||||||
|
update_tree(filter)
|
||||||
|
39
addons/material_maker/library.tscn
Normal file
39
addons/material_maker/library.tscn
Normal file
@ -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"]
|
24
addons/material_maker/library_tree.gd
Normal file
24
addons/material_maker/library_tree.gd
Normal file
@ -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
|
@ -1,7 +1,7 @@
|
|||||||
[gd_scene load_steps=7 format=2]
|
[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/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/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/widgets/tabs.gd" type="Script" id=4]
|
||||||
[ext_resource path="res://addons/material_maker/engine/renderer.tscn" type="PackedScene" id=5]
|
[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_horizontal = 3
|
||||||
size_flags_vertical = 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_right = 314.0
|
||||||
margin_bottom = 342.0
|
margin_bottom = 411.0
|
||||||
rect_min_size = Vector2( 100, 100 )
|
|
||||||
size_flags_horizontal = 3
|
|
||||||
size_flags_vertical = 3
|
size_flags_vertical = 3
|
||||||
hide_root = true
|
size_flags_stretch_ratio = 1.5
|
||||||
script = ExtResource( 2 )
|
|
||||||
|
|
||||||
[node name="Preview" parent="VBoxContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 3 )]
|
[node name="Preview" parent="VBoxContainer/HBoxContainer/VBoxContainer" instance=ExtResource( 3 )]
|
||||||
anchor_left = 0.0
|
anchor_left = 0.0
|
||||||
anchor_right = 0.0
|
anchor_right = 0.0
|
||||||
margin_left = 0.0
|
margin_left = 0.0
|
||||||
margin_top = 354.0
|
margin_top = 423.0
|
||||||
margin_right = 314.0
|
margin_right = 314.0
|
||||||
margin_bottom = 696.0
|
margin_bottom = 696.0
|
||||||
|
|
||||||
|
@ -89,6 +89,11 @@ _global_script_classes=[ {
|
|||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://addons/material_maker/widgets/gradient_editor.gd"
|
"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",
|
"base": "GraphNode",
|
||||||
"class": "MMGraphNodeGeneric",
|
"class": "MMGraphNodeGeneric",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
@ -126,6 +131,7 @@ _global_script_class_icons={
|
|||||||
"MMGenTexture": "",
|
"MMGenTexture": "",
|
||||||
"MMGradient": "",
|
"MMGradient": "",
|
||||||
"MMGradientEditor": "",
|
"MMGradientEditor": "",
|
||||||
|
"MMGraphEdit": "",
|
||||||
"MMGraphNodeGeneric": "",
|
"MMGraphNodeGeneric": "",
|
||||||
"MMGraphNodeRemote": "",
|
"MMGraphNodeRemote": "",
|
||||||
"MMNodeLink": "",
|
"MMNodeLink": "",
|
||||||
@ -156,10 +162,6 @@ gdscript/warnings/unsafe_call_argument=true
|
|||||||
window/size/width=1280
|
window/size/width=1280
|
||||||
window/size/height=720
|
window/size/height=720
|
||||||
|
|
||||||
[editor_plugins]
|
|
||||||
|
|
||||||
enabled=PoolStringArray( "material_maker" )
|
|
||||||
|
|
||||||
[logging]
|
[logging]
|
||||||
|
|
||||||
file_logging/enable_file_logging=true
|
file_logging/enable_file_logging=true
|
||||||
|
Loading…
Reference in New Issue
Block a user