material-maker/material_maker/widgets/graph_tree/hierarchy_pane.gd

130 lines
4.6 KiB
GDScript3
Raw Normal View History

2020-03-02 23:01:27 +01:00
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 = {}
2020-03-08 21:42:26 +01:00
var update_index = 0
var pending_updates = {}
2020-03-02 23:01:27 +01:00
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)
2020-03-02 23:01:27 +01:00
func update_from_graph_edit(graph_edit) -> void:
2020-03-08 21:42:26 +01:00
$Delay.stop()
pending_updates = {}
update_index += 1
for g in item_from_gen.keys():
if is_instance_valid(g):
g.disconnect("parameter_changed", self, "on_gen_parameter_changed")
item_from_gen = {}
2020-03-02 23:01:27 +01:00
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")
2020-03-08 21:42:26 +01:00
if current_generator != null and is_instance_valid(current_generator):
current_generator.disconnect("hierarchy_changed", self, "on_hierarchy_changed")
if graph_edit == null or graph_edit.top_generator == null or graph_edit.generator == null:
current_graph_edit = null
current_generator = null
2020-03-02 23:01:27 +01:00
return
current_graph_edit = graph_edit
current_graph_edit.connect("view_updated", self, "on_view_updated")
current_generator = graph_edit.generator
2020-03-08 21:42:26 +01:00
current_generator.connect("hierarchy_changed", self, "on_hierarchy_changed")
2020-03-02 23:01:27 +01:00
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)
2020-03-02 23:01:27 +01:00
func set_icon(item : TreeItem, generator : MMGenGraph, output : int) -> void:
2020-03-08 21:42:26 +01:00
var index = update_index
if output >= preview:
2020-03-02 23:01:27 +01:00
return
var result = generator.render(output, 24, true)
while result is GDScriptFunctionState:
result = yield(result, "completed")
2020-03-08 21:42:26 +01:00
if index == update_index:
var tex = ImageTexture.new()
result.copy_to_texture(tex)
result.release()
item.set_icon(1-min(generator.get_output_defs().size()-preview, 0)+output, tex)
else:
result.release()
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))
2020-03-02 23:01:27 +01:00
else:
item.set_custom_color(0, Color(1, 1, 1))
item.set_metadata(0, generator)
item_from_gen[generator] = item
generator.connect("parameter_changed", self, "on_gen_parameter_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)
2020-03-08 21:42:26 +01:00
var output_count = min(generator.get_output_defs().size(), preview)
for output in range(output_count):
on_gen_output_changed(output, generator)
2020-03-02 23:01:27 +01:00
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:
2020-03-02 23:01:27 +01:00
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_parameter_changed(param_name : String, index : int, generator) -> void:
if param_name == "__output_changed__":
on_gen_output_changed(index, generator)
func on_gen_output_changed(index : int, 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()
2020-03-08 21:42:26 +01:00
func on_hierarchy_changed() -> void:
update_from_graph_edit(current_graph_edit)
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)