-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabel.go
190 lines (164 loc) · 4.78 KB
/
label.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
package glmenu
import (
"github.com/4ydx/gltext/v4.1"
"github.com/go-gl/mathgl/mgl32"
)
type LabelAction int
const (
NOOP LabelAction = iota
GOTO_MENU
EXIT_MENU
EXIT_GAME
)
type LabelConfig struct {
Padding Padding
Action LabelAction
Goto string
}
type LabelInteraction func(
xPos, yPos float64,
button MouseClick,
isInBoundingBox bool,
)
type Label struct {
Config LabelConfig
Menu *Menu
Text *v41.Text
IsHover bool
IsClick bool
// public methods are expected to be defined by the user and run before the private method are called
// if a public method is undefined, it is skipped. Currently I have only defined onRelease as private.
OnClick LabelInteraction
onRelease LabelInteraction // set internally. handles linking different menus together, closing menus, closing game etc.
StopOnRelease bool // set to true in order to prevent onRelease being called
OnRelease LabelInteraction
OnHover LabelInteraction
OnNotHover func()
}
func (label *Label) Reset() {
label.Text.SetScale(label.Text.ScaleMin)
}
func (label *Label) GetPosition() mgl32.Vec2 {
return label.Text.Position
}
func (label *Label) GetPadding() Padding {
return label.Config.Padding
}
func (label *Label) SetString(str string, argv ...interface{}) {
if len(argv) == 0 {
label.Text.SetString(str)
} else {
label.Text.SetString(str, argv...)
}
}
func (label *Label) OrthoToScreenCoord() (X1 Point, X2 Point) {
if label.Menu != nil && label.Text != nil {
x1, x2 := label.Text.GetBoundingBox()
X1.X = x1.X + label.Menu.WindowWidth/2
X1.Y = x1.Y + label.Menu.WindowHeight/2
X2.X = x2.X + label.Menu.WindowWidth/2
X2.Y = x2.Y + label.Menu.WindowHeight/2
} else {
if label.Menu == nil {
MenuDebug("Uninitialized Menu Object")
}
if label.Text == nil {
MenuDebug("Uninitialized Text Object")
}
}
return
}
// IsClicked uses a bounding box to determine clicks
func (label *Label) IsClicked(xPos, yPos float64, button MouseClick) {
// menu rendering (and text) is positioned in orthographic projection coordinates
// but click positions are based on window coordinates
// we have to transform them
X1, X2 := label.OrthoToScreenCoord()
inBox := float32(xPos) > X1.X && float32(xPos) < X2.X && float32(yPos) > X1.Y && float32(yPos) < X2.Y
if inBox {
label.IsClick = true
if label.OnClick != nil {
label.OnClick(xPos, yPos, button, inBox)
}
}
}
// InsidePoint returns a point nearby the center of the label
// Used to locate a screen position where clicking can be simulated
func (label *Label) InsidePoint() (P Point) {
X1, X2 := label.OrthoToScreenCoord()
P.X = (X2.X-X1.X)/2 + X1.X
P.Y = (X2.Y-X1.Y)/2 + X1.Y
return
}
// IsReleased is checked for all labels in a menu when mouseup occurs
func (label *Label) IsReleased(xPos, yPos float64, button MouseClick) {
// anything flagged as clicked now needs to decide whether to execute its logic based on inBox
X1, X2 := label.OrthoToScreenCoord()
inBox := float32(xPos) > X1.X && float32(xPos) < X2.X && float32(yPos) > X1.Y && float32(yPos) < X2.Y
if label.IsClick {
if label.IsHover {
label.Text.SetColor(label.Menu.Defaults.TextHover)
} else {
label.Text.SetColor(label.Menu.Defaults.TextColor)
}
if label.OnRelease != nil {
label.OnRelease(xPos, yPos, button, inBox)
}
if !label.StopOnRelease {
label.onRelease(xPos, yPos, button, inBox)
}
label.StopOnRelease = false
}
label.IsClick = false
}
// IsHovered uses a bounding box
func (label *Label) IsHovered(xPos, yPos float64) {
X1, X2 := label.OrthoToScreenCoord()
inBox := float32(xPos) > X1.X && float32(xPos) < X2.X && float32(yPos) > X1.Y && float32(yPos) < X2.Y
label.IsHover = inBox
if inBox {
label.OnHover(xPos, yPos, MouseUnclicked, inBox)
}
}
func (label *Label) Draw() {
label.Text.Draw()
}
func (label *Label) SetPosition(v mgl32.Vec2) {
label.Text.SetPosition(v)
}
func (label *Label) Height() float32 {
return label.Text.Height()
}
func (label *Label) Width() float32 {
return label.Text.Width()
}
func (label *Label) NavigateTo() {
point := label.InsidePoint()
if label.OnHover != nil {
label.IsHovered(float64(point.X), float64(point.Y))
}
}
// NavigateAway if we end up needing to navigate away from this item then let the caller know
// because it might need that information. return value of 'true'
func (label *Label) NavigateAway() bool {
if label.IsHover {
label.IsHover = false
return true
}
return false
}
func (label *Label) Follow() bool {
if label.IsHover {
point := label.InsidePoint()
label.IsClicked(float64(point.X), float64(point.Y), MouseLeft)
label.IsReleased(float64(point.X), float64(point.Y), MouseLeft)
return true
}
return false
}
func (label *Label) IsNoop() bool {
return label.Config.Action == NOOP
}
func (label *Label) Type() FormatableType {
return FormatableLabel
}