editor

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: 30 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 editor is the core of the Weakpixel level editor: a game.Tool that edits one GOD level of a running game. It owns the level's objects by document id, applies new versions of the level document by id, picks, selects and drags objects with the mouse, moves an editor camera while the game is paused, and runs Play/Stop.

It has no widgets and no undo history. A shell drives it: the VSCode extension sends every change of the level's text through Session.Apply and turns the session's [Event]s into text edits, so VSCode owns undo, save and diff. See doc/guide/engine/editor.md.

A Session is used from the game goroutine only.

Index

Constants

View Source
const ActiveFor = 500 * time.Millisecond

ActiveFor is how long the active rate lasts after the last activity.

View Source
const InjectedPad input.DeviceID = 0xFFFF

InjectedPad is the device ID of the virtual gamepad of Injection.Pad: the highest ID, far from the IDs a device source assigns.

View Source
const ParentKey = "hierarchy:Parent"

ParentKey is the component key of hierarchy.Parent. The handles of an object that has one edit its Local (see [Session.syncLocal]) instead of the Transform, which the hierarchy stage derives every tick.

View Source
const TransformKey = "gfx:Transform"

TransformKey is the component key of gfx.Transform, the component the viewport moves objects with and the drop places them by.

Variables

View Source
var DefaultRates = Rates{Idle: 10, Active: 60, Hidden: 0}

DefaultRates are the rates a session starts with.

Functions

This section is empty.

Types

type Applied

type Applied struct{ Err error }

Applied reports the outcome of applying the level document later than Session.Open or Session.Apply returned: once the prefabs it waited for are loaded (see Session.Pending), or again after a prefab it uses changed. Err is nil when the document was applied; otherwise the level keeps its previous state.

type Camera

type Camera struct{ X, Y, Zoom float32 }

Camera reports the editor camera after it stopped moving (a pan or zoom ended, or the view was framed), so a shell can restore it with Session.SetCamera after it rebuilds the game. Zoom is never 0.

type Config

type Config struct {
	// Path names the level: its asset scope and game.Level.Path. Nothing
	// is read from it; the document comes from [Session.Open].
	Path string
	// Events receives what the user did in the viewport, synchronously on
	// the game goroutine. Nil drops them.
	Events func(Event)
	// OnReset is called by [Session.Stop] after the level objects are
	// restored, for game state outside the level (a hero a system spawned,
	// a score). Nil does nothing.
	OnReset func(e *game.Engine)
	// OnOpen is called by [Session.OpenAt] with the new file's path, after
	// Play stopped and before the document is opened, so a host can load
	// what surrounds the level (its map) or unload the previous one's. Nil
	// does nothing.
	OnOpen func(e *game.Engine, path string)
	// Stage makes the session a prefab stage: see [Stage]. Nil, the
	// default, is the level editor.
	Stage *Stage
	// FixedRate turns pacing off: the session never changes the tick rate
	// and every frame is drawn. A headless run sets it, since it ticks on
	// its own clock. See [Session.SetRates].
	FixedRate bool
}

Config configures a Session.

type Edit

type Edit struct {
	ID string
	// Part is set for an object of a multi-object prefab instance that is
	// not its root: ID is then the id of the instance root, and with
	// Override JSON is the override of the part, to go to
	// "children.<Part>" of the instance's entry.
	Part string
	// Component is the component key, "group:name" (e.g. [TransformKey]).
	Component string
	JSON      []byte
	Override  bool
}

Edit reports that the user changed one component of one object in the viewport (a finished drag). JSON is the whole component as the document holds it (asset keys, entity ids), ready to replace the old value.

For an instance of a prefab Override is set and JSON is the component's override instead: only the fields that differ from the prefab, ready to replace the instance's "components" entry, or {} when nothing differs (the shell then drops the entry).

type Event

type Event interface {
	// contains filtered or unexported methods
}

Event is something the session reports to its shell: an Edit, a Selection, a Mode, a Camera or Applied.

type Injection

type Injection struct {
	// Keys are held down.
	Keys []input.Key
	// Mouse, when set, moves the cursor to its X and Y (logical pixels),
	// holds its Buttons down in addition to the device's and adds its wheel
	// movement on every tick.
	Mouse *input.MouseFrame
	// Pad, when set, is a virtual standard gamepad with these buttons,
	// triggers and axes. It is announced as connected (as InjectedPad) the
	// first time and stays connected for the session's life, so the game's
	// router can assign it to a player like any pad. Its Device and
	// Standard fields are set by the session.
	Pad *input.PadFrame
	// Ticks is how many game ticks the input is held; 0 or less means 1.
	Ticks int
}

Injection is input a shell adds to the device's while the game plays, for an agent or a test that drives the game without a keyboard (see Session.Inject).

type LayersChanged

type LayersChanged struct{ Layers []MapLayer }

LayersChanged reports the map layers after they changed (a backdrop loaded, replaced or hot-reloaded, a layer hidden). Layers is in draw order and empty when the level has no backdrop.

type MapLayer

type MapLayer struct {
	// Map is the map's asset key (the .tmj file) and Path the layer's Tiled
	// path ("group/layer"); together they name the layer. Paths need not be
	// unique.
	Map, Path string
	// Name is the layer's own name in Tiled.
	Name string
	// Kind is "tiles" or "image".
	Kind string
	// Layer is the render layer the layer was spawned on
	// (tilemap.TileLayer.Layer), which a game's LayerFor may have chosen.
	Layer int16
	// Hidden is whether the layer is not drawn: invisible in Tiled, or
	// hidden by [Session.SetMapLayerHidden].
	Hidden bool
}

MapLayer is one tile or image layer the game spawned for a Tiled map behind the level (its backdrop), as a shell lists it.

type Mode

type Mode struct{ Play, Paused bool }

Mode reports a switch between editing (Play false) and playing, and between a running and a paused Play (Session.Pause). Paused is never set in edit mode.

type Rates

type Rates struct {
	Idle, Active, Hidden int
}

Rates are the tick rates of an editor game, in ticks per second: Idle while nothing happens in edit mode (or in a paused Play), Active while the user works in the view and for ActiveFor after, and Hidden while the host says the view is not on screen (0 stops ticks until a message or input wakes the game). An unpaused Play runs at the game's own rate.

func (Rates) Check

func (r Rates) Check() error

Check reports what makes r unusable: a negative rate, or an Active rate of 0 (the view would stop while the user works in it).

type Selection

type Selection struct{ IDs []string }

Selection reports the selected object ids after a change, in selection order. Empty means nothing is selected.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is the editor core; it implements game.Tool. Create it with New, set it as game.Config.Tool, and call Session.Open once the engine exists (for example in a Start hook).

func New

func New(cfg Config) *Session

New returns a session in edit mode with no level. It does nothing until it is attached to an engine (game.Config.Tool) and Session.Open is called.

func (*Session) Animate

func (s *Session) Animate() bool

Animate reports what Session.SetAnimate set last.

func (*Session) Apply

func (s *Session) Apply(text []byte) error

Apply applies a new version of the level document by object id: objects with a new id are created, those whose id is gone are deleted, and only the components whose JSON changed are set. It is all or nothing: on any problem (bad JSON, missing ids, a component the schema rejects) the level is left as it was and the problems are returned, joined.

Prefab instances are expanded first (overrides merged over the prefab), so the comparison sees merged components. If a prefab is still loading, Apply returns nil and the document waits (Session.Pending); it replaces any document that waited before, and its outcome is reported as Applied.

func (*Session) Bounds

func (s *Session) Bounds(id string) (gfx.Rect, bool)

Bounds returns the world bounds of the object with id, as picking sees them.

func (*Session) Camera

func (s *Session) Camera() *render.Camera

Camera returns the camera the viewport shows: the editor camera in edit mode, the game's in Play.

func (*Session) Colliders

func (s *Session) Colliders() bool

Colliders reports whether the collider outlines of edit mode are on.

func (*Session) DefaultLayer

func (s *Session) DefaultLayer(pos gfx.Vec2) (int16, bool)

DefaultLayer returns the render layer a new object placed at pos starts on, so it lands between the tile layers of the Tiled map the way it would sit in Tiled: the render layer of the nearest drawable layer below the map's topmost object layer, or the map's lowest render layer when there is none below. With several maps (a world) it uses the map whose bounds contain pos, else the first. It reports false when there is no backdrop map or the map has no object layer, and the caller keeps its own default.

func (*Session) DrawFinal

func (s *Session) DrawFinal(e *game.Engine, screen ebiten.FinalScreen)

DrawFinal implements game.Tool: it draws the Stage grid and, in edit mode, the collider outlines, the markers of objects without art, the selection box, the selection's outlines and the gizmos.

func (*Session) DrawFrame

func (s *Session) DrawFrame(e *game.Engine) bool

DrawFrame implements game.DrawGate: in an unpaused Play every frame is drawn; in edit mode, a paused Play and a stage only the first frame after a tick (nothing else changes what Draw shows), and only when that tick saw activity, Session.Invalidate was called or game.Engine.FrameChanged reports a new picture; while hidden none. With Config.FixedRate every frame is drawn.

func (*Session) Draws

func (s *Session) Draws() uint64

Draws is the number of frames drawn since the session started; with Config.FixedRate it stays 0.

func (*Session) Engine

func (s *Session) Engine() *game.Engine

Engine returns the engine the session is attached to (nil before Init).

func (*Session) Entity

func (s *Session) Entity(id string) (ecs.Entity, bool)

Entity returns the entity of the object with id.

func (*Session) Frame

func (s *Session) Frame()

Frame centers the camera on the level's objects (on the selection when there is one). It does nothing when there is nothing to frame.

func (*Session) IDs

func (s *Session) IDs() []string

IDs returns the ids of the level's objects in document order.

func (*Session) InStage

func (s *Session) InStage() bool

InStage reports whether the session is a prefab Stage.

func (*Session) Init

func (s *Session) Init(e *game.Engine)

Init implements game.Tool: it attaches the session to e.

func (*Session) Inject

func (s *Session) Inject(in Injection)

Inject adds in to the input the game reads for in.Ticks game ticks, from the next one on, on top of what the devices report. It replaces an injection that has ticks left. It works in Play only: edit mode ignores it, and Session.Stop drops what is left. A paused Play (Session.Pause) reads no input, so the ticks count only the ticks that run (Session.Step runs one).

func (*Session) Input

func (s *Session) Input(src input.Source) input.Source

Input implements game.Tool. The game reads the device through the returned source. The session polls the device itself in edit mode (the paused engine skips its input stage), and hands the pad connects and disconnects it saw then to the game's next poll, so none is lost.

func (*Session) Invalidate

func (s *Session) Invalidate()

Invalidate makes the next tick's frame drawn even if the game's picture did not change: what the session draws over it (selection, outlines, grid) may have. The bridge calls it for every message that may change the session. Call it on the game goroutine.

func (*Session) Level

func (s *Session) Level() *game.Level

Level returns the edited level, or nil before Session.Open.

func (*Session) MapLayers

func (s *Session) MapLayers() []MapLayer

MapLayers returns the tile and image layers of the Tiled maps spawned in the world (the level's backdrop), in draw order. Object and group layers spawn nothing and are not listed.

func (*Session) Name

func (s *Session) Name() string

Name returns the top-level "name" of the last document Session.Open accepted, or "" if it had none or nothing was opened. A document Open rejects for its own problems leaves the name unchanged.

func (*Session) Open

func (s *Session) Open(text []byte) error

Open loads text, a level document, as the edited level: every object must have a unique "id". It returns the document's problems, joined; nothing is spawned then. Open replaces a level opened before.

Objects may be prefab instances ("prefab" and overrides): the session loads their prefabs into the level's scope. If one is still loading, Open returns nil with the level empty and Session.Pending true; the document is applied in a later tick and the outcome reported as Applied.

func (*Session) OpenAt

func (s *Session) OpenAt(path string, text []byte) error

OpenAt switches the session to another file: text, a level document, is opened like Session.Open as the level at path (its asset scope and game.Level.Path, replacing Config.Path). Before that it stops Play (as Session.Stop does), clears the selection (emitting Selection) and forgets a camera restored with Session.SetCamera, so the new level is framed unless the shell calls SetCamera after OpenAt.

A document that does not parse is returned as an error with nothing changed. One that parses but fails to apply leaves no level open, as Open does.

func (*Session) Pause

func (s *Session) Pause(on bool)

Pause pauses (on) or resumes Play: a paused game keeps drawing, its systems do not run, the tool clock stands still (animations, particles and tweens stop), and Session.Step advances it one tick. The level is not reset; Session.Stop ends Play as usual. A change emits Mode. It does nothing in edit mode, where the game is always paused.

func (*Session) Paused

func (s *Session) Paused() bool

Paused reports whether Play is paused by Session.Pause. It is false in edit mode.

func (*Session) Pending

func (s *Session) Pending() bool

Pending reports whether a document given to Open or Apply waits for its prefabs to load.

func (*Session) Pick

func (s *Session) Pick(p gfx.Vec2) (string, bool)

Pick returns the id of the front-most object of the level whose bounds contain the world position p. When none does, an object smaller on screen than minPickPx is picked within that size around its center (the nearest center wins), so small objects stay clickable.

func (*Session) Play

func (s *Session) Play()

Play runs the game from the edited state: the game camera comes back and the game gets the input. Session.Stop returns to the state before Play. It emits Mode. Playing twice is a no-op, and so is playing in a Stage.

func (*Session) Playing

func (s *Session) Playing() bool

Playing reports whether the session is in Play mode.

func (*Session) Rates

func (s *Session) Rates() Rates

Rates returns the rates set with Session.SetRates; the zero Rates with Config.FixedRate.

func (*Session) Reveal

func (s *Session) Reveal()

Reveal pans the editor camera to the selection when it is not in view: the union of the selected objects' bounds must lie inside the visible world rectangle shrunk by 10% per side, else the camera centers on it. The zoom never changes. It does nothing when nothing is selected, none of the selection has bounds, or in Play. The pan is reported as a Camera event like any other.

func (*Session) Select

func (s *Session) Select(ids ...string)

Select replaces the selection with the ids that name objects of the level (others are ignored). It does not emit a Selection event: the caller knows what it selected.

func (*Session) Selected

func (s *Session) Selected() []string

Selected returns the selected ids in selection order.

func (*Session) SetAnimate

func (s *Session) SetAnimate(on bool)

SetAnimate lets the level move on its own in edit mode and in a Stage (on, the default), or freezes it there: with it off, the session pauses the tool clock (game.Engine.ToolTime) while not in Play, so sprite clips, animated tiles, particles, tweens and the shader Time stand still and an idle view draws no frames (see game.DrawGate). Play always animates, except while it is paused (Session.Pause pauses the tool clock too). It takes effect at the next tick.

func (*Session) SetCamera

func (s *Session) SetCamera(pos gfx.Vec2, zoom float32)

SetCamera puts the editor camera at pos with zoom, for a shell restoring the view it saved from a Camera event after a rebuild. The level is not framed when it opens afterwards, and nothing is reported back for it. A zoom of 0 or less means 1. In Play it takes effect when Play stops.

func (*Session) SetColliders

func (s *Session) SetColliders(show bool)

SetColliders turns the collider outlines drawn in edit mode on (the default) or off.

func (*Session) SetGrid

func (s *Session) SetGrid(grid float32)

SetGrid sets the grid step of a prefab Stage in world units; 0 or less draws no grid. It does nothing in a level session.

func (*Session) SetMapLayerHidden

func (s *Session) SetMapLayerHidden(mapKey, path string, hidden bool) error

SetMapLayerHidden hides or shows the map layer named by the map key and Tiled path of a MapLayer, in edit mode only: nothing is written to the map, which Tiled owns. Play and Stop keep it; a map hot reload or a new backdrop starts from Tiled's visibility again. It returns an error for an unknown layer and in Play.

func (*Session) SetRates

func (s *Session) SetRates(r Rates) error

SetRates sets the tick rates the session paces the game with (see Rates); a session starts with DefaultRates. It returns the error of Rates.Check and changes nothing then. With Config.FixedRate it does nothing.

func (*Session) SetSnap

func (s *Session) SetSnap(grid float32)

SetSnap sets the grid dragged objects snap to, in world units; 0 or less turns snapping off. Holding Alt while dragging ignores it. The grid rounds the object's Transform.Pos, so objects end on multiples of it.

func (*Session) SetVisible

func (s *Session) SetVisible(visible bool)

SetVisible says whether the host shows the view. A hidden view ticks at the hidden rate and draws nothing; showing it again counts as activity. A session starts visible.

func (*Session) Snap

func (s *Session) Snap() float32

Snap returns the snap grid, 0 when off.

func (*Session) Step

func (s *Session) Step()

Step makes the next tick run the game once while it is paused: in edit mode, or in Play paused by Session.Pause. In Play that runs, and in a Stage, it does nothing.

func (*Session) Stop

func (s *Session) Stop()

Stop ends Play (paused or not): entities created during Play are deleted, the level's objects are restored from the document (every component, and objects the game deleted come back), the selection is kept by id, every tween is cleared, Config.OnReset is called and the editor camera returns. It emits Mode. Stopping in edit mode is a no-op.

func (*Session) TPS

func (s *Session) TPS() int

TPS is the tick rate the session paces the game at in its current state: a message that woke a hidden or idle game is handled at the active rate for a tick, but TPS reports the rate the game goes back to. With Config.FixedRate it is ebiten.TPS.

func (*Session) Tick

func (s *Session) Tick(e *game.Engine) game.TickMode

Tick implements game.Tool: in Play it runs the game (unless Session.Pause paused it); in edit mode it handles the mouse and pauses the game. A paused game runs one tick after Session.Step.

func (*Session) Visible

func (s *Session) Visible() bool

Visible reports what Session.SetVisible last set.

func (*Session) Wake

func (s *Session) Wake()

Wake makes the next tick come within a frame: unless the game plays, the rate goes up to the active one at once, and that tick sets the rate of its state again (a wake alone does not keep the active rate). Call it when a message for the game arrives or the user touches the view. It is safe to call from any goroutine.

func (*Session) WindowToWorld

func (s *Session) WindowToWorld(p image.Point) gfx.Vec2

WindowToWorld converts a position in window pixels (where a shell's drop or click lands on the game's canvas, in device pixels) to world units through the final-screen transform and the current camera.

type Stage

type Stage struct {
	// Grid is the grid step in world units; 0 draws no grid.
	Grid float32
}

Stage configures a session as a prefab stage: an empty scene to edit one document in. The viewport draws a grid and the origin, and the game cannot be played or stepped (the host starts no game systems in it either). Pick the stage's background with game.Config.Background.

Directories

Path Synopsis
Package animedit is the "anim" tool of wpix-editor: an editor for *.anim.json animation definitions (doc/guide/engine/animation.md).
Package animedit is the "anim" tool of wpix-editor: an editor for *.anim.json animation definitions (doc/guide/engine/animation.md).
Package bridge connects an editor.Session to a host over JSON messages: the VSCode extension (through the webview, see transport_js.go) or, in tests, a fake.
Package bridge connects an editor.Session to a host over JSON messages: the VSCode extension (through the webview, see transport_js.go) or, in tests, a fake.
Package camzoom is the wheel zoom of the editors' cameras: each wheel unit scales the zoom by a fixed factor (so a trackpad's small deltas zoom slowly and a mouse notch a little), and the camera eases toward the target zoom around the cursor instead of jumping.
Package camzoom is the wheel zoom of the editors' cameras: each wheel unit scales the zoom by a fixed factor (so a trackpad's small deltas zoom slowly and a mouse notch a little), and the camera eases toward the target zoom around the cursor instead of jumping.
Package filekind is the one list of the file kinds wpix-editor opens: the name suffixes of each kind, whether it is edited or previewed and whether it has sound.
Package filekind is the one list of the file kinds wpix-editor opens: the name suffixes of each kind, whether it is edited or previewed and whether it has sound.
Package fxedit is the "fx" tool of wpix-editor: it makes a particle effect, an *.fx.json file (package particles), without knowing which components are involved.
Package fxedit is the "fx" tool of wpix-editor: it makes a particle effect, an *.fx.json file (package particles), without knowing which components are involved.
Package host is the entry point of a game's editor build.
Package host is the entry point of a game's editor build.
Package hostlink is what a game or tool that runs under an editor host shares with that host, whichever protocol it speaks: the protocol version, the transport interface, the connection to the host page of a browser build, and the settings the host passes (stage, first file, whether it is untitled, asset root, file index).
Package hostlink is what a game or tool that runs under an editor host shares with that host, whichever protocol it speaks: the protocol version, the transport interface, the connection to the host page of a browser build, and the settings the host passes (stage, first file, whether it is untitled, asset root, file index).
Package sheetedit is the "sheet" tool of wpix-editor: an editor for *.sheet.json grid sheets (doc/guide/engine/asset-kinds.md#grid-sheet).
Package sheetedit is the "sheet" tool of wpix-editor: an editor for *.sheet.json grid sheets (doc/guide/engine/asset-kinds.md#grid-sheet).
Package shotproto is the screenshot message pair both editor protocols share, the level editor's bridge and the tool shell: a host asks for a capture with a "screenshot" request, the game answers with a "screenshot" message that carries the PNG.
Package shotproto is the screenshot message pair both editor protocols share, the level editor's bridge and the tool shell: a host asks for a capture with a "screenshot" request, the game answers with a "screenshot" message that carries the PNG.
Package toolshell is the shell the wpix-editor tools run on: a game.Tool on an engine with no game systems, so assets load through the engine's asset server and the content is drawn by the engine's renderer.
Package toolshell is the shell the wpix-editor tools run on: a game.Tool on an engine with no game systems, so assets load through the engine's asset server and the content is drawn by the engine's renderer.
debugpanel
Package debugpanel draws a toolshell.Panel with debugui, in the native window of wpix-editor.
Package debugpanel draws a toolshell.Panel with debugui, in the native window of wpix-editor.
Package viewer is the "view" tool of wpix-editor: it previews every file kind of package filekind that is not edited, with the engine's own loaders.
Package viewer is the "view" tool of wpix-editor: it previews every file kind of package filekind that is not edited, with the engine's own loaders.
Package viewkit holds what the wpix-editor tools that show a texture share: the texture sprite kept on screen (Texture), the frames over it with hover, selection, labels and a list to pick from (Frames), and the overlay text in window pixels (Labels).
Package viewkit holds what the wpix-editor tools that show a texture share: the texture sprite kept on screen (Texture), the frames over it with hover, selection, labels and a list to pick from (Frames), and the overlay text in window pixels (Labels).
Package voiceview is the voice and dialogue previewer of wpix-editor, routed by package viewer: a read-only preview with sound of the files of package speech.
Package voiceview is the voice and dialogue previewer of wpix-editor, routed by package viewer: a read-only preview with sound of the files of package speech.

Jump to

Keyboard shortcuts

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