scaled buttons to window size
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
)
|
||||
|
||||
type aspectRatioLayout struct {
|
||||
aspectRatio float32
|
||||
rows int
|
||||
cols int
|
||||
}
|
||||
|
||||
func newAspectRatioLayout(aspectRatio float32, rows, cols int) fyne.Layout {
|
||||
return &aspectRatioLayout{aspectRatio: aspectRatio, rows: rows, cols: cols}
|
||||
}
|
||||
|
||||
func (a *aspectRatioLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
|
||||
// Calculate cell size based on width
|
||||
cellWidthBasedOnWidth := size.Width / float32(a.cols)
|
||||
cellHeightFromWidth := cellWidthBasedOnWidth / a.aspectRatio
|
||||
|
||||
// Calculate cell size based on height
|
||||
cellHeightBasedOnHeight := size.Height / float32(a.rows)
|
||||
cellWidthFromHeight := cellHeightBasedOnHeight * a.aspectRatio
|
||||
|
||||
var cellWidth, cellHeight float32
|
||||
|
||||
// Choose the smaller of the two to fit in the container
|
||||
if cellWidthBasedOnWidth*float32(a.cols) <= size.Width && cellHeightFromWidth*float32(a.rows) <= size.Height {
|
||||
cellWidth = cellWidthBasedOnWidth
|
||||
cellHeight = cellHeightFromWidth
|
||||
} else {
|
||||
cellWidth = cellWidthFromHeight
|
||||
cellHeight = cellHeightBasedOnHeight
|
||||
}
|
||||
|
||||
if cellHeight*float32(a.rows) > size.Height {
|
||||
cellHeight = size.Height / float32(a.rows)
|
||||
cellWidth = cellHeight * a.aspectRatio
|
||||
}
|
||||
|
||||
x, y := float32(0), float32(0)
|
||||
for i, o := range objects {
|
||||
o.Resize(fyne.NewSize(cellWidth, cellHeight))
|
||||
o.Move(fyne.NewPos(x, y))
|
||||
|
||||
if (i+1)%a.cols == 0 {
|
||||
x = 0
|
||||
y += cellHeight
|
||||
} else {
|
||||
x += cellWidth
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *aspectRatioLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
|
||||
return fyne.NewSize(10, 10)
|
||||
}
|
||||
@@ -25,8 +25,26 @@ func main() {
|
||||
diceInputEntry := widget.NewEntry()
|
||||
diceInputEntry.SetPlaceHolder("e.g., 2d20H, 3d6+5")
|
||||
|
||||
// Dice buttons for quick input
|
||||
diceButtonsContainer := container.NewGridWithColumns(5,
|
||||
// Roll button
|
||||
rollButton := widget.NewButton("ROLL", func() {
|
||||
diceInput := strings.TrimSpace(diceInputEntry.Text)
|
||||
if diceInput == "" {
|
||||
outputDisplay.Text = "Error: Empty input"
|
||||
outputDisplay.Refresh()
|
||||
return
|
||||
}
|
||||
|
||||
result, err := CalculateDice(diceInput)
|
||||
if err != nil {
|
||||
outputDisplay.Text = fmt.Sprintf("Error: %v", err)
|
||||
} else {
|
||||
outputDisplay.Text = strconv.FormatFloat(result, 'g', -1, 64)
|
||||
}
|
||||
outputDisplay.Refresh()
|
||||
})
|
||||
rollButton.Importance = widget.HighImportance
|
||||
|
||||
buttons := []fyne.CanvasObject{
|
||||
widget.NewButton("H", func() {
|
||||
diceInputEntry.SetText(diceInputEntry.Text + "H")
|
||||
}),
|
||||
@@ -57,29 +75,6 @@ func main() {
|
||||
widget.NewButton("d100", func() {
|
||||
diceInputEntry.SetText(diceInputEntry.Text + "d100")
|
||||
}),
|
||||
)
|
||||
|
||||
// Roll button
|
||||
rollButton := widget.NewButton("ROLL", func() {
|
||||
diceInput := strings.TrimSpace(diceInputEntry.Text)
|
||||
if diceInput == "" {
|
||||
outputDisplay.Text = "Error: Empty input"
|
||||
outputDisplay.Refresh()
|
||||
return
|
||||
}
|
||||
|
||||
result, err := CalculateDice(diceInput)
|
||||
if err != nil {
|
||||
outputDisplay.Text = fmt.Sprintf("Error: %v", err)
|
||||
} else {
|
||||
outputDisplay.Text = strconv.FormatFloat(result, 'g', -1, 64)
|
||||
}
|
||||
outputDisplay.Refresh()
|
||||
})
|
||||
rollButton.Importance = widget.HighImportance
|
||||
|
||||
// Calculator-style number buttons
|
||||
numberButtonsContainer := container.NewGridWithColumns(5,
|
||||
createCalcButton("7", diceInputEntry),
|
||||
createCalcButton("8", diceInputEntry),
|
||||
createCalcButton("9", diceInputEntry),
|
||||
@@ -123,7 +118,12 @@ func main() {
|
||||
diceInputEntry.SetText(diceInputEntry.Text + "-")
|
||||
}),
|
||||
rollButton,
|
||||
)
|
||||
}
|
||||
|
||||
buttonsContainer := container.New(newAspectRatioLayout(2.0/3.0, 7, 5))
|
||||
for _, button := range buttons {
|
||||
buttonsContainer.Add(button)
|
||||
}
|
||||
|
||||
// Output section
|
||||
outputSection := container.NewVBox(
|
||||
@@ -140,10 +140,7 @@ func main() {
|
||||
),
|
||||
nil,
|
||||
nil,
|
||||
container.NewVBox(
|
||||
widget.NewCard("Dice Options", "", diceButtonsContainer),
|
||||
widget.NewCard("Calculator", "", numberButtonsContainer),
|
||||
),
|
||||
buttonsContainer,
|
||||
)
|
||||
|
||||
myWindow.SetContent(mainContent)
|
||||
|
||||
Reference in New Issue
Block a user