Skip to content

Commit 6164f36

Browse files
authored
Merge pull request #1784 from yourarcade/guiSpeedometerHud
Gui speedometer hud
2 parents 03c2ce4 + 8d61078 commit 6164f36

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

Engine/source/T3D/vehicles/guiSpeedometer.cpp

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2020
// IN THE SOFTWARE.
2121
//-----------------------------------------------------------------------------
22-
2322
#include "gui/controls/guiBitmapCtrl.h"
2423
#include "console/consoleTypes.h"
2524
#include "T3D/gameBase/gameConnection.h"
2625
#include "T3D/vehicles/vehicle.h"
26+
#include "T3D/player.h"
2727
#include "gfx/primBuilder.h"
2828

2929
//-----------------------------------------------------------------------------
@@ -147,42 +147,57 @@ void GuiSpeedometerHud::initPersistFields()
147147
//-----------------------------------------------------------------------------
148148
/**
149149
Gui onRender method.
150-
Renders a health bar with filled background and border.
150+
Renders an analog speedometer needle over a specified bitmap background.
151151
*/
152152
void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
153153
{
154-
// Must have a connection and player control object
154+
// Must have a connection
155155
GameConnection* conn = GameConnection::getConnectionToServer();
156156
if (!conn)
157157
return;
158-
Vehicle* control = dynamic_cast<Vehicle*>(conn->getControlObject());
159-
if (!control)
160-
return;
158+
159+
// Requires either a vehicle control object or a vehicle-mounted player
160+
Vehicle* vehicle = dynamic_cast<Vehicle*>(conn->getControlObject());
161+
if(!vehicle){
162+
Player * player = dynamic_cast<Player*>(conn->getControlObject());
163+
if(!player) return;
164+
if (!player->isMounted()) return;
165+
vehicle = dynamic_cast<Vehicle*>(player->getObjectMount());
166+
if(!vehicle) return;
167+
}
161168

162169
Parent::onRender(offset,updateRect);
163170

164171
// Use the vehicle's velocity as its speed...
165-
mSpeed = control->getVelocity().len();
172+
mSpeed = vehicle->getVelocity().len();
166173
if (mSpeed > mMaxSpeed)
167174
mSpeed = mMaxSpeed;
168175

169-
// Render the needle
170-
GFX->pushWorldMatrix();
176+
// Calculate center point if necessary and roll in offsets
171177
Point2F center = mCenter;
172178
if (mIsZero(center.x) && mIsZero(center.y))
173179
{
174180
center.x = getExtent().x / 2.0f;
175181
center.y = getExtent().y / 2.0f;
176182
}
177-
MatrixF newMat(1);
178-
179-
newMat.setPosition(Point3F(getLeft() + center.x, getTop() + center.y, 0.0f));
180-
181-
F32 rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
182-
AngAxisF newRot(Point3F(0.0f,0.0f,-1.0f), rotation);
183-
184-
newRot.setMatrix(&newMat);
185-
183+
F32 fillOffset = GFX->getFillConventionOffset(); // Find the fill offset
184+
Point2F viewCenter(offset.x + fillOffset + center.x, offset.y + fillOffset + center.y);
185+
186+
// Handle rotation calculations
187+
F32 rotation, spinAngle;
188+
rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
189+
spinAngle = mDegToRad(rotation);
190+
MatrixF rotMatrix(EulerF(0.0, 0.0, spinAngle));
191+
192+
// Set up the needle vertex list
193+
Point3F vertList[5];
194+
vertList[0].set(+mNeedleLength,-mNeedleWidth,0);
195+
vertList[1].set(+mNeedleLength,+mNeedleWidth,0);
196+
vertList[2].set(-mTailLength ,+mNeedleWidth,0);
197+
vertList[3].set(-mTailLength ,-mNeedleWidth,0);
198+
vertList[4].set(+mNeedleLength,-mNeedleWidth,0); //// Get back to the start!
199+
200+
// Create a GFXStateBlock description if one has not been set.
186201
if (mBlendSB.isNull())
187202
{
188203
GFXStateBlockDesc desc;
@@ -191,22 +206,15 @@ void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
191206
desc.samplers[0].textureColorOp = GFXTOPDisable;
192207
mBlendSB = GFX->createStateBlock(desc);
193208
}
194-
195209
GFX->setStateBlock(mBlendSB);
196-
197210
GFX->setTexture(0, NULL);
198211

199-
PrimBuild::begin(GFXLineStrip, 5);
212+
// Render the needle
200213
PrimBuild::color4f(mColor.red, mColor.green, mColor.blue, mColor.alpha);
201-
202-
PrimBuild::vertex2f(+mNeedleLength,-mNeedleWidth);
203-
PrimBuild::vertex2f(+mNeedleLength,+mNeedleWidth);
204-
PrimBuild::vertex2f(-mTailLength ,+mNeedleWidth);
205-
PrimBuild::vertex2f(-mTailLength ,-mNeedleWidth);
206-
207-
//// Get back to the start!
208-
PrimBuild::vertex2f(+mNeedleLength,-mNeedleWidth);
209-
214+
PrimBuild::begin(GFXLineStrip, 5);
215+
for(int k=0; k<5; k++){
216+
rotMatrix.mulP(vertList[k]);
217+
PrimBuild::vertex2f(vertList[k].x + viewCenter.x, vertList[k].y + viewCenter.y);
218+
}
210219
PrimBuild::end();
211220
}
212-

0 commit comments

Comments
 (0)