editorcmd

package
v0.4.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 27 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 editorcmd is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package editorcmd is the scene editor's command line, which `go tool game editor` runs and which a game's own editor command runs with the game's component types and editor plugins built in. `game generate editor` writes such a command into a project: a main function built with the gameeditor tag that calls Main with the project's Components and plugins, so its scenes' components are checked against their registered types. `go tool game editor <dir>` runs that command when the project has one.

An editor command opens a project, as its directory or its game.yaml, which opens its main scene, or a scene file, which inside a project opens that project too, and optionally an animation clip on one of the scene's objects. The editor New returns sets editor.Editor.ProjectAssets, so an open project's scenes draw its own atlases, its atlases fill the asset browser and its scene files can be placed as instances (project.Resolver).

Index

Examples

Constants

View Source
const (
	DefaultWindowWidth  = 1120
	DefaultWindowHeight = 680
)

Default window size, in points, Show opens the editor at.

View Source
const BuildInfoVersion = 1

BuildInfoVersion is the version of the JSON build info file -build-hook reads (MarshalBuildInfo, ParseBuildInfo).

View Source
const Usage = "[project-dir | game.yaml | scene.yaml] [clip.animation.yaml [object-id]]"

Usage is the arguments an editor command takes after its flags.

Variables

This section is empty.

Functions

func IsProject

func IsProject(p string) bool

IsProject reports whether p names a project rather than a scene file: a directory, or a file named game.yaml.

func Main

func Main(opts Options)

Main is a game's editor command: it runs Run with the process's arguments and opts, and exits with status 2 for a usage error and 1 for any other, after printing it.

func MarshalBuildInfo

func MarshalBuildInfo(b editor.BuildInfo) ([]byte, error)

MarshalBuildInfo encodes b as the JSON build info file an editor command's -build-info flag reads: version BuildInfoVersion, then b's fields in a fixed order, indented, ending in a newline.

func New

func New(opts Options) (*editor.Editor, error)

New returns an editor on an empty scene with opts' component types, table row types, plugins, name and library, and ProjectAssets set, so the project Open opens supplies the scenes' assets. A scene opened outside any project resolves no atlases.

func Open

func Open(ed *editor.Editor, args []string) error

Open opens in ed what an editor command's arguments name, as Usage gives them. args[0] is a project, as its directory or its game.yaml, which opens the project and its main scene (editor.Editor.OpenProject), or a scene file, which opens with the project whose directory holds it, when one does (editor.Editor.OpenProjectScene, which sets ScenePath), and on its own otherwise (editor.Editor.OpenFile). args[1] is an animation clip file the animation panel opens on the object named by args[2], or on the scene's first object; that object is selected, so when the clip file does not exist yet the toolbar's Animate button starts a clip on it, and the panel's Save Clip button writes the clip to args[1]. No arguments open nothing.

Example

A game's editor command builds its editor with the game's component types, and opens a project with them.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/nsgo-dev/game/pkg/editor/editorcmd"
	"github.com/nsgo-dev/game/pkg/sceneformat"
)

func main() {
	dir, err := os.MkdirTemp("", "editorcmd")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer os.RemoveAll(dir)
	files := map[string]string{
		"game.yaml":              "version: 2\nname: Demo\nid: dev.example.demo\nappVersion: 1.0.0\nwindow:\n  width: 320\n  height: 180\nautoload:\n  mainScene: room\n  scenes:\n    - name: room\n      path: scenes/room.scene.yaml\n",
		"scenes/room.scene.yaml": "version: 2\nname: Room\nlayers:\n  - name: main\n    objects:\n      - id: hero\n        components:\n          health: {hp: 3}\n",
	}
	for name, data := range files {
		p := filepath.Join(dir, filepath.FromSlash(name))
		if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
			fmt.Println(err)
			return
		}
		if err := os.WriteFile(p, []byte(data), 0o644); err != nil {
			fmt.Println(err)
			return
		}
	}

	components := sceneformat.NewComponentRegistry()
	components.MustRegister(sceneformat.ComponentType{Name: "health", Fields: []sceneformat.Field{{Name: "hp", Kind: sceneformat.FieldInt, Default: 1}}})
	ed, err := editorcmd.New(editorcmd.Options{Components: components})
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := editorcmd.Open(ed, []string{dir}); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(ed.Doc.Name, ed.ScenePath)
	hp, _ := ed.ComponentField("hero", "health", "hp")
	fmt.Println(hp)
}
Output:
Room scenes/room.scene.yaml
3

func ParseBuildInfo

func ParseBuildInfo(data []byte) (editor.BuildInfo, error)

ParseBuildInfo decodes a JSON build info file, refusing unknown fields, trailing data, a version other than BuildInfoVersion, and a projectDir or outputDir that is missing or not absolute.

func ProjectRoot

func ProjectRoot(p string) (string, bool)

ProjectRoot returns the directory of the project p belongs to, and false when it belongs to none: p itself for a directory holding a game.yaml, the directory of a game.yaml, and for any other file the nearest directory above it that holds a game.yaml, unless a directory nearer the file holds an nsgo.yaml or a go.mod, which makes that directory the file's root as game check finds it. For a file, the directory it returns is absolute.

func Run

func Run(args []string, opts Options, stderr io.Writer, launch func(*editor.Editor)) error

Run is an editor command run with args, its command line after the program's name: it parses the -library, -schema, -check, -rename-text-key, -dry-run, -assets, -force, -assets-check, -resources, -build-hook and -build-info flags and the arguments Usage gives. With -schema it writes the project's scene JSON Schema (using opts.Components after adding built-in physics and skeletal components) to opts.Stdout (or os.Stdout) and returns without opening a window. With -check dir it checks the scenes and assets under dir with assetcheck.CheckWith using opts.Components (after adding built-in physics and skeletal components) and checks tables matching opts.DataTables, printing problems to stderr and the summary to opts.Stdout (or os.Stdout) without opening a window. With -rename-text-key dir [-dry-run] from to it plans renaming a localization text key across screens, YAML string tables, and `textkey` columns of tables matching opts.DataTables, printing the plan to opts.Stdout (or os.Stdout) and applying it unless -dry-run is set, without opening a window. With -assets game.yaml [-force | -assets-check] [-resources] it builds (or checks) the asset targets declared in game.yaml using opts.DataTables for dataTables targets, without opening a window. With -build-hook before|after -build-info file it opens the project the JSON build info file (ParseBuildInfo) names, lists the plugins that are on and set that build hook on opts.Stdout (or os.Stdout), and runs them in plugins: order (editor.Editor.RunBuildHooks), as game release does around nsgo build, without opening a window; a hook's error names its plugin. Otherwise it builds the editor with New, opens the arguments with Open, or the project in the working directory without any, refuses a project listing plugins the editor lacks when opts.RequirePlugins is set, and shows the editor with launch, or when launch is nil as Show does, in a window of opts.WindowWidth by opts.WindowHeight. Usage and flag errors are reported on stderr and wrap a usage error that Main exits 2 for.

func Serve

func Serve(cx *core.Scope, ed *editor.Editor, stderr io.Writer) *editormcp.Server

Serve serves ed's MCP server (package editormcp) on the endpoint of the project ed has open, and of each project it opens later, until cx is cleaned up, and offers its tools to nsgo mcp under go tool nsgo dev -automation, so an agent reaches a game's own editor as it reaches go tool game editor. An endpoint it cannot open, as when another editor has the project open, is reported on stderr; the editor works without it.

func Show

func Show(ed *editor.Editor)

Show shows ed in the scene editor's window, DefaultWindowWidth by DefaultWindowHeight, serving its MCP server for go tool game mcp (Serve), and returns once it closes.

Types

type Options

type Options struct {
	// Components are the component types the scenes' objects may set, which the editor checks every component and field against and lists in the inspector, together with the built-in 2D and 3D physics components (sceneformat.RegisterPhysicsComponents) the registry does not register itself; nil leaves components free-form.
	Components *sceneformat.ComponentRegistry
	// DataTables binds YAML file patterns to the Go row types used by the table grid, -check, -rename-text-key, and -assets.
	DataTables *datatable.Registry
	// Custom registers custom screen widget builders by kind (ScreenBuildOptions.Custom) so -check reports unregistered custom screen node kinds.
	Custom map[string]func(gameui.ScreenNodeDoc) gameui.CustomWidget
	// Plugins are the editor plugins built into the command, which a project turns on under plugins: in its game.yaml.
	Plugins []editor.Plugin
	// AppName is the editor's name in its window title (editor.Editor.AppName); empty means the editor's default.
	AppName string
	// Library is the library index the library panel installs packs from (editor.Editor.Library), as library.Open takes it: an https URL, an index file or a directory holding index.yaml; empty hides the panel.
	Library string
	// WindowWidth and WindowHeight are the size, in points, of the window Run shows the editor in; zero for either means the default, 1120 by 680.
	WindowWidth, WindowHeight core.Points
	// RequirePlugins makes Run refuse a project with any editor plugin problem reported by editor.Editor.PluginProblems (missing plugins, API mismatches, unsatisfied engine constraints, or extension conflicts), for a command that should not edit the project's scenes without them.
	RequirePlugins bool
	// Stdout is where -schema, -check, -rename-text-key, and -assets write their standard output; nil uses os.Stdout.
	Stdout io.Writer
}

Options are what an editor command builds its editor with.

Jump to

Keyboard shortcuts

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