Favoring Context.Textures() over direct texture assignment to controls.

This commit is contained in:
Sander Schobers 2020-05-15 14:44:55 +02:00
parent b28b3e1838
commit 02ee819a99
3 changed files with 40 additions and 33 deletions

View File

@ -10,7 +10,7 @@ type Button struct {
ControlBase ControlBase
HoverColor color.Color HoverColor color.Color
Icon Texture Icon string
Text string Text string
Type ButtonType Type ButtonType
} }
@ -24,9 +24,9 @@ const (
ButtonTypeText 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} var b = &Button{Text: text, Icon: icon}
if fn != nil { if fn != nil {
fn(b) fn(b)
@ -41,8 +41,9 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 {
if len(b.Text) != 0 { if len(b.Text) != 0 {
w += pad + font.WidthOf(b.Text) w += pad + font.WidthOf(b.Text)
} }
if b.Icon != nil && b.Icon.Height() > 0 { icon := b.icon(ctx)
iconW := b.Icon.Width() * h / b.Icon.Height() if icon != nil && icon.Height() > 0 {
iconW := icon.Width() * h / icon.Height()
w += pad + iconW w += pad + iconW
} }
if w == 0 { if w == 0 {
@ -51,6 +52,13 @@ func (b *Button) desiredSize(ctx Context) geom.PointF32 {
return geom.PtF32(w+pad, pad+h+pad) 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 { func (b *Button) DesiredSize(ctx Context) geom.PointF32 {
return b.desiredSize(ctx) return b.desiredSize(ctx)
} }
@ -125,13 +133,14 @@ func (b *Button) Render(ctx Context) {
bounds = bounds.Inset(pad) bounds = bounds.Inset(pad)
boundsH := bounds.Dy() boundsH := bounds.Dy()
pos := bounds.Min pos := bounds.Min
if b.Icon != nil && b.Icon.Height() > 0 { icon := b.icon(ctx)
icon, _ := ctx.Textures().ScaledHeight(b.Icon, boundsH) // try to pre-scale icon if icon != nil && icon.Height() > 0 {
if icon == nil { // let the renderer scale scaled, _ := ctx.Textures().ScaledHeight(icon, boundsH) // try to pre-scale scaled
icon = b.Icon if scaled == nil { // let the renderer scale
scaled = icon
} }
scale, iconWidth := ScaleToHeight(SizeOfTexture(icon), boundsH) scale, iconWidth := ScaleToHeight(SizeOfTexture(scaled), boundsH)
ctx.Renderer().DrawTextureOptions(icon, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: textColor, Scale: scale}) ctx.Renderer().DrawTextureOptions(scaled, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: textColor, Scale: scale})
pos.X += iconWidth + pad pos.X += iconWidth + pad
} }
if len(b.Text) != 0 { if len(b.Text) != 0 {

View File

@ -13,11 +13,14 @@ import (
type basic struct { type basic struct {
ui.StackPanel ui.StackPanel
plus ui.Texture
} }
func (b *basic) Init(ctx ui.Context) error { 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 style = ctx.Style()
var stretch = func(content ui.Control, margin float32) ui.Control { var stretch = func(content ui.Control, margin float32) ui.Control {
return ui.BuildSpacing(content, func(s *ui.Spacing) { 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.Label{Text: "Hello, world!"},
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) { ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
p.Children = []ui.Control{ p.Children = []ui.Control{
stretch(ui.BuildIconButton(b.plus, "Contained", func(b *ui.Button) { b.Type = ui.ButtonTypeContained }), 8), stretch(ui.BuildIconButton("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("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("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", "Text", func(b *ui.Button) { b.Type = ui.ButtonTypeText }), 8),
} }
}), }),
ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) { ui.BuildStackPanel(ui.OrientationHorizontal, func(p *ui.StackPanel) {
@ -74,13 +77,8 @@ func run() error {
if err != nil { if err != nil {
return err 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() { func main() {

View File

@ -78,8 +78,8 @@ func (t *Textures) Destroy() {
texture.Destroy() texture.Destroy()
} }
t.textures = nil t.textures = nil
for _, textures := range t.scaled { for _, texture := range t.scaled {
textures.Destroy() texture.Destroy()
} }
t.scaled = nil t.scaled = nil
} }
@ -114,26 +114,26 @@ func (t *Textures) Scaled(texture Texture, scale float32) Texture {
return scaled return scaled
} }
func (t *Textures) ScaledHeight(textures Texture, height float32) (Texture, float32) { func (t *Textures) ScaledHeight(texture Texture, height float32) (Texture, float32) {
scale := height / textures.Height() scale := height / texture.Height()
if geom.IsNaN32(scale) { if geom.IsNaN32(scale) {
return nil, 0 return nil, 0
} }
return t.Scaled(textures, scale), scale return t.Scaled(texture, scale), scale
} }
func (t *Textures) ScaledByName(name string, scale float32) Texture { func (t *Textures) ScaledByName(name string, scale float32) Texture {
textures := t.Texture(name) texture := t.Texture(name)
if textures == nil { if texture == nil {
return nil return nil
} }
return t.Scaled(textures, scale) return t.Scaled(texture, scale)
} }
type ScaledTextures map[float32]Texture type ScaledTextures map[float32]Texture
func (t ScaledTextures) Destroy() { func (t ScaledTextures) Destroy() {
for _, textures := range t { for _, texture := range t {
textures.Destroy() texture.Destroy()
} }
} }