guiCrossHairHud.cpp
Engine/source/T3D/fps/guiCrossHairHud.cpp
Classes:
class
Vary basic cross hair hud.
Public Variables
Valid object types for which the cross hair will render, this should really all be script controlled.
Public Functions
ConsoleDocClass(GuiCrossHairHud , "@brief Basic cross hair hud. Reacts <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> state of <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object. Also displays health bar <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> named objects under the cross <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">hair.\n\n</a>" "Uses the base bitmap <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap, and decides whether <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> draw or not depending " "on the current <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object and it 's state. If there is <a href="/coding/class/classshapebase/">ShapeBase</a> object under the cross hair " "and it 's named, then <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> small health bar is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">displayed.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a> <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguicrosshairhud/">GuiCrossHairHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " damageFillColor=\"1.0 0.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid red <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" " damageFrameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" " damageRect = \"15 5\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " damageOffset = \"0 -10\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiGame\n</a>" )
Detailed Description
Public Variables
const U32 ObjectMask
Valid object types for which the cross hair will render, this should really all be script controlled.
Public Functions
ConsoleDocClass(GuiCrossHairHud , "@brief Basic cross hair hud. Reacts <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> state of <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object. Also displays health bar <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> named objects under the cross <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">hair.\n\n</a>" "Uses the base bitmap <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> bitmap, and decides whether <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> draw or not depending " "on the current <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object and it 's state. If there is <a href="/coding/class/classshapebase/">ShapeBase</a> object under the cross hair " "and it 's named, then <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> small health bar is <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">displayed.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a> <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguicrosshairhud/">GuiCrossHairHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " damageFillColor=\"1.0 0.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid red <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" " damageFrameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" " damageRect = \"15 5\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " damageOffset = \"0 -10\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiGame\n</a>" )
IMPLEMENT_CONOBJECT(GuiCrossHairHud )
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 "platform/platform.h" 25 26#include "gui/core/guiControl.h" 27#include "gui/controls/guiBitmapCtrl.h" 28#include "console/consoleTypes.h" 29#include "scene/sceneManager.h" 30#include "T3D/gameBase/gameConnection.h" 31#include "T3D/shapeBase.h" 32#include "gfx/gfxDrawUtil.h" 33#include "console/engineAPI.h" 34 35 36//----------------------------------------------------------------------------- 37/// Vary basic cross hair hud. 38/// Uses the base bitmap control to render a bitmap, and decides whether 39/// to draw or not depending on the current control object and it's state. 40/// If there is ShapeBase object under the cross hair and it's named, 41/// then a small health bar is displayed. 42class GuiCrossHairHud : public GuiBitmapCtrl 43{ 44 typedef GuiBitmapCtrl Parent; 45 46 LinearColorF mDamageFillColor; 47 LinearColorF mDamageFrameColor; 48 Point2I mDamageRectSize; 49 Point2I mDamageOffset; 50 51protected: 52 void drawDamage(Point2I offset, F32 damage, F32 opacity); 53 54public: 55 GuiCrossHairHud(); 56 57 void onRender( Point2I, const RectI &); 58 static void initPersistFields(); 59 DECLARE_CONOBJECT( GuiCrossHairHud ); 60 DECLARE_CATEGORY( "Gui Game" ); 61 DECLARE_DESCRIPTION( "Basic cross hair hud. Reacts to state of control object.\n" 62 "Also displays health bar for named objects under the cross hair." ); 63}; 64 65/// Valid object types for which the cross hair will render, this 66/// should really all be script controlled. 67static const U32 ObjectMask = PlayerObjectType | VehicleObjectType; 68 69 70//----------------------------------------------------------------------------- 71 72IMPLEMENT_CONOBJECT( GuiCrossHairHud ); 73 74ConsoleDocClass( GuiCrossHairHud, 75 "@brief Basic cross hair hud. Reacts to state of control object. Also displays health bar for named objects under the cross hair.\n\n" 76 "Uses the base bitmap control to render a bitmap, and decides whether to draw or not depending " 77 "on the current control object and it's state. If there is ShapeBase object under the cross hair " 78 "and it's named, then a small health bar is displayed.\n\n" 79 80 "@tsexample\n" 81 "\n new GuiCrossHairHud()" 82 "{\n" 83 " damageFillColor = \"1.0 0.0 0.0 1.0\"; // Fills with a solid red color\n" 84 " damageFrameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n" 85 " damageRect = \"15 5\";\n" 86 " damageOffset = \"0 -10\";\n" 87 "};\n" 88 "@endtsexample\n" 89 90 "@ingroup GuiGame\n" 91); 92 93GuiCrossHairHud::GuiCrossHairHud() 94{ 95 mDamageFillColor.set( 0.0f, 1.0f, 0.0f, 1.0f ); 96 mDamageFrameColor.set( 1.0f, 0.6f, 0.0f, 1.0f ); 97 mDamageRectSize.set(50, 4); 98 mDamageOffset.set(0,32); 99} 100 101void GuiCrossHairHud::initPersistFields() 102{ 103 addGroup("Damage"); 104 addField( "damageFillColor", TypeColorF, Offset( mDamageFillColor, GuiCrossHairHud ), "As the health bar depletes, this color will represent the health loss amount." ); 105 addField( "damageFrameColor", TypeColorF, Offset( mDamageFrameColor, GuiCrossHairHud ), "Color for the health bar's frame." ); 106 addField( "damageRect", TypePoint2I, Offset( mDamageRectSize, GuiCrossHairHud ), "Size for the health bar portion of the control." ); 107 addField( "damageOffset", TypePoint2I, Offset( mDamageOffset, GuiCrossHairHud ), "Offset for drawing the damage portion of the health control." ); 108 endGroup("Damage"); 109 Parent::initPersistFields(); 110} 111 112 113//----------------------------------------------------------------------------- 114 115void GuiCrossHairHud::onRender(Point2I offset, const RectI &updateRect) 116{ 117 // Must have a connection and player control object 118 GameConnection* conn = GameConnection::getConnectionToServer(); 119 if (!conn) 120 return; 121 GameBase* control = dynamic_cast<GameBase*>(conn->getCameraObject()); 122 if (!control || !(control->getTypeMask() & ObjectMask) || !conn->isFirstPerson()) 123 return; 124 125 // Parent render. 126 Parent::onRender(offset,updateRect); 127 128 // Get control camera info 129 MatrixF cam; 130 Point3F camPos; 131 conn->getControlCameraTransform(0,&cam); 132 cam.getColumn(3, &camPos); 133 134 // Extend the camera vector to create an endpoint for our ray 135 Point3F endPos; 136 cam.getColumn(1, &endPos); 137 endPos *= gClientSceneGraph->getVisibleDistance(); 138 endPos += camPos; 139 140 // Collision info. We're going to be running LOS tests and we 141 // don't want to collide with the control object. 142 static U32 losMask = TerrainObjectType | ShapeBaseObjectType; 143 control->disableCollision(); 144 145 RayInfo info; 146 if (gClientContainer.castRay(camPos, endPos, losMask, &info)) { 147 // Hit something... but we'll only display health for named 148 // ShapeBase objects. Could mask against the object type here 149 // and do a static cast if it's a ShapeBaseObjectType, but this 150 // isn't a performance situation, so I'll just use dynamic_cast. 151 if (ShapeBase* obj = dynamic_cast<ShapeBase*>(info.object)) 152 if (obj->getShapeName()) { 153 offset.x = updateRect.point.x + updateRect.extent.x / 2; 154 offset.y = updateRect.point.y + updateRect.extent.y / 2; 155 drawDamage(offset + mDamageOffset, obj->getDamageValue(), 1); 156 } 157 } 158 159 // Restore control object collision 160 control->enableCollision(); 161} 162 163 164//----------------------------------------------------------------------------- 165/** 166 Display a damage bar ubove the shape. 167 This is a support funtion, called by onRender. 168*/ 169void GuiCrossHairHud::drawDamage(Point2I offset, F32 damage, F32 opacity) 170{ 171 mDamageFillColor.alpha = mDamageFrameColor.alpha = opacity; 172 173 // Damage should be 0->1 (0 being no damage,or healthy), but 174 // we'll just make sure here as we flip it. 175 damage = mClampF(1 - damage, 0, 1); 176 177 // Center the bar 178 RectI rect(offset, mDamageRectSize); 179 rect.point.x -= mDamageRectSize.x / 2; 180 181 // Draw the border 182 GFX->getDrawUtil()->drawRect(rect, mDamageFrameColor.toColorI()); 183 184 // Draw the damage % fill 185 rect.point += Point2I(1, 1); 186 rect.extent -= Point2I(1, 1); 187 rect.extent.x = (S32)(rect.extent.x * damage); 188 if (rect.extent.x == 1) 189 rect.extent.x = 2; 190 if (rect.extent.x > 0) 191 GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor.toColorI()); 192} 193