-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
200 lines (170 loc) · 4.04 KB
/
core.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
package ux
import (
"github.com/axe/axe-go/pkg/input"
"github.com/axe/axe-go/pkg/ui"
"github.com/axe/axe-go/pkg/util"
)
type HasComponent interface {
GetComponent(theme *Theme) *ui.Base
}
type Kind int
const (
KindDefault Kind = iota
KindNone
KindButton
KindCheckbox
KindEditable
KindScrollable
KindDropdown
KindInputText
KindInputDropdown
KindInputArea
KindInputNumber
KindCollapsible
KindDialog
)
func (k Kind) Get(defaultKind Kind) Kind {
if k == KindDefault {
return defaultKind
}
return k
}
type Base struct {
*ui.Template
Kind Kind
}
// A value that can be tracked for changes.
type Value[V any] interface {
// If the value has changed since the last time this method was called
// a non-nil value will be returned pointing to the new value.
GetChanged() *V
// Sets the value.
Set(value V)
}
// Returns a Value[V] given an optional value and its constant alternative
// if a value was not given.
func CoalesceValue[V any](value Value[V], constant V) Value[V] {
if value != nil {
return value
}
return NewConstant(constant)
}
var _ Value[int] = &Computed[int]{}
var _ Value[int] = &Constant[int]{}
var _ Value[int] = &Dynamic[int]{}
var _ Value[int] = &Live[int]{}
// A value that is changed by set.
type Constant[V any] struct {
value V
changed bool
}
func NewConstant[V any](value V) *Constant[V] {
return &Constant[V]{value: value, changed: true}
}
func (c *Constant[V]) GetChanged() *V {
if !c.changed {
return nil
}
c.changed = false
return &c.value
}
func (c *Constant[V]) Set(value V) {
c.value = value
c.changed = true
}
func (c *Constant[V]) Changed() bool {
return c.changed
}
// A value that is computed when GetChanged is invoked and it returns
// a new value then last time. Set has no effect.
type Computed[V comparable] struct {
lastValue V
getValue func() V
}
func NewComputed[V comparable](getValue func() V) *Computed[V] {
return &Computed[V]{getValue: getValue}
}
func (w *Computed[V]) GetChanged() *V {
newValue := w.getValue()
if w.lastValue == newValue {
return nil
}
w.lastValue = newValue
return &w.lastValue
}
func (w *Computed[V]) Set(value V) {
}
// A value that is computed when GetChanged is invoked but also
// supports setting.
type Dynamic[V comparable] struct {
lastValue V
getValue func() V
setValue func(V)
}
func NewDynamic[V comparable](get func() V, set func(V)) *Dynamic[V] {
return &Dynamic[V]{getValue: get, setValue: set}
}
func (d *Dynamic[V]) GetChanged() *V {
newValue := d.getValue()
if d.lastValue == newValue {
return nil
}
d.lastValue = newValue
return &d.lastValue
}
func (d *Dynamic[V]) Set(value V) {
d.setValue(value)
}
// A value that is expected to change every time GetChanged is invoked.
type Live[V any] struct {
getValue func() V
setValue func(V)
}
func NewLive[V any](get func() V, set func(V)) *Live[V] {
return &Live[V]{getValue: get, setValue: set}
}
func (d *Live[V]) GetChanged() *V {
value := d.getValue()
return &value
}
func (d *Live[V]) Set(value V) {
d.setValue(value)
}
// A counter for the number of times something occurs.
type Counter int
func (c *Counter) Add(amount int) { *c += Counter(amount) }
func (c *Counter) Changed() bool {
changed := *c > 0
*c = 0
return changed
}
// A trigger used for input actions.
type Trigger float32
func (t *Trigger) Set(value float32) { *t = Trigger(value) }
func (t Trigger) Init(inputs input.InputSystem) {}
func (t Trigger) Update(inputs input.InputSystem) (input.Data, bool) {
return input.Data{Value: float32(t)}, true
}
func (t Trigger) InputCount() int { return 0 }
// A listener is a function where multiple can be added.
type Listener[E any] func(ev E)
func (l Listener[E]) Trigger(ev E) {
if l != nil {
l(ev)
}
}
func listenerNil[E any](a Listener[E]) bool {
return a == nil
}
func listenerJoin[E any](first Listener[E], second Listener[E]) Listener[E] {
return func(ev E) {
first(ev)
second(ev)
}
}
func (a *Listener[E]) Add(b Listener[E], before bool) {
*a = util.CoalesceJoin(*a, b, before, listenerNil[E], listenerJoin[E])
}
func (a *Listener[E]) Clear() {
*a = nil
}