2018-06-21 01:43:14 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
"git.kirsle.net/apps/doodle/balance"
|
2018-07-22 00:12:22 +00:00
|
|
|
"git.kirsle.net/apps/doodle/events"
|
2018-06-21 01:43:14 +00:00
|
|
|
"git.kirsle.net/apps/doodle/level"
|
2018-07-22 00:12:22 +00:00
|
|
|
"git.kirsle.net/apps/doodle/render"
|
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.
|
|
|
|
OpenFile bool
|
|
|
|
Filename string
|
2018-09-23 22:20:45 +00:00
|
|
|
Canvas *level.Chunker
|
2018-07-24 03:10:53 +00:00
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
UI *EditorUI
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// The canvas widget that contains the map we're working on.
|
|
|
|
// XXX: in dev builds this is available at $ d.Scene.GetDrawing()
|
|
|
|
drawing *level.Canvas
|
2018-08-11 00:19:47 +00:00
|
|
|
|
2018-06-21 01:43:14 +00:00
|
|
|
// History of all the pixels placed by the user.
|
2018-08-17 03:37:19 +00:00
|
|
|
filename string // Last saved filename.
|
2018-06-21 01:43:14 +00:00
|
|
|
|
|
|
|
// Canvas size
|
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-23 22:20:45 +00:00
|
|
|
s.drawing = level.NewCanvas(balance.ChunkSize, true)
|
2018-08-17 03:37:19 +00:00
|
|
|
s.drawing.Palette = level.DefaultPalette()
|
|
|
|
if len(s.drawing.Palette.Swatches) > 0 {
|
|
|
|
s.drawing.SetSwatch(s.drawing.Palette.Swatches[0])
|
|
|
|
}
|
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-07-24 03:10:53 +00:00
|
|
|
// Were we given configuration data?
|
|
|
|
if s.Filename != "" {
|
|
|
|
log.Debug("EditorScene: Set filename to %s", s.Filename)
|
|
|
|
s.filename = s.Filename
|
|
|
|
s.Filename = ""
|
|
|
|
if s.OpenFile {
|
|
|
|
log.Debug("EditorScene: Loading map from filename at %s", s.filename)
|
|
|
|
if err := s.LoadLevel(s.filename); err != nil {
|
2018-08-11 00:19:47 +00:00
|
|
|
d.Flash("LoadLevel error: %s", err)
|
2018-07-24 03:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.Canvas != nil {
|
|
|
|
log.Debug("EditorScene: Received Canvas from caller")
|
2018-08-17 03:37:19 +00:00
|
|
|
s.drawing.Load(s.drawing.Palette, s.Canvas)
|
2018-07-24 03:10:53 +00:00
|
|
|
s.Canvas = nil
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Initialize the user interface. It references the palette and such so it
|
|
|
|
// must be initialized after those things.
|
|
|
|
s.UI = NewEditorUI(d, s)
|
2018-07-24 03:10:53 +00:00
|
|
|
d.Flash("Editor Mode. Press 'P' to play this map.")
|
|
|
|
|
2018-07-21 22:11:00 +00:00
|
|
|
s.width = d.width // TODO: canvas width = copy the window size
|
|
|
|
s.height = d.height
|
2018-06-21 01:43:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop the editor scene.
|
2018-07-22 00:12:22 +00:00
|
|
|
func (s *EditorScene) Loop(d *Doodle, ev *events.State) error {
|
2018-08-05 19:54:57 +00:00
|
|
|
s.UI.Loop(ev)
|
2018-08-17 03:37:19 +00:00
|
|
|
s.drawing.Loop(ev)
|
2018-06-21 01:43:14 +00:00
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
// Switching to Play Mode?
|
|
|
|
if ev.KeyName.Read() == "p" {
|
|
|
|
log.Info("Play Mode, Go!")
|
|
|
|
d.Goto(&PlayScene{
|
2018-09-23 22:20:45 +00:00
|
|
|
// Canvas: s.drawing.Grid(), XXX
|
2018-07-24 03:10:53 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
d.Engine.Clear(render.Magenta)
|
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-06-21 01:43:14 +00:00
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// TODO: move inside the UI. Just an approximate position for now.
|
|
|
|
s.drawing.MoveTo(render.NewPoint(0, 19))
|
|
|
|
s.drawing.Resize(render.NewRect(d.width-150, d.height-44))
|
|
|
|
s.drawing.Compute(d.Engine)
|
|
|
|
s.drawing.Present(d.Engine, s.drawing.Point())
|
|
|
|
|
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-17 03:37:19 +00:00
|
|
|
return s.drawing.LoadFilename(filename)
|
2018-08-11 00:19:47 +00:00
|
|
|
|
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-07-22 03:43:01 +00:00
|
|
|
func (s *EditorScene) SaveLevel(filename string) {
|
|
|
|
s.filename = filename
|
2018-08-11 00:19:47 +00:00
|
|
|
|
|
|
|
m := level.New()
|
|
|
|
m.Title = "Alpha"
|
|
|
|
m.Author = os.Getenv("USER")
|
|
|
|
m.Width = s.width
|
|
|
|
m.Height = s.height
|
2018-08-17 03:37:19 +00:00
|
|
|
m.Palette = s.drawing.Palette
|
2018-09-23 22:20:45 +00:00
|
|
|
m.Chunker = s.drawing.Chunker()
|
2018-06-21 01:43:14 +00:00
|
|
|
|
|
|
|
json, err := m.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SaveLevel error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, json, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Create map file error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 03:10:53 +00:00
|
|
|
// Destroy the scene.
|
|
|
|
func (s *EditorScene) Destroy() error {
|
|
|
|
return nil
|
|
|
|
}
|