guiClockHud.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "gui/core/guiControl.h"
  24. #include "console/consoleTypes.h"
  25. #include "T3D/shapeBase.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "console/engineAPI.h"
  28. //-----------------------------------------------------------------------------
  29. /// Vary basic HUD clock.
  30. /// Displays the current simulation time offset from some base. The base time
  31. /// is usually synchronized with the server as mission start time. This hud
  32. /// currently only displays minutes:seconds.
  33. class GuiClockHud : public GuiControl
  34. {
  35. typedef GuiControl Parent;
  36. bool mShowFrame;
  37. bool mShowFill;
  38. bool mTimeReversed;
  39. LinearColorF mFillColor;
  40. LinearColorF mFrameColor;
  41. LinearColorF mTextColor;
  42. S32 mTimeOffset;
  43. bool mIsPaused;
  44. S32 mPausedTime;
  45. S32 mSignificantTime;
  46. bool mSignificantTimeReached;
  47. public:
  48. GuiClockHud();
  49. void setTime(F32 newTime);
  50. void setReverseTime(F32 reverseTime);
  51. F32 getTime();
  52. static bool pauseTime(void* object, const char* index, const char* data);
  53. static bool reverseTime(void* object, const char* index, const char* data);
  54. static bool setSignificantTime(void* object, const char* index, const char* data);
  55. void onRender( Point2I, const RectI &) override;
  56. static void initPersistFields();
  57. DECLARE_CONOBJECT( GuiClockHud );
  58. DECLARE_CATEGORY( "Gui Game" );
  59. DECLARE_DESCRIPTION( "Basic HUD clock. Displays the current simulation time offset from some base." );
  60. DECLARE_CALLBACK(void, onSignificantTimeReached, (GuiClockHud* obj));
  61. };
  62. IMPLEMENT_CALLBACK(GuiClockHud, onSignificantTimeReached, void, (GuiClockHud* obj), (obj), "Called when the significant time is reached.");
  63. //-----------------------------------------------------------------------------
  64. IMPLEMENT_CONOBJECT( GuiClockHud );
  65. ConsoleDocClass( GuiClockHud,
  66. "@brief Basic HUD clock. Displays the current simulation time offset from some base.\n"
  67. "@tsexample\n"
  68. "\n new GuiClockHud()"
  69. "{\n"
  70. " fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  71. " frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  72. " textColor = \"1.0 1.0 1.0 1.0\"; // Solid white text Color\n"
  73. " showFill = \"true\";\n"
  74. " showFrame = \"true\";\n"
  75. "};\n"
  76. "@endtsexample\n\n"
  77. "@ingroup GuiGame\n"
  78. );
  79. GuiClockHud::GuiClockHud()
  80. {
  81. mShowFrame = mShowFill = true;
  82. mTimeReversed = false;
  83. mFillColor.set(0, 0, 0, 0.5);
  84. mFrameColor.set(0, 1, 0, 1);
  85. mTextColor.set( 0, 1, 0, 1 );
  86. mTimeOffset = 0;
  87. mIsPaused = false;
  88. mPausedTime = 0;
  89. mSignificantTime = 0;
  90. mSignificantTimeReached = false;
  91. }
  92. void GuiClockHud::initPersistFields()
  93. {
  94. docsURL;
  95. addGroup("Misc");
  96. addField( "showFill", TypeBool, Offset( mShowFill, GuiClockHud ), "If true, draws a background color behind the control.");
  97. addField( "showFrame", TypeBool, Offset( mShowFrame, GuiClockHud ), "If true, draws a frame around the control." );
  98. addField( "fillColor", TypeColorF, Offset( mFillColor, GuiClockHud ), "Standard color for the background of the control." );
  99. addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiClockHud ), "Color for the control's frame." );
  100. addField( "textColor", TypeColorF, Offset( mTextColor, GuiClockHud ), "Color for the text on this control." );
  101. addProtectedField("pause", TypeBool, Offset(mIsPaused, GuiClockHud), &GuiClockHud::pauseTime, &defaultProtectedGetFn, "Pause");
  102. addProtectedField("reversed", TypeBool, Offset(mTimeReversed, GuiClockHud), &GuiClockHud::reverseTime, &defaultProtectedGetFn, "reversed");
  103. addProtectedField("significantTime", TypeS32, Offset(mSignificantTime, GuiClockHud), &GuiClockHud::setSignificantTime, &defaultProtectedGetFn, "set significant timestamp");
  104. endGroup("Misc");
  105. Parent::initPersistFields();
  106. }
  107. //-----------------------------------------------------------------------------
  108. void GuiClockHud::onRender(Point2I offset, const RectI &updateRect)
  109. {
  110. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  111. // Background first
  112. if (mShowFill)
  113. drawUtil->drawRectFill(updateRect, mFillColor.toColorI());
  114. // Convert ms time into hours, minutes and seconds.
  115. S32 time = mIsPaused ? mPausedTime : S32(getTime());
  116. S32 secs = time % 60;
  117. S32 mins = (time % 3600) / 60;
  118. // Currently only displays min/sec
  119. char buf[256];
  120. dSprintf(buf,sizeof(buf), "%02d:%02d",mins,secs);
  121. // Center the text
  122. offset.x += (getWidth() - mProfile->mFont->getStrWidth((const UTF8 *)buf)) / 2;
  123. offset.y += (getHeight() - mProfile->mFont->getHeight()) / 2;
  124. drawUtil->setBitmapModulation(mTextColor.toColorI());
  125. drawUtil->drawText(mProfile->mFont, offset, buf);
  126. drawUtil->clearBitmapModulation();
  127. // Border last
  128. if (mShowFrame)
  129. drawUtil->drawRect(updateRect, mFrameColor.toColorI());
  130. if (!mIsPaused && !mSignificantTimeReached)
  131. {
  132. if (mTimeReversed && time < mSignificantTime)
  133. {
  134. onSignificantTimeReached_callback(this);
  135. mSignificantTimeReached = true;
  136. }
  137. else if (time > mSignificantTime)
  138. {
  139. onSignificantTimeReached_callback(this);
  140. mSignificantTimeReached = true;
  141. }
  142. }
  143. }
  144. //-----------------------------------------------------------------------------
  145. void GuiClockHud::setReverseTime(F32 time)
  146. {
  147. // Set the current time in seconds.
  148. mTimeReversed = true;
  149. mTimeOffset = S32(time * 1000) + Platform::getVirtualMilliseconds();
  150. mSignificantTimeReached = false;
  151. mPausedTime = getTime();
  152. }
  153. void GuiClockHud::setTime(F32 time)
  154. {
  155. // Set the current time in seconds.
  156. mTimeReversed = false;
  157. mTimeOffset = S32(time * 1000) - Platform::getVirtualMilliseconds();
  158. mSignificantTimeReached = false;
  159. mPausedTime = getTime();
  160. }
  161. F32 GuiClockHud::getTime()
  162. {
  163. // Return elapsed time in seconds.
  164. if(mTimeReversed)
  165. return F32(mTimeOffset - Platform::getVirtualMilliseconds()) / 1000;
  166. else
  167. return F32(mTimeOffset + Platform::getVirtualMilliseconds()) / 1000;
  168. }
  169. bool GuiClockHud::pauseTime(void* object, const char* index, const char* data)
  170. {
  171. GuiClockHud* obj = reinterpret_cast<GuiClockHud*>(object);
  172. obj->mIsPaused = dAtob(data);
  173. if (obj->mIsPaused)
  174. obj->mPausedTime = S32(obj->getTime());
  175. else
  176. obj->setTime(obj->mPausedTime);
  177. return true;
  178. }
  179. bool GuiClockHud::reverseTime(void* object, const char* index, const char* data)
  180. {
  181. GuiClockHud* obj = reinterpret_cast<GuiClockHud*>(object);
  182. F32 curTime = obj->getTime();
  183. bool setReverse = dAtob(data);
  184. if (setReverse)
  185. obj->setReverseTime(curTime);
  186. else
  187. obj->setTime(curTime);
  188. return true;
  189. }
  190. bool GuiClockHud::setSignificantTime(void* object, const char* index, const char* data)
  191. {
  192. GuiClockHud* obj = reinterpret_cast<GuiClockHud*>(object);
  193. obj->mSignificantTime = dAtoi(data);
  194. obj->mSignificantTimeReached = false;
  195. return true;
  196. }
  197. DefineEngineMethod(GuiClockHud, setTime, void, (F32 timeInSeconds),(60), "Sets the current base time for the clock.\n"
  198. "@param timeInSeconds Time to set the clock, in seconds (IE: 00:02 would be 120)\n"
  199. "@tsexample\n"
  200. "// Define the time, in seconds\n"
  201. "%timeInSeconds = 120;\n\n"
  202. "// Change the time on the GuiClockHud control\n"
  203. "%guiClockHud.setTime(%timeInSeconds);\n"
  204. "@endtsexample\n"
  205. )
  206. {
  207. object->setTime(timeInSeconds);
  208. }
  209. DefineEngineMethod(GuiClockHud, setReverseTime, void, (F32 timeInSeconds),(60), "@brief Sets a time for a countdown clock.\n\n"
  210. "Setting the time like this will cause the clock to count backwards from the specified time.\n\n"
  211. "@param timeInSeconds Time to set the clock, in seconds (IE: 00:02 would be 120)\n\n"
  212. "@see setTime\n"
  213. )
  214. {
  215. object->setReverseTime(timeInSeconds);
  216. }
  217. DefineEngineMethod(GuiClockHud, getTime, F32, (),, "Returns the current time, in seconds.\n"
  218. "@return timeInseconds Current time, in seconds\n"
  219. "@tsexample\n"
  220. "// Get the current time from the GuiClockHud control\n"
  221. "%timeInSeconds = %guiClockHud.getTime();\n"
  222. "@endtsexample\n"
  223. )
  224. {
  225. return object->getTime();
  226. }