doodle/level/types.go
Noah Petherbridge 0044b72943 Drag Doodads Onto Levels in Edit Mode
Add the ability to drag and drop Doodads onto the level. The Doodad
buttons on the palette now trigger a Drag/Drop behavior when clicked,
and a "blueprint colored" version of the Doodad follows your cursor,
centered on it.

Actors are assigned a random UUID ID when they are placed into a level.

The Canvas gained a MaskColor property that forces all pixels in the
drawing to render as the same color. This is a visual-only effect, and
is used when dragging Doodads in so they render as "blueprints" instead
of their actual colors until they are dropped.

Fix the chunk bitmap cache system so it saves in the $XDG_CACHE_FOLDER
instead of /tmp and has better names. They go into
`~/.config/doodle/chunks/` and have UUID file names -- but they
disappear quickly! As soon as they are cached into SDL2 they are removed
from disk.

Other changes:

- UI: Add Hovering() method that returns the widgets that are beneath
      a point (your cursor) and those that are not, for easy querying
      for event propagation.
- UI: Add ability to return an ErrStopPropagation to tell the master
      Scene (outside the UI) not to continue sending events to other
      parts of the code, so that you don't draw pixels during a drag
      event.
2018-10-20 16:03:59 -07:00

97 lines
2.2 KiB
Go

package level
import (
"encoding/json"
"fmt"
"git.kirsle.net/apps/doodle/balance"
"git.kirsle.net/apps/doodle/render"
)
// Base provides the common struct keys that are shared between Levels and
// Doodads.
type Base struct {
Version int `json:"version"` // File format version spec.
GameVersion string `json:"gameVersion"` // Game version that created the level.
Title string `json:"title"`
Author string `json:"author"`
// Every drawing type is able to embed other files inside of itself.
Files FileSystem `json:"files"`
}
// Level is the container format for Doodle map drawings.
type Level struct {
Base
Password string `json:"passwd"`
Locked bool `json:"locked"`
// Chunked pixel data.
Chunker *Chunker `json:"chunks"`
// The Palette holds the unique "colors" used in this map file, and their
// properties (solid, fire, slippery, etc.)
Palette *Palette `json:"palette"`
// Actors keep a list of the doodad instances in this map.
Actors ActorMap `json:"actors"`
}
// New creates a blank level object with all its members initialized.
func New() *Level {
return &Level{
Base: Base{
Version: 1,
},
Chunker: NewChunker(balance.ChunkSize),
Palette: &Palette{},
Actors: ActorMap{},
}
}
// Pixel associates a coordinate with a palette index.
type Pixel struct {
X int32 `json:"x"`
Y int32 `json:"y"`
PaletteIndex int32 `json:"p"`
// Private runtime values.
Swatch *Swatch `json:"-"` // pointer to its swatch, for when rendered.
}
func (p Pixel) String() string {
return fmt.Sprintf("Pixel<%s '%s' (%d,%d)>", p.Swatch.Color, p.Swatch.Name, p.X, p.Y)
}
// Point returns the pixel's point.
func (p Pixel) Point() render.Point {
return render.Point{
X: p.X,
Y: p.Y,
}
}
// MarshalJSON serializes a Pixel compactly as a simple list.
func (p Pixel) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(
`[%d, %d, %d]`,
p.X, p.Y, p.PaletteIndex,
)), nil
}
// UnmarshalJSON loads a Pixel from JSON again.
func (p *Pixel) UnmarshalJSON(text []byte) error {
var triplet []int32
err := json.Unmarshal(text, &triplet)
if err != nil {
return err
}
p.X = triplet[0]
p.Y = triplet[1]
if len(triplet) > 2 {
p.PaletteIndex = triplet[2]
}
return nil
}