guiSpeedometer.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/controls/guiBitmapCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "T3D/gameBase/gameConnection.h"
  25. #include "T3D/vehicles/vehicle.h"
  26. #include "T3D/player.h"
  27. #include "gfx/primBuilder.h"
  28. //-----------------------------------------------------------------------------
  29. /// A Speedometer control.
  30. /// This gui displays the speed of the current Vehicle based
  31. /// control object. This control only works if a server
  32. /// connection exists and its control object is a vehicle. If
  33. /// either of these requirements is false, the control is not rendered.
  34. class GuiSpeedometerHud : public GuiBitmapCtrl
  35. {
  36. typedef GuiBitmapCtrl Parent;
  37. F32 mSpeed; ///< Current speed
  38. F32 mMaxSpeed; ///< Max speed at max need pos
  39. F32 mMaxAngle; ///< Max pos of needle
  40. F32 mMinAngle; ///< Min pos of needle
  41. Point2F mCenter; ///< Center of needle rotation
  42. ColorF mColor; ///< Needle Color
  43. F32 mNeedleLength;
  44. F32 mNeedleWidth;
  45. F32 mTailLength;
  46. GFXStateBlockRef mBlendSB;
  47. public:
  48. GuiSpeedometerHud();
  49. void onRender( Point2I, const RectI &);
  50. static void initPersistFields();
  51. DECLARE_CONOBJECT( GuiSpeedometerHud );
  52. DECLARE_CATEGORY( "Gui Game" );
  53. DECLARE_DESCRIPTION( "Displays the speed of the current Vehicle-based control object." );
  54. };
  55. //-----------------------------------------------------------------------------
  56. IMPLEMENT_CONOBJECT( GuiSpeedometerHud );
  57. ConsoleDocClass( GuiSpeedometerHud,
  58. "@brief Displays the speed of the current Vehicle based control object.\n\n"
  59. "This control only works if a server connection exists, and its control "
  60. "object is a Vehicle derived class. If either of these requirements is false, "
  61. "the control is not rendered.<br>"
  62. "The control renders the speedometer needle as a colored quad, rotated to "
  63. "indicate the Vehicle speed as determined by the <i>minAngle</i>, "
  64. "<i>maxAngle</i>, and <i>maxSpeed</i> properties. This control is normally "
  65. "placed on top of a GuiBitmapCtrl representing the speedometer dial.\n\n"
  66. "@tsexample\n"
  67. "new GuiSpeedometerHud()\n"
  68. "{\n"
  69. " maxSpeed = \"100\";\n"
  70. " minAngle = \"215\";\n"
  71. " maxAngle = \"0\";\n"
  72. " color = \"1 0.3 0.3 1\";\n"
  73. " center = \"130 123\";\n"
  74. " length = \"100\";\n"
  75. " width = \"2\";\n"
  76. " tail = \"0\";\n"
  77. " //Properties not specific to this control have been omitted from this example.\n"
  78. "};\n"
  79. "@endtsexample\n\n"
  80. "@ingroup GuiContainers"
  81. );
  82. GuiSpeedometerHud::GuiSpeedometerHud()
  83. {
  84. mSpeed = 0;
  85. mMaxSpeed = 100;
  86. mMaxAngle = 0;
  87. mMinAngle = 200;
  88. mCenter.set(0,0);
  89. mNeedleWidth = 3;
  90. mNeedleLength = 10;
  91. mTailLength = 5;
  92. mColor.set(1,0,0,1);
  93. }
  94. void GuiSpeedometerHud::initPersistFields()
  95. {
  96. addGroup("Needle");
  97. addField("maxSpeed", TypeF32, Offset( mMaxSpeed, GuiSpeedometerHud ),
  98. "Maximum Vehicle speed (in Torque units per second) to represent on the "
  99. "speedo (Vehicle speeds greater than this are clamped to maxSpeed)." );
  100. addField("minAngle", TypeF32, Offset( mMinAngle, GuiSpeedometerHud ),
  101. "Angle (in radians) of the needle when the Vehicle speed is 0. An angle "
  102. "of 0 points right, 90 points up etc)." );
  103. addField("maxAngle", TypeF32, Offset( mMaxAngle, GuiSpeedometerHud ),
  104. "Angle (in radians) of the needle when the Vehicle speed is >= maxSpeed. "
  105. "An angle of 0 points right, 90 points up etc)." );
  106. addField("color", TypeColorF, Offset( mColor, GuiSpeedometerHud ),
  107. "Color of the needle" );
  108. addField("center", TypePoint2F, Offset( mCenter, GuiSpeedometerHud ),
  109. "Center of the needle, offset from the GuiSpeedometerHud control top "
  110. "left corner" );
  111. addField("length", TypeF32, Offset( mNeedleLength, GuiSpeedometerHud ),
  112. "Length of the needle from center to end" );
  113. addField("width", TypeF32, Offset( mNeedleWidth, GuiSpeedometerHud ),
  114. "Width of the needle" );
  115. addField("tail", TypeF32, Offset( mTailLength, GuiSpeedometerHud ),
  116. "Length of the needle from center to tail" );
  117. endGroup("Needle");
  118. Parent::initPersistFields();
  119. }
  120. //-----------------------------------------------------------------------------
  121. /**
  122. Gui onRender method.
  123. Renders an analog speedometer needle over a specified bitmap background.
  124. */
  125. void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
  126. {
  127. // Must have a connection
  128. GameConnection* conn = GameConnection::getConnectionToServer();
  129. if (!conn)
  130. return;
  131. // Requires either a vehicle control object or a vehicle-mounted player
  132. Vehicle* vehicle = dynamic_cast<Vehicle*>(conn->getControlObject());
  133. if(!vehicle){
  134. Player * player = dynamic_cast<Player*>(conn->getControlObject());
  135. if(!player) return;
  136. if (!player->isMounted()) return;
  137. vehicle = dynamic_cast<Vehicle*>(player->getObjectMount());
  138. if(!vehicle) return;
  139. }
  140. Parent::onRender(offset,updateRect);
  141. // Use the vehicle's velocity as its speed...
  142. mSpeed = vehicle->getVelocity().len();
  143. if (mSpeed > mMaxSpeed)
  144. mSpeed = mMaxSpeed;
  145. // Calculate center point if necessary and roll in offsets
  146. Point2F center = mCenter;
  147. if (mIsZero(center.x) && mIsZero(center.y))
  148. {
  149. center.x = getExtent().x / 2.0f;
  150. center.y = getExtent().y / 2.0f;
  151. }
  152. F32 fillOffset = GFX->getFillConventionOffset(); // Find the fill offset
  153. Point2F viewCenter(offset.x + fillOffset + center.x, offset.y + fillOffset + center.y);
  154. // Handle rotation calculations
  155. F32 rotation, spinAngle;
  156. rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
  157. spinAngle = mDegToRad(rotation);
  158. MatrixF rotMatrix(EulerF(0.0, 0.0, spinAngle));
  159. // Set up the needle vertex list
  160. Point3F vertList[5];
  161. vertList[0].set(+mNeedleLength,-mNeedleWidth,0);
  162. vertList[1].set(+mNeedleLength,+mNeedleWidth,0);
  163. vertList[2].set(-mTailLength ,+mNeedleWidth,0);
  164. vertList[3].set(-mTailLength ,-mNeedleWidth,0);
  165. vertList[4].set(+mNeedleLength,-mNeedleWidth,0); //// Get back to the start!
  166. // Create a GFXStateBlock description if one has not been set.
  167. if (mBlendSB.isNull())
  168. {
  169. GFXStateBlockDesc desc;
  170. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  171. desc.samplersDefined = true;
  172. desc.samplers[0].textureColorOp = GFXTOPDisable;
  173. mBlendSB = GFX->createStateBlock(desc);
  174. }
  175. GFX->setStateBlock(mBlendSB);
  176. GFX->setTexture(0, NULL);
  177. // Render the needle
  178. PrimBuild::color4f(mColor.red, mColor.green, mColor.blue, mColor.alpha);
  179. PrimBuild::begin(GFXLineStrip, 5);
  180. for(int k=0; k<5; k++){
  181. rotMatrix.mulP(vertList[k]);
  182. PrimBuild::vertex2f(vertList[k].x + viewCenter.x, vertList[k].y + viewCenter.y);
  183. }
  184. PrimBuild::end();
  185. }