-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathguiSpeedometer.cpp
220 lines (185 loc) · 7.95 KB
/
guiSpeedometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "gui/controls/guiBitmapCtrl.h"
#include "console/consoleTypes.h"
#include "T3D/gameBase/gameConnection.h"
#include "T3D/vehicles/vehicle.h"
#include "T3D/player.h"
#include "gfx/primBuilder.h"
//-----------------------------------------------------------------------------
/// A Speedometer control.
/// This gui displays the speed of the current Vehicle based
/// control object. This control only works if a server
/// connection exists and its control object is a vehicle. If
/// either of these requirements is false, the control is not rendered.
class GuiSpeedometerHud : public GuiBitmapCtrl
{
typedef GuiBitmapCtrl Parent;
F32 mSpeed; ///< Current speed
F32 mMaxSpeed; ///< Max speed at max need pos
F32 mMaxAngle; ///< Max pos of needle
F32 mMinAngle; ///< Min pos of needle
Point2F mCenter; ///< Center of needle rotation
LinearColorF mNeedleColor; ///< Needle Color
F32 mNeedleLength;
F32 mNeedleWidth;
F32 mTailLength;
GFXStateBlockRef mBlendSB;
public:
GuiSpeedometerHud();
void onRender( Point2I, const RectI &);
static void initPersistFields();
DECLARE_CONOBJECT( GuiSpeedometerHud );
DECLARE_CATEGORY( "Gui Game" );
DECLARE_DESCRIPTION( "Displays the speed of the current Vehicle-based control object." );
};
//-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT( GuiSpeedometerHud );
ConsoleDocClass( GuiSpeedometerHud,
"@brief Displays the speed of the current Vehicle based control object.\n\n"
"This control only works if a server connection exists, and its control "
"object is a Vehicle derived class. If either of these requirements is false, "
"the control is not rendered.<br>"
"The control renders the speedometer needle as a colored quad, rotated to "
"indicate the Vehicle speed as determined by the <i>minAngle</i>, "
"<i>maxAngle</i>, and <i>maxSpeed</i> properties. This control is normally "
"placed on top of a GuiBitmapCtrl representing the speedometer dial.\n\n"
"@tsexample\n"
"new GuiSpeedometerHud()\n"
"{\n"
" maxSpeed = \"100\";\n"
" minAngle = \"215\";\n"
" maxAngle = \"0\";\n"
" color = \"1 0.3 0.3 1\";\n"
" center = \"130 123\";\n"
" length = \"100\";\n"
" width = \"2\";\n"
" tail = \"0\";\n"
" //Properties not specific to this control have been omitted from this example.\n"
"};\n"
"@endtsexample\n\n"
"@ingroup GuiContainers"
);
GuiSpeedometerHud::GuiSpeedometerHud()
{
mSpeed = 0;
mMaxSpeed = 100;
mMaxAngle = 0;
mMinAngle = 200;
mCenter.set(0,0);
mNeedleWidth = 3;
mNeedleLength = 10;
mTailLength = 5;
mNeedleColor.set(1,0,0,1);
}
void GuiSpeedometerHud::initPersistFields()
{
addGroup("Needle");
addField("maxSpeed", TypeF32, Offset( mMaxSpeed, GuiSpeedometerHud ),
"Maximum Vehicle speed (in Torque units per second) to represent on the "
"speedo (Vehicle speeds greater than this are clamped to maxSpeed)." );
addField("minAngle", TypeF32, Offset( mMinAngle, GuiSpeedometerHud ),
"Angle (in radians) of the needle when the Vehicle speed is 0. An angle "
"of 0 points right, 90 points up etc)." );
addField("maxAngle", TypeF32, Offset( mMaxAngle, GuiSpeedometerHud ),
"Angle (in radians) of the needle when the Vehicle speed is >= maxSpeed. "
"An angle of 0 points right, 90 points up etc)." );
addField("color", TypeColorF, Offset( mNeedleColor, GuiSpeedometerHud ),
"Color of the needle" );
addField("center", TypePoint2F, Offset( mCenter, GuiSpeedometerHud ),
"Center of the needle, offset from the GuiSpeedometerHud control top "
"left corner" );
addField("length", TypeF32, Offset( mNeedleLength, GuiSpeedometerHud ),
"Length of the needle from center to end" );
addField("width", TypeF32, Offset( mNeedleWidth, GuiSpeedometerHud ),
"Width of the needle" );
addField("tail", TypeF32, Offset( mTailLength, GuiSpeedometerHud ),
"Length of the needle from center to tail" );
endGroup("Needle");
Parent::initPersistFields();
}
//-----------------------------------------------------------------------------
/**
Gui onRender method.
Renders an analog speedometer needle over a specified bitmap background.
*/
void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
{
// Must have a connection
GameConnection* conn = GameConnection::getConnectionToServer();
if (!conn)
return;
// Requires either a vehicle control object or a vehicle-mounted player
Vehicle* vehicle = dynamic_cast<Vehicle*>(conn->getControlObject());
if(!vehicle){
Player * player = dynamic_cast<Player*>(conn->getControlObject());
if(!player) return;
if (!player->isMounted()) return;
vehicle = dynamic_cast<Vehicle*>(player->getObjectMount());
if(!vehicle) return;
}
Parent::onRender(offset,updateRect);
// Use the vehicle's velocity as its speed...
mSpeed = vehicle->getVelocity().len();
if (mSpeed > mMaxSpeed)
mSpeed = mMaxSpeed;
// Calculate center point if necessary and roll in offsets
Point2F center = mCenter;
if (mIsZero(center.x) && mIsZero(center.y))
{
center.x = getExtent().x / 2.0f;
center.y = getExtent().y / 2.0f;
}
F32 fillOffset = GFX->getFillConventionOffset(); // Find the fill offset
Point2F viewCenter(offset.x + fillOffset + center.x, offset.y + fillOffset + center.y);
// Handle rotation calculations
F32 rotation, spinAngle;
rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
spinAngle = mDegToRad(rotation);
MatrixF rotMatrix(EulerF(0.0, 0.0, spinAngle));
// Set up the needle vertex list
Point3F vertList[5];
vertList[0].set(+mNeedleLength,-mNeedleWidth,0);
vertList[1].set(+mNeedleLength,+mNeedleWidth,0);
vertList[2].set(-mTailLength ,+mNeedleWidth,0);
vertList[3].set(-mTailLength ,-mNeedleWidth,0);
vertList[4].set(+mNeedleLength,-mNeedleWidth,0); //// Get back to the start!
// Create a GFXStateBlock description if one has not been set.
if (mBlendSB.isNull())
{
GFXStateBlockDesc desc;
desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
desc.samplersDefined = true;
desc.samplers[0].textureColorOp = GFXTOPDisable;
mBlendSB = GFX->createStateBlock(desc);
}
GFX->setStateBlock(mBlendSB);
GFX->setTexture(0, NULL);
// Render the needle
PrimBuild::color4f(mNeedleColor.red, mNeedleColor.green, mNeedleColor.blue, mNeedleColor.alpha);
PrimBuild::begin(GFXLineStrip, 5);
for(int k=0; k<5; k++){
rotMatrix.mulP(vertList[k]);
PrimBuild::vertex2f(vertList[k].x + viewCenter.x, vertList[k].y + viewCenter.y);
}
PrimBuild::end();
}