dialog skeleton

This commit is contained in:
2023-07-04 19:36:40 +02:00
parent 7874fc022f
commit 1451bd90c0
8 changed files with 162 additions and 51 deletions

View File

@@ -1,8 +1,36 @@
package domain package domain
import (
"game/konstructor"
"image/color"
)
type Domain struct { type Domain struct {
Context Context Context Context
} }
func (d *Domain) Init() { 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",
},
},
}
}

60
konstructor/dialog.go Normal file
View File

@@ -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()
}
}

View File

@@ -18,18 +18,28 @@ func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
return e.Settings.Screen.Width, e.Settings.Screen.Height 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 { func (e *Engine) Update(screen *ebiten.Image) error {
e.Controller.Watch() e.Controller.Watch()
if e.KContext.Screen.Type == "menu" { if e.ScreenTypeIs("menu") {
e.MenuUpdate() e.MenuUpdate()
} }
if e.ScreenTypeIs("dialog") {
e.DialogUpdate()
}
return nil return nil
} }
func (e *Engine) Draw(screen *ebiten.Image) { func (e *Engine) Draw(screen *ebiten.Image) {
if e.KContext.Screen.Type == "menu" { if e.ScreenTypeIs("menu") {
e.MenuDraw(screen) e.MenuDraw(screen)
} }
if e.ScreenTypeIs("dialog") {
e.DialogDraw(screen)
}
if e.Controller.Action3Pressed() { if e.Controller.Action3Pressed() {
os.Exit(1) os.Exit(1)
} }

10
konstructor/kcontext.go Normal file
View File

@@ -0,0 +1,10 @@
package konstructor
type KContextScreen struct {
Type string
Value string
}
type KContext struct {
Screen KContextScreen
}

View File

@@ -11,6 +11,7 @@ type ContextInterface interface {
type DomainInterface interface { type DomainInterface interface {
Init() Init()
GetDialog() Dialog
} }
type ScreenSettings struct { type ScreenSettings struct {
@@ -18,15 +19,6 @@ type ScreenSettings struct {
Height int Height int
} }
type KContextScreen struct {
Type string
Value string
}
type KContext struct {
Screen KContextScreen
}
type Settings struct { type Settings struct {
Name string Name string
Screen *ScreenSettings Screen *ScreenSettings

View File

@@ -4,20 +4,14 @@ import (
"image/color" "image/color"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text" "github.com/hajimehoshi/ebiten/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
) )
type MenuMap map[string]Menu type MenuMap map[string]Menu
type MenuLayout struct { type MenuLayout struct {
Background string Background string
DPI float64 MenuItemFont FontLayout
Size float64
Color color.Color
SelectedColor color.Color
} }
type MenuItem struct { type MenuItem struct {
@@ -28,23 +22,26 @@ type MenuItem struct {
type Menu struct { type Menu struct {
CurrentSelected int CurrentSelected int
MenuLayout MenuLayout Layout MenuLayout
MenuItems []MenuItem 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) { func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.MenuMap[e.KContext.Screen.Value] menu := e.MenuMap[e.KContext.Screen.Value]
face := e.menuGetFontFace(menu) face := GetFontFace(menu.Layout.MenuItemFont)
for i, menu_item := range menu.MenuItems { for i, menu_item := range menu.MenuItems {
var c color.Color color := menu.GetMenuItemColor(i)
if menu.CurrentSelected == i { offset := int(menu.Layout.MenuItemFont.Size) * (i + 1)
c = menu.MenuLayout.SelectedColor text.Draw(screen, menu_item.Label+"\n", face, 8, offset, color)
} else {
c = menu.MenuLayout.Color
}
offset := int(menu.MenuLayout.Size) * (i + 1)
text.Draw(screen, menu_item.Label+"\n", face, 8, offset, c)
} }
} }
@@ -65,13 +62,3 @@ func (e *Engine) MenuUpdate() {
e.MenuMap[e.KContext.Screen.Value] = menu 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
}

View File

@@ -6,9 +6,13 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"image/color"
_ "image/png" _ "image/png"
"github.com/hajimehoshi/ebiten" "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 { func LoadImage(path string) *ebiten.Image {
@@ -30,3 +34,20 @@ func LoadImage(path string) *ebiten.Image {
return out 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
}

11
main.go
View File

@@ -24,20 +24,21 @@ func main() {
k := konstructor.Konstructor{ k := konstructor.Konstructor{
KContext: &konstructor.KContext{ KContext: &konstructor.KContext{
Screen: konstructor.KContextScreen{ Screen: konstructor.KContextScreen{
Type: "menu", Type: "dialog",
Value: "MainMenu",
}, },
}, },
Domain: &d, Domain: &d,
MenuMap: konstructor.MenuMap{ MenuMap: konstructor.MenuMap{
"MainMenu": { "MainMenu": {
CurrentSelected: 0, CurrentSelected: 0,
MenuLayout: konstructor.MenuLayout{ Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72, DPI: 72,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
}, },
},
MenuItems: []konstructor.MenuItem{ MenuItems: []konstructor.MenuItem{
{ {
ID: "start", ID: "start",
@@ -58,12 +59,14 @@ func main() {
}, },
"GameMenu": { "GameMenu": {
CurrentSelected: 0, CurrentSelected: 0,
MenuLayout: konstructor.MenuLayout{ Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72, DPI: 72,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
}, },
},
MenuItems: []konstructor.MenuItem{ MenuItems: []konstructor.MenuItem{
{ {
ID: "save", ID: "save",