frontend: UI refactoring

- moved ui code to seperate file
- added a simple wrapper over ebitenui.UI to allow storage of colors
and such

Hopefully this makes it possible for colors to be changed by users in
the future
This commit is contained in:
Abdulmujeeb Raji 2023-07-23 07:57:28 +01:00
parent d525bc8fff
commit 8b0bcef568
2 changed files with 79 additions and 60 deletions

View file

@ -1,10 +1,6 @@
package main package main
import ( import (
// Image-related packages
img "image"
"image/color"
// Random // Random
"math/rand" "math/rand"
@ -12,76 +8,29 @@ import (
"log" "log"
// Ebitengine // Ebitengine
"github.com/ebitenui/ebitenui"
"github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
// Create the `Game` struct // Create the `Game` struct
type Game struct { type Game struct {
ui ebitenui.UI ui UI
} }
// Function for UI initialization const (
func uiInit() ebitenui.UI { window_width = 640
// The `hazakura` colorscheme window_height = 480
hazakura := make(map[string]color.RGBA) )
// The monotone colors
hazakura["dark_black"] = color.RGBA{0x0f, 0x0f, 0x0d, 0xff}
hazakura["black"] = color.RGBA{0x15, 0x15, 0x17, 0xff}
hazakura["dark_gray"] = color.RGBA{0x24, 0x24, 0x26, 0xff}
hazakura["gray"] = color.RGBA{0x27, 0x27, 0x2b, 0xff}
hazakura["light_gray"] = color.RGBA{0x45, 0x44, 0x49, 0xff}
hazakura["overlay"] = color.RGBA{0x5c, 0x5c, 0x61, 0xff}
hazakura["highlight"] = color.RGBA{0xd9, 0x0, 0xd7, 0xff}
hazakura["subwhite"] = color.RGBA{0xec, 0xe5, 0xea, 0xff}
// Actual* colors
hazakura["red"] = color.RGBA{0xf0, 0x69, 0x69, 0xff}
hazakura["magenta"] = color.RGBA{0xe8, 0x87, 0xbb, 0xff}
hazakura["purple"] = color.RGBA{0xa2, 0x92, 0xe8, 0xff}
hazakura["blue"] = color.RGBA{0x78, 0xb9, 0xc4, 0xff}
hazakura["cyan"] = color.RGBA{0x7e, 0xe6, 0xae, 0xff}
hazakura["green"] = color.RGBA{0x91, 0xd6, 0x5c, 0xff}
hazakura["yellow"] = color.RGBA{0xd9, 0xd5, 0x64, 0xff}
// Get the window width/height
window_width, window_height := ebiten.WindowSize()
// Define the root container
root := widget.NewContainer(
// Define the plain color to be used as a default background
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(hazakura["dark_black"])),
)
// Define the left bar container
leftBar := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(hazakura["gray"])),
)
// Set the position and size of the left bar
leftBar.SetLocation(img.Rect(0, 0, int(float64(window_width)/3.5), window_height))
// Add the left bar to the root container
root.AddChild(leftBar)
// Construct the UI
ui := ebitenui.UI{
Container: root,
}
return ui
}
// Update implements Game // Update implements Game
func (g *Game) Update() error { func (g *Game) Update() error {
g.ui.Update() g.ui.base.Update()
return nil return nil
} }
// Draw implements Game // Draw implements Game
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
// Draw the UI onto the screen // Draw the UI onto the screen
g.ui.Draw(screen) g.ui.base.Draw(screen)
} }
// Layout implements Game // Layout implements Game
@ -102,13 +51,13 @@ func main() {
window_title := "Ambition: " + window_titles[rand.Intn(len(window_titles))] window_title := "Ambition: " + window_titles[rand.Intn(len(window_titles))]
// Engine setup // Engine setup
ebiten.SetWindowSize(640, 480) ebiten.SetWindowSize(window_width, window_height)
ebiten.SetWindowTitle(window_title) ebiten.SetWindowTitle(window_title)
// Initialise the game // Initialise the game
game := Game{ game := Game{
// Initialise the UI // Initialise the UI
ui: uiInit(), ui: uiInit(window_width, window_height),
} }
// Log and exit on error // Log and exit on error

70
frontend/ui.go Normal file
View file

@ -0,0 +1,70 @@
package main
import (
// Image-related packages
img "image"
"image/color"
// EbitenUI
"github.com/ebitenui/ebitenui"
"github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
)
type UI struct {
base ebitenui.UI
colors map[string]color.RGBA
width, height int
}
// Function for UI initialization
func uiInit(width, height int) UI {
var ui UI
// The `hazakura` colorscheme
{
hazakura := make(map[string]color.RGBA)
// The monotone colors
hazakura["dark_black"] = color.RGBA{0x0f, 0x0f, 0x0d, 0xff}
hazakura["black"] = color.RGBA{0x15, 0x15, 0x17, 0xff}
hazakura["dark_gray"] = color.RGBA{0x24, 0x24, 0x26, 0xff}
hazakura["gray"] = color.RGBA{0x27, 0x27, 0x2b, 0xff}
hazakura["light_gray"] = color.RGBA{0x45, 0x44, 0x49, 0xff}
hazakura["overlay"] = color.RGBA{0x5c, 0x5c, 0x61, 0xff}
hazakura["highlight"] = color.RGBA{0xd9, 0x0, 0xd7, 0xff}
hazakura["subwhite"] = color.RGBA{0xec, 0xe5, 0xea, 0xff}
// Actual* colors
hazakura["red"] = color.RGBA{0xf0, 0x69, 0x69, 0xff}
hazakura["magenta"] = color.RGBA{0xe8, 0x87, 0xbb, 0xff}
hazakura["purple"] = color.RGBA{0xa2, 0x92, 0xe8, 0xff}
hazakura["blue"] = color.RGBA{0x78, 0xb9, 0xc4, 0xff}
hazakura["cyan"] = color.RGBA{0x7e, 0xe6, 0xae, 0xff}
hazakura["green"] = color.RGBA{0x91, 0xd6, 0x5c, 0xff}
hazakura["yellow"] = color.RGBA{0xd9, 0xd5, 0x64, 0xff}
ui.colors = hazakura
}
// Get the window width/height
ui.width = width
ui.height = height
// Define the root container
root := widget.NewContainer(
// Define the plain color to be used as a default background
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["dark_black"])),
)
// Define the left bar container
leftBar := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["gray"])),
)
// Set the position and size of the left bar
leftBar.SetLocation(img.Rect(0, 0, int(float64(width)/3.5), height))
// Add the left bar to the root container
root.AddChild(leftBar)
ui.base.Container = root
return ui
}