forked from shibukawa/nanogui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagepanel.go
151 lines (127 loc) · 3.77 KB
/
imagepanel.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
package nanogui
import (
"github.com/shibukawa/glfw"
"github.com/shibukawa/nanovgo"
)
type Image struct {
ImageID int
Name string
}
type ImagePanel struct {
WidgetImplement
images []Image
callback func(int)
thumbSize int
spacing int
margin int
mouseIndex int
}
func NewImagePanel(parent Widget) *ImagePanel {
panel := &ImagePanel{
thumbSize: 64,
spacing: 10,
margin: 10,
mouseIndex: -1,
}
InitWidget(panel, parent)
return panel
}
func (i *ImagePanel) Images() []Image {
return i.images
}
func (i *ImagePanel) SetImages(images []Image) {
i.images = images
}
func (i *ImagePanel) SetCallback(callback func(int)) {
i.callback = callback
}
func (i *ImagePanel) MouseButtonEvent(self Widget, x, y int, button glfw.MouseButton, down bool, modifier glfw.ModifierKey) bool {
index := i.indexForPosition(x, y)
if index >= 0 && i.callback != nil && down {
i.callback(index)
}
return true
}
func (i *ImagePanel) MouseMotionEvent(self Widget, x, y, relX, relY, button int, modifier glfw.ModifierKey) bool {
i.mouseIndex = i.indexForPosition(x, y)
return true
}
func (i *ImagePanel) PreferredSize(self Widget, ctx *nanovgo.Context) (int, int) {
cols, rows := i.gridSize()
w := cols*i.thumbSize + (cols-1)*i.spacing + 2*i.margin
h := rows*i.thumbSize + (rows-1)*i.spacing + 2*i.margin
return w, h
}
func (i *ImagePanel) Draw(self Widget, ctx *nanovgo.Context) {
cols, _ := i.gridSize()
x := float32(i.x)
y := float32(i.y)
thumbSize := float32(i.thumbSize)
for j, image := range i.images {
pX := x + float32(i.margin+(j%cols)*(i.thumbSize+i.spacing))
pY := y + float32(i.margin+(j/cols)*(i.thumbSize+i.spacing))
imgW, imgH, _ := ctx.ImageSize(image.ImageID)
var iw, ih, ix, iy float32
if imgW < imgH {
iw = thumbSize
ih = iw * float32(imgH) / float32(imgW)
ix = 0
iy = -(ih - thumbSize) * 0.5
} else {
ih = thumbSize
iw = ih * float32(imgH) / float32(imgW)
iy = 0
ix = -(iw - thumbSize) * 0.5
}
imgPaint := nanovgo.ImagePattern(pX+ix, pY+iy, iw, ih, 0, image.ImageID, toF(i.mouseIndex == j, 1.0, 0.7))
ctx.BeginPath()
ctx.RoundedRect(pX, pY, thumbSize, thumbSize, 5)
ctx.SetFillPaint(imgPaint)
ctx.Fill()
shadowPaint := nanovgo.BoxGradient(pX-1, pY, thumbSize+2, thumbSize+2, 5, 3, nanovgo.MONO(0, 128), nanovgo.MONO(0, 0))
ctx.BeginPath()
ctx.Rect(pX-5, pY-5, thumbSize+10, thumbSize+10)
ctx.RoundedRect(pX, pY, thumbSize, thumbSize, 6)
ctx.PathWinding(nanovgo.Hole)
ctx.SetFillPaint(shadowPaint)
ctx.Fill()
ctx.BeginPath()
ctx.RoundedRect(pX+0.5, pY+0.5, thumbSize-1, thumbSize-1, 4-0.5)
ctx.SetStrokeWidth(1.0)
ctx.SetStrokeColor(nanovgo.MONO(255, 80))
ctx.Stroke()
}
}
func (i *ImagePanel) String() string {
return i.StringHelper("ImagePanel", "")
}
func (i *ImagePanel) gridSize() (int, int) {
nCols := 1 + maxI(0, int(float32(i.w-2*i.margin-i.thumbSize)/float32(i.thumbSize+i.spacing)))
nRows := (len(i.images) + nCols - 1) / nCols
return nCols, nRows
}
func (i *ImagePanel) indexForPosition(x, y int) int {
//[[win fix get wrong index
//x,y is relative to the upper left corner of the parent window
//but here is relative to the upper left corner of self
ix, iy := i.Position()
x, y = x-ix, y-iy
//]]
pX := float32(x-i.margin) / float32(i.thumbSize+i.margin)
pY := float32(y-i.margin) / float32(i.thumbSize+i.margin)
iconRegion := float32(i.thumbSize) / float32(i.thumbSize+i.spacing)
overImage := pX-floorF(pX) < iconRegion && pY-floorF(pY) < iconRegion
gridPosX := int(pX)
gridPosY := int(pY)
gridCols, gridRows := i.gridSize()
overImage = overImage && gridPosX >= 0 && gridPosY >= 0 && gridPosX < gridCols && gridPosY < gridRows
if overImage {
//[[win fix out of array
idx := gridPosX + gridPosY*gridCols
if idx < len(i.images) {
return idx
}
//]]
}
return -1
}