2018-08-16 15:08:09 +02:00
|
|
|
tool
|
2018-08-05 18:46:23 +02:00
|
|
|
extends Panel
|
|
|
|
|
2019-01-24 22:27:41 +01:00
|
|
|
var recent_files = []
|
|
|
|
var recent_files_submenu = null
|
|
|
|
|
2018-09-04 21:45:14 +02:00
|
|
|
var editor_interface = null
|
2018-08-27 17:22:57 +02:00
|
|
|
var current_tab = null
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2019-08-12 15:58:42 +02:00
|
|
|
onready var renderer = $Renderer
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
const MENU = [
|
|
|
|
{ menu="File", command="new_material", description="New material" },
|
2018-08-07 23:21:12 +02:00
|
|
|
{ menu="File", command="load_material", shortcut="Control+O", description="Load material" },
|
2019-01-24 22:27:41 +01:00
|
|
|
{ menu="File", submenu="load_recent", description="Load recent", standalone_only=true },
|
2018-08-05 18:46:23 +02:00
|
|
|
{ menu="File" },
|
2018-08-07 23:21:12 +02:00
|
|
|
{ menu="File", command="save_material", shortcut="Control+S", description="Save material" },
|
|
|
|
{ menu="File", command="save_material_as", shortcut="Control+Shift+S", description="Save material as..." },
|
2018-08-05 18:46:23 +02:00
|
|
|
{ menu="File", command="save_all_materials", description="Save all materials..." },
|
|
|
|
{ menu="File" },
|
2018-08-07 23:21:12 +02:00
|
|
|
{ menu="File", command="export_material", shortcut="Control+E", description="Export material" },
|
2018-08-05 18:46:23 +02:00
|
|
|
{ menu="File" },
|
|
|
|
{ menu="File", command="close_material", description="Close material" },
|
2018-08-07 23:21:12 +02:00
|
|
|
{ menu="File", command="quit", shortcut="Control+Q", description="Quit" },
|
2018-08-19 02:11:52 +02:00
|
|
|
{ menu="Edit", command="edit_cut", shortcut="Control+X", description="Cut" },
|
|
|
|
{ menu="Edit", command="edit_copy", shortcut="Control+C", description="Copy" },
|
|
|
|
{ menu="Edit", command="edit_paste", shortcut="Control+V", description="Paste" },
|
2019-09-09 22:00:18 +02:00
|
|
|
{ menu="Tools", command="make_selected_nodes_editable", description="Make selected nodes editable" },
|
2018-08-07 23:21:12 +02:00
|
|
|
{ menu="Tools", command="add_to_user_library", description="Add selected node to user library" },
|
|
|
|
{ menu="Tools", command="save_user_library", description="Save user library" },
|
2018-08-25 08:07:22 +02:00
|
|
|
{ menu="Help", command="show_doc", description="User manual" },
|
2018-08-11 11:33:24 +02:00
|
|
|
{ menu="Help", command="bug_report", description="Report a bug" },
|
|
|
|
{ menu="Help" },
|
2018-08-05 18:46:23 +02:00
|
|
|
{ menu="Help", command="about", description="About" }
|
|
|
|
]
|
|
|
|
|
2018-09-04 21:45:14 +02:00
|
|
|
signal quit
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func _ready():
|
2018-09-12 19:21:42 +02:00
|
|
|
if !Engine.editor_hint:
|
|
|
|
OS.set_window_title(ProjectSettings.get_setting("application/config/name")+" v"+ProjectSettings.get_setting("application/config/release"))
|
2019-01-24 22:27:41 +01:00
|
|
|
load_recents()
|
2018-08-05 18:46:23 +02:00
|
|
|
for m in $VBoxContainer/Menu.get_children():
|
2018-08-27 12:16:37 +02:00
|
|
|
var menu = m.get_popup()
|
|
|
|
create_menu(menu, m.name)
|
|
|
|
m.connect("about_to_show", self, "menu_about_to_show", [ m.name, menu ])
|
2018-08-09 22:19:27 +02:00
|
|
|
new_material()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func create_menu(menu, menu_name):
|
2018-08-16 15:08:09 +02:00
|
|
|
menu.clear()
|
2018-08-05 18:46:23 +02:00
|
|
|
menu.connect("id_pressed", self, "_on_PopupMenu_id_pressed")
|
|
|
|
for i in MENU.size():
|
2019-01-24 22:27:41 +01:00
|
|
|
if MENU[i].has("standalone_only") and MENU[i].standalone_only and Engine.editor_hint:
|
|
|
|
continue
|
2018-08-05 18:46:23 +02:00
|
|
|
if MENU[i].menu != menu_name:
|
|
|
|
continue
|
|
|
|
if MENU[i].has("submenu"):
|
|
|
|
var submenu = PopupMenu.new()
|
2019-01-24 22:27:41 +01:00
|
|
|
var submenu_function = "create_menu_"+MENU[i].submenu
|
|
|
|
if has_method(submenu_function):
|
|
|
|
call(submenu_function, submenu)
|
|
|
|
else:
|
|
|
|
create_menu(submenu, MENU[i].submenu)
|
2018-08-05 18:46:23 +02:00
|
|
|
menu.add_child(submenu)
|
|
|
|
menu.add_submenu_item(MENU[i].description, submenu.get_name())
|
|
|
|
elif MENU[i].has("description"):
|
2018-08-07 23:21:12 +02:00
|
|
|
var shortcut = 0
|
|
|
|
if MENU[i].has("shortcut"):
|
|
|
|
for s in MENU[i].shortcut.split("+"):
|
|
|
|
if s == "Alt":
|
|
|
|
shortcut |= KEY_MASK_ALT
|
|
|
|
elif s == "Control":
|
|
|
|
shortcut |= KEY_MASK_CTRL
|
|
|
|
elif s == "Shift":
|
|
|
|
shortcut |= KEY_MASK_SHIFT
|
|
|
|
else:
|
|
|
|
shortcut |= OS.find_scancode_from_string(s)
|
|
|
|
menu.add_item(MENU[i].description, i, shortcut)
|
2018-08-05 18:46:23 +02:00
|
|
|
else:
|
|
|
|
menu.add_separator()
|
|
|
|
return menu
|
|
|
|
|
2019-01-24 22:27:41 +01:00
|
|
|
func update_recents_menu():
|
|
|
|
if recent_files_submenu != null:
|
|
|
|
recent_files_submenu.clear()
|
|
|
|
for i in recent_files.size():
|
|
|
|
recent_files_submenu.add_item(recent_files[i], i)
|
|
|
|
|
|
|
|
func create_menu_load_recent(menu):
|
|
|
|
menu.connect("id_pressed", self, "_on_LoadRecent_id_pressed")
|
|
|
|
recent_files_submenu = menu
|
|
|
|
update_recents_menu()
|
|
|
|
|
|
|
|
func load_recents():
|
|
|
|
var f = File.new()
|
|
|
|
if f.open("user://recent_files.bin", File.READ) == OK:
|
|
|
|
recent_files = parse_json(f.get_as_text())
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
func add_recent(path):
|
|
|
|
while true:
|
|
|
|
var index = recent_files.find(path)
|
|
|
|
if index >= 0:
|
|
|
|
recent_files.remove(index)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
recent_files.push_front(path)
|
|
|
|
update_recents_menu()
|
|
|
|
var f = File.new()
|
|
|
|
f.open("user://recent_files.bin", File.WRITE)
|
|
|
|
f.store_string(to_json(recent_files))
|
|
|
|
f.close()
|
|
|
|
|
2018-08-27 12:16:37 +02:00
|
|
|
func menu_about_to_show(name, menu):
|
|
|
|
for i in MENU.size():
|
|
|
|
if MENU[i].menu != name:
|
|
|
|
continue
|
|
|
|
if MENU[i].has("submenu"):
|
|
|
|
pass
|
|
|
|
elif MENU[i].has("command"):
|
|
|
|
var command_name = MENU[i].command+"_is_disabled"
|
|
|
|
if has_method(command_name):
|
|
|
|
var is_disabled = call(command_name)
|
|
|
|
menu.set_item_disabled(menu.get_item_index(i), is_disabled)
|
|
|
|
|
2018-08-07 09:31:41 +02:00
|
|
|
func new_pane():
|
2018-09-03 21:35:44 +02:00
|
|
|
var graph_edit = preload("res://addons/material_maker/graph_edit.tscn").instance()
|
2018-10-13 17:31:37 +02:00
|
|
|
graph_edit.node_factory = $NodeFactory
|
2018-09-04 21:45:14 +02:00
|
|
|
graph_edit.renderer = $Renderer
|
|
|
|
graph_edit.editor_interface = editor_interface
|
2018-08-05 18:46:23 +02:00
|
|
|
$VBoxContainer/HBoxContainer/Projects.add_child(graph_edit)
|
|
|
|
$VBoxContainer/HBoxContainer/Projects.current_tab = graph_edit.get_index()
|
2018-08-07 09:31:41 +02:00
|
|
|
return graph_edit
|
|
|
|
|
|
|
|
func new_material():
|
|
|
|
var graph_edit = new_pane()
|
2019-08-14 15:55:39 +02:00
|
|
|
graph_edit.new_material()
|
2018-08-05 18:46:23 +02:00
|
|
|
graph_edit.update_tab_title()
|
|
|
|
|
|
|
|
func load_material():
|
|
|
|
var dialog = FileDialog.new()
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.rect_min_size = Vector2(500, 500)
|
|
|
|
dialog.access = FileDialog.ACCESS_FILESYSTEM
|
2018-08-27 17:22:57 +02:00
|
|
|
dialog.mode = FileDialog.MODE_OPEN_FILES
|
2018-08-05 18:46:23 +02:00
|
|
|
dialog.add_filter("*.ptex;Procedural textures file")
|
2018-08-27 17:22:57 +02:00
|
|
|
dialog.connect("files_selected", self, "do_load_materials")
|
2018-08-05 18:46:23 +02:00
|
|
|
dialog.popup_centered()
|
|
|
|
|
2018-08-27 17:22:57 +02:00
|
|
|
func do_load_materials(filenames):
|
|
|
|
for f in filenames:
|
|
|
|
do_load_material(f)
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func do_load_material(filename):
|
2018-08-09 22:19:27 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
2019-08-12 15:58:42 +02:00
|
|
|
var node_count = 2 # So test below succeeds if graph_edit is null...
|
|
|
|
if graph_edit != null:
|
2019-08-14 15:55:39 +02:00
|
|
|
node_count = 0
|
2018-08-09 22:19:27 +02:00
|
|
|
for c in graph_edit.get_children():
|
|
|
|
if c is GraphNode:
|
2019-08-14 15:55:39 +02:00
|
|
|
print(c.name)
|
2018-08-09 22:19:27 +02:00
|
|
|
node_count += 1
|
|
|
|
if node_count > 1:
|
|
|
|
break
|
2019-08-14 15:55:39 +02:00
|
|
|
print(node_count)
|
2018-08-09 22:19:27 +02:00
|
|
|
if node_count > 1:
|
|
|
|
graph_edit = new_pane()
|
2019-08-12 15:58:42 +02:00
|
|
|
graph_edit.load_file(filename)
|
2019-01-24 22:27:41 +01:00
|
|
|
add_recent(filename)
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func save_material():
|
2018-08-10 12:31:15 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null:
|
2019-08-12 15:58:42 +02:00
|
|
|
if graph_edit.save_path != null:
|
|
|
|
graph_edit.save_file(graph_edit.save_path)
|
|
|
|
else:
|
|
|
|
save_material_as()
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func save_material_as():
|
2018-08-10 12:31:15 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null:
|
2019-08-12 15:58:42 +02:00
|
|
|
var dialog = FileDialog.new()
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.rect_min_size = Vector2(500, 500)
|
|
|
|
dialog.access = FileDialog.ACCESS_FILESYSTEM
|
|
|
|
dialog.mode = FileDialog.MODE_SAVE_FILE
|
|
|
|
dialog.add_filter("*.ptex;Procedural textures file")
|
|
|
|
dialog.connect("file_selected", graph_edit, "save_file")
|
|
|
|
dialog.popup_centered()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func close_material():
|
2018-08-27 17:22:57 +02:00
|
|
|
$VBoxContainer/HBoxContainer/Projects.close_tab()
|
2018-08-10 12:31:15 +02:00
|
|
|
|
|
|
|
func export_material():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
2018-08-27 12:16:37 +02:00
|
|
|
if graph_edit != null :
|
2018-08-26 16:22:42 +02:00
|
|
|
graph_edit.export_textures()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2018-08-27 12:16:37 +02:00
|
|
|
func export_material_is_disabled():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit == null or graph_edit.save_path == null:
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
2018-08-11 11:33:24 +02:00
|
|
|
func quit():
|
2018-08-16 15:08:09 +02:00
|
|
|
if Engine.editor_hint:
|
2018-09-04 21:45:14 +02:00
|
|
|
emit_signal("quit")
|
2018-08-16 15:08:09 +02:00
|
|
|
else:
|
|
|
|
get_tree().quit()
|
2018-08-07 09:31:41 +02:00
|
|
|
|
2018-08-19 02:11:52 +02:00
|
|
|
func edit_cut():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null:
|
|
|
|
graph_edit.cut()
|
|
|
|
|
2018-08-27 12:16:37 +02:00
|
|
|
func edit_cut_is_disabled():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
return graph_edit == null or !graph_edit.can_copy()
|
|
|
|
|
2018-08-19 02:11:52 +02:00
|
|
|
func edit_copy():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null:
|
|
|
|
graph_edit.copy()
|
|
|
|
|
2018-08-27 12:16:37 +02:00
|
|
|
func edit_copy_is_disabled():
|
|
|
|
return edit_cut_is_disabled()
|
|
|
|
|
2018-08-19 02:11:52 +02:00
|
|
|
func edit_paste():
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null:
|
|
|
|
graph_edit.paste()
|
|
|
|
|
2018-08-27 12:16:37 +02:00
|
|
|
func edit_paste_is_disabled():
|
|
|
|
var data = parse_json(OS.clipboard)
|
|
|
|
return data == null
|
|
|
|
|
2019-09-09 22:00:18 +02:00
|
|
|
func get_selected_nodes():
|
2018-08-10 12:31:15 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if graph_edit != null and graph_edit is GraphEdit:
|
2018-08-22 07:33:50 +02:00
|
|
|
var selected_nodes = []
|
2018-08-10 12:31:15 +02:00
|
|
|
for n in graph_edit.get_children():
|
2018-08-07 23:21:12 +02:00
|
|
|
if n is GraphNode and n.selected:
|
2018-08-22 07:33:50 +02:00
|
|
|
selected_nodes.append(n)
|
2019-09-09 22:00:18 +02:00
|
|
|
return selected_nodes
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
func make_selected_nodes_editable():
|
|
|
|
var selected_nodes = get_selected_nodes()
|
|
|
|
if !selected_nodes.empty():
|
|
|
|
for n in selected_nodes:
|
|
|
|
print(n.name)
|
|
|
|
n.generator.model = null
|
|
|
|
n.update_node()
|
|
|
|
|
|
|
|
func add_to_user_library():
|
|
|
|
var selected_nodes = get_selected_nodes()
|
|
|
|
if !selected_nodes.empty():
|
|
|
|
var dialog = preload("res://addons/material_maker/widgets/line_dialog.tscn").instance()
|
|
|
|
dialog.set_texts("New library element", "Select a name for the new library element")
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.connect("ok", self, "do_add_to_user_library", [ selected_nodes ])
|
|
|
|
dialog.popup_centered()
|
2018-08-22 07:33:50 +02:00
|
|
|
|
|
|
|
func do_add_to_user_library(name, nodes):
|
|
|
|
var data
|
|
|
|
if nodes.size() == 1:
|
|
|
|
data = nodes[0].serialize()
|
|
|
|
data.erase("node_position")
|
|
|
|
else:
|
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
data = graph_edit.serialize_selection()
|
2018-08-07 23:21:12 +02:00
|
|
|
var dir = Directory.new()
|
|
|
|
dir.make_dir("user://library")
|
|
|
|
dir.make_dir("user://library/user")
|
|
|
|
data.library = "user://library/user.json"
|
|
|
|
data.icon = name.right(name.rfind("/")+1).to_lower()
|
|
|
|
$VBoxContainer/HBoxContainer/VBoxContainer/Library.add_item(data, name)
|
2018-08-10 12:31:15 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
2018-08-22 07:33:50 +02:00
|
|
|
graph_edit.export_texture(nodes[0], "user://library/user/"+data.icon+".png", 64)
|
2018-08-07 23:21:12 +02:00
|
|
|
|
|
|
|
func save_user_library():
|
|
|
|
print("Saving user library")
|
|
|
|
$VBoxContainer/HBoxContainer/VBoxContainer/Library.save_library("user://library/user.json")
|
|
|
|
|
2018-08-25 08:07:22 +02:00
|
|
|
func show_doc():
|
|
|
|
var doc_path = OS.get_executable_path()
|
|
|
|
doc_path = doc_path.replace("\\", "/")
|
|
|
|
doc_path = doc_path.left(doc_path.rfind("/")+1)+"doc/index.html"
|
|
|
|
var file = File.new()
|
|
|
|
if file.exists(doc_path):
|
|
|
|
OS.shell_open(doc_path)
|
|
|
|
|
2018-08-11 11:33:24 +02:00
|
|
|
func bug_report():
|
|
|
|
OS.shell_open("https://github.com/RodZill4/godot-procedural-textures/issues")
|
|
|
|
|
|
|
|
func about():
|
2018-09-03 21:35:44 +02:00
|
|
|
var about_box = preload("res://addons/material_maker/widgets/about.tscn").instance()
|
2018-08-11 11:33:24 +02:00
|
|
|
add_child(about_box)
|
|
|
|
about_box.popup_centered()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func _on_PopupMenu_id_pressed(id):
|
|
|
|
var node_type = null
|
|
|
|
if MENU[id].has("command"):
|
2018-08-10 12:31:15 +02:00
|
|
|
var command = MENU[id].command
|
|
|
|
if has_method(command):
|
|
|
|
call(command)
|
2019-04-13 19:44:37 +02:00
|
|
|
|
2019-01-24 22:27:41 +01:00
|
|
|
func _on_LoadRecent_id_pressed(id):
|
|
|
|
do_load_material(recent_files[id])
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2018-08-07 09:31:41 +02:00
|
|
|
# Preview
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func update_preview():
|
2019-09-13 08:07:46 +02:00
|
|
|
var material_node = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control().get_node("node_Material")
|
2018-08-05 18:46:23 +02:00
|
|
|
if material_node != null:
|
2019-09-13 08:07:46 +02:00
|
|
|
var status = material_node.generator.render_textures(renderer)
|
|
|
|
while status is GDScriptFunctionState:
|
|
|
|
status = yield(status, "completed")
|
|
|
|
material_node.generator.update_materials($VBoxContainer/HBoxContainer/VBoxContainer/Preview.get_materials())
|
2018-08-07 09:31:41 +02:00
|
|
|
update_preview_2d()
|
|
|
|
|
|
|
|
func update_preview_2d(node = null):
|
2018-08-10 12:31:15 +02:00
|
|
|
var graph_edit = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
2018-08-07 09:31:41 +02:00
|
|
|
var preview = $VBoxContainer/HBoxContainer/VBoxContainer/Preview
|
|
|
|
if node == null:
|
2018-08-10 12:31:15 +02:00
|
|
|
for n in graph_edit.get_children():
|
2018-08-07 09:31:41 +02:00
|
|
|
if n is GraphNode and n.selected:
|
|
|
|
node = n
|
|
|
|
break
|
|
|
|
if node != null:
|
2019-08-17 17:35:48 +02:00
|
|
|
var status = node.generator.render(0, renderer, 1024)
|
|
|
|
while status is GDScriptFunctionState:
|
|
|
|
status = yield(status, "completed")
|
|
|
|
if status:
|
2019-08-12 15:58:42 +02:00
|
|
|
var image = renderer.get_texture().get_data()
|
|
|
|
var tex = ImageTexture.new()
|
|
|
|
tex.create_from_image(image)
|
|
|
|
preview.set_2d(tex)
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func _on_Projects_tab_changed(tab):
|
2018-08-27 17:22:57 +02:00
|
|
|
var new_tab = $VBoxContainer/HBoxContainer/Projects.get_current_tab_control()
|
|
|
|
if new_tab != current_tab:
|
|
|
|
if new_tab != null:
|
|
|
|
for c in get_incoming_connections():
|
|
|
|
if c.method_name == "update_preview" or c.method_name == "update_preview_2d":
|
|
|
|
c.source.disconnect(c.signal_name, self, c.method_name)
|
|
|
|
new_tab.connect("graph_changed", self, "update_preview")
|
|
|
|
new_tab.connect("node_selected", self, "update_preview_2d")
|
|
|
|
current_tab = new_tab
|
2018-08-07 09:31:41 +02:00
|
|
|
update_preview()
|