From 4575df572f436eb5870263da052d3a6258ad9907 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Fri, 15 Jan 2021 10:57:01 +0100 Subject: [PATCH] d2ui improvement: added switchable button widget to d2ui --- d2core/d2ui/switchable_button.go | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 d2core/d2ui/switchable_button.go diff --git a/d2core/d2ui/switchable_button.go b/d2core/d2ui/switchable_button.go new file mode 100644 index 00000000..0bda1904 --- /dev/null +++ b/d2core/d2ui/switchable_button.go @@ -0,0 +1,75 @@ +package d2ui + +var _ Widget = &Button{} + +type SwitchableButton struct { + *BaseWidget + active *Button + inactive *Button + onActivate func() + onDezactivate func() + state bool +} + +func (ui *UIManager) NewSwitchableButton(active, inactive *Button, state bool) *SwitchableButton { + base := NewBaseWidget(ui) + base.SetVisible(true) + + sbtn := &SwitchableButton{ + BaseWidget: base, + active: active, + inactive: inactive, + state: state, + } + + sbtn.OnActivated(func() {}) + sbtn.OnDezactivated(func() {}) + + return sbtn +} + +func (sbtn *SwitchableButton) SetVisible(visible bool) { + if !visible { + sbtn.active.SetVisible(false) + sbtn.inactive.SetVisible(false) + return + } + + if sbtn.state { + sbtn.active.SetVisible(true) + sbtn.inactive.SetVisible(false) + } else { + sbtn.active.SetVisible(false) + sbtn.inactive.SetVisible(true) + } +} + +func (sbtn *SwitchableButton) OnActivated(cb func()) { + sbtn.active.OnActivated(func() { + cb() + sbtn.state = false + sbtn.SetVisible(sbtn.GetVisible()) + }) +} + +func (sbtn *SwitchableButton) Activate() { + sbtn.onActivate() +} + +func (sbtn *SwitchableButton) OnDezactivated(cb func()) { + sbtn.inactive.OnActivated(func() { + cb() + sbtn.state = true + sbtn.SetVisible(sbtn.GetVisible()) + }) +} + +func (sbtn *SwitchableButton) Dezactivate() { + sbtn.onDezactivate() +} + +func (sbtn *SwitchableButton) SetPosition(x, y int) { + sbtn.BaseWidget.SetPosition(x, y) + sbtn.active.SetPosition(x, y) + sbtn.inactive.SetPosition(x, y) +}