2018-08-16 15:08:09 +02:00
|
|
|
tool
|
2018-08-05 18:46:23 +02:00
|
|
|
extends Tree
|
|
|
|
|
|
|
|
func get_drag_data(position):
|
|
|
|
var selected_item = get_selected()
|
|
|
|
if selected_item != null:
|
|
|
|
var data = selected_item.get_metadata(0)
|
2018-08-08 10:50:48 +02:00
|
|
|
if data == null:
|
|
|
|
return null
|
2018-08-07 09:31:41 +02:00
|
|
|
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)
|
2018-08-08 10:50:48 +02:00
|
|
|
elif data.has("type") and data.type == "uniform":
|
|
|
|
preview = ColorRect.new()
|
|
|
|
preview.rect_size = Vector2(32, 32)
|
2018-08-18 01:53:07 +02:00
|
|
|
if data.has("color"):
|
|
|
|
preview.color = Color(data.color.r, data.color.g, data.color.b, data.color.a)
|
2018-08-07 09:31:41 +02:00
|
|
|
else:
|
|
|
|
preview = Label.new()
|
|
|
|
preview.text = data.tree_item
|
|
|
|
set_drag_preview(preview)
|
2018-08-05 18:46:23 +02:00
|
|
|
return data
|
|
|
|
return null
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
var root = create_item()
|
2018-08-12 19:25:18 +02:00
|
|
|
var lib_path = OS.get_executable_path()
|
|
|
|
lib_path = lib_path.left(max(lib_path.rfind("\\"), lib_path.rfind("/"))+1)+"library/base.json"
|
|
|
|
if !add_library(lib_path):
|
|
|
|
add_library("res://addons/procedural_material/library/base.json")
|
2018-08-07 23:21:12 +02:00
|
|
|
add_library("user://library/user.json")
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func add_library(filename):
|
|
|
|
var root = get_root()
|
|
|
|
var file = File.new()
|
|
|
|
if file.open(filename, 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()
|
2018-08-08 10:50:48 +02:00
|
|
|
if lib != null && lib.has("lib"):
|
|
|
|
for m in lib.lib:
|
|
|
|
m.library = filename
|
|
|
|
add_item(m, m.tree_item)
|
2018-08-12 19:25:18 +02:00
|
|
|
return true
|
|
|
|
return false
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2018-08-07 23:21:12 +02:00
|
|
|
func add_item(item, item_name, item_parent = null):
|
|
|
|
if item_parent == null:
|
|
|
|
item.tree_item = item_name
|
|
|
|
item_parent = get_root()
|
2018-08-05 18:46:23 +02:00
|
|
|
var slash_position = item_name.find("/")
|
|
|
|
if slash_position == -1:
|
2018-08-07 23:21:12 +02:00
|
|
|
var new_item = null
|
|
|
|
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:
|
|
|
|
new_item = create_item(item_parent)
|
|
|
|
new_item.set_text(0, item_name)
|
2018-08-11 11:33:24 +02:00
|
|
|
new_item.collapsed = true
|
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"):
|
|
|
|
new_item.collapsed = item.collapsed
|
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:
|
|
|
|
new_parent = create_item(item_parent)
|
2018-08-11 11:33:24 +02:00
|
|
|
new_parent.collapsed = true
|
2018-08-05 18:46:23 +02:00
|
|
|
new_parent.set_text(0, prefix)
|
|
|
|
return add_item(item, suffix, new_parent)
|
2018-08-07 23:21:12 +02:00
|
|
|
|
|
|
|
func serialize_library(array, library_name, item = null):
|
|
|
|
if item == null:
|
|
|
|
item = 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:
|
|
|
|
array.append(m)
|
|
|
|
serialize_library(array, library_name, item)
|
|
|
|
item = item.get_next()
|
|
|
|
|
|
|
|
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()
|