Torque3D Documentation / _generateds / guiSpeedometer.cpp

guiSpeedometer.cpp

Engine/source/T3D/vehicles/guiSpeedometer.cpp

More...

Classes:

class

A Speedometer control.

Public Functions

ConsoleDocClass(GuiSpeedometerHud , "@brief Displays the speed of the current <a href="/coding/class/classvehicle/">Vehicle</a> based <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n\n</a>" "This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> only works <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> a server connection exists, and its <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> " "object is a <a href="/coding/class/classvehicle/">Vehicle</a> derived class. If either of these requirements is false, " "the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is not rendered.< br >" "The <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> renders the speedometer needle as a colored quad, rotated <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> " "indicate the <a href="/coding/class/classvehicle/">Vehicle</a> speed as determined by the< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >minAngle</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > , " "< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >maxAngle</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > , and< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >maxSpeed</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > properties. This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is normally " "placed on top of a <a href="/coding/class/classguibitmapctrl/">GuiBitmapCtrl</a> representing the speedometer <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">dial.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguispeedometerhud/">GuiSpeedometerHud</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " maxSpeed=\"100\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   minAngle = \"215\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   maxAngle = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   color = \"1 0.3 0.3 1\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   center = \"130 123\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   length = \"100\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   width = \"2\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   tail = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup GuiContainers" )

Detailed Description

Public Functions

ConsoleDocClass(GuiSpeedometerHud , "@brief Displays the speed of the current <a href="/coding/class/classvehicle/">Vehicle</a> based <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n\n</a>" "This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> only works <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> a server connection exists, and its <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> " "object is a <a href="/coding/class/classvehicle/">Vehicle</a> derived class. If either of these requirements is false, " "the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is not rendered.< br >" "The <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> renders the speedometer needle as a colored quad, rotated <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> " "indicate the <a href="/coding/class/classvehicle/">Vehicle</a> speed as determined by the< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >minAngle</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > , " "< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >maxAngle</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > , and< <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> >maxSpeed</<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1acb559820d9ca11295b4500f179ef6392">i</a> > properties. This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is normally " "placed on top of a <a href="/coding/class/classguibitmapctrl/">GuiBitmapCtrl</a> representing the speedometer <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">dial.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "<a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguispeedometerhud/">GuiSpeedometerHud</a>()\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " maxSpeed=\"100\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   minAngle = \"215\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   maxAngle = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   color = \"1 0.3 0.3 1\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   center = \"130 123\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   length = \"100\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   width = \"2\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   tail = \"0\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   //Properties not specific <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> this <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> have been omitted from this <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">example.\n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup GuiContainers" )

IMPLEMENT_CONOBJECT(GuiSpeedometerHud )

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