zntg/ui/examples/01_basic/basic.go

134 lines
4.0 KiB
Go

package main
import (
"image/color"
"log"
_ "opslag.de/schobers/zntg/sdlui" // import the renderer for the UI
// _ "opslag.de/schobers/zntg/allg5ui" // import the renderer for the UI
"opslag.de/schobers/zntg/ui"
)
type basic struct {
ui.StackPanel
}
func (b *basic) Init(ctx ui.Context) error {
_, err := ctx.Fonts().CreateFontPath("default", "../resources/font/OpenSans-Regular.ttf", 14)
if err != nil {
return err
}
_, err = ctx.Textures().CreateTexturePath("plus", "../resources/images/plus.png", true)
if err != nil {
return err
}
var style = ctx.Style()
var stretch = func(content ui.Control, margin float32) ui.Control {
return ui.BuildSpacing(content, func(s *ui.Spacing) {
s.Width = ui.Infinite()
s.Margin.Left = ui.Fixed(margin)
s.Margin.Top = ui.Fixed(margin)
s.Margin.Right = ui.Fixed(margin)
s.Margin.Bottom = ui.Fixed(margin)
})
}
b.Background = color.White
b.Children = []ui.Control{
&ui.Label{Text: "Hello, world!"},
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{
stretch(ui.BuildIconButton("plus", "Contained", func(b *ui.Button) { b.Type = ui.ButtonTypeContained }), 8),
stretch(ui.BuildIconButton("plus", "Icon", func(b *ui.Button) { b.Type = ui.ButtonTypeIcon }), 8),
stretch(ui.BuildIconButton("plus", "Outlined", func(b *ui.Button) { b.Type = ui.ButtonTypeOutlined }), 8),
stretch(ui.BuildIconButton("plus", "Text", func(b *ui.Button) { b.Type = ui.ButtonTypeText }), 8),
}
}),
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{
stretch(ui.BuildIconButton("plus", "Contained", func(b *ui.Button) {
b.Type = ui.ButtonTypeContained
b.Disabled = true
}), 8),
stretch(ui.BuildIconButton("plus", "Icon", func(b *ui.Button) {
b.Type = ui.ButtonTypeIcon
b.Disabled = true
}), 8),
stretch(ui.BuildIconButton("plus", "Outlined", func(b *ui.Button) {
b.Type = ui.ButtonTypeOutlined
b.Disabled = true
}), 8),
stretch(ui.BuildIconButton("plus", "Text", func(b *ui.Button) {
b.Type = ui.ButtonTypeText
b.Disabled = true
}), 8),
}
}),
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{
&ui.Checkbox{},
ui.BuildCheckbox("Check me!", nil),
}
}),
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{
ui.BuildCheckbox("", func(b *ui.Checkbox) { b.Disabled = true }),
ui.BuildCheckbox("You can't check me!", func(b *ui.Checkbox) {
b.Selected = true
b.Disabled = true
}),
}
}),
ui.Stretch(ui.BuildParagraph(
"Content"+
"\n\n"+
"Could be on multiple lines...\n"+
"And if the line is long enough (and without line breaks in the string) it will wrap around to the next line on the screen. You can test this behaviour by resizing the window. Go ahead!", nil)),
ui.Margin(ui.StretchWidth(ui.BuildTextBox(func(b *ui.TextBox) {
b.Text = "Type here..."
})), 8),
ui.Margin(ui.StretchWidth(ui.BuildTextBox(func(b *ui.TextBox) {
b.Text = "You can't type here..."
b.Disabled = true
})), 8),
ui.Margin(ui.BuildButton("Quit", func(b *ui.Button) {
b.ButtonClicked().AddHandler(func(ui.Context, ui.ControlClickedArgs) {
ctx.Quit()
})
b.Tooltip = "Will quit the application"
}), 8),
ui.Margin(ui.BuildButton("Can't quit", func(b *ui.Button) {
b.ButtonClicked().AddHandler(func(ui.Context, ui.ControlClickedArgs) {
ctx.Quit()
})
b.Tooltip = "Will not quit the application"
b.Disabled = true
}), 8),
ui.BuildLabel("Status...", func(l *ui.Label) {
l.Background = style.Palette.PrimaryDark
l.Font.Color = style.Palette.TextOnPrimary
}),
}
return nil
}
func run() error {
var render, err = ui.NewRendererDefault("Basic Example", 800, 600)
if err != nil {
return err
}
defer render.Destroy()
return ui.RunWait(render, ui.DefaultStyle(), &basic{}, true)
}
func main() {
var err = run()
if err != nil {
log.Fatal(err)
}
}