library_animations.go 3.6 KB

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