1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-17 01:51:14 -05:00
OpenDiablo2/d2core/d2ui/switchable_button.go

108 lines
2.3 KiB
Go
Raw Normal View History

package d2ui
2021-01-15 07:04:46 -05:00
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2021-01-15 07:04:46 -05:00
// static check if SwitchableButton implemented widget
var _ Widget = &SwitchableButton{}
// SwitchableButton represents switchable button widget
type SwitchableButton struct {
*BaseWidget
2021-01-16 16:04:05 -05:00
active *Button
inactive *Button
onActivate func()
onDeactivate func()
state bool
}
2021-01-15 07:04:46 -05:00
// NewSwitchableButton creates new switchable button
func (ui *UIManager) NewSwitchableButton(active, inactive *Button, state bool) *SwitchableButton {
base := NewBaseWidget(ui)
sbtn := &SwitchableButton{
BaseWidget: base,
active: active,
inactive: inactive,
state: state,
}
sbtn.bindManager(ui)
sbtn.SetVisible(false)
sbtn.OnActivated(func() {})
2021-01-16 16:04:05 -05:00
sbtn.OnDeactivated(func() {})
ui.addWidget(sbtn)
return sbtn
}
2021-01-15 07:04:46 -05:00
// SetVisible sets widget's visibility
func (sbtn *SwitchableButton) SetVisible(visible bool) {
if !visible {
sbtn.active.SetVisible(false)
sbtn.inactive.SetVisible(false)
2021-01-15 07:04:46 -05:00
return
}
if sbtn.state {
sbtn.active.SetVisible(true)
sbtn.inactive.SetVisible(false)
} else {
sbtn.active.SetVisible(false)
sbtn.inactive.SetVisible(true)
}
}
2021-01-15 07:04:46 -05:00
// OnActivated sets onActivate callback
func (sbtn *SwitchableButton) OnActivated(cb func()) {
sbtn.active.OnActivated(func() {
cb()
sbtn.state = false
sbtn.SetVisible(sbtn.GetVisible())
})
}
2021-01-15 07:04:46 -05:00
// Activate switches widget into active state
func (sbtn *SwitchableButton) Activate() {
sbtn.onActivate()
}
2021-01-16 16:04:05 -05:00
// OnDeactivated sets onDeactivate callback
func (sbtn *SwitchableButton) OnDeactivated(cb func()) {
sbtn.inactive.OnActivated(func() {
cb()
sbtn.state = true
sbtn.SetVisible(sbtn.GetVisible())
})
}
2021-01-16 16:04:05 -05:00
// Deactivate switch widget to inactive state
func (sbtn *SwitchableButton) Deactivate() {
sbtn.onDeactivate()
}
2021-01-15 07:04:46 -05:00
// SetPosition sets widget's position
func (sbtn *SwitchableButton) SetPosition(x, y int) {
sbtn.BaseWidget.SetPosition(x, y)
sbtn.active.SetPosition(x, y)
sbtn.inactive.SetPosition(x, y)
}
2021-01-15 07:04:46 -05:00
// Advance advances widget
func (sbtn *SwitchableButton) Advance(_ float64) error {
// noop
return nil
}
// Render renders widget
func (sbtn *SwitchableButton) Render(target d2interface.Surface) {
if sbtn.active.GetVisible() {
sbtn.active.Render(target)
}
if sbtn.inactive.GetVisible() {
sbtn.inactive.Render(target)
}
2021-01-15 07:04:46 -05:00
}