forked from drankfrabbin/gohotdraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
272 lines (228 loc) · 6.68 KB
/
tools.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package gohotdraw
import (
"math"
)
type Tool interface {
MouseListener
MouseMotionListener
IsActive() bool
Activate()
Deactivate()
}
type DefaultTool struct {
editor DrawingEditor
isActive bool
anchorX, anchorY int
}
func newDefaultTool(editor DrawingEditor) *DefaultTool {
tool := &DefaultTool{}
tool.editor = editor
return tool
}
func (this *DefaultTool) Activate() {
this.editor.GetView().ClearSelection()
this.isActive = true
}
func (this *DefaultTool) Deactivate() {
this.isActive = false
}
func (this *DefaultTool) IsActive() bool {
return this.isActive
}
func (this *DefaultTool) MouseDown(e *MouseEvent) {
this.anchorX = e.X
this.anchorY = e.Y
}
func (this *DefaultTool) MouseDrag(e *MouseEvent) {}
func (this *DefaultTool) MouseUp(e *MouseEvent) {}
type CreationTool struct {
*DefaultTool
anchor *Point
createdFigure Figure
prototype Figure
}
func NewCreationTool(editor DrawingEditor, prototype Figure) *CreationTool {
tool := &CreationTool{}
tool.DefaultTool = newDefaultTool(editor)
tool.prototype = prototype
return tool
}
func (this *CreationTool) MouseDown(e *MouseEvent) {
this.anchor = e.GetPoint()
this.createdFigure = this.createFigure()
this.createdFigure.SetDisplayBox(this.createdFigure, this.anchor, this.anchor)
this.editor.GetView().Add(this.createdFigure)
}
func (this *CreationTool) MouseDrag(e *MouseEvent) {
this.createdFigure.SetDisplayBox(this.createdFigure, this.anchor, e.GetPoint())
}
func (this *CreationTool) MouseUp(e *MouseEvent) {
if this.createdFigure.IsEmpty(this.createdFigure) {
this.editor.GetView().Remove(this.createdFigure)
}
this.createdFigure = nil
this.editor.ToolDone()
}
func (this *CreationTool) createFigure() Figure {
if this.prototype == nil {
panic("No prototype defined")
}
return this.prototype.Clone()
}
type NullTool struct {
*DefaultTool
}
func NewNullTool(editor DrawingEditor) *NullTool {
return &NullTool{newDefaultTool(editor)}
}
type SelectionTool struct {
*DefaultTool
currentTool Tool
}
func NewSelectionTool(editor DrawingEditor) *SelectionTool {
tool := &SelectionTool{}
tool.DefaultTool = newDefaultTool(editor)
return tool
}
func (this *SelectionTool) MouseDown(e *MouseEvent) {
selectedHandle := this.editor.GetView().FindHandle(e.GetPoint())
//fmt.Printf("handle selected: %v\n", selectedHandle)
if selectedHandle != nil {
this.currentTool = NewHandleTracker(this.editor, selectedHandle)
} else {
selectedFigure := this.editor.GetView().GetDrawing().FindFigure(e.GetPoint())
if selectedFigure != nil {
this.currentTool = NewDragTracker(this.editor, selectedFigure)
//rect := selectedFigure.GetDisplayBox()
//fmt.Printf("found figure - X: %v, Y: %v, W: %v, H: %v\n", rect.X, rect.Y, rect.Width, rect.Height)
} else {
if !e.IsShiftDown() {
this.editor.GetView().ClearSelection()
}
this.currentTool = NewAreaTracker(this.editor)
}
}
this.currentTool.MouseDown(e)
//this.currentTool.Activate()
}
func (this *SelectionTool) MouseDrag(e *MouseEvent) {
if this.currentTool != nil {
this.currentTool.MouseDrag(e)
}
}
func (this *SelectionTool) MouseUp(e *MouseEvent) {
if this.currentTool != nil {
this.currentTool.MouseUp(e)
this.currentTool.Deactivate()
this.currentTool = nil
}
}
type DragTracker struct {
*DefaultTool
anchorFigure Figure
hasMoved bool
lastX, lastY int
}
func NewDragTracker(editor DrawingEditor, anchorFigure Figure) *DragTracker {
tracker := &DragTracker{}
tracker.DefaultTool = newDefaultTool(editor)
tracker.anchorFigure = anchorFigure
return tracker
}
func (this *DragTracker) MouseDown(e *MouseEvent) {
this.DefaultTool.MouseDown(e)
this.lastX = e.X
this.lastY = e.Y
if e.IsShiftDown() {
this.editor.GetView().ToggleSelection(this.anchorFigure)
this.anchorFigure = nil
} else if !this.editor.GetView().IsFigureSelected(this.anchorFigure) {
this.editor.GetView().ClearSelection()
this.editor.GetView().AddToSelection(this.anchorFigure)
}
}
func (this *DragTracker) MouseDrag(e *MouseEvent) {
this.DefaultTool.MouseDrag(e)
this.hasMoved = math.Fabs(float64(e.X-this.anchorX)) > 3 ||
math.Fabs(float64(e.Y-this.anchorY)) > 3
if this.hasMoved {
selectedFigures := this.editor.GetView().GetSelection()
for i := 0; i < selectedFigures.Len(); i++ {
selectedFigure := selectedFigures.At(i).(Figure)
selectedFigure.MoveBy(selectedFigure, e.X-this.lastX, e.Y-this.lastY)
}
}
this.lastX = e.X
this.lastY = e.Y
}
type AreaTracker struct {
*DefaultTool
rubberband *Rectangle
}
func NewAreaTracker(editor DrawingEditor) *AreaTracker {
tracker := &AreaTracker{}
tracker.DefaultTool = newDefaultTool(editor)
return tracker
}
func (this *AreaTracker) MouseDown(e *MouseEvent) {
this.DefaultTool.MouseDown(e)
this.resizeRubberband(this.anchorX, this.anchorY, this.anchorX, this.anchorY)
}
func (this *AreaTracker) MouseDrag(e *MouseEvent) {
this.DefaultTool.MouseDrag(e)
this.drawRubberband()
this.resizeRubberband(this.anchorX, this.anchorY, e.X, e.Y)
}
func (this *AreaTracker) MouseUp(e *MouseEvent) {
this.DefaultTool.MouseUp(e)
this.selectGroup(e.IsShiftDown())
this.resizeRubberband(-1, -1, 0, 0)
}
func (this *AreaTracker) resizeRubberband(x1, y1, x2, y2 int) {
this.rubberband = NewRectangleFromPoints(&Point{x1, y1}, &Point{x2, y2})
this.drawRubberband()
}
func (this *AreaTracker) drawRubberband() {
g := this.editor.GetView().GetGraphics()
if g != nil {
this.editor.GetView().Repaint()
g.SetFGColor(GreyBlue)
g.DrawBorderFromRectDirectly(this.rubberband)
}
}
func (this *AreaTracker) selectGroup(toggle bool) {
figures := this.editor.GetView().GetDrawing().GetFigures()
for i := 0; i < figures.Len(); i++ {
currentFigure := figures.At(i).(Figure)
rect := currentFigure.GetDisplayBox()
if this.rubberband.Contains(rect.X, rect.Y) && this.rubberband.Contains(rect.X+rect.Width, rect.Y+rect.Height) {
if toggle {
this.editor.GetView().ToggleSelection(currentFigure)
} else {
this.editor.GetView().AddToSelection(currentFigure)
}
}
}
}
type HandleTracker struct {
*DefaultTool
anchorHandle Handle
}
func NewHandleTracker(editor DrawingEditor, anchorHandle Handle) *HandleTracker {
tracker := &HandleTracker{}
tracker.DefaultTool = newDefaultTool(editor)
tracker.anchorHandle = anchorHandle
return tracker
}
func (this *HandleTracker) MouseDown(e *MouseEvent) {
this.DefaultTool.MouseDown(e)
this.anchorHandle.InvokeStart(e.X, e.Y, this.editor.GetView())
}
func (this *HandleTracker) MouseDrag(e *MouseEvent) {
this.DefaultTool.MouseDrag(e)
this.anchorHandle.InvokeStep(e.X, e.Y, this.anchorX, this.anchorY, this.editor.GetView())
}
func (this *HandleTracker) MouseUp(e *MouseEvent) {
this.DefaultTool.MouseUp(e)
this.anchorHandle.InvokeEnd(e.X, e.Y, this.editor.GetView())
}