2021-01-15 04:57:01 -05:00
|
|
|
package d2ui
|
|
|
|
|
2021-01-15 07:04:46 -05:00
|
|
|
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2021-01-15 04:57:01 -05:00
|
|
|
|
2021-01-15 07:04:46 -05:00
|
|
|
// static check if SwitchableButton implemented widget
|
|
|
|
var _ Widget = &SwitchableButton{}
|
|
|
|
|
|
|
|
// SwitchableButton represents switchable button widget
|
2021-01-15 04:57:01 -05:00
|
|
|
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 04:57:01 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:04:46 -05:00
|
|
|
// NewSwitchableButton creates new switchable button
|
2021-01-15 04:57:01 -05:00
|
|
|
func (ui *UIManager) NewSwitchableButton(active, inactive *Button, state bool) *SwitchableButton {
|
|
|
|
base := NewBaseWidget(ui)
|
|
|
|
|
|
|
|
sbtn := &SwitchableButton{
|
|
|
|
BaseWidget: base,
|
|
|
|
active: active,
|
|
|
|
inactive: inactive,
|
|
|
|
state: state,
|
|
|
|
}
|
2021-01-16 15:09:58 -05:00
|
|
|
sbtn.bindManager(ui)
|
|
|
|
sbtn.SetVisible(false)
|
2021-01-15 04:57:01 -05:00
|
|
|
|
|
|
|
sbtn.OnActivated(func() {})
|
2021-01-16 16:04:05 -05:00
|
|
|
sbtn.OnDeactivated(func() {})
|
2021-01-15 04:57:01 -05:00
|
|
|
|
2021-01-16 15:09:58 -05:00
|
|
|
ui.addWidget(sbtn)
|
|
|
|
|
2021-01-15 04:57:01 -05:00
|
|
|
return sbtn
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:04:46 -05:00
|
|
|
// SetVisible sets widget's visibility
|
2021-01-15 04:57:01 -05:00
|
|
|
func (sbtn *SwitchableButton) SetVisible(visible bool) {
|
|
|
|
if !visible {
|
|
|
|
sbtn.active.SetVisible(false)
|
|
|
|
sbtn.inactive.SetVisible(false)
|
2021-01-15 07:04:46 -05:00
|
|
|
|
2021-01-15 04:57:01 -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
|
2021-01-15 04:57:01 -05:00
|
|
|
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
|
2021-01-15 04:57:01 -05:00
|
|
|
func (sbtn *SwitchableButton) Activate() {
|
|
|
|
sbtn.onActivate()
|
|
|
|
}
|
|
|
|
|
2021-01-16 16:04:05 -05:00
|
|
|
// OnDeactivated sets onDeactivate callback
|
|
|
|
func (sbtn *SwitchableButton) OnDeactivated(cb func()) {
|
2021-01-15 04:57:01 -05:00
|
|
|
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 04:57:01 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:04:46 -05:00
|
|
|
// SetPosition sets widget's position
|
2021-01-15 04:57:01 -05:00
|
|
|
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
|
2021-01-16 15:09:58 -05:00
|
|
|
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
|
|
|
}
|