animation.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. speed float32 // Animation speed multiplier
  18. channels []IChannel // List of channels
  19. }
  20. // NewAnimation creates and returns a pointer to a new Animation object.
  21. func NewAnimation() *Animation {
  22. anim := new(Animation)
  23. anim.speed = 1
  24. return anim
  25. }
  26. // SetName sets the animation name.
  27. func (anim *Animation) SetName(name string) {
  28. anim.name = name
  29. }
  30. // Name returns the animation name.
  31. func (anim *Animation) Name() string {
  32. return anim.name
  33. }
  34. // SetSpeed sets the animation speed.
  35. func (anim *Animation) SetSpeed(speed float32) {
  36. anim.speed = speed
  37. }
  38. // Speed returns the animation speed.
  39. func (anim *Animation) Speed() float32 {
  40. return anim.speed
  41. }
  42. // Reset resets the animation to the beginning.
  43. func (anim *Animation) Reset() {
  44. anim.time = anim.start
  45. // Update all channels
  46. for i := range anim.channels {
  47. ch := anim.channels[i]
  48. ch.Update(anim.start)
  49. }
  50. }
  51. // SetPaused sets whether the animation is paused.
  52. func (anim *Animation) SetPaused(state bool) {
  53. anim.paused = state
  54. }
  55. // Paused returns whether the animation is paused.
  56. func (anim *Animation) Paused() bool {
  57. return anim.paused
  58. }
  59. // SetLoop sets whether the animation is looping.
  60. func (anim *Animation) SetLoop(state bool) {
  61. anim.loop = state
  62. }
  63. // Loop returns whether the animation is looping.
  64. func (anim *Animation) Loop() bool {
  65. return anim.loop
  66. }
  67. // SetStart sets the initial time offset value.
  68. func (anim *Animation) SetStart(v float32) {
  69. anim.start = v
  70. }
  71. // Update interpolates and updates the target values for each channel.
  72. // If the animation is paused, returns false. If the animation is not paused,
  73. // returns true if the input value is inside the key frames ranges or false otherwise.
  74. func (anim *Animation) Update(delta float32) {
  75. // Check if paused
  76. if anim.paused {
  77. return
  78. }
  79. // Check if input is less than minimum
  80. anim.time = anim.time + delta * anim.speed
  81. if anim.time < anim.minTime {
  82. return
  83. }
  84. // Check if input is greater than maximum
  85. if anim.time > anim.maxTime {
  86. if anim.loop {
  87. anim.time = anim.time - anim.maxTime
  88. } else {
  89. anim.time = anim.maxTime - 0.000001
  90. anim.SetPaused(true)
  91. }
  92. }
  93. // Update all channels
  94. for i := range anim.channels {
  95. ch := anim.channels[i]
  96. ch.Update(anim.time)
  97. }
  98. }
  99. // AddChannel adds a channel to the animation.
  100. func (anim *Animation) AddChannel(ch IChannel) {
  101. // TODO (maybe) prevent user from adding two channels of the same type that share target ?
  102. // Add the channel
  103. anim.channels = append(anim.channels, ch)
  104. // Update maxTime and minTime values
  105. kf := ch.Keyframes()
  106. firstTime := kf[0]
  107. if anim.minTime > firstTime {
  108. anim.minTime = firstTime
  109. }
  110. lastTime := kf[len(kf)-1]
  111. if anim.maxTime < lastTime {
  112. anim.maxTime = lastTime
  113. }
  114. }