Merge pull request #74 from metincetin/master

Added popup menu for adding nodes on graph edit with space key or connect into empty space
This commit is contained in:
Rodz Labs 2020-01-31 07:47:43 +01:00 committed by GitHub
commit b04937bf83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 211 additions and 8 deletions

View File

@ -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:
@ -165,6 +164,7 @@ func update_graph(generators, connections) -> Array:
rv.push_back(node)
for c in connections:
.connect_node("node_"+c.from, c.from_port, "node_"+c.to, c.to_port)
return rv
func new_material() -> void:
@ -372,10 +372,22 @@ 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")
func request_popup(from,from_slot,release_position):
$"../AddNodePopup".rect_global_position = get_global_mouse_position()
$"../AddNodePopup".show()
$"../AddNodePopup".set_quick_connect(from,from_slot)
#$"../AddNodePopup".connect_to
pass
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)

View File

@ -28,6 +28,9 @@ custom_styles/bg = SubResource( 1 )
right_disconnects = true
use_snap = false
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Timer" type="Timer" parent="."]
wait_time = 0.2
@ -76,6 +79,7 @@ margin_bottom = 24.0
hint_tooltip = "Show hierarchy"
icon = SubResource( 4 )
[connection signal="connection_request" from="." to="." method="connect_node"]
[connection signal="connection_to_empty" from="." to="." method="request_popup"]
[connection signal="disconnection_request" from="." to="." method="disconnect_node"]
[connection signal="duplicate_nodes_request" from="." to="." method="duplicate_selected"]
[connection signal="gui_input" from="." to="." method="_on_GraphEdit_gui_input"]

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=2]
[gd_scene load_steps=12 format=2]
[ext_resource path="res://material_maker/main_window.gd" type="Script" id=1]
[ext_resource path="res://material_maker/library.tscn" type="PackedScene" id=2]
@ -6,17 +6,18 @@
[ext_resource path="res://material_maker/preview/preview_3d.tscn" type="PackedScene" id=4]
[ext_resource path="res://material_maker/preview/preview_3d_ui.tscn" type="PackedScene" id=5]
[ext_resource path="res://material_maker/widgets/tabs.gd" type="Script" id=6]
[ext_resource path="res://material_maker/icons/icons.svg" type="Texture" id=7]
[ext_resource path="res://material_maker/node_factory.gd" type="Script" id=8]
[ext_resource path="res://material_maker/widgets/add_node_popup.tscn" type="PackedScene" id=7]
[ext_resource path="res://material_maker/icons/icons.svg" type="Texture" id=8]
[ext_resource path="res://material_maker/node_factory.gd" type="Script" id=9]
[sub_resource type="AtlasTexture" id=1]
flags = 4
atlas = ExtResource( 7 )
atlas = ExtResource( 8 )
region = Rect2( 96, 32, 32, 32 )
[sub_resource type="AtlasTexture" id=2]
flags = 4
atlas = ExtResource( 7 )
atlas = ExtResource( 8 )
region = Rect2( 96, 64, 32, 32 )
[node name="MainWindow" type="Panel"]
@ -152,6 +153,8 @@ margin_bottom = 24.0
tab_align = 0
tab_close_display_policy = 1
[node name="AddNodePopup" parent="VBoxContainer/HBoxContainer/ProjectsPane/Projects" instance=ExtResource( 7 )]
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/HBoxContainer/ProjectsPane"]
anchor_top = 1.0
anchor_bottom = 1.0
@ -191,7 +194,7 @@ margin_bottom = 30.0
size_flags_vertical = 4
[node name="NodeFactory" type="Node" parent="."]
script = ExtResource( 8 )
script = ExtResource( 9 )
[connection signal="need_update" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview/Preview3D" to="." method="update_preview_3d"]
[connection signal="environment_selected" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview/Preview3D/Preview3DUI" to="VBoxContainer/HBoxContainer/VBoxContainer/Preview/Preview3D" method="_on_Environment_item_selected"]
[connection signal="model_selected" from="VBoxContainer/HBoxContainer/VBoxContainer/Preview/Preview3D/Preview3DUI" to="VBoxContainer/HBoxContainer/VBoxContainer/Preview/Preview3D" method="_on_Model_item_selected"]

View File

@ -0,0 +1,145 @@
extends Popup
var libraries = []
var data = []
onready var itemlist : ItemList = $PanelContainer/VBoxContainer/ItemList
onready var filter_line_edit : LineEdit = $PanelContainer/VBoxContainer/Filter
func get_current_graph():
return get_parent().get_current_tab_control()
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_selected",self,"item_selected")
itemlist.connect("item_activated",self,"item_selected")
update_list()
func filter_entered(filter):
item_selected(0)
#print (c.get_metadata(0))
#func get_selected_item_name() -> String:
# return get_item_path(itemlist.get_selected())
func add_node(data):
var node:GraphNode = get_current_graph().create_nodes(data,get_current_graph().offset_from_global_position(get_transform().xform(Vector2(0,0))))[0]
hide()
clear()
# if this node created by dragging to an empty space
if quick_connect_node != null:
var type = quick_connect_node.get_connection_output_type(quick_connect_slot)
for new_slot in node.get_connection_input_count():
if type == node.get_connection_input_type(new_slot):
#connect the first two slots with the same type
get_current_graph().connect_node(quick_connect_node.name,quick_connect_slot,node.name,new_slot)
break
quick_connect_node = null
func item_selected(index):
# checks if mouse left | enter pressed. it prevents
# adding nodes just by using arrow keys as it selects the item
if Input.is_mouse_button_pressed(BUTTON_LEFT) || Input.is_key_pressed(KEY_ENTER):
if (index>=itemlist.get_item_count()):
return
if (itemlist.is_item_selectable(index) == false):
item_selected(index+1)
return
add_node(data[index])
hide()
clear()
pass
func show():
.show()
update_list()
filter_line_edit.grab_focus()
var parent_rect = get_parent().get_parent().get_global_rect()
var clipped = parent_rect.clip(get_global_rect())
var offset = (get_rect().size-clipped.size)
rect_position = rect_position - offset
func update_list(filter : String = "") -> void:
clear_list()
data.clear()
for library in libraries:
for obj in library:
if !obj.has("type"):
continue
var show : bool = true
for f in filter.to_lower().split(" ", false):
if f != "" && obj.tree_item.to_lower().find(f) == -1:
show = false
break
if show:
data.append(obj)
itemlist.add_item(obj.tree_item, get_preview_texture(obj))
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:
image.resize(16, 16)
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
# Quickly connecting when tried to connect to empty
var quick_connect_node:GraphNode
var quick_connect_slot = 0
func set_quick_connect(from,from_slot):
quick_connect_node = get_current_graph().get_node(from)
quick_connect_slot = from_slot
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 = ""
func _on_itemlist_focus_entered():
# if itemlist received focus and no item is yet selected
# select the first item
if itemlist.get_selected_items().size() == 0:
itemlist.select(0)
pass # Replace with function body.

View File

@ -0,0 +1,34 @@
[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"]
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
allow_reselect = true
[connection signal="focus_entered" from="PanelContainer/VBoxContainer/ItemList" to="." method="_on_itemlist_focus_entered"]

View File

@ -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]