Files
desktop_dice_statistics_cal…/main.go
T

177 lines
4.4 KiB
Go
Raw Normal View History

2026-02-09 12:54:44 -06:00
package main
import (
"fmt"
2026-02-10 08:28:16 -06:00
"strconv"
2026-02-09 12:54:44 -06:00
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
2026-02-10 12:51:58 -06:00
"fyne.io/fyne/v2/theme"
2026-02-09 12:54:44 -06:00
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Dice Statistics Calculator")
2026-02-10 11:03:04 -06:00
var calculations []*calculation
var historyList *widget.List
2026-02-09 12:54:44 -06:00
// Dice input bar
2026-02-10 12:51:58 -06:00
diceInputEntry := newCustomEntry(fyne.CurrentApp().Settings().Theme().Size(theme.SizeNameText) * 2)
2026-02-09 12:54:44 -06:00
diceInputEntry.SetPlaceHolder("e.g., 2d20H, 3d6+5")
2026-02-10 11:03:04 -06:00
historyList = widget.NewList(
func() int {
return len(calculations)
},
func() fyne.CanvasObject {
return newHistoryItemWithCallback(&calculation{}, func(equation string) {
diceInputEntry.SetText(equation)
})
2026-02-10 11:03:04 -06:00
},
func(i widget.ListItemID, o fyne.CanvasObject) {
o.(*historyItem).SetCalculation(calculations[i])
},
)
2026-02-10 12:51:58 -06:00
roll := func() {
2026-02-10 10:42:42 -06:00
diceInput := strings.TrimSpace(diceInputEntry.Text)
if diceInput == "" {
return
}
2026-02-10 11:03:04 -06:00
result, diceRolls, err := CalculateDice(diceInput)
2026-02-10 10:42:42 -06:00
if err != nil {
2026-02-10 11:03:04 -06:00
// TODO: show error to user
2026-02-10 10:42:42 -06:00
} else {
2026-02-10 11:03:04 -06:00
id := 0
if len(calculations) > 0 {
id = calculations[len(calculations)-1].id + 1
}
c := &calculation{
id: id,
equation: diceInput,
diceRolls: diceRolls,
result: fmt.Sprintf("= %s", strconv.FormatFloat(result, 'g', -1, 64)),
}
2026-02-10 12:08:15 -06:00
calculations = append([]*calculation{c}, calculations...)
2026-02-10 11:03:04 -06:00
historyList.Refresh()
2026-02-10 10:42:42 -06:00
}
2026-02-10 12:51:58 -06:00
}
// Roll button
rollButton := newCustomButton2WithImportance("ROLL", widget.HighImportance, roll)
diceInputEntry.OnSubmitted = func(s string) {
roll()
}
2026-02-10 10:42:42 -06:00
buttons := []fyne.CanvasObject{
2026-02-10 12:51:58 -06:00
newCustomButton2("H", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "H")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("dX", func() {
diceInputEntry.SetText(diceInputEntry.Text + "d")
2026-02-10 08:49:31 -06:00
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d4", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d4")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d6", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d6")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d8", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d8")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("L", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "L")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d10", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d10")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d12", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d12")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d20", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d20")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("d100", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "d100")
}),
createCalcButton("7", diceInputEntry),
createCalcButton("8", diceInputEntry),
createCalcButton("9", diceInputEntry),
2026-02-10 12:51:58 -06:00
newCustomButton2("*", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "*")
}),
newCustomButton2("BKSP", func() {
2026-02-10 08:49:31 -06:00
text := diceInputEntry.Text
if len(text) > 0 {
diceInputEntry.SetText(text[:len(text)-1])
}
}),
2026-02-10 08:21:03 -06:00
createCalcButton("4", diceInputEntry),
createCalcButton("5", diceInputEntry),
createCalcButton("6", diceInputEntry),
2026-02-10 12:51:58 -06:00
newCustomButton2("/", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "/")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("C", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText("")
2026-02-10 08:21:03 -06:00
}),
createCalcButton("1", diceInputEntry),
createCalcButton("2", diceInputEntry),
createCalcButton("3", diceInputEntry),
2026-02-10 12:51:58 -06:00
newCustomButton2("+", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "+")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("📊", func() {
diceInput := strings.TrimSpace(diceInputEntry.Text)
if diceInput == "" {
return
}
ShowStatisticsWindow(diceInput)
roll()
2026-02-10 08:49:31 -06:00
}),
2026-02-10 12:51:58 -06:00
newCustomButton2(".", func() {
2026-02-10 08:21:03 -06:00
diceInputEntry.SetText(diceInputEntry.Text + ".")
}),
2026-02-10 08:49:31 -06:00
createCalcButton("0", diceInputEntry),
2026-02-10 12:51:58 -06:00
newCustomButton2("^", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "^")
}),
2026-02-10 12:51:58 -06:00
newCustomButton2("-", func() {
2026-02-10 08:49:31 -06:00
diceInputEntry.SetText(diceInputEntry.Text + "-")
2026-02-10 08:21:03 -06:00
}),
rollButton,
2026-02-10 10:42:42 -06:00
}
2026-02-10 10:45:14 -06:00
buttonsContainer := container.New(newAspectRatioLayout(3.0/2.0, 7, 5))
2026-02-10 10:42:42 -06:00
for _, button := range buttons {
buttonsContainer.Add(button)
}
2026-02-09 12:54:44 -06:00
2026-02-10 11:03:04 -06:00
topContent := container.NewBorder(
diceInputEntry,
2026-02-10 08:21:03 -06:00
nil,
nil,
2026-02-10 11:03:04 -06:00
nil,
historyList,
2026-02-09 12:54:44 -06:00
)
2026-02-10 11:03:04 -06:00
split := container.NewVSplit(topContent, buttonsContainer)
split.Offset = 0.5 // Start with a 50/50 split
myWindow.SetContent(split)
2026-02-09 12:54:44 -06:00
myWindow.Resize(fyne.NewSize(400, 600))
myWindow.ShowAndRun()
}
2026-02-10 12:51:58 -06:00
func createCalcButton(label string, entry *customEntry) *customButton2 {
return newCustomButton2(label, func() {
2026-02-09 12:54:44 -06:00
entry.SetText(entry.Text + label)
})
}