guiClockHud.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. public:
  44. GuiClockHud();
  45. void setTime(F32 newTime);
  46. void setReverseTime(F32 reverseTime);
  47. F32 getTime();
  48. void onRender( Point2I, const RectI &);
  49. static void initPersistFields();
  50. DECLARE_CONOBJECT( GuiClockHud );
  51. DECLARE_CATEGORY( "Gui Game" );
  52. DECLARE_DESCRIPTION( "Basic HUD clock. Displays the current simulation time offset from some base." );
  53. };
  54. //-----------------------------------------------------------------------------
  55. IMPLEMENT_CONOBJECT( GuiClockHud );
  56. ConsoleDocClass( GuiClockHud,
  57. "@brief Basic HUD clock. Displays the current simulation time offset from some base.\n"
  58. "@tsexample\n"
  59. "\n new GuiClockHud()"
  60. "{\n"
  61. " fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  62. " frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  63. " textColor = \"1.0 1.0 1.0 1.0\"; // Solid white text Color\n"
  64. " showFill = \"true\";\n"
  65. " showFrame = \"true\";\n"
  66. "};\n"
  67. "@endtsexample\n\n"
  68. "@ingroup GuiGame\n"
  69. );
  70. GuiClockHud::GuiClockHud()
  71. {
  72. mShowFrame = mShowFill = true;
  73. mTimeReversed = false;
  74. mFillColor.set(0, 0, 0, 0.5);
  75. mFrameColor.set(0, 1, 0, 1);
  76. mTextColor.set( 0, 1, 0, 1 );
  77. mTimeOffset = 0;
  78. }
  79. void GuiClockHud::initPersistFields()
  80. {
  81. addGroup("Misc");
  82. addField( "showFill", TypeBool, Offset( mShowFill, GuiClockHud ), "If true, draws a background color behind the control.");
  83. addField( "showFrame", TypeBool, Offset( mShowFrame, GuiClockHud ), "If true, draws a frame around the control." );
  84. addField( "fillColor", TypeColorF, Offset( mFillColor, GuiClockHud ), "Standard color for the background of the control." );
  85. addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiClockHud ), "Color for the control's frame." );
  86. addField( "textColor", TypeColorF, Offset( mTextColor, GuiClockHud ), "Color for the text on this control." );
  87. endGroup("Misc");
  88. Parent::initPersistFields();
  89. }
  90. //-----------------------------------------------------------------------------
  91. void GuiClockHud::onRender(Point2I offset, const RectI &updateRect)
  92. {
  93. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  94. // Background first
  95. if (mShowFill)
  96. drawUtil->drawRectFill(updateRect, mFillColor.toColorI());
  97. // Convert ms time into hours, minutes and seconds.
  98. S32 time = S32(getTime());
  99. S32 secs = time % 60;
  100. S32 mins = (time % 3600) / 60;
  101. // Currently only displays min/sec
  102. char buf[256];
  103. dSprintf(buf,sizeof(buf), "%02d:%02d",mins,secs);
  104. // Center the text
  105. offset.x += (getWidth() - mProfile->mFont->getStrWidth((const UTF8 *)buf)) / 2;
  106. offset.y += (getHeight() - mProfile->mFont->getHeight()) / 2;
  107. drawUtil->setBitmapModulation(mTextColor.toColorI());
  108. drawUtil->drawText(mProfile->mFont, offset, buf);
  109. drawUtil->clearBitmapModulation();
  110. // Border last
  111. if (mShowFrame)
  112. drawUtil->drawRect(updateRect, mFrameColor.toColorI());
  113. }
  114. //-----------------------------------------------------------------------------
  115. void GuiClockHud::setReverseTime(F32 time)
  116. {
  117. // Set the current time in seconds.
  118. mTimeReversed = true;
  119. mTimeOffset = S32(time * 1000) + Platform::getVirtualMilliseconds();
  120. }
  121. void GuiClockHud::setTime(F32 time)
  122. {
  123. // Set the current time in seconds.
  124. mTimeReversed = false;
  125. mTimeOffset = S32(time * 1000) - Platform::getVirtualMilliseconds();
  126. }
  127. F32 GuiClockHud::getTime()
  128. {
  129. // Return elapsed time in seconds.
  130. if(mTimeReversed)
  131. return F32(mTimeOffset - Platform::getVirtualMilliseconds()) / 1000;
  132. else
  133. return F32(mTimeOffset + Platform::getVirtualMilliseconds()) / 1000;
  134. }
  135. DefineEngineMethod(GuiClockHud, setTime, void, (F32 timeInSeconds),(60), "Sets the current base time for the clock.\n"
  136. "@param timeInSeconds Time to set the clock, in seconds (IE: 00:02 would be 120)\n"
  137. "@tsexample\n"
  138. "// Define the time, in seconds\n"
  139. "%timeInSeconds = 120;\n\n"
  140. "// Change the time on the GuiClockHud control\n"
  141. "%guiClockHud.setTime(%timeInSeconds);\n"
  142. "@endtsexample\n"
  143. )
  144. {
  145. object->setTime(timeInSeconds);
  146. }
  147. DefineEngineMethod(GuiClockHud, setReverseTime, void, (F32 timeInSeconds),(60), "@brief Sets a time for a countdown clock.\n\n"
  148. "Setting the time like this will cause the clock to count backwards from the specified time.\n\n"
  149. "@param timeInSeconds Time to set the clock, in seconds (IE: 00:02 would be 120)\n\n"
  150. "@see setTime\n"
  151. )
  152. {
  153. object->setReverseTime(timeInSeconds);
  154. }
  155. DefineEngineMethod(GuiClockHud, getTime, F32, (),, "Returns the current time, in seconds.\n"
  156. "@return timeInseconds Current time, in seconds\n"
  157. "@tsexample\n"
  158. "// Get the current time from the GuiClockHud control\n"
  159. "%timeInSeconds = %guiClockHud.getTime();\n"
  160. "@endtsexample\n"
  161. )
  162. {
  163. return object->getTime();
  164. }