mFluid.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "platform/types.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "console/consoleInternal.h"
  27. #include "math/mFluid.h"
  28. #ifndef _TICKABLE_H_
  29. #include "platform/Tickable.h"
  30. #endif
  31. #ifndef _STRINGUNIT_H_
  32. #include "string/stringUnit.h"
  33. #endif
  34. Fluid::Fluid()
  35. {
  36. mAnimationLength = 1000;
  37. mAnimationProgress = 1.0f;
  38. mEasingFunction = EasingFunction::Linear;
  39. }
  40. Fluid::~Fluid() { }
  41. void Fluid::startFluidAnimation()
  42. {
  43. mAnimationProgress = 0.0f;
  44. }
  45. F32 Fluid::getProgress(const S32 time)
  46. {
  47. mAnimationProgress += (F32)((F32)time / (F32)mAnimationLength);
  48. F32 progress = 1.0f;
  49. if (mAnimationProgress < 1.0f)
  50. {
  51. progress = mEase(mEasingFunction, mAnimationProgress);
  52. }
  53. return progress;
  54. }
  55. void Fluid::setEasingFunction(const char* label)
  56. {
  57. EasingFunction f = Linear;
  58. for (U32 i = 0; i < (sizeof(easingEnums) / sizeof(EnumTable::Enums)); i++)
  59. {
  60. if (dStricmp(easingEnums[i].label, label) == 0)
  61. f = (EasingFunction)easingEnums[i].index;
  62. }
  63. mEasingFunction = f;
  64. }
  65. const char* Fluid::getEasingFunctionDescription(const EasingFunction ease)
  66. {
  67. for (U32 i = 0; i < (sizeof(easingEnums) / sizeof(EnumTable::Enums)); i++)
  68. {
  69. if (easingEnums[i].index == ease)
  70. return easingEnums[i].label;
  71. }
  72. return StringTable->EmptyString;
  73. }
  74. //------------------------------------------------------------------------
  75. FluidColorI::FluidColorI(ColorI &color)
  76. {
  77. red = color.red;
  78. green = color.green;
  79. blue = color.blue;
  80. alpha = color.alpha;
  81. }
  82. void FluidColorI::startFluidAnimation(const ColorI &color)
  83. {
  84. mAnimationProgress = 0.0f;
  85. redStart = red;
  86. greenStart = green;
  87. blueStart = blue;
  88. alphaStart = alpha;
  89. redTarget = color.red;
  90. greenTarget = color.green;
  91. blueTarget = color.blue;
  92. alphaTarget = color.alpha;
  93. }
  94. //Returns true if we should continue to processTicks
  95. bool FluidColorI::processTick()
  96. {
  97. F32 progress = getProgress(32.0f);
  98. red = processValue(progress, redStart, redTarget);
  99. green = processValue(progress, greenStart, greenTarget);
  100. blue = processValue(progress, blueStart, blueTarget);
  101. alpha = processValue(progress, alphaStart, alphaTarget);
  102. clamp();
  103. if (mAnimationProgress >= 1.0f)
  104. {
  105. mAnimationProgress = 1.0f;
  106. return false;
  107. }
  108. return true;
  109. }
  110. void FluidColorI::set(const ColorI& in_rCopy)
  111. {
  112. red = in_rCopy.red;
  113. green = in_rCopy.green;
  114. blue = in_rCopy.blue;
  115. alpha = in_rCopy.alpha;
  116. }
  117. void FluidColorI::set(U8 red, U8 green, U8 blue, U8 alpha)
  118. {
  119. red = red;
  120. green = green;
  121. blue = blue;
  122. alpha = alpha;
  123. }
  124. //-----------------------------------------------------------------------------
  125. ConsoleType(FluidColorI, TypeFluidColorI, sizeof(FluidColorI), "")
  126. //-----------------------------------------------------------------------------
  127. ConsoleGetType(TypeFluidColorI)
  128. {
  129. // Fetch color.
  130. FluidColorI* color = (FluidColorI*)dptr;
  131. // Fetch stock color name.
  132. StringTableEntry colorName = StockColor::name(*color);
  133. // Write as color name if was found.
  134. if (colorName != StringTable->EmptyString)
  135. return colorName;
  136. // Format as color components.
  137. char* returnBuffer = Con::getReturnBuffer(256);
  138. dSprintf(returnBuffer, 256, "%d %d %d %d", color->red, color->green, color->blue, color->alpha);
  139. return returnBuffer;
  140. }
  141. //-----------------------------------------------------------------------------
  142. ConsoleSetType(TypeFluidColorI)
  143. {
  144. FluidColorI* color = (FluidColorI*)dptr;
  145. if (argc == 1)
  146. {
  147. // Is only a single argument passed?
  148. if (StringUnit::getUnitCount(argv[0], " ") == 1)
  149. {
  150. // Is this a stock color name?
  151. if (!StockColor::isColor(argv[0]))
  152. {
  153. // No, so warn.
  154. Con::warnf("FluidColorI() - Invalid single argument of '%s' could not be interpreted as a stock color name. Using default.", argv[0]);
  155. }
  156. // Set stock color (if it's invalid we'll get the default.
  157. color->set(argv[0]);
  158. return;
  159. }
  160. color->set(0, 0, 0, 255);
  161. S32 r, g, b, a;
  162. const S32 args = dSscanf(argv[0], "%d %d %d %d", &r, &g, &b, &a);
  163. color->red = r;
  164. color->green = g;
  165. color->blue = b;
  166. if (args == 4)
  167. color->alpha = a;
  168. }
  169. else if (argc == 3)
  170. {
  171. color->red = dAtoi(argv[0]);
  172. color->green = dAtoi(argv[1]);
  173. color->blue = dAtoi(argv[2]);
  174. color->alpha = 255;
  175. }
  176. else if (argc == 4)
  177. {
  178. color->red = dAtoi(argv[0]);
  179. color->green = dAtoi(argv[1]);
  180. color->blue = dAtoi(argv[2]);
  181. color->alpha = dAtoi(argv[3]);
  182. }
  183. else
  184. Con::printf("Color must be set as { r, g, b [,a] }, { r g b [b] } or { stockColorName }");
  185. }