From 1451bd90c0ed690f094d0e511b0eb75add7c4d0c Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 4 Jul 2023 19:36:40 +0200 Subject: [PATCH] dialog skeleton --- domain/domain.go | 28 ++++++++++++++++++ konstructor/dialog.go | 60 ++++++++++++++++++++++++++++++++++++++ konstructor/engine.go | 14 +++++++-- konstructor/kcontext.go | 10 +++++++ konstructor/konstructor.go | 10 +------ konstructor/menu.go | 43 ++++++++++----------------- konstructor/utils.go | 21 +++++++++++++ main.go | 27 +++++++++-------- 8 files changed, 162 insertions(+), 51 deletions(-) create mode 100644 konstructor/dialog.go create mode 100644 konstructor/kcontext.go diff --git a/domain/domain.go b/domain/domain.go index fced373..4b8b485 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -1,8 +1,36 @@ package domain +import ( + "game/konstructor" + "image/color" +) + type Domain struct { Context Context } func (d *Domain) Init() { } + +func (d *Domain) GetDialog() konstructor.Dialog { + return konstructor.Dialog{ + Layout: konstructor.DialogLayout{ + ChoiceFont: konstructor.FontLayout{ + DPI: 72, + Size: 24, + Color: color.White, + SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, + }, + }, + Choices: []konstructor.DialogChoice{ + { + ID: "one", + Label: "One", + }, + { + ID: "two", + Label: "Two", + }, + }, + } +} diff --git a/konstructor/dialog.go b/konstructor/dialog.go new file mode 100644 index 0000000..518b858 --- /dev/null +++ b/konstructor/dialog.go @@ -0,0 +1,60 @@ +package konstructor + +import ( + "image/color" + + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/text" +) + +type DialogLayout struct { + Background string + ChoiceFont FontLayout +} + +type DialogChoice struct { + ID string + Label string + Handler func() +} + +type Dialog struct { + CurrentSelected int + Layout DialogLayout + Choices []DialogChoice +} + +func (dialog *Dialog) GetChoiceColor(i int) color.Color { + if dialog.CurrentSelected == i { + return dialog.Layout.ChoiceFont.SelectedColor + } else { + return dialog.Layout.ChoiceFont.Color + } +} + +func (e *Engine) DialogDraw(screen *ebiten.Image) { + dialog := e.Domain.GetDialog() + + face := GetFontFace(dialog.Layout.ChoiceFont) + + for i, choice := range dialog.Choices { + offset := int(dialog.Layout.ChoiceFont.Size) * (i + 1) + text.Draw(screen, choice.Label+"\n", face, 8, offset, dialog.GetChoiceColor(i)) + } +} + +func (e *Engine) DialogUpdate() { + dialog := e.Domain.GetDialog() + + if e.Controller.UpPressed() && dialog.CurrentSelected != 0 { + dialog.CurrentSelected-- + } + + if e.Controller.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 { + dialog.CurrentSelected++ + } + + if e.Controller.Action0Pressed() { + dialog.Choices[dialog.CurrentSelected].Handler() + } +} diff --git a/konstructor/engine.go b/konstructor/engine.go index 5447690..9b21eb9 100644 --- a/konstructor/engine.go +++ b/konstructor/engine.go @@ -18,18 +18,28 @@ func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) { return e.Settings.Screen.Width, e.Settings.Screen.Height } +func (e *Engine) ScreenTypeIs(name string) bool { + return e.KContext.Screen.Type == name +} + func (e *Engine) Update(screen *ebiten.Image) error { e.Controller.Watch() - if e.KContext.Screen.Type == "menu" { + if e.ScreenTypeIs("menu") { e.MenuUpdate() } + if e.ScreenTypeIs("dialog") { + e.DialogUpdate() + } return nil } func (e *Engine) Draw(screen *ebiten.Image) { - if e.KContext.Screen.Type == "menu" { + if e.ScreenTypeIs("menu") { e.MenuDraw(screen) } + if e.ScreenTypeIs("dialog") { + e.DialogDraw(screen) + } if e.Controller.Action3Pressed() { os.Exit(1) } diff --git a/konstructor/kcontext.go b/konstructor/kcontext.go new file mode 100644 index 0000000..5406c57 --- /dev/null +++ b/konstructor/kcontext.go @@ -0,0 +1,10 @@ +package konstructor + +type KContextScreen struct { + Type string + Value string +} + +type KContext struct { + Screen KContextScreen +} diff --git a/konstructor/konstructor.go b/konstructor/konstructor.go index 3238800..3baeee3 100644 --- a/konstructor/konstructor.go +++ b/konstructor/konstructor.go @@ -11,6 +11,7 @@ type ContextInterface interface { type DomainInterface interface { Init() + GetDialog() Dialog } type ScreenSettings struct { @@ -18,15 +19,6 @@ type ScreenSettings struct { Height int } -type KContextScreen struct { - Type string - Value string -} - -type KContext struct { - Screen KContextScreen -} - type Settings struct { Name string Screen *ScreenSettings diff --git a/konstructor/menu.go b/konstructor/menu.go index 4bd5bf0..d04c1b0 100644 --- a/konstructor/menu.go +++ b/konstructor/menu.go @@ -4,20 +4,14 @@ import ( "image/color" "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/examples/resources/fonts" "github.com/hajimehoshi/ebiten/text" - "golang.org/x/image/font" - "golang.org/x/image/font/opentype" ) type MenuMap map[string]Menu type MenuLayout struct { - Background string - DPI float64 - Size float64 - Color color.Color - SelectedColor color.Color + Background string + MenuItemFont FontLayout } type MenuItem struct { @@ -28,23 +22,26 @@ type MenuItem struct { type Menu struct { CurrentSelected int - MenuLayout MenuLayout + Layout MenuLayout MenuItems []MenuItem } +func (menu *Menu) GetMenuItemColor(i int) color.Color { + if menu.CurrentSelected == i { + return menu.Layout.MenuItemFont.SelectedColor + } else { + return menu.Layout.MenuItemFont.Color + } +} + func (e *Engine) MenuDraw(screen *ebiten.Image) { menu := e.MenuMap[e.KContext.Screen.Value] - face := e.menuGetFontFace(menu) + face := GetFontFace(menu.Layout.MenuItemFont) for i, menu_item := range menu.MenuItems { - var c color.Color - if menu.CurrentSelected == i { - c = menu.MenuLayout.SelectedColor - } else { - c = menu.MenuLayout.Color - } - offset := int(menu.MenuLayout.Size) * (i + 1) - text.Draw(screen, menu_item.Label+"\n", face, 8, offset, c) + color := menu.GetMenuItemColor(i) + offset := int(menu.Layout.MenuItemFont.Size) * (i + 1) + text.Draw(screen, menu_item.Label+"\n", face, 8, offset, color) } } @@ -65,13 +62,3 @@ func (e *Engine) MenuUpdate() { e.MenuMap[e.KContext.Screen.Value] = menu } - -func (e *Engine) menuGetFontFace(menu Menu) font.Face { - tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf) - face, _ := opentype.NewFace(tt, &opentype.FaceOptions{ - Size: menu.MenuLayout.DPI, - DPI: menu.MenuLayout.Size, - Hinting: font.HintingVertical, - }) - return face -} diff --git a/konstructor/utils.go b/konstructor/utils.go index e519ef4..c1f2bd8 100644 --- a/konstructor/utils.go +++ b/konstructor/utils.go @@ -6,9 +6,13 @@ import ( "io/ioutil" "log" + "image/color" _ "image/png" "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/examples/resources/fonts" + "golang.org/x/image/font" + "golang.org/x/image/font/opentype" ) func LoadImage(path string) *ebiten.Image { @@ -30,3 +34,20 @@ func LoadImage(path string) *ebiten.Image { return out } + +type FontLayout struct { + DPI float64 + Size float64 + Color color.Color + SelectedColor color.Color +} + +func GetFontFace(layout FontLayout) font.Face { + tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf) + face, _ := opentype.NewFace(tt, &opentype.FaceOptions{ + Size: layout.DPI, + DPI: layout.Size, + Hinting: font.HintingVertical, + }) + return face +} diff --git a/main.go b/main.go index c5cdfa0..5299ca6 100644 --- a/main.go +++ b/main.go @@ -24,19 +24,20 @@ func main() { k := konstructor.Konstructor{ KContext: &konstructor.KContext{ Screen: konstructor.KContextScreen{ - Type: "menu", - Value: "MainMenu", + Type: "dialog", }, }, Domain: &d, MenuMap: konstructor.MenuMap{ "MainMenu": { CurrentSelected: 0, - MenuLayout: konstructor.MenuLayout{ - DPI: 72, - Size: 24, - Color: color.White, - SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, + Layout: konstructor.MenuLayout{ + MenuItemFont: konstructor.FontLayout{ + DPI: 72, + Size: 24, + Color: color.White, + SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, + }, }, MenuItems: []konstructor.MenuItem{ { @@ -58,11 +59,13 @@ func main() { }, "GameMenu": { CurrentSelected: 0, - MenuLayout: konstructor.MenuLayout{ - DPI: 72, - Size: 24, - Color: color.White, - SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, + Layout: konstructor.MenuLayout{ + MenuItemFont: konstructor.FontLayout{ + DPI: 72, + Size: 24, + Color: color.White, + SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, + }, }, MenuItems: []konstructor.MenuItem{ {