forked from KorokEngine/Korok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_lib.go
212 lines (183 loc) · 4.58 KB
/
gui_lib.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
package auto
import (
"korok.io/korok/math/f32"
"korok.io/korok/gfx"
g "korok.io/korok/gui"
)
type LayoutMan struct {
*g.Context
Options
fallback layout
layouts map[string]*layout
depth int
current *layout
// sqNum should be same for layout and drawing
sqNum int
}
func (lm *LayoutMan) initialize() {
lm.fallback.Initialize(gContext.Theme)
lm.current = &lm.fallback
lm.layouts = make(map[string]*layout)
}
func (lm *LayoutMan) Text(id g.ID, text string, style *g.TextStyle, opt *Options) *Element {
var (
elem, ready = lm.BeginElement(id, opt)
size f32.Vec2
font = style.Font
)
if font == nil {
font = lm.Context.Theme.Font
}
// draw text 最好返回最新的大小..
if ready {
size = lm.Context.DrawText(&elem.Rect, text, style)
} else {
size = lm.Context.CalcTextSize(text, 0, font, style.Size)
}
elem.Rect.W = size[0]
elem.Rect.H = size[1]
lm.EndElement(elem)
return nil
}
// Widgets: InputEditor
func (lm *LayoutMan) InputText(hint string, lyt layout, style *g.InputStyle) {
}
// Widget: Image
func (lm *LayoutMan) Image(id g.ID, tex gfx.Tex2D, style *g.ImageStyle, opt *Options) {
var (
elem, ready = lm.BeginElement(id, opt)
)
if ready {
lm.Context.DrawImage(&elem.Rect, tex, style)
} else {
if opt != nil {
elem.W = opt.W
elem.H = opt.H
}
}
lm.EndElement(elem)
}
// Widget: Button
func (lm *LayoutMan) Button(id g.ID, text string, style *g.ButtonStyle, opt *Options) (event g.EventType) {
var (
elem, ready = lm.BeginElement(id, opt)
)
if ready {
lm.Context.Button(id, &elem.Rect, text, style)
} else {
font := style.Font
if font == nil {
font = lm.Context.Theme.Font
}
size := style.TextStyle.Size
textSize := lm.CalcTextSize(text, 0, font, size)
extW := style.Padding.Left+style.Padding.Right
extH := style.Padding.Top+style.Padding.Bottom
elem.W, elem.H = textSize[0]+extW, textSize[1]+extH
}
lm.EndElement(elem)
return
}
func (lm *LayoutMan) renderTextClipped(text string, bb *g.Rect, style *g.TextStyle) {
x, y := g.Gui2Game(bb.X, bb.Y)
font := lm.Theme.Text.Font
if bb.W == 0 {
lm.DrawList.AddText(f32.Vec2{x, y}, text, font, 12, 0xFF000000, 0)
} else {
lm.DrawList.AddText(f32.Vec2{x, y}, text, font, 12, 0xFF000000, bb.W)
}
}
func (lm *LayoutMan) ImageButton(id g.ID, normal, pressed gfx.Tex2D, style *g.ImageButtonStyle, opt *Options) ( event g.EventType) {
var (
elem, ready = lm.BeginElement(id, opt)
bb = &elem.Rect
)
if ready {
event = lm.ClickEvent(id, bb)
var tex gfx.Tex2D
if event & g.EventDown != 0 {
tex = pressed
} else {
tex = normal
}
lm.DrawImage(bb, tex, &style.ImageStyle)
} else {
if opt != nil {
elem.W = opt.W
elem.H = opt.H
}
}
lm.EndElement(elem)
return
}
// Slider 需要设定一些自定义的属性,目前没有想好如何实现,先把逻辑实现了
// 用两种颜色来绘制
func (lm *LayoutMan) Slider(id g.ID, value *float32, style *g.SliderStyle, opt *Options) (e g.EventType){
var (
elem, ready = lm.BeginElement(id, opt)
bb = &elem.Rect
)
if ready {
// 说明滑动了,那么应该使用最新的值,而不是传入的值
if v, event := lm.Context.CheckSlider(id, bb); event & g.EventDragging != 0 {
*value = v
e = event
}
lm.DrawRect(bb, style.Bar, 5)
lm.DrawCircle(bb.X+bb.W*(*value), bb.Y+bb.H/2, 10, style.Knob)
} else {
// 设置默认的宽高
if elem.W == 0 {
elem.W = 120
}
if elem.H == 0 {
elem.H = 10
}
}
lm.EndElement(elem)
return
}
func (lm *LayoutMan) DefineLayout(name string, xt ViewType) {
if l, ok := lm.layouts[name]; ok {
lm.current = l
} else {
l := &layout{}; l.Initialize(gContext.Theme)
lm.layouts[name] = l
lm.current = l
}
}
func (lm *LayoutMan) Clear(names ...string) {
if size := len(names); size > 0 {
for i := 0; i < size; i++ {
delete(lm.layouts, names[i])
}
} else {
lm.layouts = make(map[string]*layout)
}
}
// 计算单个UI元素
// 如果有大小则记录出偏移和Margin
// 否则只返回元素
func (lm *LayoutMan) BeginElement(id g.ID, opt *Options) (elem *Element, ok bool){
return lm.current.BeginElement(id, opt)
}
// 结束绘制, 每绘制完一个元素都要偏移一下光标
func (lm *LayoutMan) EndElement(elem *Element) {
lm.current.EndElement(elem)
}
func (lm *LayoutMan) BeginLayout(id g.ID, opt *Options, xtype LayoutType) {
lm.depth ++
if elem, ok := lm.current.BeginLayout(id, opt, xtype); ok {
// debug-draw
if g.DebugDraw {
lm.DrawDebugBorder(elem.X, elem.Y, elem.W, elem.H, 0xFF00FF00)
}
}
}
func (lm *LayoutMan) EndLayout() {
lm.depth --
lm.current.EndLayout()
if d := lm.depth; d == 0 {
lm.current = &lm.fallback
}
}