2018-06-21 01:43:14 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
2018-09-26 17:04:46 +00:00
|
|
|
"errors"
|
2018-09-25 16:40:34 +00:00
|
|
|
"fmt"
|
2018-06-21 01:43:14 +00:00
|
|
|
"os"
|
2018-10-02 17:11:38 +00:00
|
|
|
"strings"
|
2018-06-21 01:43:14 +00:00
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/doodads"
|
2019-07-04 00:19:25 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/drawtool"
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/enum"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/level"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2018-10-19 20:31:58 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/userdir"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
"git.kirsle.net/go/render/event"
|
2018-06-21 01:43:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EditorScene manages the "Edit Level" game mode.
|
|
|
|
type EditorScene struct {
|
2018-07-24 03:10:53 +00:00
|
|
|
// Configuration for the scene initializer.
|
2018-09-26 17:04:46 +00:00
|
|
|
DrawingType enum.DrawingType
|
|
|
|
OpenFile bool
|
|
|
|
Filename string
|
|
|
|
DoodadSize int
|
2018-07-24 03:10:53 +00:00
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
UI *EditorUI
|
2018-10-28 05:22:13 +00:00
|
|
|
d *Doodle
|
2018-08-05 19:54:57 +00:00
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// The current level or doodad object being edited, based on the
|
|
|
|
// DrawingType.
|
|
|
|
Level *level.Level
|
|
|
|
Doodad *doodads.Doodad
|
2018-09-25 16:40:34 +00:00
|
|
|
|
2019-04-10 01:28:08 +00:00
|
|
|
// Custom debug overlay values.
|
|
|
|
debTool *string
|
|
|
|
debSwatch *string
|
|
|
|
debWorldIndex *string
|
|
|
|
|
2018-09-25 16:40:34 +00:00
|
|
|
// Last saved filename by the user.
|
|
|
|
filename string
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 02:00:46 +00:00
|
|
|
// Name of the scene.
|
|
|
|
func (s *EditorScene) Name() string {
|
|
|
|
return "Edit"
|
|
|
|
}
|
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// Setup the editor scene.
|
2018-07-21 22:11:00 +00:00
|
|
|
func (s *EditorScene) Setup(d *Doodle) error {
|
2019-04-10 01:28:08 +00:00
|
|
|
// Debug overlay values.
|
|
|
|
s.debTool = new(string)
|
|
|
|
s.debSwatch = new(string)
|
|
|
|
s.debWorldIndex = new(string)
|
|
|
|
customDebugLabels = []debugLabel{
|
2019-04-19 22:08:00 +00:00
|
|
|
{"Pixel:", s.debWorldIndex},
|
|
|
|
{"Tool:", s.debTool},
|
|
|
|
{"Swatch:", s.debSwatch},
|
2019-04-10 01:28:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 17:38:49 +00:00
|
|
|
// Initialize the user interface. It references the palette and such so it
|
|
|
|
// must be initialized after those things.
|
2018-10-28 05:22:13 +00:00
|
|
|
s.d = d
|
2018-10-08 17:38:49 +00:00
|
|
|
s.UI = NewEditorUI(d, s)
|
2018-09-25 16:40:34 +00:00
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// Were we given configuration data?
|
2018-07-24 03:10:53 +00:00
|
|
|
if s.Filename != "" {
|
2018-09-25 16:40:34 +00:00
|
|
|
log.Debug("EditorScene.Setup: Set filename to %s", s.Filename)
|
2018-07-24 03:10:53 +00:00
|
|
|
s.filename = s.Filename
|
|
|
|
s.Filename = ""
|
2018-09-25 16:40:34 +00:00
|
|
|
}
|
2018-09-26 17:04:46 +00:00
|
|
|
|
|
|
|
// Loading a Level or a Doodad?
|
|
|
|
switch s.DrawingType {
|
|
|
|
case enum.LevelDrawing:
|
|
|
|
if s.Level != nil {
|
|
|
|
log.Debug("EditorScene.Setup: received level from scene caller")
|
2018-10-28 05:22:13 +00:00
|
|
|
s.UI.Canvas.LoadLevel(d.Engine, s.Level)
|
2019-04-14 22:25:03 +00:00
|
|
|
s.UI.Canvas.InstallActors(s.Level.Actors)
|
2018-09-26 17:04:46 +00:00
|
|
|
} else if s.filename != "" && s.OpenFile {
|
|
|
|
log.Debug("EditorScene.Setup: Loading map from filename at %s", s.filename)
|
|
|
|
if err := s.LoadLevel(s.filename); err != nil {
|
|
|
|
d.Flash("LoadLevel error: %s", err)
|
2019-04-14 22:25:03 +00:00
|
|
|
} else {
|
|
|
|
s.UI.Canvas.InstallActors(s.Level.Actors)
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
2018-07-24 03:10:53 +00:00
|
|
|
}
|
2018-09-25 16:40:34 +00:00
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
// Write locked level?
|
|
|
|
if s.Level != nil && s.Level.Locked {
|
|
|
|
if balance.WriteLockOverride {
|
|
|
|
d.Flash("Note: write lock has been overridden")
|
|
|
|
} else {
|
|
|
|
d.Flash("That level is write-protected and cannot be viewed in the editor.")
|
|
|
|
s.Level = nil
|
|
|
|
s.UI.Canvas.ClearActors()
|
|
|
|
s.filename = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// No level?
|
|
|
|
if s.Level == nil {
|
|
|
|
log.Debug("EditorScene.Setup: initializing a new Level")
|
|
|
|
s.Level = level.New()
|
|
|
|
s.Level.Palette = level.DefaultPalette()
|
2018-10-28 05:22:13 +00:00
|
|
|
s.UI.Canvas.LoadLevel(d.Engine, s.Level)
|
2018-10-08 17:38:49 +00:00
|
|
|
s.UI.Canvas.ScrollTo(render.Origin)
|
|
|
|
s.UI.Canvas.Scrollable = true
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
case enum.DoodadDrawing:
|
2019-07-07 06:28:11 +00:00
|
|
|
// Getting a doodad from file?
|
2018-09-26 17:04:46 +00:00
|
|
|
if s.filename != "" && s.OpenFile {
|
|
|
|
log.Debug("EditorScene.Setup: Loading doodad from filename at %s", s.filename)
|
|
|
|
if err := s.LoadDoodad(s.filename); err != nil {
|
|
|
|
d.Flash("LoadDoodad error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
// Write locked doodad?
|
|
|
|
if s.Doodad != nil && s.Doodad.Locked {
|
|
|
|
if balance.WriteLockOverride {
|
|
|
|
d.Flash("Note: write lock has been overridden")
|
|
|
|
} else {
|
|
|
|
d.Flash("That doodad is write-protected and cannot be viewed in the editor.")
|
|
|
|
s.Doodad = nil
|
|
|
|
s.filename = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
// No Doodad?
|
|
|
|
if s.Doodad == nil {
|
|
|
|
log.Debug("EditorScene.Setup: initializing a new Doodad")
|
|
|
|
s.Doodad = doodads.New(s.DoodadSize)
|
2018-10-08 17:38:49 +00:00
|
|
|
s.UI.Canvas.LoadDoodad(s.Doodad)
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move inside the UI. Just an approximate position for now.
|
2019-12-28 03:16:34 +00:00
|
|
|
s.UI.Canvas.Resize(render.NewRect(s.DoodadSize, s.DoodadSize))
|
2018-10-08 17:38:49 +00:00
|
|
|
s.UI.Canvas.ScrollTo(render.Origin)
|
|
|
|
s.UI.Canvas.Scrollable = false
|
|
|
|
s.UI.Workspace.Compute(d.Engine)
|
2018-07-24 03:10:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 00:43:23 +00:00
|
|
|
// Recompute the UI Palette window for the level's palette.
|
|
|
|
s.UI.FinishSetup(d)
|
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
d.Flash("Editor Mode. Press 'P' to play this map.")
|
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:36:53 +00:00
|
|
|
// 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,
|
2019-07-02 22:24:46 +00:00
|
|
|
CanEdit: true,
|
2019-06-26 01:36:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// Loop the editor scene.
|
2019-12-22 22:11:01 +00:00
|
|
|
func (s *EditorScene) Loop(d *Doodle, ev *event.State) error {
|
2019-04-10 01:28:08 +00:00
|
|
|
// Update debug overlay values.
|
|
|
|
*s.debTool = s.UI.Canvas.Tool.String()
|
|
|
|
*s.debSwatch = s.UI.Canvas.Palette.ActiveSwatch.Name
|
|
|
|
*s.debWorldIndex = s.UI.Canvas.WorldIndexAt(s.UI.cursor).String()
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
// Has the window been resized?
|
2019-12-22 22:11:01 +00:00
|
|
|
if ev.WindowResized {
|
2018-10-19 20:31:58 +00:00
|
|
|
w, h := d.Engine.WindowSize()
|
|
|
|
if w != d.width || h != d.height {
|
|
|
|
// Not a false alarm.
|
|
|
|
d.width = w
|
|
|
|
d.height = h
|
|
|
|
s.UI.Resized(d)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
// Undo/Redo key bindings.
|
2019-12-22 22:11:01 +00:00
|
|
|
if ev.Ctrl {
|
|
|
|
if ev.KeyDown("z") {
|
2019-07-03 23:22:30 +00:00
|
|
|
s.UI.Canvas.UndoStroke()
|
2019-12-22 22:11:01 +00:00
|
|
|
} else if ev.KeyDown("y") {
|
2019-07-03 23:22:30 +00:00
|
|
|
s.UI.Canvas.RedoStroke()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
s.UI.Loop(ev)
|
2018-06-21 01:43:14 +00:00
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
// Switching to Play Mode?
|
2019-12-22 22:11:01 +00:00
|
|
|
if ev.KeyDown("p") {
|
2019-06-26 01:36:53 +00:00
|
|
|
s.Playtest()
|
2019-12-22 22:11:01 +00:00
|
|
|
} else if ev.KeyDown("l") {
|
2019-07-04 00:19:25 +00:00
|
|
|
d.Flash("Line Tool selected.")
|
|
|
|
s.UI.Canvas.Tool = drawtool.LineTool
|
2019-07-04 04:55:15 +00:00
|
|
|
s.UI.activeTool = s.UI.Canvas.Tool.String()
|
2019-12-22 22:11:01 +00:00
|
|
|
} else if ev.KeyDown("f") {
|
2019-07-04 00:19:25 +00:00
|
|
|
d.Flash("Pencil Tool selected.")
|
|
|
|
s.UI.Canvas.Tool = drawtool.PencilTool
|
2019-07-04 04:55:15 +00:00
|
|
|
s.UI.activeTool = s.UI.Canvas.Tool.String()
|
2019-12-22 22:11:01 +00:00
|
|
|
} else if ev.KeyDown("r") {
|
2019-07-04 00:19:25 +00:00
|
|
|
d.Flash("Rectangle Tool selected.")
|
|
|
|
s.UI.Canvas.Tool = drawtool.RectTool
|
2019-07-04 04:55:15 +00:00
|
|
|
s.UI.activeTool = s.UI.Canvas.Tool.String()
|
2018-07-24 03:10:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the current frame.
|
|
|
|
func (s *EditorScene) Draw(d *Doodle) error {
|
2018-08-17 03:37:19 +00:00
|
|
|
// Clear the canvas and fill it with magenta so it's clear if any spots are missed.
|
2019-07-02 22:24:46 +00:00
|
|
|
d.Engine.Clear(render.RGBA(160, 120, 160, 255))
|
Menu Toolbar for Editor + Shell Prompts + Theme
* Added a "menu toolbar" to the top of the Edit Mode with useful buttons
that work: New Level, New Doodad (same thing), Save, Save as, Open.
* Added ability for the dev console to prompt the user for a question,
which opens the console automatically. "Save", "Save as" and "Load"
ask for their filenames this way.
* Started groundwork for theming the app. The palette window is a light
brown with an orange title bar, the Menu Toolbar has a black
background, etc.
* Added support for multiple fonts instead of just monospace. DejaVu
Sans (normal and bold) are used now for most labels and window titles,
respectively. The dev console uses DejaVu Sans Mono as before.
* Update ui.Label to accept PadX and PadY separately instead of only
having the Padding option which did both.
* Improvements to Frame packing algorithm.
* Set the SDL draw mode to BLEND so we can use alpha colors properly,
so now the dev console is semi-translucent.
2018-08-12 00:30:00 +00:00
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
s.UI.Present(d.Engine)
|
2018-08-17 03:37:19 +00:00
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadLevel loads a level from disk.
|
2018-07-21 22:11:00 +00:00
|
|
|
func (s *EditorScene) LoadLevel(filename string) error {
|
2018-07-22 03:43:01 +00:00
|
|
|
s.filename = filename
|
2018-08-11 00:19:47 +00:00
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
level, err := level.LoadFile(filename)
|
2018-09-25 16:40:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("EditorScene.LoadLevel(%s): %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.DrawingType = enum.LevelDrawing
|
|
|
|
s.Level = level
|
2018-10-28 05:22:13 +00:00
|
|
|
s.UI.Canvas.LoadLevel(s.d.Engine, s.Level)
|
2018-10-19 20:31:58 +00:00
|
|
|
|
|
|
|
log.Info("Installing %d actors into the drawing", len(level.Actors))
|
|
|
|
if err := s.UI.Canvas.InstallActors(level.Actors); err != nil {
|
|
|
|
return fmt.Errorf("EditorScene.LoadLevel: InstallActors: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-25 16:40:34 +00:00
|
|
|
return nil
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveLevel saves the level to disk.
|
2018-08-17 03:37:19 +00:00
|
|
|
// TODO: move this into the Canvas?
|
2018-09-26 17:04:46 +00:00
|
|
|
func (s *EditorScene) SaveLevel(filename string) error {
|
|
|
|
if s.DrawingType != enum.LevelDrawing {
|
|
|
|
return errors.New("SaveLevel: current drawing is not a Level type")
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
if !strings.HasSuffix(filename, enum.LevelExt) {
|
|
|
|
filename += enum.LevelExt
|
2018-10-02 17:11:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:43:01 +00:00
|
|
|
s.filename = filename
|
2018-08-11 00:19:47 +00:00
|
|
|
|
2018-09-25 16:40:34 +00:00
|
|
|
m := s.Level
|
|
|
|
if m.Title == "" {
|
|
|
|
m.Title = "Alpha"
|
|
|
|
}
|
|
|
|
if m.Author == "" {
|
|
|
|
m.Author = os.Getenv("USER")
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:38:49 +00:00
|
|
|
m.Palette = s.UI.Canvas.Palette
|
|
|
|
m.Chunker = s.UI.Canvas.Chunker()
|
2018-06-21 01:43:14 +00:00
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
return m.WriteFile(filename)
|
2018-09-26 17:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadDoodad loads a doodad from disk.
|
|
|
|
func (s *EditorScene) LoadDoodad(filename string) error {
|
|
|
|
s.filename = filename
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
doodad, err := doodads.LoadFile(filename)
|
2018-09-26 17:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("EditorScene.LoadDoodad(%s): %s", filename, err)
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
2018-09-26 17:04:46 +00:00
|
|
|
|
|
|
|
s.DrawingType = enum.DoodadDrawing
|
|
|
|
s.Doodad = doodad
|
|
|
|
s.DoodadSize = doodad.Layers[0].Chunker.Size
|
2018-10-08 17:38:49 +00:00
|
|
|
s.UI.Canvas.LoadDoodad(s.Doodad)
|
2018-09-26 17:04:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveDoodad saves the doodad to disk.
|
|
|
|
func (s *EditorScene) SaveDoodad(filename string) error {
|
|
|
|
if s.DrawingType != enum.DoodadDrawing {
|
|
|
|
return errors.New("SaveDoodad: current drawing is not a Doodad type")
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
if !strings.HasSuffix(filename, enum.DoodadExt) {
|
|
|
|
filename += enum.DoodadExt
|
2018-10-02 17:11:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 17:04:46 +00:00
|
|
|
s.filename = filename
|
|
|
|
d := s.Doodad
|
|
|
|
if d.Title == "" {
|
|
|
|
d.Title = "Untitled Doodad"
|
|
|
|
}
|
|
|
|
if d.Author == "" {
|
|
|
|
d.Author = os.Getenv("USER")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: is this copying necessary?
|
2018-10-08 17:38:49 +00:00
|
|
|
d.Palette = s.UI.Canvas.Palette
|
|
|
|
d.Layers[0].Chunker = s.UI.Canvas.Chunker()
|
2018-09-26 17:04:46 +00:00
|
|
|
|
2018-10-02 17:11:38 +00:00
|
|
|
// Save it to their profile directory.
|
2018-10-19 20:31:58 +00:00
|
|
|
filename = userdir.DoodadPath(filename)
|
2018-10-02 17:11:38 +00:00
|
|
|
log.Info("Write Doodad: %s", filename)
|
2019-06-24 00:52:48 +00:00
|
|
|
return d.WriteFile(filename)
|
2018-06-21 01:43:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
// Destroy the scene.
|
|
|
|
func (s *EditorScene) Destroy() error {
|
|
|
|
return nil
|
|
|
|
}
|