From 17f366cd6b057de579d38838015a4c33717b6d2a Mon Sep 17 00:00:00 2001 From: Ilia Denisov Date: Tue, 24 Mar 2026 11:33:52 +0200 Subject: [PATCH] wip: ship calculator --- client/README.md | 23 +++++++----- client/widget/numeric/numeric.go | 42 +++++++++++++++++++++ client/widget/validator/validator.go | 55 ++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 client/widget/numeric/numeric.go create mode 100644 client/widget/validator/validator.go diff --git a/client/README.md b/client/README.md index 1eafd42..6514722 100644 --- a/client/README.md +++ b/client/README.md @@ -3,15 +3,20 @@ ## Ship Calculator ```text + Class: [ ] { Create } + Drives: [20.000] x 1.013 [O--] + Weapons: [ 0.000] x [1.000] [==0] +Armament: [ 0 ] +Schields: [ 5.500] @ [1.123] + Cargo: [30.125] @ [1.320] - Drives: [ ] @ [1.013] - Weapons: [ ] @ [1.000] -Armament: [ ] -Schields: [ ] @ [1.123] - Cargo: [ ] @ [1.320] + Mass: [ 123,45 ] [==0] + Speed: ( 12,456 ) [O--] + Attack: 0 + Defense: 100,0 - Mass: 123,45 - Speed: 12,456 l.y. - -Name: [ ] { Create } +Planet { Name } Production: +[ 100.0 ] MAT per turn produced [O--] supplied +{ N.000 } ship(s) per turn +{ M.000 } turn(s) per ship ``` diff --git a/client/widget/numeric/numeric.go b/client/widget/numeric/numeric.go new file mode 100644 index 0000000..00ef5f7 --- /dev/null +++ b/client/widget/numeric/numeric.go @@ -0,0 +1,42 @@ +package numeric + +import ( + "strconv" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/driver/mobile" + "fyne.io/fyne/v2/widget" +) + +type numericalEntry struct { + widget.Entry +} + +func NewNumericalEntry() *numericalEntry { + entry := &numericalEntry{} + entry.ExtendBaseWidget(entry) + return entry +} + +func (e *numericalEntry) TypedRune(r rune) { + if (r >= '0' && r <= '9') || r == '.' || r == ',' { + e.Entry.TypedRune(r) + } +} + +func (e *numericalEntry) TypedShortcut(shortcut fyne.Shortcut) { + paste, ok := shortcut.(*fyne.ShortcutPaste) + if !ok { + e.Entry.TypedShortcut(shortcut) + return + } + + content := paste.Clipboard.Content() + if _, err := strconv.ParseFloat(content, 64); err == nil { + e.Entry.TypedShortcut(shortcut) + } +} + +func (e *numericalEntry) Keyboard() mobile.KeyboardType { + return mobile.NumberKeyboard +} diff --git a/client/widget/validator/validator.go b/client/widget/validator/validator.go new file mode 100644 index 0000000..79a802e --- /dev/null +++ b/client/widget/validator/validator.go @@ -0,0 +1,55 @@ +package validator + +import ( + "errors" + "strconv" + + "fyne.io/fyne/v2" +) + +func NewStackValidator(first fyne.StringValidator, rest ...fyne.StringValidator) fyne.StringValidator { + if first == nil { + panic("first validator cannot be nil") + } + return func(s string) error { + if err := first(s); err != nil { + return err + } + for i := range rest { + if err := rest[i](s); err != nil { + return err + } + } + return nil + } +} + +func NewMutualValidator(other func() float64, valid func(float64) bool) fyne.StringValidator { + if other == nil { + panic("other value getter cannot be nil") + } + return func(s string) error { + myValue, err := ParseFloat(s) + if err != nil { + return err + } + if !valid(myValue) { + return errors.New("invalid value") + } + if !valid(other()) { + return errors.New("invalid other value") + } + return nil + } +} + +func FloatValueValidator(s string) error { + if _, err := ParseFloat(s); err != nil { + return errors.New("not a float value") + } + return nil +} + +func ParseFloat(s string) (float64, error) { + return strconv.ParseFloat(s, 64) +}