| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package sprite provides a 2D scene graph for rendering and animation. |
| 6 | // |
| 7 | // A tree of nodes is drawn by a rendering Engine, provided by another |
| 8 | // package. The OS-independent Go version based on the image package is: |
| 9 | // |
| 10 | // golang.org/x/mobile/exp/sprite/portable |
| 11 | // |
| 12 | // An Engine draws a screen starting at a root Node. The tree is walked |
| 13 | // depth-first, with affine transformations applied at each level. |
| 14 | // |
| 15 | // Nodes are rendered relative to their parent. |
| 16 | // |
| 17 | // Typical main loop: |
| 18 | // |
| 19 | // for each frame { |
| 20 | // quantize time.Now() to a clock.Time |
| 21 | // process UI events |
| 22 | // modify the scene's nodes and animations (Arranger values) |
| 23 | // e.Render(scene, t, sz) |
| 24 | // } |
| 25 | package spritescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/ |
| 26 | |
| 27 | import ( |
| 28 | "imagescip-go gomod github.com/golang/go/src go1.24.0 image/" |
| 29 | "image/drawscip-go gomod github.com/golang/go/src go1.24.0 `image/draw`/" |
| 30 | |
| 31 | "golang.org/x/mobile/event/sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/" |
| 32 | "golang.org/x/mobile/exp/f32scip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/" |
| 33 | "golang.org/x/mobile/exp/sprite/clockscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite/clock`/" |
| 34 | ) |
| 35 | |
| 36 | type Arrangerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Arranger# interface { |
| 37 | Arrangescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Arranger#Arrange.(e Enginescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#, n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#, t clockscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite/clock`/.Timescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite/clock`/Time#) |
| 38 | } |
| 39 | |
| 40 | type Texturescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture# interface { |
| 41 | Boundsscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture#Bounds.() (w, h int) |
| 42 | Downloadscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture#Download.(r imagescip-go gomod github.com/golang/go/src go1.24.0 image/.Rectanglescip-go gomod github.com/golang/go/src go1.24.0 image/Rectangle#, dst drawscip-go gomod github.com/golang/go/src go1.24.0 `image/draw`/.Imagescip-go gomod github.com/golang/go/src go1.24.0 `image/draw`/Image#) |
| 43 | Uploadscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture#Upload.(r imagescip-go gomod github.com/golang/go/src go1.24.0 image/.Rectanglescip-go gomod github.com/golang/go/src go1.24.0 image/Rectangle#, src imagescip-go gomod github.com/golang/go/src go1.24.0 image/.Imagescip-go gomod github.com/golang/go/src go1.24.0 image/Image#) |
| 44 | Releasescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture#Release.() |
| 45 | } |
| 46 | |
| 47 | type SubTexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/SubTex# struct { |
| 48 | Tscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/SubTex#T. Texturescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture# |
| 49 | Rscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/SubTex#R. imagescip-go gomod github.com/golang/go/src go1.24.0 image/.Rectanglescip-go gomod github.com/golang/go/src go1.24.0 image/Rectangle# |
| 50 | } |
| 51 | |
| 52 | type Enginescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine# interface { |
| 53 | Registerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#Register.(n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) |
| 54 | Unregisterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#Unregister.(n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) |
| 55 | |
| 56 | LoadTexturescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#LoadTexture.(a imagescip-go gomod github.com/golang/go/src go1.24.0 image/.Imagescip-go gomod github.com/golang/go/src go1.24.0 image/Image#) (Texturescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Texture#, error) |
| 57 | |
| 58 | SetSubTexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#SetSubTex.(n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#, x SubTexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/SubTex#) |
| 59 | SetTransformscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#SetTransform.(n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#, m f32scip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/.Affinescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/f32`/Affine#) // sets transform relative to parent. |
| 60 | |
| 61 | // Render renders the scene arranged at the given time, for the given |
| 62 | // window configuration (dimensions and resolution). |
| 63 | Renderscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#Render.(scene *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#, t clockscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite/clock`/.Timescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite/clock`/Time#, sz sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#) |
| 64 | |
| 65 | Releasescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Engine#Release.() |
| 66 | } |
| 67 | |
| 68 | // A Node is a renderable element and forms a tree of Nodes. |
| 69 | type Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node# struct { |
| 70 | Parentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Parent., FirstChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#FirstChild., LastChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#LastChild., PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling., NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node# |
| 71 | |
| 72 | Arrangerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Arranger. Arrangerscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Arranger# |
| 73 | |
| 74 | // EngineFields contains fields that should only be accessed by Engine |
| 75 | // implementations. It is exported because such implementations can be |
| 76 | // in other packages. |
| 77 | EngineFieldsscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#EngineFields. struct { |
| 78 | // TODO: separate TexDirty and TransformDirty bits? |
| 79 | Dirtyscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#$anon_d2259bd86e77f8aa#Dirty. bool |
| 80 | Indexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#$anon_d2259bd86e77f8aa#Index. int32 |
| 81 | SubTexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#$anon_d2259bd86e77f8aa#SubTex. SubTexscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/SubTex# |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // AppendChild adds a node c as a child of n. |
| 86 | // |
| 87 | // It will panic if c already has a parent or siblings. |
| 88 | func (n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) AppendChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#AppendChild().(c *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) { |
| 89 | if c.Parentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Parent. != nil || c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. != nil || c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. != nil { |
| 90 | panic("sprite: AppendChild called for an attached child Node") |
| 91 | } |
| 92 | last := n.LastChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#LastChild. |
| 93 | if last != nil { |
| 94 | last.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. = c |
| 95 | } else { |
| 96 | n.FirstChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#FirstChild. = c |
| 97 | } |
| 98 | n.LastChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#LastChild. = c |
| 99 | c.Parentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Parent. = n |
| 100 | c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. = last |
| 101 | } |
| 102 | |
| 103 | // RemoveChild removes a node c that is a child of n. Afterwards, c will have |
| 104 | // no parent and no siblings. |
| 105 | // |
| 106 | // It will panic if c's parent is not n. |
| 107 | func (n *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) RemoveChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#RemoveChild().(c *Nodescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#) { |
| 108 | if c.Parentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Parent. != n { |
| 109 | panic("sprite: RemoveChild called for a non-child Node") |
| 110 | } |
| 111 | if n.FirstChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#FirstChild. == c { |
| 112 | n.FirstChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#FirstChild. = c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. |
| 113 | } |
| 114 | if c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. != nil { |
| 115 | c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling..PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. = c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. |
| 116 | } |
| 117 | if n.LastChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#LastChild. == c { |
| 118 | n.LastChildscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#LastChild. = c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. |
| 119 | } |
| 120 | if c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. != nil { |
| 121 | c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling..NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. = c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. |
| 122 | } |
| 123 | c.Parentscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#Parent. = nil |
| 124 | c.PrevSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#PrevSibling. = nil |
| 125 | c.NextSiblingscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/exp/sprite`/Node#NextSibling. = nil |
| 126 | } |