2020-05-10 15:16:18 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
type IconButton struct {
|
|
|
|
ControlBase
|
|
|
|
|
|
|
|
Icon string
|
|
|
|
IconDisabled string
|
|
|
|
IconScale Scale
|
|
|
|
IconWidth int32
|
|
|
|
|
|
|
|
IsDisabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Scale int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ScaleCenter Scale = iota
|
|
|
|
ScaleStretch
|
|
|
|
)
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func NewIconButton(icon string, onClick EventContextFn) *IconButton {
|
2020-05-10 15:16:18 +00:00
|
|
|
return &IconButton{
|
|
|
|
ControlBase: ControlBase{
|
|
|
|
OnLeftMouseButtonClick: onClick,
|
|
|
|
},
|
|
|
|
Icon: icon,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:44:20 +00:00
|
|
|
func NewIconButtonConfig(icon string, onClick EventContextFn, configure func(*IconButton)) *IconButton {
|
2020-05-10 15:16:18 +00:00
|
|
|
button := NewIconButton(icon, onClick)
|
|
|
|
configure(button)
|
|
|
|
return button
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *IconButton) activeTexture(ctx *Context) *Texture {
|
|
|
|
if b.IsDisabled {
|
|
|
|
texture := ctx.Textures.Texture(b.IconDisabled)
|
|
|
|
if texture != nil {
|
|
|
|
return texture
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.Textures.Texture(b.Icon)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *IconButton) Render(ctx *Context) {
|
|
|
|
iconTexture := b.activeTexture(ctx)
|
|
|
|
mouseOverTexture := ctx.Textures.Texture("control-hover")
|
|
|
|
|
|
|
|
if b.IconScale == ScaleCenter {
|
|
|
|
size := iconTexture.Size()
|
|
|
|
if b.IconWidth != 0 {
|
|
|
|
size = Pt(b.IconWidth, b.IconWidth*size.Y/size.X)
|
|
|
|
}
|
|
|
|
iconTexture.CopyResize(ctx.Renderer, RectSize(b.Bounds.X+(b.Bounds.W-size.X)/2, b.Bounds.Y+(b.Bounds.H-size.Y)/2, size.X, size.Y))
|
|
|
|
} else {
|
|
|
|
iconTexture.CopyResize(ctx.Renderer, b.Bounds)
|
|
|
|
}
|
|
|
|
if b.IsMouseOver && !b.IsDisabled {
|
|
|
|
mouseOverTexture.CopyResize(ctx.Renderer, b.Bounds)
|
|
|
|
}
|
|
|
|
}
|