Implement the main menu for the ordoor binary
In this commit, we also remove code that doesn't properly belong in view-menu
This commit is contained in:
52
internal/ordoor/interfaces.go
Normal file
52
internal/ordoor/interfaces.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package ordoor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/ui"
|
||||
)
|
||||
|
||||
// These are UI interfaces covering the game entrypoint
|
||||
|
||||
func (o *Ordoor) ifaceMain() (*ui.Interface, error) {
|
||||
// TODO: Start in the "main" menu
|
||||
menu, err := o.assets.Menu("Main")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
iface, err := ui.NewInterface(menu)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: clicking these buttons should load other interfaces
|
||||
wireupClick(iface, func() {}, 2, 1) // New game
|
||||
wireupClick(iface, func() {}, 2, 2) // Load game
|
||||
disableWidget(iface, 2, 3) // Multiplayer. Disable for now.
|
||||
wireupClick(iface, func() {}, 2, 4) // Options
|
||||
wireupClick(iface, func() { o.nextState = StateExit }, 2, 5) // Quit
|
||||
|
||||
return iface, nil
|
||||
}
|
||||
|
||||
func findWidgetOrPanic(iface *ui.Interface, spec ...int) *ui.Widget {
|
||||
widget, err := iface.Widget(spec...)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Couldn't find widget %v:%+v", iface.Name, spec))
|
||||
}
|
||||
|
||||
return widget
|
||||
}
|
||||
|
||||
func wireupClick(iface *ui.Interface, f func(), spec ...int) {
|
||||
findWidgetOrPanic(iface, spec...).OnMouseClick = f
|
||||
}
|
||||
|
||||
func disableWidget(iface *ui.Interface, spec ...int) {
|
||||
findWidgetOrPanic(iface, spec...).Disable()
|
||||
}
|
||||
|
||||
func (o *Ordoor) ifaceOptions() (*ui.Interface, error) {
|
||||
return nil, nil
|
||||
}
|
@@ -5,13 +5,41 @@
|
||||
package ordoor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/audio"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/assetstore"
|
||||
"code.ur.gs/lupine/ordoor/internal/config"
|
||||
"code.ur.gs/lupine/ordoor/internal/ui"
|
||||
)
|
||||
|
||||
type gameState int
|
||||
|
||||
const (
|
||||
StateInitial gameState = 0
|
||||
StateInterface gameState = 1
|
||||
StateExit gameState = 666
|
||||
)
|
||||
|
||||
var (
|
||||
errExit = errors.New("User-requested exit action")
|
||||
)
|
||||
|
||||
type Ordoor struct {
|
||||
Config *config.Config
|
||||
assets *assetstore.AssetStore
|
||||
config *config.Config
|
||||
music *audio.Player
|
||||
win *ui.Window
|
||||
|
||||
state gameState
|
||||
nextState gameState
|
||||
|
||||
// Relevant to interface state
|
||||
iface *ui.Interface
|
||||
}
|
||||
|
||||
func Run(configFile string) error {
|
||||
@@ -20,14 +48,119 @@ func Run(configFile string) error {
|
||||
return fmt.Errorf("Couldn't load config file: %v", err)
|
||||
}
|
||||
|
||||
ordoor := &Ordoor{
|
||||
Config: cfg,
|
||||
assets, err := assetstore.New(cfg.Ordoor.DataDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to initialize asset store: %v", err)
|
||||
}
|
||||
|
||||
ordoor.PlaySkippableVideo("LOGOS")
|
||||
ordoor.PlaySkippableVideo("movie1")
|
||||
if _, err := audio.NewContext(48000); err != nil {
|
||||
return fmt.Errorf("Failed to set up audio context: %v", err)
|
||||
}
|
||||
|
||||
// TODO: load main interface
|
||||
ordoor := &Ordoor{
|
||||
assets: assets,
|
||||
config: cfg,
|
||||
state: StateInitial,
|
||||
nextState: StateInterface,
|
||||
}
|
||||
|
||||
win, err := ui.NewWindow(ordoor, "Ordoor")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create window: %v", err)
|
||||
}
|
||||
|
||||
ordoor.win = win
|
||||
|
||||
if err := ordoor.Run(); err != nil {
|
||||
return fmt.Errorf("Run returned %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Ordoor) Run() error {
|
||||
// On startup, play these two videos
|
||||
o.PlaySkippableVideo("LOGOS")
|
||||
o.PlaySkippableVideo("movie1")
|
||||
|
||||
err := o.win.Run()
|
||||
if err == errExit {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Only one music track can play at a time. This is handled at the toplevel.
|
||||
// FIXME: should take references from Sounds.dat
|
||||
func (o *Ordoor) PlayMusic(name string) error {
|
||||
if o.music != nil {
|
||||
if err := o.music.Close(); err != nil {
|
||||
return fmt.Errorf("Failed to close old music: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
sound, err := o.assets.Sound(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to find sound %v: %v", name, err)
|
||||
}
|
||||
|
||||
player, err := sound.InfinitePlayer()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to generate music player for %v: %v", name, err)
|
||||
}
|
||||
o.music = player
|
||||
player.Play()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Ordoor) setupInterface() error {
|
||||
o.PlayMusic("music_interface")
|
||||
initial, err := o.ifaceMain()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.iface = initial
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *Ordoor) Update(screenX, screenY int) error {
|
||||
// Perform state transitions
|
||||
if o.state != o.nextState {
|
||||
log.Printf("State transition: %v -> %v", o.state, o.nextState)
|
||||
switch o.nextState {
|
||||
case StateInterface: // Setup, move state to interface
|
||||
if err := o.setupInterface(); err != nil {
|
||||
return err
|
||||
}
|
||||
case StateExit:
|
||||
{
|
||||
return errExit
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("Unknown state transition: %v -> %v", o.state, o.nextState)
|
||||
}
|
||||
}
|
||||
|
||||
// State transition is finished, hooray
|
||||
o.state = o.nextState
|
||||
|
||||
switch o.state {
|
||||
case StateInterface:
|
||||
return o.iface.Update(screenX, screenY)
|
||||
default:
|
||||
return fmt.Errorf("Unknown state: %v", o.state)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Ordoor) Draw(screen *ebiten.Image) error {
|
||||
switch o.state {
|
||||
case StateInterface:
|
||||
return o.iface.Draw(screen)
|
||||
default:
|
||||
return fmt.Errorf("Unknown state: %v", o.state)
|
||||
}
|
||||
}
|
||||
|
@@ -6,15 +6,15 @@ import (
|
||||
)
|
||||
|
||||
func (o *Ordoor) PlayVideo(name string, skippable bool) {
|
||||
filename := o.Config.DataFile("SMK/" + name + ".smk")
|
||||
filename := o.config.DataFile("SMK/" + name + ".smk")
|
||||
|
||||
if len(o.Config.VideoPlayer) == 0 {
|
||||
if len(o.config.VideoPlayer) == 0 {
|
||||
log.Printf("Video player not configured, skipping video %v", filename)
|
||||
return
|
||||
}
|
||||
|
||||
argc := o.Config.VideoPlayer[0]
|
||||
argv := append(o.Config.VideoPlayer[1:])
|
||||
argc := o.config.VideoPlayer[0]
|
||||
argv := append(o.config.VideoPlayer[1:])
|
||||
if skippable {
|
||||
argv = append(argv, "--input-conf=skippable.mpv.conf")
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ import (
|
||||
// mind. The interface transparently scales them all to the current screen size
|
||||
// to compensate.
|
||||
type Interface struct {
|
||||
Name string
|
||||
menu *assetstore.Menu
|
||||
static []*assetstore.Sprite // Static elements in the interface
|
||||
ticks int
|
||||
@@ -28,6 +29,8 @@ type Interface struct {
|
||||
|
||||
func NewInterface(menu *assetstore.Menu) (*Interface, error) {
|
||||
iface := &Interface{
|
||||
Name: menu.Name,
|
||||
|
||||
menu: menu,
|
||||
}
|
||||
|
||||
@@ -59,6 +62,10 @@ func (i *Interface) Update(screenX, screenY int) error {
|
||||
|
||||
// Iterate through all widgets, update mouse state
|
||||
for _, widget := range i.widgets {
|
||||
if widget.disabled {
|
||||
continue // No activity for disabled widgets
|
||||
}
|
||||
|
||||
mouseIsOver := mousePos.In(widget.Bounds)
|
||||
widget.hovering(mouseIsOver)
|
||||
widget.mouseButton(mouseIsOver && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft))
|
||||
@@ -188,11 +195,17 @@ func (i *Interface) widgetFromRecord(record *menus.Record) (*Widget, error) {
|
||||
}
|
||||
widget.hoverAnimation = hovers
|
||||
|
||||
sprite, err := i.menu.Sprite(record.Share + 1)
|
||||
pressed, err := i.menu.Sprite(record.Share + 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
widget.mouseButtonDownImage = sprite.Image
|
||||
widget.mouseButtonDownImage = pressed.Image
|
||||
|
||||
disabled, err := i.menu.Sprite(record.Share + 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
widget.disabledImage = disabled.Image
|
||||
}
|
||||
|
||||
return widget, nil
|
||||
|
@@ -25,6 +25,9 @@ type Widget struct {
|
||||
OnMouseClick func()
|
||||
OnMouseUp func()
|
||||
|
||||
disabled bool
|
||||
disabledImage *ebiten.Image
|
||||
|
||||
// These are expected to have the same dimensions as the Bounds
|
||||
hoverAnimation []*ebiten.Image
|
||||
hoverState bool
|
||||
@@ -39,6 +42,13 @@ type Widget struct {
|
||||
sprite *assetstore.Sprite
|
||||
}
|
||||
|
||||
func (w *Widget) Disable() {
|
||||
w.hovering(false)
|
||||
w.mouseButton(false)
|
||||
|
||||
w.disabled = true
|
||||
}
|
||||
|
||||
func (w *Widget) hovering(value bool) {
|
||||
if w.OnHoverEnter != nil && !w.hoverState && value {
|
||||
w.OnHoverEnter()
|
||||
@@ -72,6 +82,10 @@ func (w *Widget) mouseButton(value bool) {
|
||||
}
|
||||
|
||||
func (w *Widget) Image(aniStep int) (*ebiten.Image, error) {
|
||||
if w.disabled {
|
||||
return w.disabledImage, nil
|
||||
}
|
||||
|
||||
if w.hoverState && w.mouseButtonState {
|
||||
return w.mouseButtonDownImage, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user