animfile

package
v0.0.0-...-94fff03 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const AllClip = "all"

AllClip is the name of the one clip of a sheet that has no tags: every frame, in order, looping.

View Source
const BodyBoxID = "body"

BodyBoxID is the ID of the hurt boxes that come from "body".

View Source
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.

View Source
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.

View Source
const Version = 1

Version is the only format version this package reads and writes.

Variables

This section is empty.

Functions

func Diagnose

func Diagnose(f *File, s *Sheet) (*Resolved, []Finding)

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.

func Encode

func Encode(f *File) ([]byte, error)

Encode writes f as indented JSON (two spaces, clips in slice order, a trailing newline). It does not validate f.

Types

type Box

type Box struct {
	Kind BoxKind
	ID   string
	Rect Rect
}

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).

const (
	// BoxHurt is where the entity can be hit ("hurt", and the "body" boxes).
	BoxHurt BoxKind = iota
	// BoxHit is where the entity deals a hit ("hit").
	BoxHit
)

Box kinds.

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

func (c Clips) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler: an object with the clips in slice order.

func (*Clips) UnmarshalJSON

func (c *Clips) UnmarshalJSON(data []byte) error

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

func Decode(data []byte) (*File, error)

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

func Starter(s *Sheet, ref string) *File

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

type FrameRange struct {
	From int  `json:"from"`
	To   *int `json:"to,omitempty"`
}

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.

const (
	// Loop restarts from the first frame.
	Loop LoopMode = iota
	// Once stops on the last frame.
	Once
	// PingPong plays forward, then backward, without repeating the end frames.
	PingPong
)

Loop modes.

type NamedClip

type NamedClip struct {
	Name string
	Clip Clip
}

NamedClip is one entry of the "clips" object.

type Point

type Point struct {
	X int32 `json:"x"`
	Y int32 `json:"y"`
}

Point is an integer point in pixels.

func UnevenTrim

func UnevenTrim(s *Sheet, tag SheetTag) (a, b Point, uneven bool)

UnevenTrim reports whether the frames of tag have different trim offsets, and two offsets that differ.

type Rect

type Rect struct {
	X int32 `json:"x"`
	Y int32 `json:"y"`
	W int32 `json:"w"`
	H int32 `json:"h"`
}

Rect is an integer rectangle in pixels: X, Y is the top-left corner.

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

func Resolve(f *File, tags []Tag) (*Resolved, error)

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

func DecodeSheet(data []byte) (*Sheet, error)

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.

func (*Sheet) PlayOrder

func (s *Sheet) PlayOrder() []Tag

PlayOrder returns the sheet's tags as Resolve input: names, loop modes and durations in play order.

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

type SheetTag struct {
	Name     string
	From, To int
	Reverse  bool
	Loop     LoopMode
}

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

func (t SheetTag) FrameIndices() []int

FrameIndices returns the tag's file frame indices in play order (backward for a reverse tag).

type Tag

type Tag struct {
	Name      string
	Loop      LoopMode
	Durations []time.Duration
}

Tag is a clip of the sheet as Resolve needs it: name, loop mode and frame durations in play order. Get them from Sheet.PlayOrder, or from loaded clips.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL