Noah Petherbridge
5956863996
* 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.
48 lines
1000 B
Go
48 lines
1000 B
Go
package level
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"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
|
|
}
|
|
|
|
// LoadJSON loads a map from JSON file.
|
|
func LoadJSON(filename string) (*Level, error) {
|
|
fh, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fh.Close()
|
|
|
|
m := New()
|
|
decoder := json.NewDecoder(fh)
|
|
err = decoder.Decode(&m)
|
|
if err != nil {
|
|
return m, err
|
|
}
|
|
|
|
// Inflate the private instance values.
|
|
m.Palette.Inflate()
|
|
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]
|
|
}
|
|
return m, err
|
|
}
|