animation.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 animation
  5. package animation
  6. // Animation is a keyframe animation, containing channels.
  7. // Each channel animates a specific property of an object.
  8. // Animations can span multiple objects and properties.
  9. type Animation struct {
  10. name string // Animation name
  11. loop bool // Whether the animation loops
  12. paused bool // Whether the animation is paused
  13. start float32 // Initial time offset value
  14. time float32 // Total running time
  15. minTime float32 // Minimum time value across all channels
  16. maxTime float32 // Maximum time value across all channels
  17. channels []IChannel // List of channels
  18. }
  19. // NewAnimation creates and returns a pointer to a new Animation object.
  20. func NewAnimation() *Animation {
  21. anim := new(Animation)
  22. return anim
  23. }
  24. // SetName sets the animation name.
  25. func (anim *Animation) SetName(name string) {
  26. anim.name = name
  27. }
  28. // Name returns the animation name.
  29. func (anim *Animation) Name() string {
  30. return anim.name
  31. }
  32. // Reset resets the animation to the beginning.
  33. func (anim *Animation) Reset() {
  34. anim.time = anim.start
  35. anim.paused = false
  36. // TODO reset channels?
  37. }
  38. // SetPaused sets whether the animation is paused.
  39. func (anim *Animation) SetPaused(state bool) {
  40. anim.paused = state
  41. }
  42. // Paused returns whether the animation is paused.
  43. func (anim *Animation) Paused() bool {
  44. return anim.paused
  45. }
  46. // SetLoop sets whether the animation is looping.
  47. func (anim *Animation) SetLoop(state bool) {
  48. anim.loop = state
  49. }
  50. // Loop returns whether the animation is looping.
  51. func (anim *Animation) Loop() bool {
  52. return anim.loop
  53. }
  54. // SetStart sets the initial time offset value.
  55. func (anim *Animation) SetStart(v float32) {
  56. anim.start = v
  57. }
  58. // Update interpolates and updates the target values for each channel.
  59. // If the animation is paused, returns false. If the animation is not paused,
  60. // returns true if the input value is inside the key frames ranges or false otherwise.
  61. func (anim *Animation) Update(delta float32) {
  62. // Check if paused
  63. if anim.paused {
  64. return
  65. }
  66. // Check if input is less than minimum
  67. anim.time = anim.time + delta
  68. if anim.time < anim.minTime {
  69. return
  70. }
  71. // Check if input is greater than maximum
  72. if anim.time > anim.maxTime {
  73. if anim.loop {
  74. anim.time = anim.time - anim.maxTime
  75. } else {
  76. anim.time = anim.maxTime - 0.000001
  77. anim.SetPaused(true)
  78. }
  79. }
  80. // Update all channels
  81. for i := 0; i < len(anim.channels); i++ {
  82. ch := anim.channels[i]
  83. ch.Update(anim.time)
  84. }
  85. }
  86. // AddChannel adds a channel to the animation.
  87. func (anim *Animation) AddChannel(ch IChannel) {
  88. // TODO (maybe) prevent user from adding two channels of the same type that share target ?
  89. // Add the channel
  90. anim.channels = append(anim.channels, ch)
  91. // Update maxTime and minTime values
  92. kf := ch.Keyframes()
  93. firstTime := kf[0]
  94. if anim.minTime > firstTime {
  95. anim.minTime = firstTime
  96. }
  97. lastTime := kf[len(kf)-1]
  98. if anim.maxTime < lastTime {
  99. anim.maxTime = lastTime
  100. }
  101. }