Sander Schobers
9641719579
Refactored event handling to be able to "handle" events so no other controls will handle the same event again.
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package tins2020
|
|
|
|
type HoverEffect int
|
|
|
|
const (
|
|
HoverEffectLigthen HoverEffect = iota
|
|
HoverEffectColor
|
|
)
|
|
|
|
type IconButton struct {
|
|
ControlBase
|
|
|
|
Icon string
|
|
IconDisabled string
|
|
IconHeight int32
|
|
IconScale Scale
|
|
IconWidth int32
|
|
|
|
IconActive HoverEffect
|
|
IconHover HoverEffect
|
|
|
|
IsActive bool
|
|
IsDisabled bool
|
|
}
|
|
|
|
func NewIconButton(icon string, onClick EventContextFn) *IconButton {
|
|
return &IconButton{
|
|
ControlBase: ControlBase{
|
|
OnLeftMouseButtonClick: onClick,
|
|
},
|
|
Icon: icon,
|
|
}
|
|
}
|
|
|
|
func NewIconButtonConfig(icon string, onClick EventContextFn, configure func(*IconButton)) *IconButton {
|
|
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)
|
|
|
|
hover := b.IsMouseOver && !b.IsDisabled
|
|
if (hover && b.IconHover == HoverEffectColor) || (b.IsActive && b.IconActive == HoverEffectColor) {
|
|
iconTexture.SetColor(MustHexColor("#15569F"))
|
|
}
|
|
|
|
if b.IconScale == ScaleCenter {
|
|
size := iconTexture.Size()
|
|
if b.IconWidth != 0 {
|
|
size = Pt(b.IconWidth, b.IconWidth*size.Y/size.X)
|
|
} else if b.IconHeight != 0 {
|
|
size = Pt(b.IconHeight*size.X/size.Y, b.IconHeight)
|
|
}
|
|
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 (hover && b.IconHover == HoverEffectLigthen) || (b.IsActive && b.IconActive == HoverEffectLigthen) {
|
|
SetDrawColor(ctx.Renderer, TransparentWhite)
|
|
ctx.Renderer.FillRect(b.Bounds.SDLPtr())
|
|
}
|
|
iconTexture.SetColor(White)
|
|
}
|
|
|
|
type Scale int
|
|
|
|
const (
|
|
ScaleCenter Scale = iota
|
|
ScaleStretch
|
|
)
|