-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-temp.gd
194 lines (136 loc) · 5.17 KB
/
Test-temp.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
extends Control
var _vertices: Array = [];
var _handles: Array= [];
var _handleSize:int = 10;
var _mouseOverHandleIndex = -1;
var _draggingHandleIndex = -1;
var _draggingType:int = -1;
enum VertexHandleStyle {MouseOver, Dragging, None}
enum HandleType {Vertex, Segment}
class utils:
static func _global_to_control(globalCoords: Vector2) -> Vector2:
var globalControlPos = get_global_rect().position;
return Vector2(globalCoords.x - globalControlPos.x, globalCoords.y - globalControlPos.y);
class HandleTheme:
var onMouseOverSize:float;
var onMouseOverColor:Color;
var onMousePressedSize: float;
var onMousePressedColor:Color;
class Handle:
var theme: HandleTheme;
var position: Vector2 = Vector2.ZERO setget setPosition, getPosition;
func setPosition(value:Vector2): position = value;
func getPosition() -> Vector2: return position;
func _init(theme:HandleTheme, position:Vector2):
self.theme = theme;
self.position = position;
func isMouseOver(globalPos:Vector2):
var relativePos:Vector2 = _global_to_control(globalPos);
var distance:float = (position-relativePos).length();
return true if distance <= _handleSize else false;
func draw(node:CanvasItem): assert(false, "Not implemented...");
class VertexHandle extends Handle:
var _vertexId:int = -1;
func _init(vertexId:int, theme:HandleTheme, position:Vector2):
super(theme, position);
_vertexId = vertexId;
func draw(node:CanvasItem, handleSize:float, style:int):
if !_hasHandle: return;
match(style):
VertexHandleStyle.MouseOver,VertexHandleStyle.Dragging:
node.draw_circle(position,handleSize,Color.red)
_:
node.draw_circle(position,handleSize,Color.aqua)
class SegmentHandle extends Handle:
func _init(): pass
class Vertex:
enum MovementConstraint { Horizontal, Vertical, TwoD, None}
var _movementConstraint: int = MovementConstraint.None;
var _hasHandle: bool = false;
var position: Vector2 = Vector2.ZERO setget setPosition, getPosition;
func setPosition(value:Vector2): position = value;
func getPosition() -> Vector2: return position;
func _init(position:Vector2):
self.position = position;
func moveTo(position:Vector2) -> Vector2:
match(_movementConstraint):
MovementConstraint.Horizontal:
self.position.x = position.x;
MovementConstraint.Vertical:
self.position.y = position.y;
MovementConstraint.Two2D:
self.position = position;
return position
func draw(node:CanvasItem, handleSize:float, style:int):
if !_hasHandle: return;
match(style):
VertexHandleStyle.MouseOver,VertexHandleStyle.Dragging:
node.draw_circle(position,handleSize,Color.red)
_:
node.draw_circle(position,handleSize,Color.aqua)
# Called when the node enters the scene tree for the first time.
func _ready():
var size: Vector2 = rect_size;
var theme = HandleTheme.new();
theme.onMouseOverColor = Color.aqua
theme.onMouseOverSize = 5;
theme.onMousePressedColor = Color.red;
theme.onMousePressedSize = 5;
_vertices = [ Vector2(0,size.y), Vector2(size.x/3.0,size.y/2), Vector2(size.x*2/3.0,size.y/2), Vector2(size.x,size.y)];
_handles = [ VertexHandle.new(1,theme, Vector2(size.x/3.0,size.y/2)),
VertexHandle.new(2,theme,Vector2(size.x*2/3.0,size.y/2))];
func _process(delta):
pass;
func _draw():
var size: Vector2 = rect_size;
for i in range(0,_vertices.size()-1):
draw_line(_vertices[i].position, _vertices[i+1].position, Color.white,5.0,true);
var style:int = VertexHandleStyle.None;
if _mouseOverHandleIndex == i:
style = VertexHandleStyle.MouseOver
elif _draggingHandleIndex == i:
style = VertexHandleStyle.Dragging
_vertices[i].draw(self, _handleSize, style);
#draw now segment handles
"""
for i in range(0,_segmentHandles.size()):
var segmentIndex:int = _segmentHandles[i];
var midPoint = (_vertices[segmentIndex+1].position - _vertices[segmentIndex].position)/2.0 + _vertices[segmentIndex].position;
draw_circle(midPoint, _handleSize, Color.chartreuse);
"""
func _handleDraggingStart(handleIndex: int):
_draggingHandleIndex = handleIndex;
func _handleDraggingEnd(handleIndex: int):
_draggingHandleIndex = -1;
func _handleDraggingMove(globalPos:Vector2):
var relativePos:Vector2 = _global_to_control(globalPos);
_vertices[_draggingHandleIndex].moveTo(relativePos);
#_handles[1].position = (_handles[2].position - _handles[0].position)/2.0 + _handles[0].position;
update();
pass;
func _input(event):
if event is InputEventMouseButton:
#print("Mouse click at: ", _global_to_control(event.position));
if event.button_index == 1:
var index = _getHandleIndexAtGlobalPos(event.position);
if event.pressed:
_handleDraggingStart(index);
else:
_handleDraggingEnd(index);
elif event is InputEventMouseMotion:
if _draggingHandleIndex != -1:
_handleDraggingMove(event.position);
else:
var index = _getHandleIndexAtGlobalPos(event.position);
if index != _mouseOverHandleIndex:
_mouseOverHandleIndex = index;
update();
func _getHandleIndexAtGlobalPos(globalPos:Vector2) -> int:
var foundIndex = -1;
var i = 0;
while i < _handles.size() && foundIndex==-1:
if(_handles[i].isMouseOver(globalPos)):
foundIndex = i;
else:
i+=1;
return foundIndex;