2020-05-10 15:16:18 +00:00
|
|
|
package tins2020
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
type HoverEffect int
|
|
|
|
|
|
|
|
const (
|
|
|
|
HoverEffectLigthen HoverEffect = iota
|
|
|
|
HoverEffectColor
|
|
|
|
)
|
|
|
|
|
2020-05-10 15:16:18 +00:00
|
|
|
type IconButton struct {
|
|
|
|
ControlBase
|
|
|
|
|
|
|
|
Icon string
|
|
|
|
IconDisabled string
|
2020-05-10 21:00:19 +00:00
|
|
|
IconHeight int32
|
2020-05-10 15:16:18 +00:00
|
|
|
IconScale Scale
|
|
|
|
IconWidth int32
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
IconActive HoverEffect
|
|
|
|
IconHover HoverEffect
|
|
|
|
|
2020-05-11 12:41:42 +00:00
|
|
|
IsActive bool
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-05-11 12:41:42 +00:00
|
|
|
|
|
|
|
texture = ctx.Textures.Texture(b.Icon)
|
|
|
|
if len(b.IconDisabled) == 0 {
|
|
|
|
return texture
|
|
|
|
}
|
|
|
|
color, err := HexColor(b.IconDisabled)
|
|
|
|
if err == nil {
|
|
|
|
texture.SetColor(color)
|
|
|
|
}
|
|
|
|
return texture
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
return ctx.Textures.Texture(b.Icon)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *IconButton) Render(ctx *Context) {
|
|
|
|
iconTexture := b.activeTexture(ctx)
|
2020-05-11 09:44:50 +00:00
|
|
|
|
|
|
|
hover := b.IsMouseOver && !b.IsDisabled
|
|
|
|
if (hover && b.IconHover == HoverEffectColor) || (b.IsActive && b.IconActive == HoverEffectColor) {
|
|
|
|
iconTexture.SetColor(MustHexColor("#15569F"))
|
|
|
|
}
|
2020-05-10 15:16:18 +00:00
|
|
|
|
|
|
|
if b.IconScale == ScaleCenter {
|
|
|
|
size := iconTexture.Size()
|
|
|
|
if b.IconWidth != 0 {
|
|
|
|
size = Pt(b.IconWidth, b.IconWidth*size.Y/size.X)
|
2020-05-10 21:00:19 +00:00
|
|
|
} else if b.IconHeight != 0 {
|
|
|
|
size = Pt(b.IconHeight*size.X/size.Y, b.IconHeight)
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
if (hover && b.IconHover == HoverEffectLigthen) || (b.IsActive && b.IconActive == HoverEffectLigthen) {
|
|
|
|
SetDrawColor(ctx.Renderer, TransparentWhite)
|
|
|
|
ctx.Renderer.FillRect(b.Bounds.SDLPtr())
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
iconTexture.SetColor(White)
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
2020-05-11 09:44:50 +00:00
|
|
|
|
|
|
|
type Scale int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ScaleCenter Scale = iota
|
|
|
|
ScaleStretch
|
|
|
|
)
|