Allow dialogues to be hidden or shown
To do this, MENU and SUBMENU are split into two types (at last), and a Widget type is introduced. This should allow lots of code to be removed at some point.
This commit is contained in:
119
internal/ui/value.go
Normal file
119
internal/ui/value.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (d *Driver) realId(id string) string {
|
||||
return fmt.Sprintf("%v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
func (d *Driver) Value(id string, into *string) error {
|
||||
for _, valueable := range d.valueables() {
|
||||
if valueable.id() == d.realId(id) {
|
||||
*into = valueable.value()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Couldn't find valueable widget %v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
func (d *Driver) SetValue(id, value string) error {
|
||||
for _, valueable := range d.valueables() {
|
||||
if valueable.id() == d.realId(id) {
|
||||
valueable.setValue(value)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Couldn't find valueable widget %v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
func (d *Driver) ValueBool(id string, into *bool) error {
|
||||
var vStr string
|
||||
if err := d.Value(id, &vStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*into = vStr == "1"
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) SetValueBool(id string, value bool) error {
|
||||
vStr := "0"
|
||||
if value {
|
||||
vStr = "1"
|
||||
}
|
||||
|
||||
return d.SetValue(id, vStr)
|
||||
}
|
||||
|
||||
func (d *Driver) SetFreeze(id string, value bool) error {
|
||||
for _, freezable := range d.freezables() {
|
||||
if freezable.id() == d.realId(id) {
|
||||
freezable.setFreezeState(value)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Couldn't find clickable widget %v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
func (d *Driver) OnClick(id string, f func()) error {
|
||||
for _, clickable := range d.clickables() {
|
||||
if clickable.id() == d.realId(id) {
|
||||
clickable.onClick(f)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// We need to be able to wire up items inside dialogues too
|
||||
for _, dialogue := range d.dialogues {
|
||||
for _, clickable := range dialogue.clickables() {
|
||||
if clickable.id() == d.realId(id) {
|
||||
clickable.onClick(f)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Couldn't find clickable widget %v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
// FIXME: HURK. Surely I'm missing something? steps is value:offset
|
||||
func (d *Driver) ConfigureSlider(id string, steps map[int]int) error {
|
||||
|
||||
for _, clickable := range d.clickables() {
|
||||
if slider, ok := clickable.(*slider); ok && slider.id() == d.realId(id) {
|
||||
slider.steps = steps
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Couldn't find slider %v:%v", d.menu.Name, id)
|
||||
}
|
||||
|
||||
func (d *Driver) ValueInt(id string, into *int) error {
|
||||
var vStr string
|
||||
if err := d.Value(id, &vStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(vStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*into = value
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Driver) SetValueInt(id string, value int) error {
|
||||
vStr := strconv.Itoa(value)
|
||||
|
||||
return d.SetValue(id, vStr)
|
||||
}
|
Reference in New Issue
Block a user