afxSpellCastBar.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "console/engineAPI.h"
  26. #include "gui/core/guiControl.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "afx/ui/afxProgressBase.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. class afxSpellCastBar : public GuiControl, public afxProgressBase
  31. {
  32. typedef GuiControl Parent;
  33. bool want_border;
  34. bool want_background;
  35. bool use_alt_final_color;
  36. LinearColorF rgba_background;
  37. LinearColorF rgba_border;
  38. LinearColorF rgba_fill;
  39. LinearColorF rgba_fill_final;
  40. F32 fraction;
  41. public:
  42. /*C*/ afxSpellCastBar();
  43. virtual void onRender(Point2I, const RectI&);
  44. void setFraction(F32 frac);
  45. F32 getFraction() const { return fraction; }
  46. virtual void setProgress(F32 value) { setFraction(value); }
  47. virtual void onStaticModified(const char* slotName, const char* newValue = NULL);
  48. static void initPersistFields();
  49. DECLARE_CONOBJECT(afxSpellCastBar);
  50. DECLARE_CATEGORY("AFX");
  51. };
  52. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  53. IMPLEMENT_CONOBJECT(afxSpellCastBar);
  54. ConsoleDocClass( afxSpellCastBar,
  55. "@brief A GUI progress bar useful as a spell casting bar.\n\n"
  56. "@ingroup afxGUI\n"
  57. "@ingroup AFX\n"
  58. );
  59. afxSpellCastBar::afxSpellCastBar()
  60. {
  61. want_border = true;
  62. want_background = true;
  63. use_alt_final_color = false;
  64. rgba_background.set(0.0f, 0.0f, 0.0f, 0.5f);
  65. rgba_border.set(0.5f, 0.5f, 0.5f, 1.0f);
  66. rgba_fill.set(0.0f, 1.0f, 1.0f, 1.0f);
  67. rgba_fill_final.set(0.0f, 1.0f, 1.0f, 1.0f);
  68. fraction = 0.5f;
  69. }
  70. void afxSpellCastBar::setFraction(F32 frac)
  71. {
  72. fraction = mClampF(frac, 0.0f, 1.0f);
  73. }
  74. void afxSpellCastBar::onStaticModified(const char* slotName, const char* newValue)
  75. {
  76. Parent::onStaticModified(slotName, newValue);
  77. if (dStricmp(slotName, "fillColorFinal") == 0)
  78. use_alt_final_color = true;
  79. }
  80. // STATIC
  81. void afxSpellCastBar::initPersistFields()
  82. {
  83. addGroup("Colors");
  84. addField( "backgroundColor", TypeColorF, Offset(rgba_background, afxSpellCastBar),
  85. "...");
  86. addField( "borderColor", TypeColorF, Offset(rgba_border, afxSpellCastBar),
  87. "...");
  88. addField( "fillColor", TypeColorF, Offset(rgba_fill, afxSpellCastBar),
  89. "...");
  90. addField( "fillColorFinal", TypeColorF, Offset(rgba_fill_final, afxSpellCastBar),
  91. "...");
  92. endGroup("Colors");
  93. Parent::initPersistFields();
  94. }
  95. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  96. void afxSpellCastBar::onRender(Point2I offset, const RectI &updateRect)
  97. {
  98. LinearColorF color;
  99. // draw the background
  100. if (want_background)
  101. {
  102. color.set(rgba_background.red, rgba_background.green, rgba_background.blue, rgba_background.alpha*fade_amt);
  103. GFX->getDrawUtil()->drawRectFill(updateRect, color.toColorI());
  104. }
  105. // calculate the rectangle dimensions
  106. RectI rect(updateRect);
  107. rect.extent.x = (S32)(rect.extent.x * fraction);
  108. // draw the filled part of bar
  109. if (fraction >= 1.0f && use_alt_final_color)
  110. color.set(rgba_fill_final.red, rgba_fill_final.green, rgba_fill_final.blue, rgba_fill_final.alpha*fade_amt);
  111. else
  112. color.set(rgba_fill.red, rgba_fill.green, rgba_fill.blue, rgba_fill.alpha*fade_amt);
  113. GFX->getDrawUtil()->drawRectFill(rect, color.toColorI());
  114. // draw the border
  115. if (want_border)
  116. {
  117. color.set(rgba_border.red, rgba_border.green, rgba_border.blue, rgba_border.alpha*fade_amt);
  118. GFX->getDrawUtil()->drawRect(updateRect, color.toColorI());
  119. }
  120. }
  121. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  122. DefineEngineMethod(afxSpellCastBar, setProgress, void, (float percentDone),,
  123. "Set the progress percentage on the progress-bar.\n\n"
  124. "@ingroup AFX")
  125. {
  126. object->setFraction(percentDone);
  127. }
  128. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//