-
Notifications
You must be signed in to change notification settings - Fork 2
/
GraphVertex.gd
74 lines (66 loc) · 2.08 KB
/
GraphVertex.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class_name GraphVertex
extends TextureRect
var click_toggling = false
var dragging = false
var original_pos = Vector2(0,0)
var original_mouse_pos = Vector2()
var adjs_delta_angle = []
var speed = Vector2()
var accel = Vector2()
func _ready():
_on_GraphVertex_mouse_entered()
_on_GraphVertex_mouse_exited()
func _on_GraphVertex_mouse_entered():
get_parent().setHoveredVertex(self)
func _on_GraphVertex_mouse_exited():
if( get_parent().hovered_vertex==self ):
get_parent().setHoveredVertex(null)
dragging = false
set_process(false)
click_toggling = false
func _mouse_click():
var graph = get_parent()
if graph.clicked_vertex == null:
graph.clicked_vertex = self
elif graph.clicked_vertex != self:
graph.toggleEdge(self,graph.clicked_vertex)
graph.clicked_vertex = null
graph.updateCalculations()
elif graph.clicked_vertex == self:
graph.clicked_vertex = null
func _gui_input(event):
if( event is InputEventMouseButton):
if event.button_index==BUTTON_LEFT:
if event.is_pressed():
click_toggling = true
original_pos = rect_global_position
original_mouse_pos = get_global_mouse_position()
dragging = true
set_process(true)
#get_tree().set_input_as_handled() # It doesnt work as expected
else:
if click_toggling:
_mouse_click()
click_toggling = false
dragging = false
set_process(false)
#get_tree().set_input_as_handled() # It doesnt work as expected
elif event.button_index==BUTTON_RIGHT:
if event.is_pressed():
var graph = get_parent()
graph.removeVertex(self)
graph.updateCalculations()
get_tree().set_input_as_handled()
elif( event is InputEventMouseMotion ):
if( dragging ):
raise()
var delta_pos = get_global_mouse_position() - original_mouse_pos
rect_global_position = original_pos + get_global_mouse_position() - original_mouse_pos
if( delta_pos.length_squared() > 2 ):
click_toggling = false
get_parent().update()
func _exit_tree():
if get_parent().hovered_vertex == self:
get_parent().hovered_vertex = null
if get_parent().clicked_vertex == self:
get_parent().clicked_vertex = null