chore: font/face code improvement

This commit is contained in:
Muhammad Nauman Raza 2023-07-23 17:07:39 +01:00
parent 65c57d1d3e
commit 769e90fa88

View file

@ -19,6 +19,7 @@ import (
"golang.org/x/image/font" "golang.org/x/image/font"
) )
// The UI struct
type UI struct { type UI struct {
base ebitenui.UI base ebitenui.UI
colors map[string]color.RGBA colors map[string]color.RGBA
@ -67,8 +68,8 @@ func uiInit(width, height int) UI {
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["dark_black"])), widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["dark_black"])),
) )
// Load the text font // Set the heading size and font
face, _ := loadFont(10) heading_face, _ := makeFace(18, fonts.IosevkaBold_ttf)
tabProfile := widget.NewTabBookTab("Profile", tabProfile := widget.NewTabBookTab("Profile",
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["gray"])), widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(ui.colors["gray"])),
@ -91,7 +92,7 @@ func uiInit(width, height int) UI {
widget.ContainerOpts.Layout(widget.NewAnchorLayout()), widget.ContainerOpts.Layout(widget.NewAnchorLayout()),
) )
inventoryButton := widget.NewText( inventoryButton := widget.NewText(
widget.TextOpts.Text("Inventory content", face, color.Black), widget.TextOpts.Text("Inventory content", heading_face, color.Black),
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter), widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{ widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter, HorizontalPosition: widget.AnchorLayoutPositionCenter,
@ -105,7 +106,7 @@ func uiInit(width, height int) UI {
widget.ContainerOpts.Layout(widget.NewAnchorLayout()), widget.ContainerOpts.Layout(widget.NewAnchorLayout()),
) )
otherButton := widget.NewText( otherButton := widget.NewText(
widget.TextOpts.Text("Other content", face, color.Black), widget.TextOpts.Text("Other content", heading_face, color.Black),
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter), widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{ widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter, HorizontalPosition: widget.AnchorLayoutPositionCenter,
@ -117,7 +118,7 @@ func uiInit(width, height int) UI {
// Create the tabbook widget // Create the tabbook widget
leftTabs := widget.NewTabBook( leftTabs := widget.NewTabBook(
widget.TabBookOpts.TabButtonImage(buttonImage), widget.TabBookOpts.TabButtonImage(buttonImage),
widget.TabBookOpts.TabButtonText(face, &widget.ButtonTextColor{Idle: color.White}), widget.TabBookOpts.TabButtonText(heading_face, &widget.ButtonTextColor{Idle: color.White}),
widget.TabBookOpts.TabButtonSpacing(0), widget.TabBookOpts.TabButtonSpacing(0),
widget.TabBookOpts.ContainerOpts( widget.TabBookOpts.ContainerOpts(
widget.ContainerOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{ widget.ContainerOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
@ -151,6 +152,7 @@ func uiInit(width, height int) UI {
return ui return ui
} }
// Load a button image
func loadButtonImage() (*widget.ButtonImage, error) { func loadButtonImage() (*widget.ButtonImage, error) {
idle := image.NewNineSliceColor(hazakura["black"]) idle := image.NewNineSliceColor(hazakura["black"])
hover := image.NewNineSliceColor(hazakura["light_gray"]) hover := image.NewNineSliceColor(hazakura["light_gray"])
@ -167,8 +169,9 @@ func loadButtonImage() (*widget.ButtonImage, error) {
}, nil }, nil
} }
func loadFont(size float64) (font.Face, error) { // Function to create a face providing font size and file (from assets)
ttfFont, err := truetype.Parse(fonts.IosevkaBold_ttf) func makeFace(size float64, fontfile []byte) (font.Face, error) {
ttfFont, err := truetype.Parse(fontfile)
if err != nil { if err != nil {
return nil, err return nil, err
} }