mirror of
https://github.com/Relintai/material-maker.git
synced 2024-12-23 21:16:54 +01:00
117 lines
4.0 KiB
GDScript
117 lines
4.0 KiB
GDScript
extends Tree
|
|
|
|
export(int, 0, 3) var preview : int = 0
|
|
|
|
var default_texture : ImageTexture = null
|
|
var current_graph_edit = null
|
|
var current_generator = null
|
|
var item_from_gen : Dictionary = {}
|
|
|
|
var pending_updates = {}
|
|
|
|
signal group_selected
|
|
|
|
func _ready() -> void:
|
|
var default_image = Image.new()
|
|
default_image.create(24, 24, false, Image.FORMAT_RGBA8)
|
|
default_image.fill(Color(0.0, 0.0, 0.0, 0.0))
|
|
default_texture = ImageTexture.new()
|
|
default_texture.create_from_image(default_image)
|
|
|
|
func update_from_graph_edit(graph_edit) -> void:
|
|
for g in item_from_gen.keys():
|
|
if is_instance_valid(g):
|
|
g.disconnect("output_changed", self, "on_gen_output_changed")
|
|
item_from_gen = {}
|
|
set_column_expand(0, true)
|
|
columns = preview+1
|
|
for i in range(1, columns):
|
|
set_column_expand(i, false)
|
|
set_column_min_width(i, 28)
|
|
clear()
|
|
pending_updates = {}
|
|
if current_graph_edit != null and is_instance_valid(current_graph_edit):
|
|
current_graph_edit.disconnect("view_updated", self, "on_view_updated")
|
|
if graph_edit == null or graph_edit.top_generator == null or graph_edit.generator == null:
|
|
current_graph_edit = null
|
|
current_generator = null
|
|
return
|
|
current_graph_edit = graph_edit
|
|
current_graph_edit.connect("view_updated", self, "on_view_updated")
|
|
current_generator = graph_edit.generator
|
|
var file_name = "PTex"
|
|
if graph_edit.save_path != null:
|
|
file_name = graph_edit.save_path.get_file()
|
|
fill_item(create_item(null), graph_edit.top_generator, graph_edit.generator, file_name)
|
|
|
|
func set_icon(item : TreeItem, generator : MMGenGraph, output : int) -> void:
|
|
if output >= preview:
|
|
return
|
|
var result = generator.render(output, 24, true)
|
|
while result is GDScriptFunctionState:
|
|
result = yield(result, "completed")
|
|
var tex = ImageTexture.new()
|
|
result.copy_to_texture(tex)
|
|
item.set_icon(1-min(generator.get_output_defs().size()-preview, 0)+output, tex)
|
|
result.release()
|
|
|
|
func set_icons(item : TreeItem, generator : MMGenGraph) -> void:
|
|
var output_count = min(generator.get_output_defs().size(), preview)
|
|
for output in range(output_count):
|
|
set_icon(item, generator, output)
|
|
|
|
func fill_item(item : TreeItem, generator : MMGenGraph, selected : MMGenGraph, name = null) -> void:
|
|
item.set_text(0, name if name != null else generator.get_type_name())
|
|
if generator == selected:
|
|
item.set_custom_color(0, Color(0.5, 0.5, 1))
|
|
else:
|
|
item.set_custom_color(0, Color(1, 1, 1))
|
|
item.set_metadata(0, generator)
|
|
item_from_gen[generator] = item
|
|
generator.connect("output_changed", self, "on_gen_output_changed", [ generator ])
|
|
if preview > 0 and generator.get_output_defs().size() > 0:
|
|
for i in range(min(preview, generator.get_output_defs().size())):
|
|
item.set_icon(i+1, default_texture)
|
|
call_deferred("set_icons", item, generator)
|
|
for c in generator.get_children():
|
|
if c is MMGenGraph:
|
|
if c.is_template():
|
|
continue
|
|
fill_item(create_item(item), c, selected)
|
|
|
|
func _on_Hierarchy_item_double_clicked() -> void:
|
|
emit_signal("group_selected", get_selected().get_metadata(0))
|
|
|
|
func on_view_updated(generator) -> void:
|
|
if item_from_gen.has(current_generator):
|
|
item_from_gen[current_generator].set_custom_color(0, Color(1, 1, 1))
|
|
current_generator = generator
|
|
if item_from_gen.has(current_generator):
|
|
item_from_gen[current_generator].set_custom_color(0, Color(0.5, 0.5, 1))
|
|
|
|
func on_gen_output_changed(index, generator) -> void:
|
|
if item_from_gen.has(generator) and index < preview:
|
|
if !pending_updates.has(generator):
|
|
pending_updates[generator] = [index]
|
|
elif pending_updates[generator].find(index) == -1:
|
|
pending_updates[generator].push_back(index)
|
|
$Delay.stop()
|
|
$Delay.start()
|
|
|
|
func _on_Delay_timeout() -> void:
|
|
for generator in pending_updates.keys():
|
|
var item = item_from_gen[generator]
|
|
for index in pending_updates[generator]:
|
|
set_icon(item, generator, index)
|
|
|
|
func _on_Hierarchy_gui_input(event):
|
|
if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT and event.pressed:
|
|
$ContextMenu.rect_position = get_global_mouse_position()
|
|
$ContextMenu.popup()
|
|
|
|
func _on_ContextMenu_id_pressed(id):
|
|
preview = id
|
|
update_from_graph_edit(current_graph_edit)
|
|
|
|
|