Skip to content

Commit

Permalink
Added optional preview to reroute node.
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Oct 8, 2023
1 parent 9a20d5f commit 16f21fa
Show file tree
Hide file tree
Showing 11 changed files with 2,003 additions and 1,602 deletions.
5 changes: 4 additions & 1 deletion addons/material_maker/engine/nodes/gen_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ func get_output_defs(_show_hidden : bool = false) -> Array:
return []

func get_source(input_index : int) -> OutputPort:
return get_parent().get_port_source(name, input_index)
var parent : MMGenGraph = get_parent()
if parent == null:
return null
return parent.get_port_source(name, input_index)

func get_targets(output_index : int) -> Array:
var parent = get_parent()
Expand Down
2 changes: 1 addition & 1 deletion addons/material_maker/engine/nodes/gen_reroute.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func get_output_defs(_show_hidden : bool = false) -> Array:
return [ { type=port_type } ]

func get_parameter_defs() -> Array:
return []
return [{ name="preview", type="enum", values=["none", "small", "large", "huge"], default=0 }]

func _get_shader_code(uv : String, output_index : int, context : MMGenContext) -> ShaderCode:
var source = get_source(0)
Expand Down
84 changes: 81 additions & 3 deletions material_maker/nodes/reroute/reroute.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
extends MMGraphNodeMinimal


const PREVIEW_SIZES : Array[int] = [ 0, 64, 128, 192]


func _ready() -> void:
super._ready()
var current_theme : Theme = mm_globals.main_window.theme
for stylebox in current_theme.get_stylebox_list("Reroute"):
add_theme_stylebox_override(stylebox, current_theme.get_stylebox(stylebox, "Reroute"))
close_button.visible = false
set_theme_type("Reroute")
call_deferred("on_connections_changed")

func set_generator(g : MMGenBase) -> void:
super.set_generator(g)
generator.parameter_changed.connect(self.on_parameter_changed)
await set_preview(g.get_parameter("preview"))
update_node()

func set_theme_type(type : StringName):
var current_theme : Theme = mm_globals.main_window.theme
for constant in current_theme.get_constant_list(type):
add_theme_constant_override(constant, current_theme.get_constant(constant, type))
for stylebox in current_theme.get_stylebox_list(type):
add_theme_stylebox_override(stylebox, current_theme.get_stylebox(stylebox, type))

func on_connections_changed():
var graph_edit = get_parent()
var color : Color = Color(1.0, 1.0, 1.0)
Expand All @@ -26,3 +42,65 @@ func on_connections_changed():
port_type = node.generator.get_input_defs()[c.from_port].type
set_slot(0, true, type, color, true, type, color)
generator.set_port_type(port_type)
update_preview()

func update_preview(preview : Control = null):
if preview == null:
if $Contents.get_child_count() == 0:
return
preview = $Contents.get_child(0)
var preview_source : MMGenBase.OutputPort = generator.get_source(0)
if preview_source == null:
preview.set_generator(null)
else:
preview.set_generator(preview_source.generator, preview_source.output_index)

func _on_gui_input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
accept_event()
var menu : PopupMenu = PopupMenu.new()
menu.add_item("No preview")
menu.add_item("Small preview")
menu.add_item("Large preview")
menu.add_item("Huge preview")
add_child(menu)
menu.id_pressed.connect(self.on_context_menu)
menu.popup(Rect2(get_global_mouse_position(), Vector2(0, 0)))

func on_context_menu(id : int):
var old_value = generator.get_parameter("preview")
if old_value != id and get_parent().get("undoredo") != null:
var node_hier_name = generator.get_hier_name()
var undo_command = { type="setparams", node=node_hier_name, params={ preview=old_value } }
var redo_command = { type="setparams", node=node_hier_name, params={ preview=id } }
get_parent().undoredo.add("Set parameter value", [ undo_command ], [ redo_command ])
generator.set_parameter("preview", id)

func on_parameter_changed(n : String, v):
if n == "preview":
var old_size : Vector2 = size
await set_preview(v)
disable_undoredo_for_offset = true
position_offset -= (size-old_size)/2
disable_undoredo_for_offset = false

func set_preview(v : int):
var preview : Control = null
if $Contents.get_child_count() > 0:
preview = $Contents.get_child(0)
if v == 0:
if preview:
preview.queue_free()
set_theme_type("Reroute")
else:
if ! preview:
preview = preload("res://material_maker/panels/preview_2d/preview_2d_node.tscn").instantiate()
$Contents.add_child(preview)
preview.mouse_filter = Control.MOUSE_FILTER_IGNORE
update_preview(preview)
var preview_size : int = PREVIEW_SIZES[v]
preview.custom_minimum_size = Vector2(preview_size, preview_size)
set_theme_type("ReroutePreview")
await get_tree().process_frame
size = Vector2(0, 0)

11 changes: 9 additions & 2 deletions material_maker/nodes/reroute/reroute.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ GraphNode/styles/frame = SubResource("1")
GraphNode/styles/position = null
GraphNode/styles/selected_frame = SubResource("2")

[node name="reroute" type="GraphNode"]
[node name="Reroute" type="GraphNode"]
clip_children = 2
custom_minimum_size = Vector2(24, 24)
offset_right = 24.0
offset_bottom = 24.0
Expand All @@ -55,7 +56,13 @@ slot/0/right_icon = null
slot/0/draw_stylebox = true
script = ExtResource("1")

[node name="Foo" type="Control" parent="."]
[node name="Contents" type="MarginContainer" parent="."]
custom_minimum_size = Vector2(24, 24)
layout_mode = 2
mouse_filter = 2
theme_override_constants/margin_left = 1
theme_override_constants/margin_top = 1
theme_override_constants/margin_right = 1
theme_override_constants/margin_bottom = 1

[connection signal="gui_input" from="." to="." method="_on_gui_input"]
Loading

0 comments on commit 16f21fa

Please sign in to comment.