-
Notifications
You must be signed in to change notification settings - Fork 2
/
selection.go
76 lines (64 loc) · 1.61 KB
/
selection.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
package main
import (
. "gopaint/reza"
)
type SelectionRect struct {
rect Rect
penWhite *Pen
penBorder *Pen
penResizer *Pen
brushResizer *Brush
}
func NewSelectionRect() *SelectionRect {
sr := &SelectionRect{}
sr.penWhite = NewSolidPen(1, NewRgb(255, 255, 255))
sr.penBorder = NewUserStylePen(1, NewRgb(0, 120, 215), []uint32{3, 4})
sr.penResizer = NewSolidPen(1, NewRgb(85, 85, 85))
sr.brushResizer = NewSolidBrush(NewRgb(255, 255, 255))
return sr
}
func (sr *SelectionRect) Dispose() {
sr.penBorder.Dispose()
sr.brushResizer.Dispose()
sr.penResizer.Dispose()
sr.penWhite.Dispose()
}
func (sr *SelectionRect) Clear() {
sr.rect = Rect{}
}
func (sr *SelectionRect) SetRect(rect *Rect) {
sr.rect = *rect
}
func (sr *SelectionRect) GetRect() Rect {
return sr.rect
}
func (sr *SelectionRect) IsEmpty() bool {
return !(sr.rect.Width() >= 1 && sr.rect.Height() >= 1)
}
func (sr *SelectionRect) GetClosestRectPoint(mouse *Point, dist int) (onPoint bool, rectPoint int) {
points := sr.rect.GetEightPoints()
for i := range points {
point := &points[i]
pdist := point.DistanceI(mouse)
if pdist < dist {
return true, i
}
}
return false, 0
}
func (sr *SelectionRect) Draw(g *Graphics) {
points := sr.rect.GetEightPoints()
const halfSize = 3
g.DrawRectangleEx(&sr.rect, sr.penWhite, nil)
g.DrawRectangleEx(&sr.rect, sr.penBorder, nil)
for i := range points {
point := &points[i]
rcHandle := Rect{
Left: point.X - halfSize,
Right: point.X + halfSize,
Top: point.Y - halfSize,
Bottom: point.Y + halfSize,
}
g.DrawRectangleEx(&rcHandle, sr.penResizer, sr.brushResizer)
}
}