-
Notifications
You must be signed in to change notification settings - Fork 30
/
imageviewbox.go
340 lines (283 loc) · 7.24 KB
/
imageviewbox.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
*/
package winc
import (
"fmt"
"time"
"github.com/tadvi/winc/w32"
)
type direction int
const (
DirNone direction = iota
DirX
DirY
DirX2
DirY2
)
var ImageBoxPen = NewPen(w32.PS_GEOMETRIC, 2, NewSolidColorBrush(RGB(140, 140, 220)))
var ImageBoxHiPen = NewPen(w32.PS_GEOMETRIC, 2, NewSolidColorBrush(RGB(220, 140, 140)))
var ImageBoxMarkBrush = NewSolidColorBrush(RGB(40, 40, 40))
var ImageBoxMarkPen = NewPen(w32.PS_GEOMETRIC, 2, ImageBoxMarkBrush)
type ImageBox struct {
Name string
Type int
X, Y, X2, Y2 int
underMouse bool // dynamic value
}
func (b *ImageBox) Rect() *Rect {
return NewRect(b.X, b.Y, b.X2, b.Y2)
}
// ImageViewBox is image view with boxes.
type ImageViewBox struct {
ControlBase
bmp *Bitmap
mouseLeft bool
modified bool // used by GUI to see if any image box modified
add bool
Boxes []*ImageBox // might be persisted to file
dragBox *ImageBox
selBox *ImageBox
dragStartX, dragStartY int
resize direction
onSelectedChange EventManager
onAdd EventManager
onModify EventManager
}
func NewImageViewBox(parent Controller) *ImageViewBox {
iv := new(ImageViewBox)
iv.InitWindow("winc_ImageViewBox", parent, w32.WS_EX_CONTROLPARENT, w32.WS_CHILD|w32.WS_VISIBLE)
RegMsgHandler(iv)
iv.SetFont(DefaultFont)
iv.SetText("")
iv.SetSize(200, 65)
return iv
}
func (iv *ImageViewBox) OnSelectedChange() *EventManager {
return &iv.onSelectedChange
}
func (iv *ImageViewBox) OnAdd() *EventManager {
return &iv.onAdd
}
func (iv *ImageViewBox) OnModify() *EventManager {
return &iv.onModify
}
func (iv *ImageViewBox) IsModified() bool { return iv.modified }
func (iv *ImageViewBox) SetModified(modified bool) { iv.modified = modified }
func (iv *ImageViewBox) IsLoaded() bool { return iv.bmp != nil }
func (iv *ImageViewBox) AddMode() bool { return iv.add }
func (iv *ImageViewBox) SetAddMode(add bool) { iv.add = add }
func (iv *ImageViewBox) HasSelected() bool { return iv.selBox != nil && iv.bmp != nil }
func (iv *ImageViewBox) wasModified() {
iv.modified = true
iv.onModify.Fire(NewEvent(iv, nil))
}
func (iv *ImageViewBox) DeleteSelected() {
if iv.selBox != nil {
for i, b := range iv.Boxes {
if b == iv.selBox {
iv.Boxes = append(iv.Boxes[:i], iv.Boxes[i+1:]...)
iv.selBox = nil
iv.Invalidate(true)
iv.wasModified()
iv.onSelectedChange.Fire(NewEvent(iv, nil))
return
}
}
}
}
func (iv *ImageViewBox) NameSelected() string {
if iv.selBox != nil {
return iv.selBox.Name
}
return ""
}
func (iv *ImageViewBox) SetNameSelected(name string) {
if iv.selBox != nil {
iv.selBox.Name = name
iv.wasModified()
}
}
func (iv *ImageViewBox) TypeSelected() int {
if iv.selBox != nil {
return iv.selBox.Type
}
return 0
}
func (iv *ImageViewBox) SetTypeSelected(typ int) {
if iv.selBox != nil {
iv.selBox.Type = typ
iv.wasModified()
}
}
func (ib *ImageViewBox) updateHighlight(x, y int) bool {
var changed bool
for _, b := range ib.Boxes {
under := x >= b.X && y >= b.Y && x <= b.X2 && y <= b.Y2
if b.underMouse != under {
changed = true
}
b.underMouse = under
/*if sel {
break // allow only one to be underMouse
}*/
}
return changed
}
func (ib *ImageViewBox) isUnderMouse(x, y int) *ImageBox {
for _, b := range ib.Boxes {
if x >= b.X && y >= b.Y && x <= b.X2 && y <= b.Y2 {
return b
}
}
return nil
}
func (ib *ImageViewBox) getCursor(x, y int) uint16 {
for _, b := range ib.Boxes {
switch d := ib.resizingDirection(b, x, y); d {
case DirY, DirY2:
return w32.IDC_SIZENS
case DirX, DirX2:
return w32.IDC_SIZEWE
}
// w32.IDC_SIZEALL or w32.IDC_SIZE for resize
}
return w32.IDC_ARROW
}
func (ib *ImageViewBox) resizingDirection(b *ImageBox, x, y int) direction {
if b == nil {
return DirNone
}
switch {
case b.X == x || b.X == x-1 || b.X == x+1:
return DirX
case b.X2 == x || b.X2 == x-1 || b.X2 == x+1:
return DirX2
case b.Y == y || b.Y == y-1 || b.Y == y+1:
return DirY
case b.Y2 == y || b.Y2 == y-1 || b.Y2 == y+1:
return DirY2
}
return DirNone
}
func (ib *ImageViewBox) resizeToDirection(b *ImageBox, x, y int) {
switch ib.resize {
case DirX:
b.X = x
case DirY:
b.Y = y
case DirX2:
b.X2 = x
case DirY2:
b.Y2 = y
}
}
func (ib *ImageViewBox) drag(b *ImageBox, x, y int) {
w, h := b.X2-b.X, b.Y2-b.Y
nx := ib.dragStartX - b.X
ny := ib.dragStartY - b.Y
b.X = x - nx
b.Y = y - ny
b.X2 = b.X + w
b.Y2 = b.Y + h
ib.dragStartX, ib.dragStartY = x, y
}
func (iv *ImageViewBox) DrawImageFile(filepath string) (err error) {
iv.bmp, err = NewBitmapFromFile(filepath, RGB(255, 255, 0))
iv.selBox = nil
iv.modified = false
iv.onSelectedChange.Fire(NewEvent(iv, nil))
iv.onModify.Fire(NewEvent(iv, nil))
return
}
func (iv *ImageViewBox) DrawImage(bmp *Bitmap) {
iv.bmp = bmp
iv.selBox = nil
iv.modified = false
iv.onSelectedChange.Fire(NewEvent(iv, nil))
iv.onModify.Fire(NewEvent(iv, nil))
}
func (iv *ImageViewBox) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
case w32.WM_SIZE, w32.WM_SIZING:
iv.Invalidate(true)
case w32.WM_ERASEBKGND:
return 1 // important
case w32.WM_CREATE:
internalTrackMouseEvent(iv.hwnd)
case w32.WM_PAINT:
if iv.bmp != nil {
canvas := NewCanvasFromHwnd(iv.hwnd)
defer canvas.Dispose()
iv.SetSize(iv.bmp.Size())
canvas.DrawBitmap(iv.bmp, 0, 0)
for _, b := range iv.Boxes {
// old code used NewSystemColorBrush(w32.COLOR_BTNFACE) w32.COLOR_WINDOW
pen := ImageBoxPen
if b.underMouse {
pen = ImageBoxHiPen
}
canvas.DrawRect(b.Rect(), pen)
if b == iv.selBox {
x1 := []int{b.X, b.X2, b.X2, b.X}
y1 := []int{b.Y, b.Y, b.Y2, b.Y2}
for i := 0; i < len(x1); i++ {
r := NewRect(x1[i]-2, y1[i]-2, x1[i]+2, y1[i]+2)
canvas.DrawFillRect(r, ImageBoxMarkPen, ImageBoxMarkBrush)
}
}
}
}
case w32.WM_MOUSEMOVE:
x, y := genPoint(lparam)
if iv.dragBox != nil {
if iv.resize == DirNone {
iv.drag(iv.dragBox, x, y)
iv.wasModified()
} else {
iv.resizeToDirection(iv.dragBox, x, y)
iv.wasModified()
}
iv.Invalidate(true)
} else {
if !iv.add {
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(iv.getCursor(x, y))))
}
// do not call repaint if underMouse item did not change.
if iv.updateHighlight(x, y) {
iv.Invalidate(true)
}
}
if iv.mouseLeft {
internalTrackMouseEvent(iv.hwnd)
iv.mouseLeft = false
}
case w32.WM_MOUSELEAVE:
iv.dragBox = nil
iv.mouseLeft = true
iv.updateHighlight(-1, -1)
iv.Invalidate(true)
case w32.WM_LBUTTONUP:
iv.dragBox = nil
case w32.WM_LBUTTONDOWN:
x, y := genPoint(lparam)
if iv.add {
now := time.Now()
s := fmt.Sprintf("field%s", now.Format("020405"))
b := &ImageBox{Name: s, underMouse: true, X: x, Y: y, X2: x + 150, Y2: y + 30}
iv.Boxes = append(iv.Boxes, b)
iv.selBox = b
iv.wasModified()
iv.onAdd.Fire(NewEvent(iv, nil))
} else {
iv.dragBox = iv.isUnderMouse(x, y)
iv.selBox = iv.dragBox
iv.dragStartX, iv.dragStartY = x, y
iv.resize = iv.resizingDirection(iv.dragBox, x, y)
}
iv.Invalidate(true)
iv.onSelectedChange.Fire(NewEvent(iv, nil))
case w32.WM_RBUTTONDOWN:
}
return w32.DefWindowProc(iv.hwnd, msg, wparam, lparam)
}