animation.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. }
  36. // SetPaused sets whether the animation is paused.
  37. func (anim *Animation) SetPaused(state bool) {
  38. anim.paused = state
  39. }
  40. // Paused returns whether the animation is paused.
  41. func (anim *Animation) Paused() bool {
  42. return anim.paused
  43. }
  44. // SetLoop sets whether the animation is looping.
  45. func (anim *Animation) SetLoop(state bool) {
  46. anim.loop = state
  47. }
  48. // Loop returns whether the animation is looping.
  49. func (anim *Animation) Loop() bool {
  50. return anim.loop
  51. }
  52. // SetStart sets the initial time offset value.
  53. func (anim *Animation) SetStart(v float32) {
  54. anim.start = v
  55. }
  56. // Update interpolates and updates the target values for each channel.
  57. // If the animation is paused, returns false. If the animation is not paused,
  58. // returns true if the input value is inside the key frames ranges or false otherwise.
  59. func (anim *Animation) Update(delta float32) bool {
  60. // Check if paused
  61. if anim.paused {
  62. return false
  63. }
  64. // Check if input is less than minimum
  65. anim.time = anim.time + delta
  66. if anim.time < anim.minTime {
  67. return false
  68. }
  69. // Check if input is greater than maximum
  70. if anim.time > anim.maxTime {
  71. if anim.loop {
  72. anim.Reset()
  73. } else {
  74. return false
  75. }
  76. }
  77. // Update all channels
  78. for i := 0; i < len(anim.channels); i++ {
  79. ch := anim.channels[i]
  80. ch.Update(anim.time)
  81. }
  82. return true
  83. }
  84. // AddChannel adds a channel to the animation.
  85. func (anim *Animation) AddChannel(ch IChannel) {
  86. // TODO (maybe) prevent user from adding two channels of the same type that share target ?
  87. // Add the channel
  88. anim.channels = append(anim.channels, ch)
  89. // Update maxTime and minTime values
  90. kf := ch.Keyframes()
  91. firstKf := kf[0]
  92. if anim.minTime > firstKf {
  93. anim.minTime = firstKf
  94. }
  95. lastKf := kf[len(kf)-1]
  96. if anim.maxTime < lastKf {
  97. anim.maxTime = lastKf
  98. }
  99. }