-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.go
139 lines (115 loc) · 3.21 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
package main
import (
. "gopaint/reza"
"github.com/shahfarhadreza/go-gdiplus"
"github.com/fogleman/gg"
"github.com/lxn/win"
)
type ToolMouseEvent struct {
pt Point
lastPt Point
mbutton int
context *gdiplus.Graphics
image *DrawingImage
canvas *DrawingCanvas
}
type ToolKeyEvent struct {
keycode int
context *gdiplus.Graphics
image *DrawingImage
canvas *DrawingCanvas
}
type ToolDrawEvent struct {
gdi32 *Graphics
graphics *gdiplus.Graphics
mouse Point
}
type Tool interface {
initialize()
Dispose()
prepare() // gets called everytime user chooses this tool
leave() // gets called everytime user switches from this to another tool
changeSize(size int) // gets called when user changes size in the size dropdown button from the ribbon
draw(e *ToolDrawEvent)
getCursor(ptMouse *Point) win.HCURSOR
mouseMoveEvent(e *ToolMouseEvent)
mouseDownEvent(e *ToolMouseEvent)
mouseUpEvent(e *ToolMouseEvent)
keyPressEvent(e *ToolKeyEvent)
}
type ToolBasic struct {
}
func (tool *ToolBasic) keyPressEvent(e *ToolKeyEvent) {
}
func (tool *ToolBasic) changeSize(size int) {
}
func (tool *ToolBasic) leave() {
}
func (tool *ToolBasic) getCursor(ptMouse *Point) win.HCURSOR {
return mainWindow.hCursorArrow
}
func AsGdiplusColor(color *Color) *gdiplus.Color {
return gdiplus.NewColor(color.R, color.G, color.B, color.A)
}
func FromGdiplusColor(color *gdiplus.Color) Color {
return Rgba(color.GetR(), color.GetG(), color.GetB(), color.GetA())
}
func GetColorForeground() (color gdiplus.Color) {
c := mainWindow.color1.GetColor()
color.Argb = gdiplus.MakeARGB(c.A, c.R, c.G, c.B)
return
}
func GetColorBackground() (color gdiplus.Color) {
c := mainWindow.color2.GetColor()
color.Argb = gdiplus.MakeARGB(c.A, c.R, c.G, c.B)
return
}
func GetColorForeBack(mouseButton int) (color gdiplus.Color) {
if mouseButton == MouseButtonRight {
return GetColorBackground()
}
return GetColorForeground()
}
func GetOutlineAndFillColors(mouseButton int) (outline, fill gdiplus.Color) {
if mouseButton == MouseButtonRight {
return GetColorBackground(), GetColorForeground()
}
return GetColorForeground(), GetColorBackground()
}
// brush and pen must be disposed by the caller
func GetPenAndBrush(mbutton int, penwidth float32) (*gdiplus.Pen, *gdiplus.Brush) {
outline, fill := GetOutlineAndFillColors(mbutton)
var pen *gdiplus.Pen = nil
var brush *gdiplus.Brush = nil
if mainWindow.menuSolidOutline.IsToggled() {
pen = gdiplus.NewPen(&outline, penwidth)
}
if mainWindow.menuSolidFill.IsToggled() {
brush = &gdiplus.NewSolidBrush(&fill).Brush
}
return pen, brush
}
func ggDrawPolygon(gc *gg.Context, points []Point) {
gc.NewSubPath()
for i, pt := range points {
if i == 0 {
gc.MoveTo(float64(pt.X), float64(pt.Y))
} else {
gc.LineTo(float64(pt.X), float64(pt.Y))
}
}
gc.ClosePath()
}
func GetStartAndEnd(start, end Point) (s gdiplus.Point, e gdiplus.Point) {
startPoint := start
endPoint := end
if startPoint.X > endPoint.X {
startPoint.X, endPoint.X = endPoint.X, startPoint.X
}
if startPoint.Y > endPoint.Y {
startPoint.Y, endPoint.Y = endPoint.Y, startPoint.Y
}
s.X, s.Y = int32(startPoint.X), int32(startPoint.Y)
e.X, e.Y = int32(endPoint.X), int32(endPoint.Y)
return
}