Skip to content

Commit 98e4dca

Browse files
committed
Added examples for group layout
1 parent 5122614 commit 98e4dca

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## nk-group-layout
2+
3+
This example shows, how to use group layout provided by nuklear API.
4+
5+
Please note, that the following explanations are taken from the nuklear doc, available [here](https://github.com/vurtun/nuklear/tree/master/doc)
6+
7+
## Groups
8+
Groups are basically windows inside windows. They allow to subdivide space in a window to layout widgets as a group. Almost all more complex widget layouting requirements can be solved using groups and basic layouting fuctionality. Groups just like windows are identified by an unique name and internally keep track of scrollbar offsets by default. However additional versions are provided to directly manage the scrollbar.
9+
10+
To create a group you have to call one of the three `nk.NkGroupBegin` functions to start group declarations and `nk.NkGroupEnd` at the end. Furthermore it is required to check the return value of `nk.NkGroupBegin` and only process widgets inside the window if the value is not 0. Nesting groups is possible and even encouraged since many layouting schemes can only be achieved by nesting. Groups, unlike windows, need `nk.NkGroupEnd` to be only called if the corosponding `nk.NkGroupBegin` call does not return 0.
11+
Note that group names should be unique.
12+
//TODO
13+
## Install
14+
Make sure, that the following packages are installed
15+
- github.com/go-gl/gl/v3.2-core/gl
16+
- github.com/go-gl/glfw/v3.2/glfw
17+
- github.com/golang-ui/nuklear/nk
18+
- github.com/xlab/closer
19+
20+
Then run
21+
```
22+
$ go run main.go
23+
```
24+
Now a runnable binary should have been created in your $GOBIN path
25+
26+
### Maintainer
27+
jannst <mkawaganga@gmail.com>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"runtime"
6+
"time"
7+
8+
"github.com/go-gl/gl/v3.2-core/gl"
9+
"github.com/go-gl/glfw/v3.2/glfw"
10+
"github.com/golang-ui/nuklear/nk"
11+
"github.com/xlab/closer"
12+
)
13+
14+
const (
15+
winWidth = 800
16+
winHeight = 550
17+
18+
maxVertexBuffer = 512 * 1024
19+
maxElementBuffer = 128 * 1024
20+
)
21+
22+
func init() {
23+
runtime.LockOSThread()
24+
}
25+
26+
func main() {
27+
if err := glfw.Init(); err != nil {
28+
closer.Fatalln(err)
29+
}
30+
glfw.WindowHint(glfw.ContextVersionMajor, 3)
31+
glfw.WindowHint(glfw.ContextVersionMinor, 2)
32+
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
33+
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
34+
win, err := glfw.CreateWindow(winWidth, winHeight, "Nuklear Demo", nil, nil)
35+
if err != nil {
36+
closer.Fatalln(err)
37+
}
38+
win.MakeContextCurrent()
39+
40+
width, height := win.GetSize()
41+
log.Printf("glfw: created window %dx%d", width, height)
42+
43+
if err := gl.Init(); err != nil {
44+
closer.Fatalln("opengl: init failed:", err)
45+
}
46+
gl.Viewport(0, 0, int32(width), int32(height))
47+
48+
ctx := nk.NkPlatformInit(win, nk.PlatformInstallCallbacks)
49+
50+
atlas := nk.NewFontAtlas()
51+
nk.NkFontStashBegin(&atlas)
52+
//Use default font so we dont have to manage assets
53+
sansFont := nk.NkFontAtlasAddDefault(atlas, 14, nil)
54+
nk.NkFontStashEnd()
55+
if sansFont != nil {
56+
nk.NkStyleSetFont(ctx, sansFont.Handle())
57+
}
58+
59+
exitC := make(chan struct{}, 1)
60+
doneC := make(chan struct{}, 1)
61+
closer.Bind(func() {
62+
close(exitC)
63+
<-doneC
64+
})
65+
66+
state := &State{
67+
bgColor: nk.NkRgba(28, 48, 62, 255),
68+
groupStates: make([]int32, 16),
69+
}
70+
fpsTicker := time.NewTicker(time.Second / 30)
71+
for {
72+
select {
73+
case <-exitC:
74+
nk.NkPlatformShutdown()
75+
glfw.Terminate()
76+
fpsTicker.Stop()
77+
close(doneC)
78+
return
79+
case <-fpsTicker.C:
80+
if win.ShouldClose() {
81+
close(exitC)
82+
continue
83+
}
84+
glfw.PollEvents()
85+
gfxMain(win, ctx, state)
86+
}
87+
}
88+
}
89+
90+
func gfxMain(win *glfw.Window, ctx *nk.Context, state *State) {
91+
nk.NkPlatformNewFrame()
92+
93+
// Layout
94+
bounds := nk.NkRect(10, 10, 700, 500)
95+
//Begin Window
96+
if nk.NkBegin(ctx, "Group Layout Demo", bounds, nk.WindowBorder|nk.WindowMovable|nk.WindowScalable|nk.WindowMinimizable|nk.WindowTitle) > 0 {
97+
98+
nk.NkLayoutRowStatic(ctx, 300, 200, 2)
99+
//create group with border and title
100+
if nk.NkGroupBegin(ctx, "Group 1", nk.WindowBorder|nk.WindowTitle) > 0 {
101+
//create row with dynamic calculated height and 1 column
102+
nk.NkLayoutRowDynamic(ctx, 0, 1)
103+
//add 16 widgets
104+
for i := 0; i < 16; i++ {
105+
nk.NkSelectableLabel(ctx, getGroupStatus(state.groupStates[i]), nk.TextCentered, &state.groupStates[i])
106+
}
107+
//Do not forget to end group
108+
nk.NkGroupEnd(ctx)
109+
}
110+
111+
//lets create a floating group. Therefore we need to use layout space api
112+
nk.NkLayoutSpaceBegin(ctx, nk.Static, 150, 10000)
113+
//Define the rectangle, in which our group will be placed
114+
nk.NkLayoutSpacePush(ctx, nk.NkRect(150, 10, 300, 100))
115+
//try to add the group
116+
if nk.NkGroupBegin(ctx, "Group 2", nk.WindowBorder|nk.WindowTitle) > 0 {
117+
//create row with dynamic calculated height and 1 column
118+
nk.NkLayoutRowDynamic(ctx, 0, 1)
119+
//add 16 widgets
120+
for i := 0; i < 16; i++ {
121+
//add selector widget
122+
nk.NkSelectableLabel(ctx, getGroupStatus(state.groupStates[i]), nk.TextCentered, &state.groupStates[i])
123+
}
124+
//Do not forget to end group
125+
nk.NkGroupEnd(ctx)
126+
}
127+
nk.NkLayoutSpaceEnd(ctx)
128+
129+
}
130+
//End Window
131+
nk.NkEnd(ctx)
132+
133+
// Render
134+
bg := make([]float32, 4)
135+
nk.NkColorFv(bg, state.bgColor)
136+
width, height := win.GetSize()
137+
gl.Viewport(0, 0, int32(width), int32(height))
138+
gl.Clear(gl.COLOR_BUFFER_BIT)
139+
gl.ClearColor(bg[0], bg[1], bg[2], bg[3])
140+
nk.NkPlatformRender(nk.AntiAliasingOn, maxVertexBuffer, maxElementBuffer)
141+
win.SwapBuffers()
142+
}
143+
144+
func getGroupStatus(b int32) string {
145+
if b != 0 {
146+
return "selected"
147+
} else {
148+
return "not selected"
149+
}
150+
}
151+
152+
type State struct {
153+
bgColor nk.Color
154+
prop int32
155+
groupStates []int32
156+
}
18.1 KB
Loading

0 commit comments

Comments
 (0)