Implemented scene events and callback handlers for those events. Manager defaults also added.
This commit is contained in:
@@ -11,13 +11,16 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
//setup manager
|
||||
manager := groovy.NewManager()
|
||||
|
||||
loadScenes(&manager)
|
||||
|
||||
manager.SetDimensions(groovy.Area{Width: 1280, Height: 720})
|
||||
ebiten.SetWindowSize(manager.Info.Dimension.Width, manager.Info.Dimension.Height)
|
||||
ebiten.SetWindowTitle(manager.Info.Name)
|
||||
|
||||
loadScenes(&manager)
|
||||
|
||||
//identification
|
||||
fmt.Println(manager.Info.Name + ": v" + manager.Info.Version)
|
||||
|
||||
if err := ebiten.RunGame(&manager); err != nil {
|
||||
@@ -28,14 +31,31 @@ func main() {
|
||||
|
||||
// Example loading of two scenes
|
||||
func loadScenes(m *groovy.Manager) {
|
||||
//call the loaders for each scene
|
||||
loadSplash(m)
|
||||
loadMenu(m)
|
||||
|
||||
//reset the manager to start scene 1
|
||||
m.ResetScenes()
|
||||
}
|
||||
|
||||
// creates splash screen, sets completion handler
|
||||
func loadSplash(m *groovy.Manager) {
|
||||
//create splash screen and append as first scene in manager
|
||||
sceneSplash := splashmenu.NewSplash()
|
||||
m.AddScene(&sceneSplash)
|
||||
|
||||
sceneMenu := splashmenu.NewMenu()
|
||||
m.AddScene(&sceneMenu)
|
||||
|
||||
//sets current scene to the splash menu
|
||||
m.SetCurrentScene(0)
|
||||
|
||||
m.SetDimensions(groovy.Area{Width: 1280, Height: 720})
|
||||
}
|
||||
|
||||
// creates menu screen, populates the options, sets key event handler
|
||||
func loadMenu(m *groovy.Manager) {
|
||||
//create menu with defined options, append to manager
|
||||
sceneMenu := splashmenu.NewMenu()
|
||||
sceneMenu.SetOptions(map[int]splashmenu.MenuOption{
|
||||
1: {Description: "splash", SelectionEvent: groovy.RESET, Mapping: ebiten.Key1},
|
||||
2: {Description: "menu"},
|
||||
3: {Description: "swing"},
|
||||
4: {Description: "exit", SelectionEvent: groovy.ENDGAME, Mapping: ebiten.Key4},
|
||||
})
|
||||
|
||||
m.AddScene(&sceneMenu)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
"image/color"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
||||
@@ -13,7 +15,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
menuFont font.Face
|
||||
titleFont font.Face
|
||||
menuFont font.Face
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -23,8 +26,17 @@ func init() {
|
||||
}
|
||||
|
||||
const dpi = 72
|
||||
titleFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: 16,
|
||||
DPI: dpi,
|
||||
Hinting: font.HintingVertical,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
menuFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: 12,
|
||||
Size: 10,
|
||||
DPI: dpi,
|
||||
Hinting: font.HintingVertical,
|
||||
})
|
||||
@@ -34,35 +46,62 @@ func init() {
|
||||
}
|
||||
|
||||
type Menu struct {
|
||||
bgcolor color.RGBA
|
||||
completed bool
|
||||
bgcolor color.RGBA
|
||||
options map[int]MenuOption
|
||||
events map[groovy.SceneEvent]func()
|
||||
}
|
||||
|
||||
func NewMenu() Menu {
|
||||
return Menu{
|
||||
bgcolor: color.RGBA{0x33, 0x33, 0x99, 0xFF},
|
||||
completed: false,
|
||||
bgcolor: color.RGBA{0x33, 0x33, 0x99, 0xFF},
|
||||
events: make(map[groovy.SceneEvent]func()),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) Draw(screen *ebiten.Image) {
|
||||
screen.Fill(m.bgcolor)
|
||||
text.Draw(screen, "menu", menuFont, 40, 40, color.White)
|
||||
text.Draw(screen, "menu", titleFont, 40, 40, color.White)
|
||||
|
||||
m.RenderOptions(screen)
|
||||
}
|
||||
|
||||
func (m Menu) SetEventHandler(event groovy.SceneEvent, f func()) {
|
||||
m.events[event] = f
|
||||
}
|
||||
|
||||
func (m *Menu) SetOptions(options map[int]MenuOption) {
|
||||
m.options = make(map[int]MenuOption)
|
||||
|
||||
for k, v := range options {
|
||||
m.options[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) RenderOptions(screen *ebiten.Image) {
|
||||
|
||||
var offset int = 20
|
||||
|
||||
for k, v := range m.options {
|
||||
m.options[k] = v
|
||||
text.Draw(screen, strconv.Itoa(k)+": "+v.Description, menuFont, 40, 60+offset*k, color.White)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *Menu) Update() error {
|
||||
|
||||
var keysPressed []ebiten.Key
|
||||
keysPressed = inpututil.AppendPressedKeys(keysPressed[:0])
|
||||
for _, k := range keysPressed {
|
||||
if k == ebiten.KeyEnter {
|
||||
m.completed = true
|
||||
|
||||
for _, o := range m.options {
|
||||
if m.events[o.SelectionEvent] != nil {
|
||||
for _, k := range keysPressed {
|
||||
if k == o.Mapping {
|
||||
m.events[o.SelectionEvent]()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Menu) Completed() bool {
|
||||
return m.completed
|
||||
}
|
||||
|
||||
13
examples/splashmenu/scenes/menuoption.go
Normal file
13
examples/splashmenu/scenes/menuoption.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type MenuOption struct {
|
||||
Description string
|
||||
SelectionEvent groovy.SceneEvent
|
||||
Mapping ebiten.Key
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
@@ -34,14 +35,19 @@ func init() {
|
||||
|
||||
type Splash struct {
|
||||
bgcolor color.RGBA
|
||||
completed bool
|
||||
increment int
|
||||
events map[groovy.SceneEvent]func()
|
||||
}
|
||||
|
||||
// GetSceneEvents implements groovy.Scene.
|
||||
func (*Splash) GetSceneEvents() []groovy.SceneEvent {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSplash() Splash {
|
||||
return Splash{
|
||||
bgcolor: color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
completed: false,
|
||||
bgcolor: color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
events: make(map[groovy.SceneEvent]func()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,12 +64,12 @@ func (s *Splash) Update() error {
|
||||
s.bgcolor.B = (s.bgcolor.B - 2) % 0xFF
|
||||
|
||||
if s.bgcolor.R == 0x00 {
|
||||
s.completed = true
|
||||
s.events[groovy.COMPLETED]()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Splash) Completed() bool {
|
||||
return s.completed
|
||||
func (s Splash) SetEventHandler(event groovy.SceneEvent, f func()) {
|
||||
s.events[event] = f
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user