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

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

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 {
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

View File

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

View File

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