afxStatusBar.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "T3D/gameBase/gameConnection.h"
  28. #include "T3D/shapeBase.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "afx/ui/afxProgressBase.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. class afxStatusBar : public GuiControl, public afxProgressBase
  33. {
  34. typedef GuiControl Parent;
  35. LinearColorF rgba_fill;
  36. F32 fraction;
  37. ShapeBase* shape;
  38. bool show_energy;
  39. bool monitor_player;
  40. public:
  41. /*C*/ afxStatusBar();
  42. virtual void onRender(Point2I, const RectI&);
  43. void setFraction(F32 frac);
  44. F32 getFraction() const { return fraction; }
  45. virtual void setProgress(F32 value) { setFraction(value); }
  46. void setShape(ShapeBase* s);
  47. void clearShape() { setShape(NULL); }
  48. virtual bool onWake();
  49. virtual void onSleep();
  50. virtual void onMouseDown(const GuiEvent &event);
  51. virtual void onDeleteNotify(SimObject*);
  52. static void initPersistFields();
  53. DECLARE_CONOBJECT(afxStatusBar);
  54. DECLARE_CATEGORY("AFX");
  55. };
  56. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  57. IMPLEMENT_CONOBJECT(afxStatusBar);
  58. ConsoleDocClass( afxStatusBar,
  59. "@brief A GUI status bar for tracking and displaying health and energy of ShapeBase "
  60. "objects.\n\n"
  61. "@ingroup afxGUI\n"
  62. "@ingroup AFX\n"
  63. );
  64. afxStatusBar::afxStatusBar()
  65. {
  66. rgba_fill.set(0.0f, 1.0f, 1.0f, 1.0f);
  67. fraction = 1.0f;
  68. shape = 0;
  69. show_energy = false;
  70. monitor_player = false;
  71. }
  72. void afxStatusBar::setFraction(F32 frac)
  73. {
  74. fraction = mClampF(frac, 0.0f, 1.0f);
  75. }
  76. void afxStatusBar::setShape(ShapeBase* s)
  77. {
  78. if (shape)
  79. clearNotify(shape);
  80. shape = s;
  81. if (shape)
  82. deleteNotify(shape);
  83. }
  84. void afxStatusBar::onDeleteNotify(SimObject* obj)
  85. {
  86. if (shape == (ShapeBase*)obj)
  87. {
  88. shape = NULL;
  89. return;
  90. }
  91. Parent::onDeleteNotify(obj);
  92. }
  93. bool afxStatusBar::onWake()
  94. {
  95. if (!Parent::onWake())
  96. return false;
  97. return true;
  98. }
  99. void afxStatusBar::onSleep()
  100. {
  101. //clearShape();
  102. Parent::onSleep();
  103. }
  104. // STATIC
  105. void afxStatusBar::initPersistFields()
  106. {
  107. docsURL;
  108. addField("fillColor", TypeColorF, Offset(rgba_fill, afxStatusBar),
  109. "...");
  110. addField("displayEnergy", TypeBool, Offset(show_energy, afxStatusBar),
  111. "...");
  112. addField("monitorPlayer", TypeBool, Offset(monitor_player, afxStatusBar),
  113. "...");
  114. Parent::initPersistFields();
  115. }
  116. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  117. void afxStatusBar::onRender(Point2I offset, const RectI &updateRect)
  118. {
  119. if (!shape)
  120. return;
  121. if (shape->getDamageState() != ShapeBase::Enabled)
  122. fraction = 0.0f;
  123. else
  124. fraction = (show_energy) ? shape->getEnergyValue() : (1.0f - shape->getDamageValue());
  125. // set alpha value for the fill area
  126. rgba_fill.alpha = 1.0f;
  127. // calculate the rectangle dimensions
  128. RectI rect(updateRect);
  129. rect.extent.x = (S32)(rect.extent.x*fraction);
  130. // draw the filled part of bar
  131. GFX->getDrawUtil()->drawRectFill(rect, rgba_fill.toColorI());
  132. }
  133. void afxStatusBar::onMouseDown(const GuiEvent &event)
  134. {
  135. GuiControl *parent = getParent();
  136. if (parent)
  137. parent->onMouseDown(event);
  138. }
  139. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  140. DefineEngineMethod(afxStatusBar, setProgress, void, (float percentDone),,
  141. "Set the progress percentage on the status-bar.\n\n"
  142. "@ingroup AFX")
  143. {
  144. object->setFraction(percentDone);
  145. }
  146. DefineEngineMethod(afxStatusBar, setShape, void, (ShapeBase* shape),,
  147. "Associate a ShapeBase-derived object with the status-bar.\n\n"
  148. "@ingroup AFX")
  149. {
  150. object->setShape(shape);
  151. }
  152. DefineEngineMethod(afxStatusBar, clearShape, void, (),,
  153. "Clear out any ShapeBase-derived object associated with the status-bar.\n\n"
  154. "@ingroup AFX")
  155. {
  156. object->clearShape();
  157. }
  158. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//