2018-08-16 15:08:09 +02:00
|
|
|
tool
|
2019-10-08 22:17:33 +02:00
|
|
|
extends VBoxContainer
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2019-10-08 22:17:33 +02:00
|
|
|
var libraries = []
|
|
|
|
|
2019-10-13 10:14:43 +02:00
|
|
|
onready var tree : Tree = $Tree
|
2019-10-19 00:37:12 +02:00
|
|
|
onready var filter_line_edit : LineEdit = $HBoxContainer/Filter
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2019-10-08 22:17:33 +02:00
|
|
|
tree.set_column_expand(0, true)
|
|
|
|
tree.set_column_expand(1, false)
|
|
|
|
tree.set_column_min_width(1, 32)
|
2019-09-25 08:04:36 +02:00
|
|
|
var lib_path = OS.get_executable_path().get_base_dir()+"/library/base.json"
|
2018-08-12 19:25:18 +02:00
|
|
|
if !add_library(lib_path):
|
2018-09-03 21:35:44 +02:00
|
|
|
add_library("res://addons/material_maker/library/base.json")
|
2018-08-07 23:21:12 +02:00
|
|
|
add_library("user://library/user.json")
|
2019-10-08 22:17:33 +02:00
|
|
|
update_tree()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2019-10-19 00:37:12 +02:00
|
|
|
func _unhandled_input(event : InputEvent) -> void:
|
|
|
|
if event is InputEventKey and event.pressed and event.command and event.scancode == KEY_F:
|
|
|
|
filter_line_edit.grab_focus()
|
2019-10-19 02:01:30 +02:00
|
|
|
filter_line_edit.select_all()
|
2019-10-19 00:37:12 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_selected_item_name() -> String:
|
2019-11-10 15:02:00 +01:00
|
|
|
return get_item_path(tree.get_selected())
|
|
|
|
|
|
|
|
func get_selected_item_doc_name() -> String:
|
|
|
|
var item : TreeItem = tree.get_selected()
|
|
|
|
if item == null:
|
|
|
|
return ""
|
|
|
|
var m : Dictionary = item.get_metadata(0)
|
|
|
|
if m == null or !m.has("icon"):
|
|
|
|
return ""
|
|
|
|
return m.icon
|
2019-10-13 10:14:43 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func add_library(file_name : String, filter : String = "") -> bool:
|
2019-10-08 22:17:33 +02:00
|
|
|
var root = tree.get_root()
|
2018-08-05 18:46:23 +02:00
|
|
|
var file = File.new()
|
2019-10-08 22:17:33 +02:00
|
|
|
if file.open(file_name, File.READ) != OK:
|
2018-08-12 19:25:18 +02:00
|
|
|
return false
|
2018-08-05 18:46:23 +02:00
|
|
|
var lib = parse_json(file.get_as_text())
|
|
|
|
file.close()
|
2019-10-08 22:17:33 +02:00
|
|
|
if lib != null and lib.has("lib"):
|
2018-08-08 10:50:48 +02:00
|
|
|
for m in lib.lib:
|
2019-10-08 22:17:33 +02:00
|
|
|
m.library = file_name
|
|
|
|
libraries.push_back(lib.lib)
|
2018-08-12 19:25:18 +02:00
|
|
|
return true
|
|
|
|
return false
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_tree(filter : String = "") -> void:
|
2019-10-08 22:17:33 +02:00
|
|
|
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 != "")
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_preview_texture(data : Dictionary) -> ImageTexture:
|
2019-10-08 22:17:33 +02:00
|
|
|
if data.has("icon") and data.has("library"):
|
|
|
|
var image_path = data.library.left(data.library.rfind("."))+"/"+data.icon+".png"
|
|
|
|
var t : ImageTexture
|
2019-10-27 10:12:03 +01:00
|
|
|
if image_path.left(6) == "res://":
|
|
|
|
image_path = ProjectSettings.globalize_path(image_path)
|
|
|
|
t = ImageTexture.new()
|
|
|
|
var image : Image = Image.new()
|
|
|
|
image.load(image_path)
|
|
|
|
t.create_from_image(image)
|
2019-10-08 22:17:33 +02:00
|
|
|
return t
|
|
|
|
return null
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func add_item(item, item_name, item_icon = null, item_parent = null, force_expand = false) -> TreeItem:
|
2018-08-07 23:21:12 +02:00
|
|
|
if item_parent == null:
|
|
|
|
item.tree_item = item_name
|
2019-10-08 22:17:33 +02:00
|
|
|
item_parent = tree.get_root()
|
2018-08-05 18:46:23 +02:00
|
|
|
var slash_position = item_name.find("/")
|
|
|
|
if slash_position == -1:
|
2019-10-08 22:17:33 +02:00
|
|
|
var new_item : TreeItem = null
|
2018-08-07 23:21:12 +02:00
|
|
|
var c = item_parent.get_children()
|
|
|
|
while c != null:
|
|
|
|
if c.get_text(0) == item_name:
|
|
|
|
new_item = c
|
|
|
|
break
|
|
|
|
c = c.get_next()
|
|
|
|
if new_item == null:
|
2019-10-08 22:17:33 +02:00
|
|
|
new_item = tree.create_item(item_parent)
|
2018-08-07 23:21:12 +02:00
|
|
|
new_item.set_text(0, item_name)
|
2019-10-08 22:17:33 +02:00
|
|
|
new_item.collapsed = !force_expand
|
|
|
|
if item_icon != null:
|
|
|
|
new_item.set_icon(1, item_icon)
|
|
|
|
new_item.set_icon_max_width(1, 32)
|
2018-08-22 07:33:50 +02:00
|
|
|
if item.has("type") || item.has("nodes"):
|
2018-08-11 11:33:24 +02:00
|
|
|
new_item.set_metadata(0, item)
|
|
|
|
if item.has("collapsed"):
|
2019-10-08 22:17:33 +02:00
|
|
|
new_item.collapsed = item.collapsed and !force_expand
|
2018-08-05 18:46:23 +02:00
|
|
|
return new_item
|
|
|
|
else:
|
|
|
|
var prefix = item_name.left(slash_position)
|
|
|
|
var suffix = item_name.right(slash_position+1)
|
|
|
|
var new_parent = null
|
|
|
|
var c = item_parent.get_children()
|
|
|
|
while c != null:
|
|
|
|
if c.get_text(0) == prefix:
|
|
|
|
new_parent = c
|
|
|
|
break
|
|
|
|
c = c.get_next()
|
|
|
|
if new_parent == null:
|
2019-10-08 22:17:33 +02:00
|
|
|
new_parent = tree.create_item(item_parent)
|
|
|
|
new_parent.collapsed = !force_expand
|
2018-08-05 18:46:23 +02:00
|
|
|
new_parent.set_text(0, prefix)
|
2019-10-08 22:17:33 +02:00
|
|
|
return add_item(item, suffix, item_icon, new_parent, force_expand)
|
2018-08-07 23:21:12 +02:00
|
|
|
|
2019-11-07 08:25:57 +01:00
|
|
|
func get_item_path(item : TreeItem) -> String:
|
2019-11-10 15:02:00 +01:00
|
|
|
if item == null:
|
|
|
|
return ""
|
2019-11-07 08:25:57 +01:00
|
|
|
var item_path = item.get_text(0)
|
|
|
|
var item_parent = item.get_parent()
|
|
|
|
while item_parent != tree.get_root():
|
|
|
|
item_path = item_parent.get_text(0)+"/"+item_path
|
|
|
|
item_parent = item_parent.get_parent()
|
|
|
|
return item_path
|
|
|
|
|
|
|
|
func get_icon_name(item_name : String) -> String:
|
|
|
|
return item_name.to_lower().replace("/", "_").replace(" ", "_")
|
|
|
|
|
|
|
|
func serialize_library(array : Array, library_name : String = "", item : TreeItem = null, icon_dir : String = "") -> void:
|
2018-08-07 23:21:12 +02:00
|
|
|
if item == null:
|
2019-10-08 22:17:33 +02:00
|
|
|
item = tree.get_root()
|
2018-08-07 23:21:12 +02:00
|
|
|
item = item.get_children()
|
|
|
|
while item != null:
|
2019-10-24 09:00:31 +02:00
|
|
|
if item.get_metadata(0) != null:
|
|
|
|
var m : Dictionary = item.get_metadata(0)
|
|
|
|
if library_name == "" or (m.has("library") and m.library == library_name):
|
|
|
|
var copy : Dictionary = m.duplicate()
|
|
|
|
copy.erase("library")
|
2019-11-05 20:00:28 +01:00
|
|
|
copy.collapsed = item.collapsed
|
2019-11-07 08:25:57 +01:00
|
|
|
if icon_dir != "" and m.has("icon"):
|
|
|
|
var src_path = m.library.get_basename()+"/"+m.icon+".png"
|
|
|
|
var icon_name : String = get_icon_name(get_item_path(item))
|
|
|
|
var icon_path = icon_dir+"/"+icon_name+".png"
|
|
|
|
var dir : Directory = Directory.new()
|
|
|
|
dir.copy(src_path, icon_path)
|
|
|
|
copy.icon = icon_name
|
2019-10-24 09:00:31 +02:00
|
|
|
array.append(copy)
|
2019-11-05 20:00:28 +01:00
|
|
|
elif !item.collapsed:
|
2019-11-07 08:25:57 +01:00
|
|
|
var item_path = get_item_path(item)
|
2019-11-05 20:00:28 +01:00
|
|
|
array.append({ tree_item=item_path, collapsed=false })
|
2019-11-07 08:25:57 +01:00
|
|
|
serialize_library(array, library_name, item, icon_dir)
|
2018-08-07 23:21:12 +02:00
|
|
|
item = item.get_next()
|
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func save_library(library_name : String, item : TreeItem = null) -> void:
|
|
|
|
var array : Array = []
|
2018-08-07 23:21:12 +02:00
|
|
|
serialize_library(array, library_name)
|
|
|
|
var file = File.new()
|
|
|
|
if file.open(library_name, File.WRITE) == OK:
|
2019-10-27 14:36:16 +01:00
|
|
|
file.store_string(JSON.print({lib=array}, "\t", true))
|
2018-08-07 23:21:12 +02:00
|
|
|
file.close()
|
2019-10-08 22:17:33 +02:00
|
|
|
|
2019-11-05 20:00:28 +01:00
|
|
|
func _on_Filter_text_changed(filter : String) -> void:
|
2019-10-08 22:17:33 +02:00
|
|
|
update_tree(filter)
|
2019-11-05 20:00:28 +01:00
|
|
|
|
|
|
|
func export_libraries(path : String) -> void:
|
|
|
|
var dir : Directory = Directory.new()
|
|
|
|
var icon_path = path.get_basename()
|
|
|
|
dir.make_dir(icon_path)
|
|
|
|
var array = []
|
|
|
|
serialize_library(array, "", null, icon_path)
|
|
|
|
var file = File.new()
|
|
|
|
if file.open(path, File.WRITE) == OK:
|
|
|
|
file.store_string(JSON.print({lib=array}, "\t", true))
|
|
|
|
file.close()
|