19
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
20
// IN THE SOFTWARE.
21
21
// -----------------------------------------------------------------------------
22
-
23
22
#include " gui/controls/guiBitmapCtrl.h"
24
23
#include " console/consoleTypes.h"
25
24
#include " T3D/gameBase/gameConnection.h"
26
25
#include " T3D/vehicles/vehicle.h"
26
+ #include " T3D/player.h"
27
27
#include " gfx/primBuilder.h"
28
28
29
29
// -----------------------------------------------------------------------------
@@ -147,42 +147,57 @@ void GuiSpeedometerHud::initPersistFields()
147
147
// -----------------------------------------------------------------------------
148
148
/* *
149
149
Gui onRender method.
150
- Renders a health bar with filled background and border .
150
+ Renders an analog speedometer needle over a specified bitmap background .
151
151
*/
152
152
void GuiSpeedometerHud::onRender (Point2I offset, const RectI &updateRect)
153
153
{
154
- // Must have a connection and player control object
154
+ // Must have a connection
155
155
GameConnection* conn = GameConnection::getConnectionToServer ();
156
156
if (!conn)
157
157
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
+ }
161
168
162
169
Parent::onRender (offset,updateRect);
163
170
164
171
// Use the vehicle's velocity as its speed...
165
- mSpeed = control ->getVelocity ().len ();
172
+ mSpeed = vehicle ->getVelocity ().len ();
166
173
if (mSpeed > mMaxSpeed )
167
174
mSpeed = mMaxSpeed ;
168
175
169
- // Render the needle
170
- GFX->pushWorldMatrix ();
176
+ // Calculate center point if necessary and roll in offsets
171
177
Point2F center = mCenter ;
172
178
if (mIsZero (center.x ) && mIsZero (center.y ))
173
179
{
174
180
center.x = getExtent ().x / 2 .0f ;
175
181
center.y = getExtent ().y / 2 .0f ;
176
182
}
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.
186
201
if (mBlendSB .isNull ())
187
202
{
188
203
GFXStateBlockDesc desc;
@@ -191,22 +206,15 @@ void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
191
206
desc.samplers [0 ].textureColorOp = GFXTOPDisable;
192
207
mBlendSB = GFX->createStateBlock (desc);
193
208
}
194
-
195
209
GFX->setStateBlock (mBlendSB );
196
-
197
210
GFX->setTexture (0 , NULL );
198
211
199
- PrimBuild::begin (GFXLineStrip, 5 );
212
+ // Render the needle
200
213
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
+ }
210
219
PrimBuild::end ();
211
220
}
212
-
0 commit comments