-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmain.go
182 lines (161 loc) · 4.3 KB
/
main.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
package main
import (
"C"
"log"
"runtime"
"time"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/golang-ui/nuklear/nk"
"github.com/xlab/closer"
)
const (
winWidth = 400
winHeight = 500
maxVertexBuffer = 512 * 1024
maxElementBuffer = 128 * 1024
)
func init() {
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
closer.Fatalln(err)
}
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
win, err := glfw.CreateWindow(winWidth, winHeight, "Nuklear Demo", nil, nil)
if err != nil {
closer.Fatalln(err)
}
win.MakeContextCurrent()
width, height := win.GetSize()
log.Printf("glfw: created window %dx%d", width, height)
if err := gl.Init(); err != nil {
closer.Fatalln("opengl: init failed:", err)
}
gl.Viewport(0, 0, int32(width), int32(height))
ctx := nk.NkPlatformInit(win, nk.PlatformInstallCallbacks)
atlas := nk.NewFontAtlas()
nk.NkFontStashBegin(&atlas)
// sansFont := nk.NkFontAtlasAddFromBytes(atlas, MustAsset("assets/FreeSans.ttf"), 16, nil)
// config := nk.NkFontConfig(14)
// config.SetOversample(1, 1)
// config.SetRange(nk.NkFontChineseGlyphRanges())
// simsunFont := nk.NkFontAtlasAddFromFile(atlas, "/Library/Fonts/Microsoft/SimHei.ttf", 14, &config)
nk.NkFontStashEnd()
// if simsunFont != nil {
// nk.NkStyleSetFont(ctx, simsunFont.Handle())
// }
exitC := make(chan struct{}, 1)
doneC := make(chan struct{}, 1)
closer.Bind(func() {
close(exitC)
<-doneC
})
state := &State{
bgColor: nk.NkRgba(28, 48, 62, 255),
}
nk.NkTexteditInitDefault(&state.text)
fpsTicker := time.NewTicker(time.Second / 30)
for {
select {
case <-exitC:
nk.NkPlatformShutdown()
glfw.Terminate()
fpsTicker.Stop()
close(doneC)
return
case <-fpsTicker.C:
if win.ShouldClose() {
close(exitC)
continue
}
glfw.PollEvents()
gfxMain(win, ctx, state)
}
}
}
func gfxMain(win *glfw.Window, ctx *nk.Context, state *State) {
nk.NkPlatformNewFrame()
// Layout
bounds := nk.NkRect(50, 50, 230, 250)
update := nk.NkBegin(ctx, "Demo", bounds,
nk.WindowBorder|nk.WindowMovable|nk.WindowScalable|nk.WindowMinimizable|nk.WindowTitle)
if update > 0 {
nk.NkLayoutRowStatic(ctx, 30, 80, 1)
{
if nk.NkButtonLabel(ctx, "button") > 0 {
log.Println("[INFO] button pressed!")
}
}
nk.NkLayoutRowDynamic(ctx, 30, 2)
{
if nk.NkOptionLabel(ctx, "easy", flag(state.opt == Easy)) > 0 {
state.opt = Easy
}
if nk.NkOptionLabel(ctx, "hard", flag(state.opt == Hard)) > 0 {
state.opt = Hard
}
}
nk.NkLayoutRowDynamic(ctx, 30, 1)
{
nk.NkEditBuffer(ctx, nk.EditField, &state.text, nk.NkFilterDefault)
if nk.NkButtonLabel(ctx, "Print Entered Text") > 0 {
log.Println(state.text.GetGoString())
}
}
nk.NkLayoutRowDynamic(ctx, 25, 1)
{
nk.NkPropertyInt(ctx, "Compression:", 0, &state.prop, 100, 10, 1)
}
nk.NkLayoutRowDynamic(ctx, 20, 1)
{
nk.NkLabel(ctx, "background:", nk.TextLeft)
}
nk.NkLayoutRowDynamic(ctx, 25, 1)
{
size := nk.NkVec2(nk.NkWidgetWidth(ctx), 400)
if nk.NkComboBeginColor(ctx, state.bgColor, size) > 0 {
nk.NkLayoutRowDynamic(ctx, 120, 1)
cf := nk.NkColorCf(state.bgColor)
cf = nk.NkColorPicker(ctx, cf, nk.ColorFormatRGBA)
state.bgColor = nk.NkRgbCf(cf)
nk.NkLayoutRowDynamic(ctx, 25, 1)
r, g, b, a := state.bgColor.RGBAi()
r = nk.NkPropertyi(ctx, "#R:", 0, r, 255, 1, 1)
g = nk.NkPropertyi(ctx, "#G:", 0, g, 255, 1, 1)
b = nk.NkPropertyi(ctx, "#B:", 0, b, 255, 1, 1)
a = nk.NkPropertyi(ctx, "#A:", 0, a, 255, 1, 1)
state.bgColor.SetRGBAi(r, g, b, a)
nk.NkComboEnd(ctx)
}
}
}
nk.NkEnd(ctx)
// Render
bg := make([]float32, 4)
nk.NkColorFv(bg, state.bgColor)
width, height := win.GetSize()
gl.Viewport(0, 0, int32(width), int32(height))
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.ClearColor(bg[0], bg[1], bg[2], bg[3])
nk.NkPlatformRender(nk.AntiAliasingOn, maxVertexBuffer, maxElementBuffer)
win.SwapBuffers()
}
type Option uint8
const (
Easy Option = 0
Hard Option = 1
)
type State struct {
bgColor nk.Color
prop int32
opt Option
text nk.TextEdit
}
func onError(code int32, msg string) {
log.Printf("[glfw ERR]: error %d: %s", code, msg)
}