mFluid.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mFluid.h"
  24. Fluid::Fluid()
  25. {
  26. mAnimationLength = 1000;
  27. mAnimationProgress = 1.0f;
  28. mEasingFunction = EasingFunction::Linear;
  29. }
  30. Fluid::~Fluid() { }
  31. void Fluid::startFluidAnimation()
  32. {
  33. mAnimationProgress = 0.0f;
  34. }
  35. F32 Fluid::getProgress(const S32 time)
  36. {
  37. mAnimationProgress += (F32)((F32)time / (F32)mAnimationLength);
  38. F32 progress = 1.0f;
  39. if (mAnimationProgress < 1.0f)
  40. {
  41. progress = mEase(mEasingFunction, mAnimationProgress);
  42. }
  43. return progress;
  44. }
  45. void Fluid::setEasingFunction(const char* label)
  46. {
  47. EasingFunction f = Linear;
  48. for (U32 i = 0; i < (sizeof(easingEnums) / sizeof(EnumTable::Enums)); i++)
  49. {
  50. if (dStricmp(easingEnums[i].label, label) == 0)
  51. f = (EasingFunction)easingEnums[i].index;
  52. }
  53. mEasingFunction = f;
  54. }
  55. const char* Fluid::getEasingFunctionDescription(const EasingFunction ease)
  56. {
  57. for (U32 i = 0; i < (sizeof(easingEnums) / sizeof(EnumTable::Enums)); i++)
  58. {
  59. if (easingEnums[i].index == ease)
  60. return easingEnums[i].label;
  61. }
  62. return StringTable->EmptyString;
  63. }
  64. //------------------------------------------------------------------------
  65. void FluidColorI::startFluidAnimation(const ColorI &color)
  66. {
  67. mAnimationProgress = 0.0f;
  68. redStart = red;
  69. greenStart = green;
  70. blueStart = blue;
  71. alphaStart = alpha;
  72. redTarget = color.red;
  73. greenTarget = color.green;
  74. blueTarget = color.blue;
  75. alphaTarget = color.alpha;
  76. }
  77. //Returns true if we should continue to processTicks
  78. bool FluidColorI::processTick()
  79. {
  80. F32 progress = getProgress(32.0f);
  81. red = processValue(progress, redStart, redTarget);
  82. green = processValue(progress, greenStart, greenTarget);
  83. blue = processValue(progress, blueStart, blueTarget);
  84. alpha = processValue(progress, alphaStart, alphaTarget);
  85. clamp();
  86. if (mAnimationProgress >= 1.0f)
  87. {
  88. mAnimationProgress = 1.0f;
  89. return false;
  90. }
  91. return true;
  92. }
  93. void FluidColorI::set(const ColorI& in_rCopy)
  94. {
  95. red = in_rCopy.red;
  96. green = in_rCopy.green;
  97. blue = in_rCopy.blue;
  98. alpha = in_rCopy.alpha;
  99. }