zntg/ui/examples/01_basic/basic.go
Sander Schobers cdfb863ab0 Added SDL backend.
Added Action{,s}. List of actions that can be used to defer cleanup code (see NewRenderer implementations).
Added TextInputEvent (replaces the old KeyPressEvent) and added to new events KeyDown & KeyUp.
Added VSync to NewRendererOptions.
Removed IconScale from button.
Added ImageSource interface that replaces the Image/Texture method on the Texture interface. This makes converting back a texture to an image optional (since this is atypical for a hardware texture for instance).
Added new KeyModifier: OSCommand (Windows/Command key).
Added KeyState that can keep the state of keys (pressed or not).
Added KeyEnter, representing the Enter key.
Changed signatures of CreateTexture methods in Renderer.
Changed signatures of icon related method (removed factories).
Basic example now depends on sdlgui.
2020-05-15 09:20:44 +02:00

92 lines
2.4 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/geom"
"opslag.de/schobers/zntg/ui"
)
type basic struct {
ui.StackPanel
plus ui.Texture
}
func (b *basic) Init(ctx ui.Context) error {
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(b.plus, "Contained", func(b *ui.Button) { b.Type = ui.ButtonTypeContained }), 8),
stretch(ui.BuildIconButton(b.plus, "Icon", func(b *ui.Button) { b.Type = ui.ButtonTypeIcon }), 8),
stretch(ui.BuildIconButton(b.plus, "Outlined", func(b *ui.Button) { b.Type = ui.ButtonTypeOutlined }), 8),
stretch(ui.BuildIconButton(b.plus, "Text", func(b *ui.Button) { b.Type = ui.ButtonTypeText }), 8),
}
}),
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{
&ui.Checkbox{},
ui.BuildCheckbox("Check me!", nil),
}
}),
ui.Stretch(&ui.Label{Text: "Content"}),
ui.Margin(ui.StretchWidth(ui.BuildTextBox(func(b *ui.TextBox) {
b.Text = "Type here..."
})), 8),
ui.Margin(ui.BuildButton("Quit", func(b *ui.Button) {
b.OnClick(func(geom.PointF32, ui.MouseButton) {
ctx.Quit()
})
}), 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.NewRenderer("Basic Example", 800, 600)
if err != nil {
return err
}
defer render.Destroy()
err = render.RegisterFont("default", "../resources/font/OpenSans-Regular.ttf", 14)
if err != nil {
return err
}
plus, err := render.CreateTexturePath("../resources/images/plus.png", true)
if err != nil {
return err
}
defer plus.Destroy()
return ui.RunWait(render, ui.DefaultStyle(), &basic{plus: plus}, true)
}
func main() {
var err = run()
if err != nil {
log.Fatal(err)
}
}