-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolselect.go
286 lines (267 loc) · 7.35 KB
/
toolselect.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
. "gopaint/reza"
"strconv"
"github.com/shahfarhadreza/go-gdiplus"
win "github.com/lxn/win"
)
const (
SelectActionNone = iota
SelectActionSelecting
SelectActionMoving
SelectActionResizing
)
type ToolSelect struct {
ToolBasic
penBorder *Pen
selection *SelectionRect
startPoint Point
currentAction int
selected bool
bitmap *BitmapGraphics
}
func (tool *ToolSelect) initialize() {
tool.selection = NewSelectionRect()
tool.penBorder = NewUserStylePen(1, NewRgb(0, 0, 0), []uint32{3, 4})
tool.currentAction = SelectActionNone
}
func (tool *ToolSelect) Dispose() {
if tool.penBorder != nil {
tool.penBorder.Dispose()
}
if tool.selection != nil {
tool.selection.Dispose()
}
if tool.bitmap != nil {
tool.bitmap.Dispose()
}
}
func (tool *ToolSelect) prepare() {
tool.currentAction = SelectActionNone
tool.selected = false
tool.selection.Clear()
if tool.bitmap != nil {
tool.bitmap.Dispose()
tool.bitmap = nil
}
//tool.SelectAll()
}
func (tool *ToolSelect) leave() {
tool.finalizeSelection()
tool.selected = false
tool.currentAction = SelectActionNone
tool.selection.Clear()
tool.updateStatus()
if tool.bitmap != nil {
tool.bitmap.Dispose()
tool.bitmap = nil
}
}
func (tool *ToolSelect) getCursor(ptMouse *Point) win.HCURSOR {
if tool.selected {
rect := tool.selection.GetRect()
if onpoint, point := tool.selection.GetClosestRectPoint(ptMouse, 6); onpoint {
switch point {
case RectPointTop, RectPointBottom:
return mainWindow.hCursorSizeNS
case RectPointLeft, RectPointRight:
return mainWindow.hCursorSizeWE
case RectPointTopLeft, RectPointBottomRight:
return mainWindow.hCursorSizeNWSE
case RectPointTopRight, RectPointBottomLeft:
return mainWindow.hCursorSizeNESW
}
} else {
if rect.IsPointInside(ptMouse) {
return mainWindow.hCursorMove
}
}
}
return mainWindow.hCursorArrow
}
func (tool *ToolSelect) draw(e *ToolDrawEvent) {
g := e.gdi32
if tool.selection != nil {
rect := tool.selection.GetRect()
if !tool.selection.IsEmpty() {
if tool.bitmap != nil {
canvas := mainWindow.workspace.canvas
visibleRect := canvas.GetVisibleRect()
newRect := rect
if newRect.Bottom > visibleRect.Bottom {
newRect.Bottom = visibleRect.Bottom
}
if newRect.Right > visibleRect.Right {
newRect.Right = visibleRect.Right
}
g.BitBlt(newRect.Left, newRect.Top,
newRect.Width(), newRect.Height(), tool.bitmap.Hdc,
0, 0, win.SRCCOPY)
}
if tool.currentAction == SelectActionSelecting || tool.currentAction == SelectActionMoving {
g.DrawRectangleEx(&rect, tool.penBorder, nil)
} else {
tool.selection.Draw(g)
}
}
}
}
func (tool *ToolSelect) SelectAll() {
image := mainWindow.workspace.canvas.image
tool.selected = true
tool.currentAction = SelectActionNone
tool.selection.SetRect(&Rect{
Left: 0,
Top: 0,
Right: image.Width(),
Bottom: image.Height(),
})
}
func (tool *ToolSelect) Deselect() {
tool.finalizeSelection()
tool.selected = false
tool.currentAction = SelectActionNone
tool.selection.Clear()
}
func (tool *ToolSelect) finalizeSelection() {
image := mainWindow.workspace.canvas.image
if tool.bitmap != nil {
rect := tool.selection.GetRect()
image.context3.BitBlt(rect.Left, rect.Top, rect.Width(), rect.Height(), tool.bitmap.Hdc, 0, 0, win.SRCCOPY)
tool.bitmap.Dispose()
tool.bitmap = nil
}
}
func (tool *ToolSelect) DeleteSelection() {
if tool.selection.IsEmpty() {
return
}
image := mainWindow.workspace.canvas.image
if tool.bitmap == nil {
// replace the area with background color
context := image.context
color := GetColorBackground()
brush := gdiplus.NewSolidBrush(&color)
rect := tool.selection.GetRect()
w, h := rect.Width(), rect.Height()
context.FillRectangleI(brush.AsBrush(), int32(rect.Left), int32(rect.Top), int32(w), int32(h))
brush.Dispose()
} else {
tool.bitmap.Dispose()
tool.bitmap = nil
}
tool.selected = false
tool.currentAction = SelectActionNone
tool.selection.Clear()
}
func (tool *ToolSelect) mouseDownEvent(e *ToolMouseEvent) {
mbutton := e.mbutton
if mbutton == MouseButtonLeft || mbutton == MouseButtonRight {
tool.startPoint = e.pt
rect := tool.selection.GetRect()
if tool.selected {
if onpoint, point := tool.selection.GetClosestRectPoint(&e.pt, 6); onpoint {
switch point {
case RectPointTop, RectPointBottom:
tool.currentAction = SelectActionResizing
case RectPointLeft, RectPointRight:
tool.currentAction = SelectActionResizing
case RectPointTopLeft, RectPointBottomRight:
tool.currentAction = SelectActionResizing
case RectPointTopRight, RectPointBottomLeft:
tool.currentAction = SelectActionResizing
}
} else if rect.IsPointInside(&e.pt) {
tool.currentAction = SelectActionMoving
if !tool.selection.IsEmpty() {
w, h := rect.Width(), rect.Height()
if tool.bitmap == nil {
//log.Printf("w %d, h %d, x %d y %d\n", tool.rect.Width(), tool.rect.Height(), tool.rect.Left, tool.rect.Top)
tool.bitmap = NewBitmapGraphics(w, h)
bitmapContext := tool.bitmap.Graphics
bitmapContext.BitBlt(0, 0, w, h, e.image.context3.GetHDC(), rect.Left, rect.Top, win.SRCCOPY)
// replace the area with background color
context := e.image.context3
gcolor := GetColorBackground()
color := FromGdiplusColor(&gcolor)
brush := NewSolidBrush(&color)
pen := NewSolidPen(1, &color)
context.FillRectangleEx(&rect, pen, brush)
pen.Dispose()
brush.Dispose()
}
}
} else {
tool.finalizeSelection()
tool.selected = false
tool.currentAction = SelectActionSelecting
tool.selection.Clear()
}
} else {
tool.selected = false
tool.currentAction = SelectActionSelecting
tool.selection.Clear()
}
tool.updateStatus()
}
}
func (tool *ToolSelect) mouseMoveEvent(e *ToolMouseEvent) {
mbutton := e.mbutton
if mbutton == MouseButtonLeft || mbutton == MouseButtonRight {
if tool.currentAction == SelectActionSelecting {
startPoint, endPoint := GetStartAndEnd(tool.startPoint, e.pt)
rect := Rect{
Left: int(startPoint.X),
Top: int(startPoint.Y),
Right: int(endPoint.X),
Bottom: int(endPoint.Y),
}
tool.selection.SetRect(&rect)
tool.updateStatus()
} else if tool.currentAction == SelectActionMoving {
rect := tool.selection.GetRect()
rectOrigin := Point{
X: rect.Left,
Y: rect.Top,
}
ptDist := e.pt.Distance(&e.lastPt)
newPos := Point{
X: rectOrigin.X + ptDist.X,
Y: rectOrigin.Y + ptDist.Y,
}
rect = Rect{
Left: newPos.X,
Top: newPos.Y,
Right: newPos.X + rect.Width(),
Bottom: newPos.Y + rect.Height(),
}
tool.selection.SetRect(&rect)
}
}
}
func (tool *ToolSelect) mouseUpEvent(e *ToolMouseEvent) {
mbutton := e.mbutton
//image := e.image
if mbutton == MouseButtonLeft || mbutton == MouseButtonRight {
if tool.currentAction == SelectActionSelecting {
// if we have at least 1x1 selected
if !tool.selection.IsEmpty() {
tool.selected = true
} else {
tool.selected = false
}
}
tool.currentAction = SelectActionNone
}
}
func (tool *ToolSelect) updateStatus() {
status := mainWindow.statusSelSize
if status != nil {
if !tool.selection.IsEmpty() {
rect := tool.selection.GetRect()
status.Update(strconv.Itoa(rect.Width()) + ", " + strconv.Itoa(rect.Height()) + "px")
} else {
status.Update("")
}
}
}