/
switch.go
328 lines (296 loc) · 8.06 KB
/
switch.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
/*
* switch.go
*
* Copyright 2018-2022 Dariusz Sikora <ds@isangeles.dev>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package mtk
import (
"fmt"
"image/color"
"golang.org/x/image/colornames"
"github.com/gopxl/pixel"
"github.com/gopxl/pixel/imdraw"
)
// Switch struct represents graphical switch for values.
type Switch struct {
bgDraw *imdraw.IMDraw
bgSpr *pixel.Sprite
prevButton *Button
nextButton *Button
valueText *Text
valueSprite *pixel.Sprite
label *Text
info *InfoWindow
drawArea pixel.Rect // updated on each draw
size pixel.Vec
color color.Color
index int
focused bool
disabled bool
hovered bool
values []SwitchValue
onChange func(s *Switch, old, new *SwitchValue)
}
// Tuple for switch values, contains value to
// display(view) and real value.
type SwitchValue struct {
View interface{}
Value interface{}
}
// NewSwitch creates new switch with specified size and color.
func NewSwitch(params Params) *Switch {
s := new(Switch)
// Background.
s.bgDraw = imdraw.New(nil)
s.bgSpr = params.Background
s.size = params.Size.SwitchSize()
s.color = params.MainColor
s.bgSpr = params.Background
// Buttons.
buttonColor := params.SecColor
if buttonColor == nil {
buttonColor = colornames.Red
}
buttonParams := Params{
Size: params.Size - 2,
Shape: ShapeSquare,
MainColor: buttonColor,
}
s.prevButton = NewButton(buttonParams)
s.prevButton.SetOnClickFunc(s.onPrevButtonClicked)
s.nextButton = NewButton(buttonParams)
s.nextButton.SetOnClickFunc(s.onNextButtonClicked)
// Label & info.
labelParams := Params{
SizeRaw: pixel.V(s.Size().X, 0),
FontSize: params.Size - 1,
}
s.label = NewText(labelParams)
s.label.Align(AlignCenter)
infoParams := Params{
FontSize: SizeSmall,
MainColor: pixel.RGBA{0.1, 0.1, 0.1, 0.5},
}
s.info = NewInfoWindow(infoParams)
// Values.
s.index = 0
s.valueText = NewText(labelParams)
s.updateValueView()
return s
}
// Draw draws switch.
func (s *Switch) Draw(t pixel.Target, matrix pixel.Matrix) {
// Calculating draw area.
s.drawArea = MatrixToDrawArea(matrix, s.Size())
// Background.
if s.bgSpr != nil {
s.bgSpr.Draw(t, matrix)
} else {
DrawRectangle(t, s.DrawArea(), s.color)
}
// Value view.
valueDA := s.valueText.DrawArea()
if s.valueSprite == nil {
s.valueText.Draw(t, matrix)
} else {
s.valueSprite.Draw(t, matrix)
valueDA = MatrixToDrawArea(matrix, s.valueSprite.Frame().Size())
}
// Label & info window.
labelPos := MoveBC(s.Size(), s.label.Size())
s.label.Draw(t, matrix.Moved(labelPos))
if s.info != nil && s.hovered {
s.info.Draw(t)
}
// Buttons.
prevButtonPos := LeftOf(valueDA, s.prevButton.Size(), 10)
nextButtonPos := RightOf(valueDA, s.nextButton.Size(), 10)
s.prevButton.Draw(t, Matrix().Moved(prevButtonPos))
s.nextButton.Draw(t, Matrix().Moved(nextButtonPos))
}
// Update updates switch and all elements.
func (s *Switch) Update(win *Window) {
if s.Disabled() {
return
}
// Mouse events.
if s.DrawArea().Contains(win.MousePosition()) {
s.hovered = true
if s.info != nil {
s.info.Update(win)
}
} else {
s.hovered = false
}
// Elements update.
s.prevButton.Update(win)
s.nextButton.Update(win)
}
// SetBackground sets specified sprite as switch
// background, also removes background color.
func (s *Switch) SetBackground(spr *pixel.Sprite) {
s.bgSpr = spr
s.color = nil
}
// SetColor sets specified color as switch background
// color.
func (s *Switch) SetColor(c color.Color) {
s.color = c
}
// SetLabel sets specified text as label.
func (s *Switch) SetLabel(t string) {
s.label.SetText(t)
}
// SetInfo sets specified text as info.
func (s *Switch) SetInfo(t string) {
s.info.SetText(t)
}
// SetNextButtonBackground sets specified sprite as next
// button background.
func (s *Switch) SetNextButtonBackground(spr *pixel.Sprite) {
s.nextButton.SetBackground(spr)
}
// SetPrevButtonBackground sets specified sprite as previous
// button background.
func (s *Switch) SetPrevButtonBackground(spr *pixel.Sprite) {
s.prevButton.SetBackground(spr)
}
// SetValues sets specified list with values as switch values.
func (s *Switch) SetValues(values ...SwitchValue) {
s.values = values
s.updateValueView()
}
// SwtIntValues adds to witch all integers from
// specified min/max range.
func (s *Switch) SetIntValues(min, max int) {
values := make([]SwitchValue, max+1-min)
for i, n := 0, min; i < len(values); i, n = i+1, n+1 {
values[i] = SwitchValue{fmt.Sprintf("%d", n), n}
}
s.SetValues(values...)
}
// Focus toggles focus on element.
func (s *Switch) Focus(focus bool) {
s.focused = focus
}
// Focused checks whether switch is focused.
func (s *Switch) Focused() bool {
return s.focused
}
// Active toggles switch activity.
func (s *Switch) Active(active bool) {
s.prevButton.Active(active)
s.nextButton.Active(active)
s.disabled = !active
}
// Disabled checks whether switch is active.
func (s *Switch) Disabled() bool {
return s.disabled
}
// Size returns switch background size.
func (s *Switch) Size() pixel.Vec {
if s.bgSpr == nil {
return s.size
}
return s.bgSpr.Frame().Size()
}
// DrawArea returns current switch background position and size.
func (s *Switch) DrawArea() pixel.Rect {
return s.drawArea
}
// Value returns current switch value.
func (s *Switch) Value() *SwitchValue {
if s.index >= len(s.values) || s.index < 0 {
return nil
}
return &s.values[s.index]
}
// Reset sets value with first index as current
// value.
func (s *Switch) Reset() {
s.SetIndex(0)
}
// Find checks if switch constains specified value and returns
// index of this value or -1 if switch does not contains
// such value.
func (s *Switch) Find(value interface{}) int {
for i, v := range s.values {
if value == v.Value {
return i
}
}
return -1
}
// Find searches switch values for value with specified index
// and returns this value or nil if switch does not contains
// value with such index.
func (s *Switch) FindValue(index int) *SwitchValue {
if index >= len(s.values) || index < 0 {
return nil
}
return &s.values[index]
}
// SetIndex sets value with specified index as current value
// of this switch. If specified value is bigger than maximal
// possible index, then index of first value is set, if specified
// index is smaller than minimal, then index of last value is set.
func (s *Switch) SetIndex(index int) {
if index > len(s.values)-1 {
s.index = 0
} else if index < 0 {
s.index = len(s.values) - 1
} else {
s.index = index
}
s.updateValueView()
}
// Sets specified function as function triggered on on switch value change.
func (s *Switch) SetOnChangeFunc(f func(s *Switch, old, new *SwitchValue)) {
s.onChange = f
}
// updateValueView updates value view with current switch value.
func (s *Switch) updateValueView() {
if s.values == nil || len(s.values) < 1 {
return
}
if pic, ok := s.Value().View.(pixel.Picture); ok {
s.valueSprite = pixel.NewSprite(pic, pic.Bounds())
} else {
label, _ := s.Value().View.(string)
s.valueText.SetText(label)
}
}
// Triggered after next button clicked.
func (s *Switch) onNextButtonClicked(b *Button) {
oldIndex := s.index
s.SetIndex(s.index + 1)
if s.onChange != nil {
oldValue := s.FindValue(oldIndex)
s.onChange(s, oldValue, s.Value())
}
}
// Triggered after prev button clicked.
func (s *Switch) onPrevButtonClicked(b *Button) {
oldIndex := s.index
s.SetIndex(s.index - 1)
if s.onChange != nil {
s.onChange(s, s.FindValue(oldIndex), s.Value())
}
}