-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandles.go
96 lines (75 loc) · 2.07 KB
/
handles.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
package gohotdraw
import (
_ "fmt"
)
const (
HANDLESIZE = 8
)
func GetHandleDisplayBox(handle Handle) *Rectangle {
p := handle.Locate()
return &Rectangle{p.X - HANDLESIZE/2, p.Y - HANDLESIZE/2, HANDLESIZE, HANDLESIZE}
}
func HandleContainsPoint(handle Handle, p *Point) bool {
return GetHandleDisplayBox(handle).ContainsPoint(p)
}
func DrawHandle(handle Handle, g Graphics) {
r := GetHandleDisplayBox(handle)
g.SetFGColor(LightGray)
g.DrawBorderedRectFromRect(r)
}
type Handle interface {
Locate() *Point
InvokeStart(x, y int, view DrawingView)
InvokeStep(x, y, anchorX, anchorY int, view DrawingView)
InvokeEnd(x, y int, view DrawingView)
GetOwner() Figure
}
type DefaultHandle struct {
owner Figure
}
func NewDefaultHandle(owner Figure) *DefaultHandle {
return &DefaultHandle{owner: owner}
}
func (this *DefaultHandle) GetOwner() Figure {
return this.owner
}
func (this *DefaultHandle) InvokeStart(x, y int, view DrawingView) {
//do nothing, subclasses can implement
}
func (this *DefaultHandle) InvokeStep(x, y, anchorX, anchorY int, view DrawingView) {
//do nothing, subclasses can implement
}
func (this *DefaultHandle) InvokeEnd(x, y int, view DrawingView) {
//do nothing, subclasses can implement
}
type LocatorHandle struct {
*DefaultHandle
locator Locator
}
func NewLocatorHandle(owner Figure, l Locator) *LocatorHandle {
handle := &LocatorHandle{}
handle.DefaultHandle = NewDefaultHandle(owner)
handle.locator = l
return handle
}
func (this *LocatorHandle) GetLocator() Locator {
return this.locator
}
//Locates the handle on the figure.
//The handle is drawn centered around the returned point.
func (this *LocatorHandle) Locate() *Point {
return this.locator.Locate(this.GetOwner())
}
//type NullHandle struct {
// *LocatorHandle
//}
//func NewNullHandle(owner Figure, locator Locator) *NullHandle {
// handle := &NullHandle{}
// handle.LocatorHandle = NewLocatorHandle(owner, locator)
// return handle
//}
//func (this *NullHandle) Draw(g Graphics) {
// r := GetHandleDisplayBox(this)
// g.SetFGColor(Red)
// g.DrawBorderedRect(r.X, r.Y, r.Width, r.Height)
//}