ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Add dockable Art tools, animation and previews, guarded asset workflows, and the Web Asset Studio built on the shared Art Core. Expand English and Russian documentation, interactive Try scenes, examples and editor icons. Add audio waveform navigation, source analysis, guarded previews and a browser Worker mixer. Preserve source changes and recovery with bounded checks, and strengthen native, browser and interoperability CI. Prepare version 0.12.5 and manually built native artifact candidates. Fix the production site image demo build dependency. Reuse immutable scene indices when capturing keyframes. Wait for real Persist save/load completion in component tests. Stabilize browser Game View captures outside pointer hover. Aseprite interchange and persistent Audio EDL recovery remain limited; the artifact workflow does not publish releases or change stable.
464 lines
11 KiB
Go
464 lines
11 KiB
Go
//go:build aego_editor
|
|
|
|
package docsession
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"aego/asset/loader"
|
|
"aego/core/errs"
|
|
"aego/core/ids"
|
|
"aego/core/log"
|
|
"aego/editor/core/command"
|
|
"aego/editor/core/context"
|
|
"aego/editor/core/document"
|
|
"aego/editor/core/history/commands"
|
|
"aego/editor/core/play"
|
|
"aego/editor/core/selection"
|
|
"aego/editor/core/shape"
|
|
"aego/engine"
|
|
"aego/engine/i18n"
|
|
"aego/engine/module"
|
|
"aego/engine/problems"
|
|
"aego/render"
|
|
"aego/scene"
|
|
"aego/scene/sceneio"
|
|
"aego/scene/scriptcomp"
|
|
"aego/schema"
|
|
"aego/ui/core"
|
|
"aego/ui/dnd"
|
|
)
|
|
|
|
const (
|
|
MaxNodes = 10_000
|
|
ScenePath = "try.aescene"
|
|
// Limits apply to string-list bridge edits, not to existing scene files.
|
|
MaxStringListItems = 64
|
|
MaxStringBytes = 4096
|
|
MaxStringListBytes = 16 << 10
|
|
)
|
|
|
|
const channel log.Channel = "docs"
|
|
|
|
type Options struct {
|
|
Engine *engine.Engine
|
|
Renderer *render.Renderer
|
|
Loader *loader.Loader
|
|
Types *scene.Types
|
|
Sections *module.Sections
|
|
Scripts context.ScriptHost
|
|
UI *core.Context
|
|
Clipboard context.DataClipboard
|
|
Log *log.Logger
|
|
OnChange func(text []byte)
|
|
// BeforeStart can isolate run-local services when a Try scene is restarted.
|
|
BeforeStart func()
|
|
Bare bool
|
|
}
|
|
|
|
type State struct {
|
|
Loaded bool `json:"loaded"`
|
|
Running bool `json:"running"`
|
|
Paused bool `json:"paused"`
|
|
Frames uint64 `json:"frames"`
|
|
Restarts int `json:"restarts"`
|
|
Nodes int `json:"nodes"`
|
|
CanUndo bool `json:"canUndo"`
|
|
CanRedo bool `json:"canRedo"`
|
|
}
|
|
|
|
type Session struct {
|
|
opt Options
|
|
ctx *context.Editor
|
|
edit *document.Manager
|
|
doc *document.SceneDocument
|
|
run *document.SceneDocument
|
|
sel *selection.Service
|
|
play *play.Controller
|
|
origin []byte
|
|
text []byte
|
|
pick string
|
|
unsub func()
|
|
dirty bool
|
|
want bool
|
|
restarts int
|
|
scratch []ids.NodeID
|
|
}
|
|
|
|
func New(opt Options) *Session {
|
|
if opt.Log == nil {
|
|
opt.Log = log.Nop()
|
|
}
|
|
s := &Session{opt: opt, sel: selection.New()}
|
|
s.edit = s.manager()
|
|
s.ctx = &context.Editor{
|
|
Engine: opt.Engine,
|
|
Renderer: opt.Renderer,
|
|
Loader: opt.Loader,
|
|
Types: opt.Types,
|
|
Docs: s.manager(),
|
|
Selection: s.sel,
|
|
Commands: command.New(),
|
|
UI: opt.UI,
|
|
DnD: &dnd.State{},
|
|
Clipboard: opt.Clipboard,
|
|
Log: opt.Log.With(channel),
|
|
Settings: context.DefaultSettings(),
|
|
Scripts: opt.Scripts,
|
|
Collapsed: map[string]bool{},
|
|
Shapes: shape.NewRegistry(),
|
|
Sections: opt.Sections,
|
|
Problems: problems.New(),
|
|
}
|
|
s.play = play.New(s.ctx)
|
|
s.ctx.Play = s.play
|
|
return s
|
|
}
|
|
|
|
func (s *Session) manager() *document.Manager {
|
|
opt := document.Options{Types: s.opt.Types, Log: s.opt.Log}
|
|
if s.opt.Loader != nil {
|
|
opt.Resolver = s.opt.Loader
|
|
}
|
|
return document.NewManager(opt)
|
|
}
|
|
|
|
func (s *Session) Bare() bool {
|
|
return s.opt.Bare
|
|
}
|
|
|
|
func (s *Session) Editor() *context.Editor {
|
|
return s.ctx
|
|
}
|
|
|
|
func (s *Session) Controller() *play.Controller {
|
|
return s.play
|
|
}
|
|
|
|
func (s *Session) Text() []byte {
|
|
return s.text
|
|
}
|
|
|
|
func (s *Session) parse(data []byte) (*scene.Scene, error) {
|
|
if s.opt.Loader == nil {
|
|
return nil, errs.New(errs.EditorDocument, "the docs session has no asset loader")
|
|
}
|
|
sc, rep, err := sceneio.Load(data, ScenePath, s.opt.Types, s.opt.Loader, sceneio.LoadOptions{KeepIDs: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, w := range rep.Warnings {
|
|
s.ctx.Notify(context.LevelWarn, fmt.Sprintf("%s:%d: %s", ScenePath, w.Pos.Line, w.Msg))
|
|
}
|
|
for _, u := range rep.Unknown {
|
|
s.ctx.Notify(context.LevelWarn, i18n.Note("editor.docsession.component_u_is_not_available", "component {u} is not available in the docs runtime", i18n.Str("u", u)))
|
|
}
|
|
return sc, nil
|
|
}
|
|
|
|
func (s *Session) Load(data []byte, pick string) error {
|
|
sc, err := s.parse(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.origin, s.pick, s.restarts = bytes.Clone(data), pick, 0
|
|
s.install(sc)
|
|
s.want = true
|
|
s.start()
|
|
return nil
|
|
}
|
|
|
|
func (s *Session) Reset() error {
|
|
if s.origin == nil {
|
|
return errs.New(errs.EditorDocument, "no scene is loaded")
|
|
}
|
|
sc, err := s.parse(s.origin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.install(sc)
|
|
s.want = true
|
|
s.start()
|
|
return nil
|
|
}
|
|
|
|
func (s *Session) install(sc *scene.Scene) {
|
|
s.play.Stop()
|
|
if s.unsub != nil {
|
|
s.unsub()
|
|
}
|
|
s.edit = s.manager()
|
|
s.doc = s.edit.New(ScenePath, sc)
|
|
s.ctx.Docs = s.manager()
|
|
s.run = s.ctx.Docs.New(ScenePath, sc)
|
|
s.unsub = s.doc.History.Subscribe(func() { s.dirty = true })
|
|
s.dirty = false
|
|
s.text = sceneio.Save(sc)
|
|
s.sel.Set()
|
|
if n, ok := s.find(s.pick); ok {
|
|
s.Select(n)
|
|
}
|
|
s.emit()
|
|
}
|
|
|
|
func (s *Session) component(full string) (*schema.TypeDescriptor, bool) {
|
|
if full == "" {
|
|
return nil, false
|
|
}
|
|
for _, td := range s.opt.Types.Schema().Components() {
|
|
if td.FullName == full {
|
|
return td, true
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (s *Session) find(full string) (ids.NodeID, bool) {
|
|
sc := s.doc.Scene()
|
|
order := sc.PreOrder()
|
|
if td, ok := s.component(full); ok {
|
|
for _, h := range order {
|
|
if _, has := sc.GetByType(h, td.ID); has {
|
|
return sc.ID(h), true
|
|
}
|
|
}
|
|
}
|
|
for _, h := range order {
|
|
if h != sc.Root() {
|
|
return sc.ID(h), true
|
|
}
|
|
}
|
|
return ids.NodeID{}, false
|
|
}
|
|
|
|
func (s *Session) emit() {
|
|
if s.opt.OnChange != nil {
|
|
s.opt.OnChange(s.text)
|
|
}
|
|
}
|
|
|
|
func (s *Session) start() {
|
|
if s.run == nil {
|
|
return
|
|
}
|
|
if s.opt.BeforeStart != nil {
|
|
s.opt.BeforeStart()
|
|
}
|
|
if err := s.play.Start(s.run); err != nil {
|
|
s.ctx.Notify(context.LevelError, err.Error())
|
|
}
|
|
}
|
|
|
|
func (s *Session) restart() {
|
|
paused := s.play.State() == play.Paused
|
|
s.start()
|
|
if paused {
|
|
s.play.Pause()
|
|
}
|
|
s.restarts++
|
|
}
|
|
|
|
func (s *Session) TogglePause() {
|
|
if s.play.State() == play.Stopped {
|
|
s.want = true
|
|
s.start()
|
|
return
|
|
}
|
|
s.play.Pause()
|
|
}
|
|
|
|
func (s *Session) prune() {
|
|
if s.doc == nil {
|
|
return
|
|
}
|
|
sc := s.doc.Scene()
|
|
s.sel.Prune(s.doc.ID, func(id ids.NodeID) bool {
|
|
h := sc.Handle(id)
|
|
return !h.IsZero() && sc.IsAlive(h)
|
|
})
|
|
}
|
|
|
|
func (s *Session) Undo() error {
|
|
if s.doc == nil {
|
|
return nil
|
|
}
|
|
err := s.doc.History.Undo()
|
|
s.prune()
|
|
return err
|
|
}
|
|
|
|
func (s *Session) Redo() error {
|
|
if s.doc == nil {
|
|
return nil
|
|
}
|
|
err := s.doc.History.Redo()
|
|
s.prune()
|
|
return err
|
|
}
|
|
|
|
func (s *Session) Set(component, field string, v float64) error {
|
|
return s.set(component, field, func(fd *schema.FieldDescriptor) (schema.Value, error) {
|
|
val := schema.Value{Kind: fd.Kind}
|
|
switch fd.Kind {
|
|
case schema.KindFloat32, schema.KindFloat64:
|
|
val.F = v
|
|
case schema.KindInt32, schema.KindInt64, schema.KindBool, schema.KindEnum:
|
|
val.I = int64(v)
|
|
default:
|
|
return schema.Value{}, errs.New(errs.EditorCommand, field+" is not a number field")
|
|
}
|
|
return val, nil
|
|
})
|
|
}
|
|
|
|
// SetString routes string edits through the same property history as the UI.
|
|
func (s *Session) SetString(component, field, value string) error {
|
|
return s.set(component, field, func(fd *schema.FieldDescriptor) (schema.Value, error) {
|
|
if fd.Kind != schema.KindString {
|
|
return schema.Value{}, errs.New(errs.EditorCommand, field+" is not a string field")
|
|
}
|
|
return schema.String(value), nil
|
|
})
|
|
}
|
|
|
|
// SetStringList uses the same property history as the Inspector. Bound bridge
|
|
// input independently of scene loading, and never accept malformed tag names
|
|
// which would be silently ignored by the runtime tag index.
|
|
func (s *Session) SetStringList(component, field string, values []string) error {
|
|
return s.set(component, field, func(fd *schema.FieldDescriptor) (schema.Value, error) {
|
|
if fd.Kind != schema.KindList || fd.Elem != schema.KindString {
|
|
return schema.Value{}, errs.New(errs.EditorCommand, field+" is not a string-list field")
|
|
}
|
|
if len(values) > MaxStringListItems {
|
|
return schema.Value{}, errs.New(errs.EditorCommand, "string list exceeds its item budget")
|
|
}
|
|
items := make([]schema.Value, len(values))
|
|
bytes := 0
|
|
for i, value := range values {
|
|
bytes += len(value)
|
|
if len(value) > MaxStringBytes || bytes > MaxStringListBytes {
|
|
return schema.Value{}, errs.New(errs.EditorCommand, "string list exceeds its byte budget")
|
|
}
|
|
if component == scene.TagsName && strings.EqualFold(fd.Name, "tags") && !scene.ValidTag(value) {
|
|
return schema.Value{}, errs.New(errs.EditorCommand, "malformed tag name")
|
|
}
|
|
items[i] = schema.String(value)
|
|
}
|
|
return schema.List(items...), nil
|
|
})
|
|
}
|
|
|
|
// SetScriptEnabled performs the same undoable operation as the Inspector's
|
|
// attachment switch, on the single selected node and an existing script only.
|
|
func (s *Session) SetScriptEnabled(name string, enabled bool) error {
|
|
if s.doc == nil {
|
|
return errs.New(errs.EditorDocument, "no scene is loaded")
|
|
}
|
|
if name == "" || len(name) > 256 {
|
|
return errs.New(errs.EditorCommand, "invalid script name")
|
|
}
|
|
selected := s.Selected()
|
|
if len(selected) != 1 {
|
|
return errs.New(errs.EditorCommand, "exactly one node must be selected")
|
|
}
|
|
sc := s.doc.Scene()
|
|
h := sc.Handle(selected[0])
|
|
if h.IsZero() || !sc.IsAlive(h) {
|
|
return errs.New(errs.EditorCommand, "selected node is absent")
|
|
}
|
|
td, ok := sc.Registry().ByName(name)
|
|
if !ok || td.Kind != schema.TypeScript {
|
|
return errs.New(errs.EditorCommand, "unknown script "+name)
|
|
}
|
|
scripts, ok := scriptcomp.ScriptsOf(sc, h)
|
|
if !ok {
|
|
return errs.New(errs.EditorCommand, "selected node has no scripts")
|
|
}
|
|
i := scripts.Index(name)
|
|
if i < 0 {
|
|
return errs.New(errs.EditorCommand, "script is not attached to the selected node")
|
|
}
|
|
before := scripts.Attachments[i].Enabled
|
|
if before == enabled {
|
|
return nil
|
|
}
|
|
return s.doc.History.Exec(&commands.SetScriptEnabled{Node: selected[0], TypeName: name, Before: before, After: enabled})
|
|
}
|
|
|
|
func (s *Session) set(component, field string, value func(*schema.FieldDescriptor) (schema.Value, error)) error {
|
|
if s.doc == nil {
|
|
return errs.New(errs.EditorDocument, "no scene is loaded")
|
|
}
|
|
td, ok := s.component(component)
|
|
if !ok {
|
|
return errs.New(errs.SchemaUnknownType, "unknown component "+component)
|
|
}
|
|
var fd *schema.FieldDescriptor
|
|
for i := range td.Fields {
|
|
if strings.EqualFold(td.Fields[i].Name, field) {
|
|
fd = &td.Fields[i]
|
|
break
|
|
}
|
|
}
|
|
if fd == nil {
|
|
return errs.New(errs.SchemaUnknownType, component+" has no field "+field)
|
|
}
|
|
val, err := value(fd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
nodes := append([]ids.NodeID(nil), s.Selected()...)
|
|
if len(nodes) == 0 {
|
|
n, ok := s.find(component)
|
|
if !ok {
|
|
return errs.New(errs.EditorCommand, "no node has "+component)
|
|
}
|
|
nodes = append(nodes, n)
|
|
}
|
|
return s.doc.Props.Set(nil, nodes, td.ID, fd.ID, val)
|
|
}
|
|
|
|
func (s *Session) Tick(dt float64) {
|
|
if s.doc != nil && s.dirty && !s.doc.History.InTransaction() {
|
|
s.dirty = false
|
|
s.text = sceneio.Save(s.doc.Scene())
|
|
s.emit()
|
|
if s.want {
|
|
s.restart()
|
|
}
|
|
}
|
|
if sc := s.play.Scene(); sc != nil && sc.Stats().Alive > MaxNodes {
|
|
s.play.Stop()
|
|
s.want = false
|
|
s.ctx.Notify(context.LevelError, fmt.Sprintf("the scene grew past %d nodes, the game was stopped", MaxNodes))
|
|
}
|
|
s.play.Tick(dt)
|
|
}
|
|
|
|
func (s *Session) State() State {
|
|
st := State{Loaded: s.doc != nil, Frames: s.play.Frames(), Restarts: s.restarts}
|
|
switch s.play.State() {
|
|
case play.Playing:
|
|
st.Running = true
|
|
case play.Paused:
|
|
st.Running, st.Paused = true, true
|
|
}
|
|
if sc := s.play.Scene(); sc != nil {
|
|
st.Nodes = sc.Stats().Alive - 1
|
|
}
|
|
if s.doc != nil {
|
|
st.CanUndo, st.CanRedo = s.doc.History.CanUndo(), s.doc.History.CanRedo()
|
|
}
|
|
return st
|
|
}
|
|
|
|
func (s *Session) Close() {
|
|
s.play.Stop()
|
|
if s.unsub != nil {
|
|
s.unsub()
|
|
s.unsub = nil
|
|
}
|
|
}
|