Add Play/Edit Buttons to Toggle Between Modes

* Instead of needing to press the "P" and "E" keys to toggle from edit
  mode to play mode (and back again), respectively, the UI now draws a
  "Play (P)" or "Edit (E)" button on the bottom right corner of the
  level canvas. Clicking it will toggle the mode.
This commit is contained in:
Noah 2019-06-25 18:36:53 -07:00
parent 4c2e8eca49
commit 9efe16582c
4 changed files with 85 additions and 10 deletions

View File

@ -54,4 +54,12 @@ var (
// Color for draggable doodad. // Color for draggable doodad.
DragColor = render.MustHexColor("#0099FF") DragColor = render.MustHexColor("#0099FF")
PlayButtonFont = render.Text{
FontFilename: "./fonts/DejaVuSans-Bold.ttf",
Size: 16,
Padding: 4,
Color: render.RGBA(255, 255, 0, 255),
Stroke: render.RGBA(100, 100, 0, 255),
}
) )

View File

@ -125,6 +125,15 @@ func (s *EditorScene) Setup(d *Doodle) error {
return nil return nil
} }
// Playtest switches the level into Play Mode.
func (s *EditorScene) Playtest() {
log.Info("Play Mode, Go!")
s.d.Goto(&PlayScene{
Filename: s.filename,
Level: s.Level,
})
}
// Loop the editor scene. // Loop the editor scene.
func (s *EditorScene) Loop(d *Doodle, ev *events.State) error { func (s *EditorScene) Loop(d *Doodle, ev *events.State) error {
// Update debug overlay values. // Update debug overlay values.
@ -148,11 +157,7 @@ func (s *EditorScene) Loop(d *Doodle, ev *events.State) error {
// Switching to Play Mode? // Switching to Play Mode?
if ev.KeyName.Read() == "p" { if ev.KeyName.Read() == "p" {
log.Info("Play Mode, Go!") s.Playtest()
d.Goto(&PlayScene{
Filename: s.filename,
Level: s.Level,
})
return nil return nil
} }

View File

@ -40,6 +40,7 @@ type EditorUI struct {
Workspace *ui.Frame Workspace *ui.Frame
MenuBar *ui.Frame MenuBar *ui.Frame
StatusBar *ui.Frame StatusBar *ui.Frame
PlayButton *ui.Button
// Palette window. // Palette window.
Palette *ui.Window Palette *ui.Window
@ -78,6 +79,15 @@ func NewEditorUI(d *Doodle, s *EditorScene) *EditorUI {
u.StatusBar = u.SetupStatusBar(d) u.StatusBar = u.SetupStatusBar(d)
u.Workspace = u.SetupWorkspace(d) // important that this is last! u.Workspace = u.SetupWorkspace(d) // important that this is last!
u.PlayButton = ui.NewButton("Play", ui.NewLabel(ui.Label{
Text: "Play (P)",
Font: balance.PlayButtonFont,
}))
u.PlayButton.Handle(ui.Click, func(p render.Point) {
u.Scene.Playtest()
})
u.Supervisor.Add(u.PlayButton)
// Position the Canvas inside the frame. // Position the Canvas inside the frame.
u.Workspace.Pack(u.Canvas, ui.Pack{ u.Workspace.Pack(u.Canvas, ui.Pack{
Anchor: ui.N, Anchor: ui.N,
@ -151,6 +161,23 @@ func (u *EditorUI) Resized(d *Doodle) {
u.ExpandCanvas(d.Engine) u.ExpandCanvas(d.Engine)
} }
// Position the Play button over the workspace.
{
btn := u.PlayButton
btn.Compute(d.Engine)
var (
wsP = u.Workspace.Point()
wsSize = u.Workspace.Size()
btnSize = btn.Size()
padding int32 = 8
)
btn.MoveTo(render.NewPoint(
wsP.X+wsSize.W-btnSize.W-padding,
wsP.Y+wsSize.H-btnSize.H-padding,
))
}
} }
// Loop to process events and update the UI. // Loop to process events and update the UI.
@ -221,6 +248,7 @@ func (u *EditorUI) Present(e render.Engine) {
u.MenuBar.Present(e, u.MenuBar.Point()) u.MenuBar.Present(e, u.MenuBar.Point())
u.StatusBar.Present(e, u.StatusBar.Point()) u.StatusBar.Present(e, u.StatusBar.Point())
u.Workspace.Present(e, u.Workspace.Point()) u.Workspace.Present(e, u.Workspace.Point())
u.PlayButton.Present(e, u.PlayButton.Point())
// Are we dragging a Doodad canvas? // Are we dragging a Doodad canvas?
if u.Supervisor.IsDragging() { if u.Supervisor.IsDragging() {

View File

@ -5,6 +5,7 @@ import (
"git.kirsle.net/apps/doodle/lib/events" "git.kirsle.net/apps/doodle/lib/events"
"git.kirsle.net/apps/doodle/lib/render" "git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/lib/ui"
"git.kirsle.net/apps/doodle/pkg/balance" "git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/doodads" "git.kirsle.net/apps/doodle/pkg/doodads"
"git.kirsle.net/apps/doodle/pkg/level" "git.kirsle.net/apps/doodle/pkg/level"
@ -24,6 +25,10 @@ type PlayScene struct {
drawing *uix.Canvas drawing *uix.Canvas
scripting *scripting.Supervisor scripting *scripting.Supervisor
// UI widgets.
supervisor *ui.Supervisor
editButton *ui.Button
// Custom debug labels. // Custom debug labels.
debPosition *string debPosition *string
debViewport *string debViewport *string
@ -43,6 +48,7 @@ func (s *PlayScene) Name() string {
func (s *PlayScene) Setup(d *Doodle) error { func (s *PlayScene) Setup(d *Doodle) error {
s.d = d s.d = d
s.scripting = scripting.NewSupervisor() s.scripting = scripting.NewSupervisor()
s.supervisor = ui.NewSupervisor()
// Initialize debug overlay values. // Initialize debug overlay values.
s.debPosition = new(string) s.debPosition = new(string)
@ -56,6 +62,16 @@ func (s *PlayScene) Setup(d *Doodle) error {
{"Scroll:", s.debScroll}, {"Scroll:", s.debScroll},
} }
// Initialize the "Edit Map" button.
s.editButton = ui.NewButton("Edit", ui.NewLabel(ui.Label{
Text: "Edit (E)",
Font: balance.PlayButtonFont,
}))
s.editButton.Handle(ui.Click, func(p render.Point) {
s.EditLevel()
})
s.supervisor.Add(s.editButton)
// Initialize the drawing canvas. // Initialize the drawing canvas.
s.drawing = uix.NewCanvas(balance.ChunkSize, false) s.drawing = uix.NewCanvas(balance.ChunkSize, false)
s.drawing.Name = "play-canvas" s.drawing.Name = "play-canvas"
@ -114,6 +130,15 @@ func (s *PlayScene) Setup(d *Doodle) error {
return nil return nil
} }
// EditLevel toggles out of Play Mode to edit the level.
func (s *PlayScene) EditLevel() {
log.Info("Edit Mode, Go!")
s.d.Goto(&EditorScene{
Filename: s.Filename,
Level: s.Level,
})
}
// Loop the editor scene. // Loop the editor scene.
func (s *PlayScene) Loop(d *Doodle, ev *events.State) error { func (s *PlayScene) Loop(d *Doodle, ev *events.State) error {
// Update debug overlay values. // Update debug overlay values.
@ -122,6 +147,8 @@ func (s *PlayScene) Loop(d *Doodle, ev *events.State) error {
*s.debViewport = s.drawing.Viewport().String() *s.debViewport = s.drawing.Viewport().String()
*s.debScroll = s.drawing.Scroll.String() *s.debScroll = s.drawing.Scroll.String()
s.supervisor.Loop(ev)
// Has the window been resized? // Has the window been resized?
if resized := ev.Resized.Now; resized { if resized := ev.Resized.Now; resized {
w, h := d.Engine.WindowSize() w, h := d.Engine.WindowSize()
@ -135,11 +162,7 @@ func (s *PlayScene) Loop(d *Doodle, ev *events.State) error {
// Switching to Edit Mode? // Switching to Edit Mode?
if ev.KeyName.Read() == "e" { if ev.KeyName.Read() == "e" {
log.Info("Edit Mode, Go!") s.EditLevel()
d.Goto(&EditorScene{
Filename: s.Filename,
Level: s.Level,
})
return nil return nil
} }
@ -167,6 +190,17 @@ func (s *PlayScene) Draw(d *Doodle) error {
// Draw out bounding boxes. // Draw out bounding boxes.
d.DrawCollisionBox(s.Player) d.DrawCollisionBox(s.Player)
// Draw the Edit button.
var (
canSize = s.drawing.Size()
size = s.editButton.Size()
padding int32 = 8
)
s.editButton.Present(d.Engine, render.Point{
X: canSize.W - size.W - padding,
Y: canSize.H - size.H - padding,
})
return nil return nil
} }