| OLD | NEW |
| (Empty) |
| 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/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) | |
| 24 // } | |
| 25 package sprite // import "golang.org/x/mobile/sprite" | |
| 26 | |
| 27 import ( | |
| 28 "image" | |
| 29 "image/draw" | |
| 30 | |
| 31 "golang.org/x/mobile/f32" | |
| 32 "golang.org/x/mobile/sprite/clock" | |
| 33 ) | |
| 34 | |
| 35 type Arranger interface { | |
| 36 Arrange(e Engine, n *Node, t clock.Time) | |
| 37 } | |
| 38 | |
| 39 type Texture interface { | |
| 40 Bounds() (w, h int) | |
| 41 Download(r image.Rectangle, dst draw.Image) | |
| 42 Upload(r image.Rectangle, src image.Image) | |
| 43 Unload() | |
| 44 } | |
| 45 | |
| 46 type SubTex struct { | |
| 47 T Texture | |
| 48 R image.Rectangle | |
| 49 } | |
| 50 | |
| 51 type Engine interface { | |
| 52 Register(n *Node) | |
| 53 Unregister(n *Node) | |
| 54 | |
| 55 LoadTexture(a image.Image) (Texture, error) | |
| 56 | |
| 57 SetSubTex(n *Node, x SubTex) | |
| 58 SetTransform(n *Node, m f32.Affine) // sets transform relative to parent
. | |
| 59 | |
| 60 Render(scene *Node, t clock.Time) | |
| 61 } | |
| 62 | |
| 63 // A Node is a renderable element and forms a tree of Nodes. | |
| 64 type Node struct { | |
| 65 Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node | |
| 66 | |
| 67 Arranger Arranger | |
| 68 | |
| 69 // EngineFields contains fields that should only be accessed by Engine | |
| 70 // implementations. It is exported because such implementations can be | |
| 71 // in other packages. | |
| 72 EngineFields struct { | |
| 73 // TODO: separate TexDirty and TransformDirty bits? | |
| 74 Dirty bool | |
| 75 Index int32 | |
| 76 SubTex SubTex | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 // AppendChild adds a node c as a child of n. | |
| 81 // | |
| 82 // It will panic if c already has a parent or siblings. | |
| 83 func (n *Node) AppendChild(c *Node) { | |
| 84 if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil { | |
| 85 panic("sprite: AppendChild called for an attached child Node") | |
| 86 } | |
| 87 last := n.LastChild | |
| 88 if last != nil { | |
| 89 last.NextSibling = c | |
| 90 } else { | |
| 91 n.FirstChild = c | |
| 92 } | |
| 93 n.LastChild = c | |
| 94 c.Parent = n | |
| 95 c.PrevSibling = last | |
| 96 } | |
| 97 | |
| 98 // RemoveChild removes a node c that is a child of n. Afterwards, c will have | |
| 99 // no parent and no siblings. | |
| 100 // | |
| 101 // It will panic if c's parent is not n. | |
| 102 func (n *Node) RemoveChild(c *Node) { | |
| 103 if c.Parent != n { | |
| 104 panic("sprite: RemoveChild called for a non-child Node") | |
| 105 } | |
| 106 if n.FirstChild == c { | |
| 107 n.FirstChild = c.NextSibling | |
| 108 } | |
| 109 if c.NextSibling != nil { | |
| 110 c.NextSibling.PrevSibling = c.PrevSibling | |
| 111 } | |
| 112 if n.LastChild == c { | |
| 113 n.LastChild = c.PrevSibling | |
| 114 } | |
| 115 if c.PrevSibling != nil { | |
| 116 c.PrevSibling.NextSibling = c.NextSibling | |
| 117 } | |
| 118 c.Parent = nil | |
| 119 c.PrevSibling = nil | |
| 120 c.NextSibling = nil | |
| 121 } | |
| OLD | NEW |