From 02ee819a99eb50bb6405127d027cb486782c9040 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Fri, 15 May 2020 14:44:55 +0200 Subject: [PATCH] Favoring Context.Textures() over direct texture assignment to controls. --- ui/button.go | 31 ++++++++++++++++++++----------- ui/examples/01_basic/basic.go | 22 ++++++++++------------ ui/textures.go | 20 ++++++++++---------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/ui/button.go b/ui/button.go index d340ee3..83eb956 100644 --- a/ui/button.go +++ b/ui/button.go @@ -10,7 +10,7 @@ type Button struct { ControlBase HoverColor color.Color - Icon Texture + Icon string Text string Type ButtonType } @@ -24,9 +24,9 @@ const ( ButtonTypeText ) -func BuildButton(text string, fn func(b *Button)) *Button { return BuildIconButton(nil, text, fn) } +func BuildButton(text string, fn func(b *Button)) *Button { return BuildIconButton("", text, fn) } -func BuildIconButton(icon Texture, text string, fn func(b *Button)) *Button { +func BuildIconButton(icon, text string, fn func(b *Button)) *Button { var b = &Button{Text: text, Icon: icon} if fn != nil { fn(b) @@ -41,8 +41,9 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 { if len(b.Text) != 0 { w += pad + font.WidthOf(b.Text) } - if b.Icon != nil && b.Icon.Height() > 0 { - iconW := b.Icon.Width() * h / b.Icon.Height() + icon := b.icon(ctx) + if icon != nil && icon.Height() > 0 { + iconW := icon.Width() * h / icon.Height() w += pad + iconW } if w == 0 { @@ -51,6 +52,13 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 { return geom.PtF32(w+pad, pad+h+pad) } +func (b *Button) icon(ctx Context) Texture { + if b.Icon == "" { + return nil + } + return ctx.Textures().Texture(b.Icon) +} + func (b *Button) DesiredSize(ctx Context) geom.PointF32 { return b.desiredSize(ctx) } @@ -125,13 +133,14 @@ func (b *Button) Render(ctx Context) { bounds = bounds.Inset(pad) boundsH := bounds.Dy() pos := bounds.Min - if b.Icon != nil && b.Icon.Height() > 0 { - icon, _ := ctx.Textures().ScaledHeight(b.Icon, boundsH) // try to pre-scale icon - if icon == nil { // let the renderer scale - icon = b.Icon + icon := b.icon(ctx) + if icon != nil && icon.Height() > 0 { + scaled, _ := ctx.Textures().ScaledHeight(icon, boundsH) // try to pre-scale scaled + if scaled == nil { // let the renderer scale + scaled = icon } - scale, iconWidth := ScaleToHeight(SizeOfTexture(icon), boundsH) - ctx.Renderer().DrawTextureOptions(icon, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: textColor, Scale: scale}) + scale, iconWidth := ScaleToHeight(SizeOfTexture(scaled), boundsH) + ctx.Renderer().DrawTextureOptions(scaled, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: textColor, Scale: scale}) pos.X += iconWidth + pad } if len(b.Text) != 0 { diff --git a/ui/examples/01_basic/basic.go b/ui/examples/01_basic/basic.go index d9e1fde..d2549bb 100644 --- a/ui/examples/01_basic/basic.go +++ b/ui/examples/01_basic/basic.go @@ -13,11 +13,14 @@ import ( type basic struct { ui.StackPanel - - plus ui.Texture } func (b *basic) Init(ctx ui.Context) error { + _, 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) { @@ -34,10 +37,10 @@ func (b *basic) Init(ctx ui.Context) error { &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), + 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) { @@ -74,13 +77,8 @@ func run() error { 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) + return ui.RunWait(render, ui.DefaultStyle(), &basic{}, true) } func main() { diff --git a/ui/textures.go b/ui/textures.go index 8897fce..f19f02f 100644 --- a/ui/textures.go +++ b/ui/textures.go @@ -78,8 +78,8 @@ func (t *Textures) Destroy() { texture.Destroy() } t.textures = nil - for _, textures := range t.scaled { - textures.Destroy() + for _, texture := range t.scaled { + texture.Destroy() } t.scaled = nil } @@ -114,26 +114,26 @@ func (t *Textures) Scaled(texture Texture, scale float32) Texture { return scaled } -func (t *Textures) ScaledHeight(textures Texture, height float32) (Texture, float32) { - scale := height / textures.Height() +func (t *Textures) ScaledHeight(texture Texture, height float32) (Texture, float32) { + scale := height / texture.Height() if geom.IsNaN32(scale) { return nil, 0 } - return t.Scaled(textures, scale), scale + return t.Scaled(texture, scale), scale } func (t *Textures) ScaledByName(name string, scale float32) Texture { - textures := t.Texture(name) - if textures == nil { + texture := t.Texture(name) + if texture == nil { return nil } - return t.Scaled(textures, scale) + return t.Scaled(texture, scale) } type ScaledTextures map[float32]Texture func (t ScaledTextures) Destroy() { - for _, textures := range t { - textures.Destroy() + for _, texture := range t { + texture.Destroy() } }