wip: ship calculator
This commit is contained in:
+14
-9
@@ -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
|
||||
```
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user