forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_button.go
271 lines (229 loc) · 6.36 KB
/
image_button.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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gui
import (
"github.com/leapar/engine/texture"
"github.com/leapar/engine/window"
)
// ImageButton represents an image button GUI element
type ImageButton struct {
*Panel // Embedded Panel
label *Label // Label panel
iconLabel bool // True if icon
image *Image // pointer to button image (may be nil)
styles *ImageButtonStyles // pointer to current button styles
mouseOver bool // true if mouse is over button
pressed bool // true if button is pressed
stateImages [ButtonDisabled + 1]*texture.Texture2D // array of images for each button state
}
// ButtonState specifies a button state.
type ButtonState int
// The possible button states.
const (
ButtonNormal ButtonState = iota
ButtonOver
ButtonPressed
ButtonDisabled
// ButtonFocus
)
// ImageButtonStyle contains the styling of an ImageButton.
type ImageButtonStyle BasicStyle
// ImageButtonStyles contains one ImageButtonStyle for each possible ImageButton state.
type ImageButtonStyles struct {
Normal ImageButtonStyle
Over ImageButtonStyle
Focus ImageButtonStyle
Pressed ImageButtonStyle
Disabled ImageButtonStyle
}
// NewImageButton creates and returns a pointer to a new ImageButton widget
// with the specified image.
func NewImageButton(normalImgPath string) (*ImageButton, error) {
b := new(ImageButton)
b.styles = &StyleDefault().ImageButton
tex, err := texture.NewTexture2DFromImage(normalImgPath)
if err != nil {
return nil, err
}
b.stateImages[ButtonNormal] = tex
b.image = NewImageFromTex(tex)
// Initializes the button panel
b.Panel = NewPanel(0, 0)
b.Panel.SetContentSize(b.image.Width(), b.image.Height())
b.Panel.SetBorders(5, 5, 5, 5)
b.Panel.Add(b.image)
// Subscribe to panel events
b.Panel.Subscribe(OnKeyDown, b.onKey)
b.Panel.Subscribe(OnKeyUp, b.onKey)
b.Panel.Subscribe(OnMouseUp, b.onMouse)
b.Panel.Subscribe(OnMouseDown, b.onMouse)
b.Panel.Subscribe(OnCursor, b.onCursor)
b.Panel.Subscribe(OnCursorEnter, b.onCursor)
b.Panel.Subscribe(OnCursorLeave, b.onCursor)
b.Panel.Subscribe(OnEnable, func(name string, ev interface{}) { b.update() })
b.Panel.Subscribe(OnResize, func(name string, ev interface{}) { b.recalc() })
b.recalc()
b.update()
return b, nil
}
// SetText sets the text of the label
func (b *ImageButton) SetText(text string) {
if b.iconLabel && b.label != nil {
b.Panel.Remove(b.label)
b.label.Dispose()
b.label = nil
}
b.iconLabel = false
if b.label == nil {
// Create label
b.label = NewLabel(text)
b.Panel.Add(b.label)
} else {
b.label.SetText(text)
}
b.recalc()
}
// SetIcon sets the icon
func (b *ImageButton) SetIcon(icode string) {
if b.iconLabel == false && b.label != nil {
b.Panel.Remove(b.label)
b.label.Dispose()
b.label = nil
}
b.iconLabel = true
if b.label == nil {
// Create icon
b.label = NewIcon(icode)
b.Panel.Add(b.label)
} else {
b.label.SetText(icode)
}
b.recalc()
}
// SetFontSize sets the font size of the label/icon
func (b *ImageButton) SetFontSize(size float64) {
if b.label != nil {
b.label.SetFontSize(size)
b.recalc()
}
}
// SetImage sets the button left image from the specified filename
// If there is currently a selected icon, it is removed
func (b *ImageButton) SetImage(state ButtonState, imgfile string) error {
tex, err := texture.NewTexture2DFromImage(imgfile)
if err != nil {
return err
}
if b.stateImages[state] != nil {
b.stateImages[state].Dispose()
}
b.stateImages[state] = tex
b.update()
return nil
}
// Dispose releases resources used by this widget
func (b *ImageButton) Dispose() {
b.Panel.Dispose()
for _, tex := range b.stateImages {
if tex != nil {
tex.Dispose()
}
}
}
// SetStyles set the button styles overriding the default style
func (b *ImageButton) SetStyles(bs *ImageButtonStyles) {
b.styles = bs
b.update()
}
// onCursor process subscribed cursor events
func (b *ImageButton) onCursor(evname string, ev interface{}) {
switch evname {
case OnCursorEnter:
b.mouseOver = true
b.update()
case OnCursorLeave:
b.pressed = false
b.mouseOver = false
b.update()
}
b.root.StopPropagation(StopAll)
}
// onMouseEvent process subscribed mouse events
func (b *ImageButton) onMouse(evname string, ev interface{}) {
switch evname {
case OnMouseDown:
b.root.SetKeyFocus(b)
b.pressed = true
b.update()
b.Dispatch(OnClick, nil)
case OnMouseUp:
b.pressed = false
b.update()
default:
return
}
b.root.StopPropagation(StopAll)
}
// onKey processes subscribed key events
func (b *ImageButton) onKey(evname string, ev interface{}) {
kev := ev.(*window.KeyEvent)
if evname == OnKeyDown && kev.Keycode == window.KeyEnter {
b.pressed = true
b.update()
b.Dispatch(OnClick, nil)
b.root.StopPropagation(Stop3D)
return
}
if evname == OnKeyUp && kev.Keycode == window.KeyEnter {
b.pressed = false
b.update()
b.root.StopPropagation(Stop3D)
return
}
return
}
// update updates the button visual state
func (b *ImageButton) update() {
if !b.Enabled() {
if b.stateImages[ButtonDisabled] != nil {
b.image.SetTexture(b.stateImages[ButtonDisabled])
}
b.applyStyle(&b.styles.Disabled)
return
}
if b.pressed {
if b.stateImages[ButtonPressed] != nil {
b.image.SetTexture(b.stateImages[ButtonPressed])
}
b.applyStyle(&b.styles.Pressed)
return
}
if b.mouseOver {
if b.stateImages[ButtonOver] != nil {
b.image.SetTexture(b.stateImages[ButtonOver])
}
b.applyStyle(&b.styles.Over)
return
}
b.image.SetTexture(b.stateImages[ButtonNormal])
b.applyStyle(&b.styles.Normal)
}
// applyStyle applies the specified button style
func (b *ImageButton) applyStyle(bs *ImageButtonStyle) {
b.Panel.ApplyStyle(&bs.PanelStyle)
if b.label != nil {
b.label.SetColor4(&bs.FgColor)
}
}
// recalc recalculates all dimensions and position from inside out
func (b *ImageButton) recalc() {
// Only need to recal if there's a label preset
if b.label != nil {
width := b.Panel.ContentWidth()
height := b.Panel.ContentHeight()
x := (width - b.label.Width()) / 2
y := (height - b.label.Height()) / 2
b.label.SetPosition(x, y)
}
}