rand/cmd/rand-gui/ui.go
2019-01-09 18:21:20 -05:00

133 lines
3.5 KiB
Go

package main
// helper functions for UI
import (
"time"
//"github.com/golang-ui/nuklear/nk"
"gitlab.wow.st/gmp/nuklear/nk"
)
const (
initWidth = 600
initHeight = 400
maxVertexBuffer = 512 * 1024
maxElementBuffer = 128 * 1024
frametime = time.Second / 30
rows = 3
)
var (
white nk.Color
atlas *nk.FontAtlas
sansFont *nk.Font
sansFontHandle *nk.UserFont
ctx *nk.Context
width, height int
y, lastY float32
)
func uiInit() {
platformInit()
mkWin()
white = nk.NkRgba(255,255,255,255)
table := []nk.Color{
nk.NkRgba(70, 70, 70, 255), // NK_COLOR_TEXT
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_WINDOW
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_HEADER
nk.NkRgba(0, 0, 0, 255), // NK_COLOR_BORDER
nk.NkRgba(185, 185, 185, 255), // NK_COLOR_BUTTON
nk.NkRgba(170, 170, 170, 255), // NK_COLOR_BUTTON_HOVER
nk.NkRgba(160, 160, 160, 255), // NK_COLOR_BUTTON_ACTIVE
nk.NkRgba(150, 150, 150, 255), // NK_COLOR_TOGGLE
nk.NkRgba(120, 120, 120, 255), // NK_COLOR_TOGGLE_HOVER
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_TOGGLE_CURSOR
nk.NkRgba(190, 190, 190, 255), // NK_COLOR_SELECT
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_SELECT_ACTIVE
nk.NkRgba(190, 190, 190, 255), // NK_COLOR_SLIDER
nk.NkRgba(80, 80, 80, 255), // NK_COLOR_SLIDER_CURSOR
nk.NkRgba(70, 70, 70, 255), // NK_COLOR_SLIDER_CURSOR_HOVER
nk.NkRgba(60, 60, 60, 255), // NK_COLOR_SLIDER_CURSOR_ACTIVE
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_PROPERTY
nk.NkRgba(150, 150, 150, 255), // NK_COLOR_EDIT
nk.NkRgba(0, 0, 0, 255), // NK_COLOR_EDIT_CURSOR
nk.NkRgba(175, 175, 175, 255), // NK_COLOR_COMBO
nk.NkRgba(160, 160, 160, 255), // NK_COLOR_CHART
nk.NkRgba(45, 45, 45, 255), // NK_COLOR_CHART_COLOR
nk.NkRgba( 255, 0, 0, 255), // NK_COLOR_CHART_COLOR_HIGHLIGHT
nk.NkRgba(180, 180, 180, 255), // NK_COLOR_SCROLLBAR
nk.NkRgba(140, 140, 140, 255), // NK_COLOR_SCROLLBAR_CURSOR
nk.NkRgba(150, 150, 150, 255), // NK_COLOR_SCROLLBAR_CURSOR_HOVER
nk.NkRgba(160, 160, 160, 255), // NK_COLOR_SCROLLBAR_CURSOR_ACTIVE
nk.NkRgba(180, 180, 180, 255), // NK_COLOR_TAB_HEADER
}
_ = table
// this scheme comes out all white on Android.
//nk.NkStyleFromTable(ctx, table)
}
func loadFont() {
atlas = nk.NewFontAtlas()
nk.NkFontStashBegin(&atlas)
sansFont = nk.NkFontAtlasAddFromBytes(atlas, MustAsset("assets/DroidSans.ttf"), fontSize, nil)
nk.NkFontStashEnd()
if sansFont != nil {
sansFontHandle = sansFont.Handle()
nk.NkStyleSetFont(ctx, sansFontHandle)
}
log(Info, "Font added")
}
func row(i int32,hs ...int) {
h := 1
if len(hs) > 0 {
h = hs[0]
}
nk.NkLayoutRowStatic(ctx,float32(height / (h * rows)),int32(float32(-i)+float32(width-25)/float32(i)),i)
}
func button(text string) bool {
return nk.NkButtonLabel(ctx,text) > 0
}
func label(text string, optss ...nk.Flags) {
var opts nk.Flags
if len(optss) > 0 {
opts = optss[0]
} else {
opts = nk.TextAlignLeft | nk.TextAlignMiddle
}
nk.NkLabel(ctx,text,opts)
}
func editstring(buf []byte) {
nk.NkEditFocus(ctx, nk.EditBox)
nk.NkEditStringZeroTerminated(ctx, nk.EditBox, buf, int32(len(buf)), nk.NkFilterAscii)
}
func uiBegin() bool {
nk.NkPlatformNewFrame()
// NewFrame()
lastY = y
width, height = GetSize()
bounds := nk.NkRect(0,0,float32(width),float32(height))
ret := nk.NkBegin(ctx, "Rand", bounds, nk.WindowScrollAutoHide) > 0
wpos := nk.NkWidgetPosition(ctx)
lastY = y
y = wpos.Y()
return ret
}
func uiEnd() {
nk.NkEnd(ctx)
nk.NkPlatformRender(nk.AntiAliasingOn, maxVertexBuffer, maxElementBuffer)
SwapBuffers()
}