2018-07-22 17:20:27 +02:00
|
|
|
tool
|
|
|
|
extends Container
|
|
|
|
|
2018-07-28 09:37:51 +02:00
|
|
|
var popup_menu = null
|
2018-07-22 17:20:27 +02:00
|
|
|
var popup_position = Vector2(0, 0)
|
|
|
|
|
2018-07-26 08:31:28 +02:00
|
|
|
var texture_preview_material = null
|
2018-07-25 06:20:36 +02:00
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
var preview_maximized = false
|
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
const MENU = [
|
2018-08-03 08:16:38 +02:00
|
|
|
{ command="new_texture", description="New material" },
|
|
|
|
{ command="load_texture", description="Load material" },
|
|
|
|
{ command="save_texture", description="Save material" },
|
|
|
|
{ command="save_texture_as", description="Save material as..." },
|
|
|
|
{ command="export_texture", description="Export material" },
|
2018-08-01 07:50:58 +02:00
|
|
|
{ },
|
2018-07-28 09:37:51 +02:00
|
|
|
{ submenu="generator", description="Generator" },
|
|
|
|
{ name="image", description="Image", in_submenu="generator" },
|
2018-08-01 07:50:58 +02:00
|
|
|
{ name="pattern", description="Pattern", in_submenu="generator" },
|
2018-07-28 09:37:51 +02:00
|
|
|
{ name="bricks", description="Bricks", in_submenu="generator" },
|
|
|
|
{ name="perlin", description="Perlin noise", in_submenu="generator" },
|
|
|
|
{ name="voronoi", description="Voronoi Noise", in_submenu="generator" },
|
|
|
|
{ submenu="filter", description="Filter" },
|
|
|
|
{ name="colorize", description="Colorize", in_submenu="filter" },
|
|
|
|
{ name="blend", description="Blend", in_submenu="filter" },
|
|
|
|
{ name="transform", description="Transform", in_submenu="filter" },
|
|
|
|
{ name="warp", description="Warp", in_submenu="filter" },
|
|
|
|
{ name="normal_map", description="Normal Map", in_submenu="filter" }
|
2018-07-22 17:20:27 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
func _ready():
|
2018-07-26 00:08:34 +02:00
|
|
|
# Duplicate the material we'll modify and store the shaders
|
2018-07-28 21:21:05 +02:00
|
|
|
$Preview/Preview/SelectedPreview.material = $Preview/Preview/SelectedPreview.material.duplicate(true)
|
|
|
|
texture_preview_material = $Preview/Preview/SelectedPreview.material
|
2018-07-22 17:20:27 +02:00
|
|
|
$GraphEdit.add_valid_connection_type(0, 0)
|
2018-07-28 09:37:51 +02:00
|
|
|
# create or update popup menu
|
|
|
|
if popup_menu != null:
|
|
|
|
popup_menu.queue_free()
|
|
|
|
popup_menu = create_menu()
|
|
|
|
$GraphEdit.add_child(popup_menu)
|
|
|
|
|
|
|
|
func create_menu(in_submenu = null):
|
|
|
|
var menu = PopupMenu.new()
|
|
|
|
menu.connect("id_pressed", self, "_on_PopupMenu_id_pressed")
|
2018-07-22 17:20:27 +02:00
|
|
|
for i in MENU.size():
|
2018-07-28 09:37:51 +02:00
|
|
|
if MENU[i].has("in_submenu"):
|
|
|
|
if in_submenu != MENU[i].in_submenu:
|
|
|
|
continue
|
|
|
|
elif in_submenu != null:
|
|
|
|
continue
|
|
|
|
if MENU[i].has("submenu"):
|
|
|
|
var submenu = create_menu(MENU[i].submenu)
|
|
|
|
menu.add_child(submenu)
|
|
|
|
menu.add_submenu_item(MENU[i].description, submenu.get_name())
|
2018-08-01 07:50:58 +02:00
|
|
|
elif MENU[i].has("description"):
|
2018-07-28 09:37:51 +02:00
|
|
|
menu.add_item(MENU[i].description, i)
|
2018-08-01 07:50:58 +02:00
|
|
|
else:
|
|
|
|
menu.add_separator()
|
2018-07-28 09:37:51 +02:00
|
|
|
return menu
|
2018-07-22 17:20:27 +02:00
|
|
|
|
|
|
|
func _on_GraphEdit_popup_request(position):
|
|
|
|
popup_position = position
|
2018-07-28 09:37:51 +02:00
|
|
|
popup_menu.popup(Rect2(position, popup_menu.rect_size))
|
2018-07-22 17:20:27 +02:00
|
|
|
|
|
|
|
func _on_PopupMenu_id_pressed(id):
|
|
|
|
var node_type = null
|
2018-07-22 23:25:05 +02:00
|
|
|
if MENU[id].has("command"):
|
|
|
|
call(MENU[id].command)
|
|
|
|
elif MENU[id].has("name"):
|
2018-08-05 18:46:23 +02:00
|
|
|
$GraphEdit.create_node({type=MENU[id].name}, popup_position)
|
|
|
|
|
|
|
|
func set_save_path(graph_edit, path):
|
2018-08-03 08:16:38 +02:00
|
|
|
if !Engine.editor_hint:
|
2018-08-05 18:46:23 +02:00
|
|
|
if path != null:
|
|
|
|
OS.set_window_title("Procedural textures (%s)" % path)
|
2018-08-03 08:16:38 +02:00
|
|
|
else:
|
|
|
|
OS.set_window_title("Procedural textures")
|
|
|
|
|
|
|
|
func new_texture():
|
|
|
|
$GraphEdit.new_material()
|
2018-07-22 17:20:27 +02:00
|
|
|
|
2018-07-22 23:25:05 +02:00
|
|
|
func load_texture():
|
2018-08-05 18:46:23 +02:00
|
|
|
$GraphEdit.load_file()
|
2018-08-03 08:16:38 +02:00
|
|
|
|
2018-07-22 23:25:05 +02:00
|
|
|
func save_texture():
|
2018-08-05 18:46:23 +02:00
|
|
|
$GraphEdit.save_file()
|
2018-08-03 08:16:38 +02:00
|
|
|
|
|
|
|
func save_texture_as():
|
2018-08-05 18:46:23 +02:00
|
|
|
$GraphEdit.save_file_as()
|
|
|
|
|
|
|
|
func export_texture():
|
|
|
|
$GraphEdit.export_textures()
|
|
|
|
|
|
|
|
func get_selected_node():
|
|
|
|
var rv = null
|
|
|
|
for c in $GraphEdit.get_children():
|
|
|
|
if c is GraphNode && c.selected:
|
|
|
|
if rv != null:
|
|
|
|
return null
|
|
|
|
else:
|
|
|
|
rv = c
|
|
|
|
return rv
|
2018-07-26 00:08:34 +02:00
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
func generate_shader():
|
2018-08-05 18:46:23 +02:00
|
|
|
var selected_node = get_selected_node()
|
2018-07-30 08:45:19 +02:00
|
|
|
if $GraphEdit/Material != null:
|
|
|
|
$GraphEdit/Material.update_materials($Preview/Preview.get_materials())
|
2018-08-05 18:46:23 +02:00
|
|
|
if selected_node != null:
|
2018-08-03 22:38:48 +02:00
|
|
|
$GraphEdit.setup_material(texture_preview_material, selected_node.get_textures(), selected_node.generate_shader())
|
2018-07-22 17:20:27 +02:00
|
|
|
|
2018-07-24 08:04:25 +02:00
|
|
|
func _on_GraphEdit_node_selected(node):
|
2018-08-05 18:46:23 +02:00
|
|
|
var selected_node = get_selected_node()
|
|
|
|
if selected_node != null:
|
2018-08-03 22:38:48 +02:00
|
|
|
$GraphEdit.setup_material(texture_preview_material, selected_node.get_textures(), selected_node.generate_shader())
|
2018-07-26 08:31:28 +02:00
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func _on_SelectedPreview_gui_input(ev):
|
|
|
|
if ev is InputEventMouseButton && ev.button_index == 1 && ev.doubleclick:
|
|
|
|
if preview_maximized:
|
|
|
|
$Preview/Preview/SelectedPreview/SelectedPreviewAnimation.play("minimize")
|
|
|
|
else:
|
|
|
|
$Preview/Preview/SelectedPreview/SelectedPreviewAnimation.play("maximize")
|
|
|
|
preview_maximized = !preview_maximized
|
|
|
|
|