library_animations.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  102. func (d *Decoder) decAnimation(start xml.StartElement, la *LibraryAnimations) error {
  103. anim := new(Animation)
  104. la.Animation = append(la.Animation, anim)
  105. anim.Id = findAttrib(start, "id").Value
  106. anim.Name = findAttrib(start, "name").Value
  107. for {
  108. child, _, err := d.decNextChild(start)
  109. if err != nil || child.Name.Local == "" {
  110. return err
  111. }
  112. if child.Name.Local == "source" {
  113. source, err := d.decSource(child)
  114. if err != nil {
  115. return err
  116. }
  117. anim.Source = append(anim.Source, source)
  118. continue
  119. }
  120. if child.Name.Local == "sampler" {
  121. err = d.decSampler(child, anim)
  122. if err != nil {
  123. return nil
  124. }
  125. continue
  126. }
  127. if child.Name.Local == "channel" {
  128. err = d.decChannel(child, anim)
  129. if err != nil {
  130. return nil
  131. }
  132. continue
  133. }
  134. }
  135. }
  136. func (d *Decoder) decSampler(start xml.StartElement, anim *Animation) error {
  137. sp := new(Sampler)
  138. anim.Sampler = append(anim.Sampler, sp)
  139. sp.Id = findAttrib(start, "id").Value
  140. for {
  141. child, _, err := d.decNextChild(start)
  142. if err != nil || child.Name.Local == "" {
  143. return err
  144. }
  145. if child.Name.Local == "input" {
  146. inp, err := d.decInput(child)
  147. if err != nil {
  148. return err
  149. }
  150. sp.Input = append(sp.Input, inp)
  151. continue
  152. }
  153. }
  154. }
  155. func (d *Decoder) decChannel(start xml.StartElement, anim *Animation) error {
  156. ch := new(Channel)
  157. ch.Source = findAttrib(start, "source").Value
  158. ch.Target = findAttrib(start, "target").Value
  159. anim.Channel = append(anim.Channel, ch)
  160. return nil
  161. }