From cc6d9c8ddee545704dd9afd77d2a0ebb15e21244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Metin=20=C3=87ET=C4=B0N?= Date: Wed, 29 Jan 2020 15:01:42 +0300 Subject: [PATCH] Added popup menu for adding nodes on graph edit with space key --- material_maker/graph_edit.gd | 7 +- material_maker/graph_edit.tscn | 6 +- material_maker/widgets/add_node_popup.gd | 96 ++++++++++++++++++++++ material_maker/widgets/add_node_popup.tscn | 33 ++++++++ project.godot | 7 +- 5 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 material_maker/widgets/add_node_popup.gd create mode 100644 material_maker/widgets/add_node_popup.tscn diff --git a/material_maker/graph_edit.gd b/material_maker/graph_edit.gd index f165d0d..91b7e98 100644 --- a/material_maker/graph_edit.gd +++ b/material_maker/graph_edit.gd @@ -34,7 +34,6 @@ func _gui_input(event) -> void: remove_selection() # Misc. useful functions - func get_source(node, port) -> Dictionary: for c in get_connection_list(): if c.to == node and c.to_port == port: @@ -372,6 +371,10 @@ func set_last_selected(node) -> void: last_selected = null func _on_GraphEdit_gui_input(event) -> void: + if event.is_action_pressed("ui_library_popup") && get_rect().has_point(get_local_mouse_position()): + $AddNodePopup.rect_global_position = get_global_mouse_position() + $AddNodePopup.show() + if event is InputEventMouseButton: call_deferred("check_last_selected") @@ -379,3 +382,5 @@ func check_last_selected() -> void: if last_selected != null and !(is_instance_valid(last_selected) and last_selected.selected): last_selected = null emit_signal("node_selected", null) + + diff --git a/material_maker/graph_edit.tscn b/material_maker/graph_edit.tscn index 870f343..019fc21 100644 --- a/material_maker/graph_edit.tscn +++ b/material_maker/graph_edit.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://material_maker/graph_edit.gd" type="Script" id=1] [ext_resource path="res://material_maker/icons/icons.svg" type="Texture" id=2] +[ext_resource path="res://material_maker/widgets/add_node_popup.tscn" type="PackedScene" id=3] [sub_resource type="StyleBoxFlat" id=1] bg_color = Color( 0.6, 0.6, 0.6, 0 ) @@ -34,6 +35,7 @@ wait_time = 0.2 one_shot = true [node name="GraphUI" type="HBoxContainer" parent="."] +editor/display_folded = true anchor_left = 1.0 anchor_right = 1.0 margin_left = -262.0 @@ -75,6 +77,8 @@ margin_right = 246.0 margin_bottom = 24.0 hint_tooltip = "Show hierarchy" icon = SubResource( 4 ) + +[node name="AddNodePopup" parent="." instance=ExtResource( 3 )] [connection signal="connection_request" from="." to="." method="connect_node"] [connection signal="disconnection_request" from="." to="." method="disconnect_node"] [connection signal="duplicate_nodes_request" from="." to="." method="duplicate_selected"] diff --git a/material_maker/widgets/add_node_popup.gd b/material_maker/widgets/add_node_popup.gd new file mode 100644 index 0000000..c8fa9df --- /dev/null +++ b/material_maker/widgets/add_node_popup.gd @@ -0,0 +1,96 @@ +extends Popup + +var libraries = [] +var data = [] +onready var itemlist : ItemList = $PanelContainer/VBoxContainer/ItemList +onready var filter_line_edit : LineEdit = $PanelContainer/VBoxContainer/Filter + +func _ready() -> void: + + var lib_path = OS.get_executable_path().get_base_dir()+"/library/base.json" + if !add_library(lib_path): + add_library("res://material_maker/library/base.json") + add_library("user://library/user.json") + + + filter_line_edit.connect("text_changed" ,self,"update_list") + filter_line_edit.connect("text_entered",self,"filter_entered") + itemlist.connect("item_activated",self,"item_activated") + + update_list() + +func filter_entered(filter): + item_activated(0) +#func get_selected_item_name() -> String: +# return get_item_path(itemlist.get_selected()) +func item_activated(index): + if (index>=itemlist.get_item_count()): + return + if (itemlist.is_item_selectable(index) == false): + item_activated(index+1) + return + get_parent().create_nodes(data[index],get_parent().offset_from_global_position(get_global_transform().xform(get_local_mouse_position()))) + hide() + clear() + pass +func show(): + .show() + filter_line_edit.grab_focus() +func update_list(filter=""): + clear_list() + data.clear() + for library in libraries: + for obj in library: + + if (obj.tree_item.to_lower().find(filter)!=-1 || filter == ""): + data.append(obj) + itemlist.add_item(obj.tree_item.split("/")[-1],get_preview_texture(obj),obj.has("type")) + + +func get_preview_texture(data : Dictionary) -> ImageTexture: + if data.has("icon") and data.has("library"): + var image_path = data.library.left(data.library.rfind("."))+"/"+data.icon+".png" + var t : ImageTexture + if image_path.left(6) == "res://": + image_path = ProjectSettings.globalize_path(image_path) + t = ImageTexture.new() + var image : Image = Image.new() + if image.load(image_path) == OK: + t.create_from_image(image) + else: + print("Cannot load image "+image_path) + return t + return null + + +func clear_list(): + itemlist.clear() +func add_library(file_name : String, filter : String = "") -> bool: + + var file = File.new() + if file.open(file_name, File.READ) != OK: + return false + var lib = parse_json(file.get_as_text()) + file.close() + if lib != null and lib.has("lib"): + for m in lib.lib: + m.library = file_name + libraries.push_back(lib.lib) + return true + return false + + +func _input(event): + if event is InputEventMouseButton and event.is_pressed() and event.button_index == BUTTON_LEFT: + if !get_rect().has_point(event.position): + clear() + hide() + +func _unhandled_input(event): + if event is InputEventKey and event.scancode == KEY_ESCAPE: + clear() + hide() + +func clear(): + filter_line_edit.text = "" + update_list() \ No newline at end of file diff --git a/material_maker/widgets/add_node_popup.tscn b/material_maker/widgets/add_node_popup.tscn new file mode 100644 index 0000000..7a29cec --- /dev/null +++ b/material_maker/widgets/add_node_popup.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://material_maker/widgets/add_node_popup.gd" type="Script" id=1] + +[node name="AddNodePopup" type="Popup"] +visible = true +margin_right = 228.0 +margin_bottom = 319.0 +mouse_filter = 1 +script = ExtResource( 1 ) + +[node name="PanelContainer" type="PanelContainer" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer"] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 221.0 +margin_bottom = 312.0 + +[node name="Filter" type="LineEdit" parent="PanelContainer/VBoxContainer"] +margin_right = 214.0 +margin_bottom = 24.0 +rect_min_size = Vector2( 0, 20 ) +size_flags_horizontal = 3 + +[node name="ItemList" type="ItemList" parent="PanelContainer/VBoxContainer"] +margin_top = 28.0 +margin_right = 214.0 +margin_bottom = 305.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 diff --git a/project.godot b/project.godot index a8a601b..4c287d5 100644 --- a/project.godot +++ b/project.godot @@ -177,7 +177,6 @@ _global_script_class_icons={ [application] config/name="Material Maker" -config/description="An open source, extensible procedural material generation tool" run/main_scene="res://material_maker/main_window.tscn" config/use_custom_user_dir=true config/custom_user_dir_name="material_maker" @@ -185,6 +184,7 @@ boot_splash/image="res://rodz_labs_logo.png" boot_splash/fullsize=false boot_splash/bg_color=Color( 0.0901961, 0.0941176, 0.141176, 1 ) config/icon="res://icon.png" +config/description="An open source, extensible procedural material generation tool" config/release="0.8" [autoload] @@ -220,6 +220,11 @@ toggle_fullscreen={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":true,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777221,"unicode":0,"echo":false,"script":null) ] } +ui_library_popup={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) + ] +} [logging]