-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.go
162 lines (145 loc) · 3.91 KB
/
menu.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
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"github.com/skelterjohn/go.wde"
)
type MenuOps interface {
Bounds(item interface{}) image.Rectangle
Draw(item interface{}, dst draw.Image, r image.Rectangle)
}
type MenuWidget struct {
ImageWidget
// general settings
ops MenuOps
options []interface{}
lastSelected int
maxWidth int
height int // per option
// details of current instance
origin int
reply chan interface{}
hover image.Point
}
type StringMenuOps struct {
toStr func(interface{}) string
}
func (ops StringMenuOps) str(item interface{}) string {
if ops.toStr == nil {
str, ok := item.(string)
if ok {
return str
}
return item.(fmt.Stringer).String()
}
return ops.toStr(item)
}
func (ops StringMenuOps) Bounds(item interface{}) image.Rectangle {
font := G.font.luxi
return image.Rect(0, 0, font.PixelWidth(ops.str(item)), font.PixelHeight())
}
func (ops StringMenuOps) Draw(item interface{}, dst draw.Image, r image.Rectangle) {
centre := image.Pt((r.Min.X + r.Max.X) / 2, (r.Min.Y + r.Max.Y) / 2)
font := G.font.luxi
font.DrawC(dst, color.RGBA{0, 0, 0, 255}, r, ops.str(item), centre)
}
func mkMenu(ops MenuOps, options... interface{}) MenuWidget {
menu := MenuWidget{ops: ops, options: options}
for _, item := range options {
r := ops.Bounds(item)
if r.Dx() > menu.maxWidth {
menu.maxWidth = r.Dx()
}
if r.Dy() > menu.height {
menu.height = r.Dy()
}
}
return menu
}
func (menu *MenuWidget) SetDefault(item interface{}) bool {
for i, opt := range menu.options {
if item == opt {
menu.lastSelected = i
return true
}
}
return false
}
func (menu *MenuWidget) Popup(bounds image.Rectangle, refresh chan Widget, mouse image.Point) chan interface{} {
menu.refresh = refresh
w := menu.maxWidth
ih := menu.height
h := ih * len(menu.options)
topLeft := mouse.Sub(image.Point{w / 2, ih / 2})
// if close to upper/lower boundary, align menu with edge
// (otherwise the aligment is such that the mouse is at the centre of a menu item)
if mouse.Y < bounds.Min.Y + ih/2 {
topLeft.Y = bounds.Min.Y
} else if mouse.Y > bounds.Max.Y - ih/2 {
topLeft.Y = bounds.Max.Y - ih
}
topLeft.Y -= ih * menu.lastSelected
r := image.Rectangle{topLeft, topLeft.Add(image.Pt(w, h))}
min := r.Min.Sub(bounds.Min)
max := bounds.Max.Sub(r.Max)
dx := 0
if min.X < 0 {
dx = -min.X
} else if max.X < 0 {
dx = max.X
}
menu.origin = 0
if max.Y < 0 {
menu.origin = -ceil(-max.Y, ih)
} else if min.Y < 0 {
menu.origin = ceil(-min.Y, ih)
}
dy := menu.origin * ih
r = r.Add(image.Pt(dx, dy))
menu.img = image.NewRGBA(r)
menu.hover = mouse
menu.reply = make(chan interface{})
menu.refresh <- menu
return menu.reply
}
func (menu *MenuWidget) Drag(mouse image.Point, finished bool, moved bool) bool {
contained := mouse.In(menu.Rect())
if !finished {
menu.hover = mouse
menu.refresh <- menu
return contained
}
defer func() {
menu.img = image.NewRGBA(image.Rect(0, 0, 0, 0))
menu.refresh <- menu
close(menu.reply)
}()
if !contained {
menu.reply <- nil
return false
}
i := mod(menu.origin + (mouse.Y - menu.Rect().Min.Y) / menu.height, len(menu.options))
menu.reply <- menu.options[i]
menu.lastSelected = i
return contained
}
func (menu *MenuWidget) Draw(screen wde.Image, r image.Rectangle) {
dst, _ := menu.Img(r)
border := color.RGBA{0x88, 0x88, 0x88, 255}
bg_norm := color.RGBA{0xee, 0xee, 0xcc, 255}
bg_sel := color.RGBA{0xdd, 0xdd, 0xdd, 255}
drawBorders(dst, menu.Rect().Inset(-1), border, bg_norm)
hover_i := mod(menu.origin + (menu.hover.Y - menu.Rect().Min.Y) / menu.height, len(menu.options))
ih := menu.height
for j := 0; j < len(menu.options); j++ {
item_r := image.Rect(r.Min.X, r.Min.Y + j*ih, r.Max.X, r.Min.Y + (j+1)*ih)
i := mod(menu.origin + j, len(menu.options))
if i == hover_i {
draw.Draw(dst, item_r, &image.Uniform{bg_sel}, image.ZP, draw.Over)
}
menu.ops.Draw(menu.options[i], dst, item_r)
}
screen.CopyRGBA(dst, r)
}