2018-06-17 17:29:57 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-08-11 00:19:47 +00:00
|
|
|
"fmt"
|
2018-06-17 17:29:57 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToJSON serializes the level as JSON.
|
|
|
|
func (m *Level) ToJSON() ([]byte, error) {
|
|
|
|
out := bytes.NewBuffer([]byte{})
|
|
|
|
encoder := json.NewEncoder(out)
|
|
|
|
encoder.SetIndent("", "\t")
|
|
|
|
err := encoder.Encode(m)
|
|
|
|
return out.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// WriteJSON writes a level to JSON on disk.
|
|
|
|
func (m *Level) WriteJSON(filename string) error {
|
|
|
|
fh, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Level.WriteJSON(%s): failed to create file: %s", filename, err)
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
_ = fh
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:29:57 +00:00
|
|
|
// LoadJSON loads a map from JSON file.
|
2018-08-11 00:19:47 +00:00
|
|
|
func LoadJSON(filename string) (*Level, error) {
|
2018-06-17 17:29:57 +00:00
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2018-08-11 00:19:47 +00:00
|
|
|
return nil, err
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Decode the JSON file from disk.
|
2018-08-11 00:19:47 +00:00
|
|
|
m := New()
|
2018-06-17 17:29:57 +00:00
|
|
|
decoder := json.NewDecoder(fh)
|
|
|
|
err = decoder.Decode(&m)
|
2018-08-11 00:19:47 +00:00
|
|
|
if err != nil {
|
2018-09-23 22:20:45 +00:00
|
|
|
return m, fmt.Errorf("level.LoadJSON: JSON decode error: %s", err)
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Inflate the chunk metadata to map the pixels to their palette indexes.
|
|
|
|
m.Chunker.Inflate(m.Palette)
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Inflate the private instance values.
|
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
|
|
|
m.Palette.Inflate()
|
2018-08-11 00:19:47 +00:00
|
|
|
for _, px := range m.Pixels {
|
|
|
|
if int(px.PaletteIndex) > len(m.Palette.Swatches) {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"pixel %s references palette index %d but there are only %d swatches in the palette",
|
|
|
|
px, px.PaletteIndex, len(m.Palette.Swatches),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
px.Palette = m.Palette
|
|
|
|
px.Swatch = m.Palette.Swatches[px.PaletteIndex]
|
|
|
|
}
|
2018-06-17 17:29:57 +00:00
|
|
|
return m, err
|
|
|
|
}
|