-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
195 lines (143 loc) · 3.58 KB
/
frame.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
package glui
import (
)
// wrapper for Body, Menu and FocusRect
type Frame struct {
winW int
winH int
maxW int
maxH int
maxZIndex int
P1 *DrawPass1Data
P2 *DrawPass2Data
Body *Body
Menu *Menu
FocusRect *FocusRect
state *FrameState
}
// skinmap and glyphmap can be shared across multiple windows/frames/layers
func newFrame(isFirst bool, skin *SkinMap, glyphs *GlyphMap) *Frame {
frame := &Frame{
0, 0, 0, 0, 0,
newDrawPass1Data(skin), newDrawPass2Data(glyphs),
nil, nil, nil, newFrameState(),
}
frame.Body = newBody(frame, isFirst)
frame.Menu = newMenu(frame)
frame.FocusRect = newFocusRect(frame)
return frame
}
func (e *Frame) syncWindowSize(winW, winH int) {
e.winW, e.winH = winW, winH
e.P1.winW, e.P1.winH = winW, winH
e.P2.winW, e.P2.winH = winW, winH
e.ForcePosDirty()
}
func (e *Frame) dirty() bool {
return e.P1.dirty() || e.P2.dirty()
}
func (e *Frame) posDirty() bool {
return e.P1.posDirty() || e.P2.posDirty()
}
func (e *Frame) clearPosDirty() {
e.P1.clearPosDirty()
e.P2.clearPosDirty()
}
func (e *Frame) ForcePosDirty() {
e.P1.forcePosDirty()
e.P2.forcePosDirty()
}
// needed when switching frames
func (e *Frame) ForceAllDirty() {
e.P1.ForceAllDirty()
e.P2.ForceAllDirty()
}
func (e *Frame) GetPos() (int, int) {
x, y := 0, 0
if e.maxW < e.winW {
x = (e.winW - e.maxW)/2
}
if e.maxH < e.winH {
y = (e.winH - e.maxH)/2
}
return x, y
}
func (e *Frame) GetSize() (int, int) {
w, h := e.winW, e.winH
if w > e.maxW {
w = e.maxW
}
if h > e.maxH {
h = e.maxH
}
return w, h
}
func (e *Frame) show() {
e.Body.Show()
}
func (e *Frame) CalcDepth() {
stack := newElementStack()
for ; stack.dirty; {
stack.dirty = false
e.Body.CalcDepth(stack)
}
stack.dirty = true
// add some offset for menu, so it definitely lies above all body elements
// XXX: why?
stack.offset = 2*stack.maxZIndex()
for ; stack.dirty; {
stack.dirty = false
e.Menu.CalcDepth(stack)
}
e.maxZIndex = stack.maxZIndex()
}
func (e *Frame) CalcPos() {
if e.maxZIndex == 0 {
panic("depth not yet calculated")
}
x, y := e.GetPos()
w, h := e.GetSize()
e.Body.CalcPos(w, h, e.maxZIndex)
e.Menu.CalcPos(w, h, e.maxZIndex)
e.FocusRect.CalcPos(w, h, e.maxZIndex)
if x != 0 || y != 0 {
e.Body.Translate(x, y)
e.Menu.Translate(x, y)
e.FocusRect.Translate(x, y)
}
e.P1.SyncImagesToTexture()
}
func (e *Frame) Animate(tick uint64) {
e.Body.Animate(tick)
e.Menu.Animate(tick)
e.FocusRect.Animate(tick)
}
// if this function returns `false` incorrectly, then oldMouseElement probably doesnt correctly have Body as ancestor
func (e *Frame) findMouseElement(oldMouseElement Element, x, y int) (Element, bool) {
if e.Menu.IsHit(x, y) {
if oldMouseElement == nil {
oldMouseElement = e.Menu
} else if !hasAncestor(oldMouseElement, e.Menu) {
oldMouseElement = e.Menu
newMouseElement, _ := findHitElement(oldMouseElement, x, y)
return newMouseElement, false
}
return findHitElement(oldMouseElement, x, y)
} else {
if oldMouseElement == nil {
oldMouseElement = e.Body
} else if !hasAncestor(oldMouseElement, e.Body) {
oldMouseElement = e.Body
newMouseElement, _ := findHitElement(oldMouseElement, x, y)
return newMouseElement, false
}
return findHitElement(oldMouseElement, x, y)
}
}
func (e *Frame) Clear() {
e.Body.ClearChildren()
e.Menu.ClearChildren()
}
func (e *Frame) CurrentTick() uint64 {
return e.state.lastTick
}