From 4e071994f6bc3a71201cce1230c57ebd6903de01 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 28 Feb 2021 00:18:38 +0100 Subject: [PATCH] More initial work / experiments on the mesh data resource editor plugin. --- .../MDIGizmoPlugin.gd | 13 +++- .../mesh_data_resource_editor/MIDGizmo.gd | 77 ++++++++++++++----- .../mesh_data_resource_editor/plugin.gd | 25 ++++++ 3 files changed, 95 insertions(+), 20 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/MDIGizmoPlugin.gd b/game/addons/mesh_data_resource_editor/MDIGizmoPlugin.gd index be7c655a..c06dfc28 100644 --- a/game/addons/mesh_data_resource_editor/MDIGizmoPlugin.gd +++ b/game/addons/mesh_data_resource_editor/MDIGizmoPlugin.gd @@ -3,6 +3,8 @@ extends EditorSpatialGizmoPlugin const MDIGizmo = preload("res://addons/mesh_data_resource_editor/MIDGizmo.gd") +var plugin + func _init(): create_material("main", Color(1, 0, 0)) create_handle_material("handles") @@ -15,7 +17,14 @@ func get_priority(): func create_gizmo(spatial): if spatial is MeshDataInstance: - print("new gizmo") - return MDIGizmo.new() + var gizmo = MDIGizmo.new() + + gizmo.plugin = plugin + plugin.register_gizmo(gizmo) + + return gizmo else: return null + +func is_handle_highlighted(gizmo, index): + pass diff --git a/game/addons/mesh_data_resource_editor/MIDGizmo.gd b/game/addons/mesh_data_resource_editor/MIDGizmo.gd index 2a4a4e78..3da96960 100644 --- a/game/addons/mesh_data_resource_editor/MIDGizmo.gd +++ b/game/addons/mesh_data_resource_editor/MIDGizmo.gd @@ -3,17 +3,15 @@ extends EditorSpatialGizmo var gizmo_size = 3.0 +var plugin + +var vertices : PoolVector3Array +var indices : PoolIntArray + func set_handle(index: int, camera: Camera, point: Vector2): - #print(index) - #print(point) - - - pass func redraw(): - #print("MIDGizmo redraw") - clear() var node : MeshDataInstance = get_spatial_node() @@ -28,25 +26,68 @@ func redraw(): var handles_material : SpatialMaterial = get_plugin().get_material("handles", self) - var handles : PoolVector3Array = mdr.array[ArrayMesh.ARRAY_VERTEX] - add_handles(handles, handles_material) + vertices = mdr.array[ArrayMesh.ARRAY_VERTEX] + #add_handles(vertices, handles_material) var material = get_plugin().get_material("main", self) - var lines : PoolVector3Array = PoolVector3Array() var indices : PoolIntArray = mdr.array[ArrayMesh.ARRAY_INDEX] - if indices.size() % 3 == 0: - for i in range(0, len(indices), 3): - lines.append(handles[i]) - lines.append(handles[i + 1]) + var lines : PoolVector3Array = PoolVector3Array() + + if vertices.size() % 3 == 0: + for i in range(0, len(vertices), 3): + lines.append(vertices[i]) + lines.append(vertices[i + 1]) - lines.append(handles[i + 1]) - lines.append(handles[i + 2]) + lines.append(vertices[i + 1]) + lines.append(vertices[i + 2]) - lines.append(handles[i + 2]) - lines.append(handles[i]) + lines.append(vertices[i + 2]) + lines.append(vertices[i]) add_lines(lines, material, false) +func forward_spatial_gui_input(index, camera, event): + if event is InputEventMouseButton: + var gt : Transform = get_spatial_node().global_transform + var ray_from : Vector3 = camera.global_transform.origin + var gpoint : Vector2 = event.get_position() + var grab_threshold : float = 4 +# var grab_threshold : float = 4 * EDSCALE; + + if event.get_button_index() == BUTTON_LEFT: + if event.is_pressed(): + + var mouse_pos = event.get_position() + +# if (_gizmo_select(p_index, _edit.mouse_pos)) +# return true; + + # select vertex + var closest_idx : int = -1 + var closest_dist : float = 1e10 + + var vertices_size : int = vertices.size() + for i in range(vertices_size): + var joint_pos_3d : Vector3 = gt.xform(vertices[i]) + var joint_pos_2d : Vector2 = camera.unproject_position(joint_pos_3d) + var dist_3d : float = ray_from.distance_to(joint_pos_3d) + var dist_2d : float = gpoint.distance_to(joint_pos_2d) + + if (dist_2d < grab_threshold && dist_3d < closest_dist): + closest_dist = dist_3d; + closest_idx = i; + + if (closest_idx >= 0): + print("f") + else: + print("nf") + + return false + + +func _notification(what): + if what == NOTIFICATION_PREDELETE: + plugin.unregister_gizmo(self) diff --git a/game/addons/mesh_data_resource_editor/plugin.gd b/game/addons/mesh_data_resource_editor/plugin.gd index c34ad77c..353ee21f 100644 --- a/game/addons/mesh_data_resource_editor/plugin.gd +++ b/game/addons/mesh_data_resource_editor/plugin.gd @@ -5,8 +5,33 @@ const MdiGizmoPlugin = preload("res://addons/mesh_data_resource_editor/MDIGizmoP var gizmo_plugin = MdiGizmoPlugin.new() +var active_gizmos : Array = [] + func _enter_tree(): + gizmo_plugin.plugin = self + add_spatial_gizmo_plugin(gizmo_plugin) + + set_input_event_forwarding_always_enabled() func _exit_tree(): remove_spatial_gizmo_plugin(gizmo_plugin) + +#func forward_spatial_gui_input(camera, event): +# return forward_spatial_gui_input(0, camera, event) + +func register_gizmo(gizmo): + active_gizmos.append(gizmo) + +func unregister_gizmo(gizmo): + for i in range(active_gizmos.size()): + if active_gizmos[i] == gizmo: + active_gizmos.remove(i) + return + +func forward_spatial_gui_input(index, camera, event): + for g in active_gizmos: + if g.forward_spatial_gui_input(index, camera, event): + return true + + return false