diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2024-12-26 19:05:45 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:57 +0100 |
| commit | bc06cd09407de831ac30f0300cb4135cbe325120 (patch) | |
| tree | 53d5eb0f18eb2f889c6d35021ad85a23e7600514 /go | |
| parent | 5684983b963eeb2f498028360ebc17cc2fb32564 (diff) | |
| download | muhqs-game-bc06cd09407de831ac30f0300cb4135cbe325120.tar.gz muhqs-game-bc06cd09407de831ac30f0300cb4135cbe325120.zip | |
ui: add new number choice
Diffstat (limited to 'go')
| -rw-r--r-- | go/ui/choice.go | 65 | ||||
| -rw-r--r-- | go/ui/textBox.go | 47 |
2 files changed, 101 insertions, 11 deletions
diff --git a/go/ui/choice.go b/go/ui/choice.go index 28f03f72..9b229cb6 100644 --- a/go/ui/choice.go +++ b/go/ui/choice.go @@ -54,3 +54,68 @@ func NewActionChoice(x, y int, actions []game.Action, onClick func(*Choice, int, func NewPermChoice(x, y int, perms []game.Permanent, onClick func(*Choice, int, int)) *Choice { return NewChoice(x, y, utils.TypedSliceToInterfaceSlice(perms), onClick) } + +const ( + NUMBER_CHOICE_BUTTON_WIDTH int = 40 + NUMBER_CHOICE_INPUT_WIDTH int = 40 + NUMBER_CHOICE_SUBMIT_WIDTH int = 100 + NUMBER_CHOICE_HEIGHT int = 40 + NUMBER_CHOICE_WIDTH = 2*NUMBER_CHOICE_BUTTON_WIDTH + NUMBER_CHOICE_INPUT_WIDTH + NUMBER_CHOICE_SUBMIT_WIDTH +) + +type NumberChoice struct { + Clickable + WidgetBase + numberInput *NumberInput + buttons [3]Button + AllowNegativ bool +} + +func NewNumberChoice(x, y int, startValue int, onSubmit func(*NumberChoice)) *NumberChoice { + ni := NewNumberInput(x+NUMBER_CHOICE_BUTTON_WIDTH, y, NUMBER_CHOICE_INPUT_WIDTH, NUMBER_CHOICE_HEIGHT, startValue) + var nc *NumberChoice + nc = &NumberChoice{ + WidgetBase: NewWidgetBase(x, y, NUMBER_CHOICE_WIDTH, NUMBER_CHOICE_HEIGHT), + numberInput: ni, + buttons: [3]Button{ + NewSimpleButton( + x, y, + NUMBER_CHOICE_BUTTON_WIDTH, NUMBER_CHOICE_HEIGHT, + "+1", + func(*SimpleButton) { ni.Add(1) }), + NewSimpleButton( + x+NUMBER_CHOICE_BUTTON_WIDTH+NUMBER_CHOICE_INPUT_WIDTH, y, + NUMBER_CHOICE_BUTTON_WIDTH, NUMBER_CHOICE_HEIGHT, + "-1", + func(*SimpleButton) { if ni.Number() > 0 || nc.AllowNegativ { ni.Add(-1) }}), + NewSimpleButton( + x+2*NUMBER_CHOICE_BUTTON_WIDTH+NUMBER_CHOICE_INPUT_WIDTH, y, + NUMBER_CHOICE_SUBMIT_WIDTH, NUMBER_CHOICE_HEIGHT, + "submit", + func(*SimpleButton) { onSubmit(nc) }), + }, + AllowNegativ: false, + } + + return nc +} + +func (nc *NumberChoice) Click(x, y int) { + for _, b := range nc.buttons { + if b.Contains(x, y) { + b.Click(x, y) + return + } + } +} + +func (nc *NumberChoice) Draw(screen *ebiten.Image) { + for _, b := range nc.buttons { + b.Draw(screen) + } + nc.numberInput.Draw(screen) +} + +func (nc *NumberChoice) GetChoosen() int { + return nc.numberInput.Number() +} diff --git a/go/ui/textBox.go b/go/ui/textBox.go index 66b90698..4fc2b533 100644 --- a/go/ui/textBox.go +++ b/go/ui/textBox.go @@ -3,6 +3,7 @@ package ui import ( "fmt" "image/color" + "strconv" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" @@ -193,23 +194,25 @@ type TextInput struct { label string } -func NewTextInput(x, y int, width, height int, label string) *TextInput { - w := &TextInput{ - TextBox: *(NewFixedTextBox(x, y, width, height, "").Centering(true)), - input: "", - label: label, - } +func (ti *TextInput) InitTextInput(x, y int, width, height int, label string) { + ti.TextBox = *(NewFixedTextBox(x, y, width, height, "").Centering(true)) + ti.input = "" + ti.label = label - w.renderImpl = func() *ebiten.Image { - if w.input == "" { - w.text = w.label + ti.renderImpl = func() *ebiten.Image { + if ti.input == "" { + ti.text = ti.label } else { - w.text = w.input + ti.text = ti.input } - return w.render() + return ti.render() } +} +func NewTextInput(x, y int, width, height int, label string) *TextInput { + w := &TextInput{} + w.InitTextInput(x, y, width, height, label) return w } @@ -234,3 +237,25 @@ func (ti *TextInput) HandleKey() { func (ti *TextInput) Text() string { return ti.input } + +type NumberInput struct { + TextInput + n int +} + +func NewNumberInput(x, y int, height, width int, number int) *NumberInput { + ni := &NumberInput{ + n: 0, + } + ni.InitTextInput(x, y, width, height, strconv.Itoa(number)) + return ni +} + +func (ni *NumberInput) Add(x int) { + ni.n = ni.n + x + ni.SetInput(strconv.Itoa(ni.n)) +} + +func (ni *NumberInput) Number() int { + return ni.n +} |
