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
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 {

View File

@ -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() {

View File

@ -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()
}
}