guiEaseViewCtrl.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. docsURL;
  45. Parent::initPersistFields();
  46. addField("ease", TypeEaseF, Offset( mEase, GuiEaseViewCtrl ) );
  47. addField("easeColor", TypeColorF, Offset( mEaseColor, GuiEaseViewCtrl ) );
  48. addField("easeWidth", TypeF32, Offset(mEaseWidth, GuiEaseViewCtrl ) );
  49. addField("axisColor", TypeColorF, Offset( mAxisColor, GuiEaseViewCtrl ) );
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool GuiEaseViewCtrl::onWake()
  53. {
  54. if (!Parent::onWake())
  55. return false;
  56. setActive(true);
  57. return true;
  58. }
  59. //-----------------------------------------------------------------------------
  60. void GuiEaseViewCtrl::onSleep()
  61. {
  62. setActive(false);
  63. Parent::onSleep();
  64. }
  65. //-----------------------------------------------------------------------------
  66. void GuiEaseViewCtrl::onRender(Point2I offset, const RectI &updateRect)
  67. {
  68. const F32 W = getExtent().x;
  69. const F32 H = getExtent().y;
  70. const F32 plotW = W;
  71. const F32 plotH = H;
  72. const F32 zeroX = offset.x + 1.f;
  73. const F32 zeroY = offset.y;
  74. // Draw axis.
  75. GFX->getDrawUtil()->drawLine( zeroX, zeroY + 0.0f, zeroX, zeroY + plotH, mAxisColor.toColorI());
  76. GFX->getDrawUtil()->drawLine( zeroX, zeroY + plotH, zeroX + plotW, zeroY + plotH, mAxisColor.toColorI());
  77. F32 numPoints = W;
  78. F32 lastX = zeroX;
  79. F32 lastY = zeroY + plotH;
  80. // Draw curve.
  81. for( S32 i = 1; i <= numPoints; ++ i )
  82. {
  83. F32 x = ( F32 ) i / ( F32 ) numPoints;
  84. F32 y = mEase.getValue( x, 0, 1, 1 );
  85. x = zeroX + x * plotW;
  86. y = zeroY + plotH - y * plotH;
  87. GFX->getDrawUtil()->drawLine( lastX, lastY, x, y, mEaseColor.toColorI());
  88. lastX = x;
  89. lastY = y;
  90. }
  91. }