library_animations.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2016 The G3N 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. package collada
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. )
  10. //
  11. // Library Animations
  12. //
  13. type LibraryAnimations struct {
  14. Id string
  15. Name string
  16. Asset *Asset
  17. Animation []*Animation
  18. }
  19. // Dump prints out information about the LibraryAnimations
  20. func (la *LibraryAnimations) Dump(out io.Writer, indent int) {
  21. if la == nil {
  22. return
  23. }
  24. fmt.Fprintf(out, "%sLibraryAnimations id:%s name:%s\n", sIndent(indent), la.Id, la.Name)
  25. for _, an := range la.Animation {
  26. an.Dump(out, indent+step)
  27. }
  28. }
  29. //
  30. // Animation
  31. //
  32. type Animation struct {
  33. Id string
  34. Name string
  35. Animation []*Animation
  36. Source []*Source
  37. Sampler []*Sampler
  38. Channel []*Channel
  39. }
  40. // Dump prints out information about the Animation
  41. func (an *Animation) Dump(out io.Writer, indent int) {
  42. fmt.Fprintf(out, "%sAnimation id:%s name:%s\n", sIndent(indent), an.Id, an.Name)
  43. ind := indent + step
  44. for _, source := range an.Source {
  45. source.Dump(out, ind)
  46. }
  47. for _, sampler := range an.Sampler {
  48. sampler.Dump(out, ind)
  49. }
  50. for _, channel := range an.Channel {
  51. channel.Dump(out, ind)
  52. }
  53. for _, child := range an.Animation {
  54. child.Dump(out, ind)
  55. }
  56. }
  57. //
  58. // Sampler
  59. //
  60. type Sampler struct {
  61. Id string
  62. Input []Input // One or more
  63. }
  64. // Dump prints out information about the Sampler
  65. func (sp *Sampler) Dump(out io.Writer, indent int) {
  66. fmt.Fprintf(out, "%sSampler id:%s\n", sIndent(indent), sp.Id)
  67. ind := indent + step
  68. for _, inp := range sp.Input {
  69. inp.Dump(out, ind)
  70. }
  71. }
  72. //
  73. // Channel
  74. //
  75. type Channel struct {
  76. Source string
  77. Target string
  78. }
  79. // Dump prints out information about the Channel
  80. func (ch *Channel) Dump(out io.Writer, indent int) {
  81. fmt.Fprintf(out, "%sChannel source:%s target:%s\n", sIndent(indent), ch.Source, ch.Target)
  82. }
  83. func (d *Decoder) decLibraryAnimations(start xml.StartElement, dom *Collada) error {
  84. la := new(LibraryAnimations)
  85. dom.LibraryAnimations = la
  86. la.Id = findAttrib(start, "id").Value
  87. la.Name = findAttrib(start, "name").Value
  88. for {
  89. child, _, err := d.decNextChild(start)
  90. if err != nil || child.Name.Local == "" {
  91. return err
  92. }
  93. if child.Name.Local == "animation" {
  94. err := d.decAnimation(child, la)
  95. if err != nil {
  96. return err
  97. }
  98. continue
  99. }
  100. }
  101. return nil
  102. }
  103. func (d *Decoder) decAnimation(start xml.StartElement, la *LibraryAnimations) error {
  104. anim := new(Animation)
  105. la.Animation = append(la.Animation, anim)
  106. anim.Id = findAttrib(start, "id").Value
  107. anim.Name = findAttrib(start, "name").Value
  108. for {
  109. child, _, err := d.decNextChild(start)
  110. if err != nil || child.Name.Local == "" {
  111. return err
  112. }
  113. if child.Name.Local == "source" {
  114. source, err := d.decSource(child)
  115. if err != nil {
  116. return err
  117. }
  118. anim.Source = append(anim.Source, source)
  119. continue
  120. }
  121. if child.Name.Local == "sampler" {
  122. err = d.decSampler(child, anim)
  123. if err != nil {
  124. return nil
  125. }
  126. continue
  127. }
  128. if child.Name.Local == "channel" {
  129. err = d.decChannel(child, anim)
  130. if err != nil {
  131. return nil
  132. }
  133. continue
  134. }
  135. }
  136. return nil
  137. }
  138. func (d *Decoder) decSampler(start xml.StartElement, anim *Animation) error {
  139. sp := new(Sampler)
  140. anim.Sampler = append(anim.Sampler, sp)
  141. sp.Id = findAttrib(start, "id").Value
  142. for {
  143. child, _, err := d.decNextChild(start)
  144. if err != nil || child.Name.Local == "" {
  145. return err
  146. }
  147. if child.Name.Local == "input" {
  148. inp, err := d.decInput(child)
  149. if err != nil {
  150. return err
  151. }
  152. sp.Input = append(sp.Input, inp)
  153. continue
  154. }
  155. }
  156. return nil
  157. }
  158. func (d *Decoder) decChannel(start xml.StartElement, anim *Animation) error {
  159. ch := new(Channel)
  160. ch.Source = findAttrib(start, "source").Value
  161. ch.Target = findAttrib(start, "target").Value
  162. anim.Channel = append(anim.Channel, ch)
  163. return nil
  164. }