Documentation
¶
Overview ¶
Package animfile reads and checks the file formats package anim loads, without Ebiten, the ECS or any GPU work: the Aseprite sprite sheet export (DecodeSheet) and the *.anim.json animation definition (Decode, Encode, Resolve).
Package anim's loaders are thin adapters over this package, and `wpix anim` uses it to validate and create definitions on the command line, so the loader and the tool cannot disagree about what a valid file is. It imports the standard library only.
The format reference is doc/guide/engine/animation.md.
Index ¶
- Constants
- func Diagnose(f *File, s *Sheet) (*Resolved, []Finding)
- func Encode(f *File) ([]byte, error)
- type Box
- type BoxKind
- type Clip
- type Clips
- type File
- type Finding
- type FrameRange
- type Level
- type LoopMode
- type NamedClip
- type Point
- type Rect
- type Resolved
- type ResolvedClip
- type Shape
- type Sheet
- type SheetFrame
- type SheetTag
- type Tag
Constants ¶
const AllClip = "all"
AllClip is the name of the one clip of a sheet that has no tags: every frame, in order, looping.
const BodyBoxID = "body"
BodyBoxID is the ID of the hurt boxes that come from "body".
const MaxPortableTexture = 8192
MaxPortableTexture is the largest texture side every target can load: WebGL's MAX_TEXTURE_SIZE is 8192 on many laptops, phones and browsers.
const Suffix = ".anim.json"
Suffix is the file name suffix of an animation definition. Package anim routes ClipSet keys with it to the definition loader.
const Version = 1
Version is the only format version this package reads and writes.
Variables ¶
This section is empty.
Functions ¶
func Diagnose ¶
Diagnose checks f against its sheet s the way the loader does and adds the warnings and notes of "wpix anim validate". It is the one place both the command and the editor take their findings from.
If Resolve fails, the result is nil and the findings are its errors, one per line. Otherwise it returns the resolved definition and the warnings (texture too large, uneven trims under a pivot) and infos (tags the file does not list), in that order.
Types ¶
type Box ¶
Box is a resolved hit or hurt box, relative to the pivot. Package anim converts it to gfx.Box.
type BoxKind ¶
type BoxKind uint8
BoxKind says what a Box is for. It mirrors gfx.BoxKind (same order).
type Clip ¶
type Clip struct {
Loop *bool `json:"loop,omitempty"`
Speed *float64 `json:"speed,omitempty"`
FPS *int `json:"fps,omitempty"`
Durations []int `json:"durations,omitempty"` // ms, one per frame
Pivot *Point `json:"pivot,omitempty"`
Shapes []Shape `json:"shapes,omitempty"`
}
Clip holds the overrides of one clip.
type Clips ¶
type Clips []NamedClip
Clips is the "clips" object in file order. A Go map would lose the order and silently drop duplicate names; both matter for the error report (and for Encode, which writes them in slice order).
func (Clips) MarshalJSON ¶
MarshalJSON implements json.Marshaler: an object with the clips in slice order.
func (*Clips) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. Unknown fields inside a clip are errors.
type File ¶
type File struct {
Version int `json:"version"`
Sheet string `json:"sheet"`
Pivot *Point `json:"pivot,omitempty"`
DefaultClip string `json:"defaultClip,omitempty"`
Body []Rect `json:"body,omitempty"`
Clips Clips `json:"clips,omitempty"`
}
File is an *.anim.json animation definition (version 1): gameplay data layered over an Aseprite export, its Sheet.
{
"version": 1,
"sheet": "hero.json",
"pivot": {"x": 19, "y": 45},
"defaultClip": "idle",
"body": [{"x": -4, "y": -13, "w": 8, "h": 13}],
"clips": {
"slash": {"loop": false, "fps": 8,
"shapes": [{"kind": "hit", "id": "slash", "frames": {"from": 2, "to": 3},
"rect": {"x": 6, "y": -11, "w": 37, "h": 10}}]}
}
}
All geometry is integer pixels: pivots in the trimmed sprite image, rects relative to the pivot, y down. Frame indices are in play order. Optional values are pointers so that "absent" differs from zero.
func Decode ¶
Decode parses an animation definition. It rejects syntax errors, unknown fields, a float where an int is expected, a version other than Version and a missing "sheet" or one that is itself a definition. Everything that needs the sheet is checked by Resolve.
func Starter ¶
Starter returns the definition "wpix anim create" writes for sheet s, whose path relative to the definition is ref: every tag in sheet order as {"loop": true}, which keeps the sheet's behavior (a ping-pong tag stays ping-pong) and shows where "false" goes; the first tag as defaultClip; no pivot, so the sheet's default origin applies, and no boxes.
type Finding ¶
type Finding struct {
Level Level
// Clip names the clip a warning or info is about; empty otherwise (an
// error line already names its clip and shape).
Clip string
Msg string
}
Finding is one line of a Diagnose report.
type FrameRange ¶
FrameRange is an inclusive range of play-order frame indices; a nil To means the single frame From.
type Level ¶
type Level string
Level is the severity of a Finding.
const ( // LevelError is anything the loader rejects. LevelError Level = "error" // LevelWarn is legal but probably not what the author wants. LevelWarn Level = "warn" // LevelInfo is something worth knowing that needs no change. LevelInfo Level = "info" )
Finding levels, as printed by "wpix anim validate".
type LoopMode ¶
type LoopMode uint8
LoopMode says what a clip does at its last frame. It mirrors anim.LoopMode (same names, same order), which package anim converts to.
type Resolved ¶
type Resolved struct {
// Default is "defaultClip"; empty if the file has none.
Default string
// Clips has one entry per tag, in tag order, including tags the file
// does not list (they get the top-level pivot and body only).
Clips []ResolvedClip
// Body is the union of the "body" rects, relative to the pivot; zero
// without any. It is the same for every clip: the fixed body that
// per-frame hurt shapes may replace (a collider uses this one).
Body Rect
// Omitted lists the tags without an entry in "clips", in tag order. It is
// information, not an error: those clips keep the sheet's loop mode and
// timing.
Omitted []string
}
Resolved is a definition applied to its sheet's tags.
func Resolve ¶
Resolve applies f to tags and checks everything that needs the sheet: unknown or duplicate clip names, an unknown "defaultClip", "body" and shape rects (w, h > 0), shape kinds and frame ranges, "durations" (length, positive), "fps" and "speed" (> 0). Every problem is reported, one per line of the joined error, each naming its clip and shape.
Rules: "loop" false is Once, true keeps Loop/PingPong (else Loop). Timing starts from "durations", else "fps" (1000/fps ms, whole ms, at least 1), else the tag's durations; "speed" then divides. Each frame's boxes are the body boxes, unless a hurt shape covers the frame (then the clip's hurt shapes replace them there), followed by the shapes covering the frame in file order.
type ResolvedClip ¶
type ResolvedClip struct {
Name string
// Listed reports whether the file has an entry for the clip.
Listed bool
Loop LoopMode
// Durations are the final frame durations, in play order.
Durations []time.Duration
// Pivot is the clip's pivot, else the top-level one; nil if neither.
Pivot *Point
// Boxes are the boxes per frame (nil for a frame without), all sharing
// one backing array with each frame's capacity capped at its length. Nil
// if the clip has no boxes at all.
Boxes [][]Box
}
ResolvedClip is one clip with every override applied.
type Shape ¶
type Shape struct {
Kind string `json:"kind"` // "hit" or "hurt"
ID string `json:"id,omitempty"`
Frames *FrameRange `json:"frames,omitempty"` // nil means every frame
Rect Rect `json:"rect"`
}
Shape is a hit or hurt box over a range of frames.
type Sheet ¶
type Sheet struct {
// Frames are the frames in file order.
Frames []SheetFrame
// Tags are the clips in file order. A file without tags has one tag,
// AllClip, over every frame.
Tags []SheetTag
// Size is the sheet image's size in pixels ("meta.size"); zero when
// absent.
Size Point
}
Sheet is an Aseprite "Sprite sheet" export (File > Export Sprite Sheet, JSON data with "Meta: Tags" checked), hash or array form:
{
"frames": { "hero 0.aseprite": { "frame": {...}, "duration": 100,
"spriteSourceSize": {"x":73,"y":54,...} }, ... },
"meta": { "image": "hero.png",
"frameTags": [ {"name":"run","from":2,"to":5,"direction":"forward"} ] }
}
Only what animation needs is read: per-frame durations and trim offsets, and the tags. The frame rectangles belong to the atlas loader, which reads the same file and keeps the same (file) order.
func DecodeSheet ¶
DecodeSheet parses and checks an Aseprite export. Unknown fields are allowed (the export has many). The frames are checked first; if they are fine, every tag problem is reported, joined into one error.
type SheetFrame ¶
type SheetFrame struct {
// Name is the frame's key (hash form) or "filename" (array form).
Name string
// Duration is the positive frame duration.
Duration time.Duration
// Trimmed reports whether the export trimmed the frame's transparent
// border ("trimmed").
Trimmed bool
// Offset is where the trimmed image starts in the untrimmed frame
// ("spriteSourceSize" x, y); zero when absent.
Offset Point
}
SheetFrame is one frame of a Sheet.
type SheetTag ¶
SheetTag is one Aseprite tag: a clip over the inclusive file frame range From..To.
Directions: forward and reverse loop; pingpong and pingpong_reverse ping-pong. Reverse directions play the frames backward. A tag's "repeat" is ignored.
func (SheetTag) FrameIndices ¶
FrameIndices returns the tag's file frame indices in play order (backward for a reverse tag).