2018-08-11 00:19:47 +00:00
|
|
|
package level
|
|
|
|
|
2020-07-10 02:38:37 +00:00
|
|
|
import (
|
2024-05-27 22:14:00 +00:00
|
|
|
"encoding/json"
|
2023-02-18 20:45:36 +00:00
|
|
|
"errors"
|
2020-07-10 02:38:37 +00:00
|
|
|
"fmt"
|
2024-05-27 22:14:00 +00:00
|
|
|
"os"
|
2020-07-10 02:38:37 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
)
|
2018-08-11 00:19:47 +00:00
|
|
|
|
2023-02-18 20:45:36 +00:00
|
|
|
// Palettes are limited to uint8 in length, to aid image compression.
|
|
|
|
const PaletteSizeLimit = 256
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// DefaultPalette returns a sensible default palette.
|
|
|
|
func DefaultPalette() *Palette {
|
|
|
|
return &Palette{
|
|
|
|
Swatches: []*Swatch{
|
|
|
|
&Swatch{
|
|
|
|
Name: "solid",
|
|
|
|
Color: render.Black,
|
|
|
|
Solid: true,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "decoration",
|
|
|
|
Color: render.Grey,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "fire",
|
|
|
|
Color: render.Red,
|
|
|
|
Fire: true,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "water",
|
2019-07-14 21:18:44 +00:00
|
|
|
Color: render.RGBA(0, 0, 255, 180),
|
2018-08-11 00:19:47 +00:00
|
|
|
Water: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:43:23 +00:00
|
|
|
// NewBlueprintPalette returns the blueprint theme's color palette.
|
2021-06-10 05:36:32 +00:00
|
|
|
// DEPRECATED in favor of DefaultPalettes.
|
2019-06-26 00:43:23 +00:00
|
|
|
func NewBlueprintPalette() *Palette {
|
|
|
|
return &Palette{
|
|
|
|
Swatches: []*Swatch{
|
|
|
|
&Swatch{
|
|
|
|
Name: "solid",
|
|
|
|
Color: render.RGBA(254, 254, 254, 255),
|
|
|
|
Solid: true,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "decoration",
|
|
|
|
Color: render.Grey,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "fire",
|
|
|
|
Color: render.RGBA(255, 80, 0, 255),
|
|
|
|
Fire: true,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "water",
|
|
|
|
Color: render.RGBA(0, 153, 255, 255),
|
|
|
|
Water: true,
|
|
|
|
},
|
|
|
|
&Swatch{
|
|
|
|
Name: "electric",
|
|
|
|
Color: render.RGBA(255, 255, 0, 255),
|
|
|
|
Solid: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
// NewPalette initializes a blank palette.
|
|
|
|
func NewPalette() *Palette {
|
|
|
|
return &Palette{
|
|
|
|
Swatches: []*Swatch{},
|
|
|
|
byName: map[string]int{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 22:14:00 +00:00
|
|
|
// LoadPaletteFromFile reads a list of Swatches from a palette.json file.
|
|
|
|
func LoadPaletteFromFile(filename string) (*Palette, error) {
|
|
|
|
var (
|
|
|
|
pal = NewPalette()
|
|
|
|
bin, err = os.ReadFile(filename)
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(bin, &pal.Swatches)
|
|
|
|
pal.update()
|
|
|
|
return pal, err
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Palette holds an index of colors used in a drawing.
|
|
|
|
type Palette struct {
|
|
|
|
Swatches []*Swatch `json:"swatches"`
|
|
|
|
|
|
|
|
// Private runtime values
|
2018-08-17 03:37:19 +00:00
|
|
|
ActiveSwatch *Swatch `json:"-"` // name of the actively selected color
|
2018-08-11 00:19:47 +00:00
|
|
|
byName map[string]int // Cache map of swatches by name
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Inflate the palette swatch caches. Always call this method after you have
|
|
|
|
// initialized the palette (i.e. loaded it from JSON); this will update the
|
|
|
|
// "color by name" cache and assign the index numbers to each swatch.
|
|
|
|
func (p *Palette) Inflate() {
|
|
|
|
p.update()
|
|
|
|
}
|
|
|
|
|
2022-03-26 20:55:06 +00:00
|
|
|
// FlushCaches if you have modified the swatches, especially if you have
|
|
|
|
// changed the name of an existing color. This invalidates the "by name"
|
|
|
|
// cache and rebuilds it from scratch.
|
|
|
|
func (p *Palette) FlushCaches() {
|
|
|
|
p.byName = nil
|
|
|
|
p.update()
|
|
|
|
}
|
|
|
|
|
2023-02-18 20:45:36 +00:00
|
|
|
// NewSwatch adds a new swatch to the palette.
|
|
|
|
func (p *Palette) NewSwatch() (*Swatch, error) {
|
2020-07-10 02:38:37 +00:00
|
|
|
p.update()
|
|
|
|
|
|
|
|
var (
|
|
|
|
index = len(p.Swatches)
|
2020-11-17 07:20:24 +00:00
|
|
|
name = fmt.Sprintf("color %d", len(p.Swatches))
|
2020-07-10 02:38:37 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 20:45:36 +00:00
|
|
|
if index > PaletteSizeLimit {
|
|
|
|
return nil, errors.New("only 256 colors are supported in a palette")
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:38:37 +00:00
|
|
|
p.Swatches = append(p.Swatches, &Swatch{
|
|
|
|
Name: name,
|
|
|
|
Color: render.Magenta,
|
|
|
|
index: index,
|
|
|
|
})
|
|
|
|
p.byName[name] = index
|
|
|
|
|
2023-02-18 20:45:36 +00:00
|
|
|
return p.Swatches[index], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSwatch adds a new swatch to the palette.
|
|
|
|
func (p *Palette) AddSwatch(swatch *Swatch) error {
|
|
|
|
p.update()
|
|
|
|
|
|
|
|
var index = len(p.Swatches)
|
|
|
|
if len(p.Swatches) > PaletteSizeLimit {
|
|
|
|
return errors.New("only 256 colors are supported in a palette")
|
|
|
|
}
|
|
|
|
|
2024-05-27 22:14:00 +00:00
|
|
|
swatch.index = index
|
2023-02-18 20:45:36 +00:00
|
|
|
p.Swatches = append(p.Swatches, swatch)
|
|
|
|
p.byName[swatch.Name] = index
|
|
|
|
|
|
|
|
return nil
|
2020-07-10 02:38:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Get a swatch by name.
|
|
|
|
func (p *Palette) Get(name string) (result *Swatch, exists bool) {
|
|
|
|
p.update()
|
|
|
|
|
|
|
|
if index, ok := p.byName[name]; ok && index < len(p.Swatches) {
|
|
|
|
result = p.Swatches[index]
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the internal caches and such.
|
|
|
|
func (p *Palette) update() {
|
|
|
|
// Initialize the name cache if nil or if the size disagrees with the
|
|
|
|
// length of the swatches available.
|
|
|
|
if p.byName == nil || len(p.byName) != len(p.Swatches) {
|
|
|
|
// Initialize the name cache.
|
|
|
|
p.byName = map[string]int{}
|
|
|
|
for i, swatch := range p.Swatches {
|
|
|
|
swatch.index = i
|
|
|
|
p.byName[swatch.Name] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 04:18:22 +00:00
|
|
|
|
|
|
|
// ReplacePalette installs a new palette into your level.
|
|
|
|
// Your existing level colors, by index, are replaced by the incoming
|
|
|
|
// palette. If the new palette is smaller, extraneous indices are
|
|
|
|
// left alone.
|
|
|
|
func (l *Level) ReplacePalette(pal *Palette) {
|
|
|
|
for i, swatch := range pal.Swatches {
|
|
|
|
if i >= len(l.Palette.Swatches) {
|
|
|
|
l.Palette.Swatches = append(l.Palette.Swatches, swatch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ugly code, but can't just replace the swatch
|
|
|
|
// wholesale -- the inflated level data means existing
|
|
|
|
// pixels already have refs to their Swatch and they
|
|
|
|
// will keep those refs until you fully save and exit
|
|
|
|
// out of the editor.
|
|
|
|
l.Palette.Swatches[i].Name = swatch.Name
|
|
|
|
l.Palette.Swatches[i].Color = swatch.Color
|
|
|
|
l.Palette.Swatches[i].Pattern = swatch.Pattern
|
|
|
|
l.Palette.Swatches[i].Solid = swatch.Solid
|
|
|
|
l.Palette.Swatches[i].Fire = swatch.Fire
|
|
|
|
l.Palette.Swatches[i].Water = swatch.Water
|
|
|
|
}
|
|
|
|
}
|