guiSpeedometer.cpp 8.2 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. LinearColorF mNeedleColor; ///< Needle Color
  43. F32 mNeedleLength;
  44. F32 mNeedleWidth;
  45. F32 mTailLength;
  46. GFXStateBlockRef mBlendSB;
  47. public:
  48. GuiSpeedometerHud();
  49. void onRender( Point2I, const RectI &) override;
  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. mNeedleColor.set(1,0,0,1);
  93. }
  94. void GuiSpeedometerHud::initPersistFields()
  95. {
  96. docsURL;
  97. addGroup("Needle");
  98. addFieldV("maxSpeed", TypeRangedF32, Offset( mMaxSpeed, GuiSpeedometerHud ), &CommonValidators::PositiveFloat,
  99. "Maximum Vehicle speed (in Torque units per second) to represent on the "
  100. "speedo (Vehicle speeds greater than this are clamped to maxSpeed)." );
  101. addFieldV("minAngle", TypeRangedF32, Offset( mMinAngle, GuiSpeedometerHud ), &CommonValidators::DegreeRange,
  102. "Angle (in radians) of the needle when the Vehicle speed is 0. An angle "
  103. "of 0 points right, 90 points up etc)." );
  104. addFieldV("maxAngle", TypeRangedF32, Offset( mMaxAngle, GuiSpeedometerHud ), &CommonValidators::DegreeRange,
  105. "Angle (in radians) of the needle when the Vehicle speed is >= maxSpeed. "
  106. "An angle of 0 points right, 90 points up etc)." );
  107. addField("color", TypeColorF, Offset( mNeedleColor, GuiSpeedometerHud ),
  108. "Color of the needle" );
  109. addField("center", TypePoint2F, Offset( mCenter, GuiSpeedometerHud ),
  110. "Center of the needle, offset from the GuiSpeedometerHud control top "
  111. "left corner" );
  112. addFieldV("length", TypeRangedF32, Offset( mNeedleLength, GuiSpeedometerHud ), &CommonValidators::PositiveFloat,
  113. "Length of the needle from center to end" );
  114. addFieldV("width", TypeRangedF32, Offset( mNeedleWidth, GuiSpeedometerHud ), &CommonValidators::PositiveFloat,
  115. "Width of the needle" );
  116. addFieldV("tail", TypeRangedF32, Offset( mTailLength, GuiSpeedometerHud ), &CommonValidators::PositiveFloat,
  117. "Length of the needle from center to tail" );
  118. endGroup("Needle");
  119. Parent::initPersistFields();
  120. }
  121. //-----------------------------------------------------------------------------
  122. /**
  123. Gui onRender method.
  124. Renders an analog speedometer needle over a specified bitmap background.
  125. */
  126. void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
  127. {
  128. // Must have a connection
  129. GameConnection* conn = GameConnection::getConnectionToServer();
  130. if (!conn)
  131. return;
  132. // Requires either a vehicle control object or a vehicle-mounted player
  133. Vehicle* vehicle = dynamic_cast<Vehicle*>(conn->getControlObject());
  134. if(!vehicle){
  135. Player * player = dynamic_cast<Player*>(conn->getControlObject());
  136. if(!player) return;
  137. if (!player->isMounted()) return;
  138. vehicle = dynamic_cast<Vehicle*>(player->getObjectMount());
  139. if(!vehicle) return;
  140. }
  141. Parent::onRender(offset,updateRect);
  142. // Use the vehicle's velocity as its speed...
  143. mSpeed = vehicle->getVelocity().len();
  144. if (mSpeed > mMaxSpeed)
  145. mSpeed = mMaxSpeed;
  146. // Calculate center point if necessary and roll in offsets
  147. Point2F center = mCenter;
  148. if (mIsZero(center.x) && mIsZero(center.y))
  149. {
  150. center.x = getExtent().x / 2.0f;
  151. center.y = getExtent().y / 2.0f;
  152. }
  153. F32 fillOffset = GFX->getFillConventionOffset(); // Find the fill offset
  154. Point2F viewCenter(offset.x + fillOffset + center.x, offset.y + fillOffset + center.y);
  155. // Handle rotation calculations
  156. F32 rotation, spinAngle;
  157. rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
  158. spinAngle = mDegToRad(rotation);
  159. MatrixF rotMatrix(EulerF(0.0, 0.0, spinAngle));
  160. // Set up the needle vertex list
  161. Point3F vertList[5];
  162. vertList[0].set(+mNeedleLength,-mNeedleWidth,0);
  163. vertList[1].set(+mNeedleLength,+mNeedleWidth,0);
  164. vertList[2].set(-mTailLength ,+mNeedleWidth,0);
  165. vertList[3].set(-mTailLength ,-mNeedleWidth,0);
  166. vertList[4].set(+mNeedleLength,-mNeedleWidth,0); //// Get back to the start!
  167. // Create a GFXStateBlock description if one has not been set.
  168. if (mBlendSB.isNull())
  169. {
  170. GFXStateBlockDesc desc;
  171. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  172. desc.samplersDefined = true;
  173. mBlendSB = GFX->createStateBlock(desc);
  174. }
  175. GFX->setStateBlock(mBlendSB);
  176. GFX->setTexture(0, NULL);
  177. // Render the needle
  178. PrimBuild::color4f(mNeedleColor.red, mNeedleColor.green, mNeedleColor.blue, mNeedleColor.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. }