From 65aa5f53d54b4590b98a559fccfb1cc7c7644187 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 17 Feb 2023 00:29:26 +0100 Subject: implement unit and artifact switch action Simplify the Choice Widget and make it a TextBox variant. Drive-by fixes: * ui.Collection: prioritize newer widgets during active widget selection * game.FullAction: fix incorrect cost function. Full actions cost all available actions. Also fix the cost check --- go/ui/choice.go | 62 ++++++++++++++++++++--------------------------------- go/ui/collection.go | 5 ++++- 2 files changed, 27 insertions(+), 40 deletions(-) (limited to 'go/ui') diff --git a/go/ui/choice.go b/go/ui/choice.go index 28878604..04b8f725 100644 --- a/go/ui/choice.go +++ b/go/ui/choice.go @@ -9,66 +9,50 @@ import ( ) type Choice struct { - X, Y int - Width, choiceHeight int - choices []string - onClick func(*Choice, int, int) - buttons []Button + TextBox + choices []interface{} + onClick func(*Choice, int, int) } func choiceButtonOnClickDummy(b *SimpleButton) {} -func NewChoice(x, y int, width, choiceHeight int, choices []string, - onClick func(*Choice, int, int), -) *Choice { - c := &Choice{x, y, width, choiceHeight, choices, onClick, []Button{}} - - for i, choice := range c.choices { - b := NewSimpleButton(c.X, c.Y+i*c.choiceHeight, c.Width, c.choiceHeight, choice, - choiceButtonOnClickDummy) +func NewChoice(x, y int, choices []interface{}, onClick func(*Choice, int, int)) *Choice { + c := &Choice{ + TextBox: *(NewAutoTextBox(x, y, "")), + choices: choices, + onClick: onClick, + } - c.buttons = append(c.buttons, b) + c.renderImpl = func() *ebiten.Image { + c.setText() + return c.render() } return c } -func (c *Choice) Draw(screen *ebiten.Image) { - for _, b := range c.buttons { - b.Draw(screen) +func (c *Choice) setText() { + c.Width, c.Height = -1, -1 + t := "" + for _, choice := range c.choices { + t = fmt.Sprintf("%s%v\n", t, choice) } + + c.text = t[:len(t)-1] } func (c *Choice) GetChoosen(x, y int) int { - return (y - c.Y) / c.choiceHeight + i := (y - c.Y) / (c.Height / len(c.choices)) + return i } func (c *Choice) Click(x, y int) { c.onClick(c, x, y) } -func (c *Choice) Contains(x, y int) bool { - height := len(c.choices) * c.choiceHeight - return x >= c.X && x <= c.X+c.Width && y >= c.Y && y <= c.Y+height -} - -func (c *Choice) FindObjectAt(x, y int) interface{} { return nil } -func (c *Choice) Render() *ebiten.Image { return nil } - -func NewObjectChoice(x, y int, objs []interface{}, onClick func(*Choice, int, int)) *Choice { - // TODO: use dynamic with width and height - width := 200 - choiceHeight := 40 - labels := make([]string, 0, len(objs)) - for _, obj := range objs { - labels = append(labels, fmt.Sprintf("%v", obj)) - } - return NewChoice(x, y, width, choiceHeight, labels, onClick) -} - func NewActionChoice(x, y int, actions []game.Action, onClick func(*Choice, int, int)) *Choice { - return NewObjectChoice(x, y, utils.TypedSliceToInterfaceSlice(actions), onClick) + return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(actions), onClick) } func NewPermChoice(x, y int, perms []game.Permanent, onClick func(*Choice, int, int)) *Choice { - return NewObjectChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick) + return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick) } diff --git a/go/ui/collection.go b/go/ui/collection.go index d75e3267..027d0d99 100644 --- a/go/ui/collection.go +++ b/go/ui/collection.go @@ -69,7 +69,10 @@ func (c *Collection) Update() error { x, y := ebiten.CursorPosition() var activeWidget Widget - for _, w := range c.widgets { + // Iterate the widget in reverse order to ensure that upper widgets are + // considered first + for i := len(c.widgets) - 1; i >= 0; i-- { + w := c.widgets[i] if !w.Contains(x, y) { continue } -- cgit v1.2.3