guiEaseViewCtrl.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "gui/editor/guiEaseViewCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "math/mMath.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. IMPLEMENT_CONOBJECT( GuiEaseViewCtrl );
  27. ConsoleDocClass( GuiEaseViewCtrl,
  28. "@brief Control to visualize an EaseF.\n\n"
  29. "Editor use only.\n\n"
  30. "@see EaseF\n\n"
  31. "@internal"
  32. );
  33. //-----------------------------------------------------------------------------
  34. GuiEaseViewCtrl::GuiEaseViewCtrl()
  35. {
  36. mEase.set(Ease::In, Ease::Cubic);
  37. mAxisColor.set(1,1,1,1);
  38. mEaseColor.set(1,1,1,1);
  39. mEaseWidth = 4.0f;
  40. }
  41. //-----------------------------------------------------------------------------
  42. void GuiEaseViewCtrl::initPersistFields()
  43. {
  44. Parent::initPersistFields();
  45. addField("ease", TypeEaseF, Offset( mEase, GuiEaseViewCtrl ) );
  46. addField("easeColor", TypeColorF, Offset( mEaseColor, GuiEaseViewCtrl ) );
  47. addField("easeWidth", TypeF32, Offset(mEaseWidth, GuiEaseViewCtrl ) );
  48. addField("axisColor", TypeColorF, Offset( mAxisColor, GuiEaseViewCtrl ) );
  49. }
  50. //-----------------------------------------------------------------------------
  51. bool GuiEaseViewCtrl::onWake()
  52. {
  53. if (!Parent::onWake())
  54. return false;
  55. setActive(true);
  56. return true;
  57. }
  58. //-----------------------------------------------------------------------------
  59. void GuiEaseViewCtrl::onSleep()
  60. {
  61. setActive(false);
  62. Parent::onSleep();
  63. }
  64. //-----------------------------------------------------------------------------
  65. void GuiEaseViewCtrl::onRender(Point2I offset, const RectI &updateRect)
  66. {
  67. const F32 W = getExtent().x;
  68. const F32 H = getExtent().y;
  69. const F32 plotW = W;
  70. const F32 plotH = H;
  71. const F32 zeroX = offset.x + 1.f;
  72. const F32 zeroY = offset.y;
  73. // Draw axis.
  74. GFX->getDrawUtil()->drawLine( zeroX, zeroY + 0.0f, zeroX, zeroY + plotH, mAxisColor.toColorI());
  75. GFX->getDrawUtil()->drawLine( zeroX, zeroY + plotH, zeroX + plotW, zeroY + plotH, mAxisColor.toColorI());
  76. F32 numPoints = W;
  77. F32 lastX = zeroX;
  78. F32 lastY = zeroY + plotH;
  79. // Draw curve.
  80. for( S32 i = 1; i <= numPoints; ++ i )
  81. {
  82. F32 x = ( F32 ) i / ( F32 ) numPoints;
  83. F32 y = mEase.getValue( x, 0, 1, 1 );
  84. x = zeroX + x * plotW;
  85. y = zeroY + plotH - y * plotH;
  86. GFX->getDrawUtil()->drawLine( lastX, lastY, x, y, mEaseColor.toColorI());
  87. lastX = x;
  88. lastY = y;
  89. }
  90. }