guiShapeNameHud.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/core/guiControl.h"
  24. #include "gui/3d/guiTSControl.h"
  25. #include "console/consoleTypes.h"
  26. #include "scene/sceneManager.h"
  27. #include "T3D/gameBase/gameConnection.h"
  28. #include "T3D/shapeBase.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "console/engineAPI.h"
  31. #include "T3D/missionMarker.h" //> ZOD: Team coloring
  32. //----------------------------------------------------------------------------
  33. /// Displays name & damage above shape objects.
  34. ///
  35. /// This control displays the name and damage value of all named
  36. /// ShapeBase objects on the client. The name and damage of objects
  37. /// within the control's display area are overlayed above the object.
  38. ///
  39. /// This GUI control must be a child of a TSControl, and a server connection
  40. /// and control object must be present.
  41. ///
  42. /// This is a stand-alone control and relies only on the standard base GuiControl.
  43. class GuiShapeNameHud : public GuiControl {
  44. typedef GuiControl Parent;
  45. // field data
  46. ColorF mFillColor;
  47. ColorF mFrameColor;
  48. ColorF mTextColor;
  49. ColorF mLabelFillColor;
  50. ColorF mLabelFrameColor;
  51. // ZOD: Team coloring
  52. ColorF mEnemyTextColor;
  53. ColorF mNeutralTextColor;
  54. //> ZOD: End addition
  55. F32 mVerticalOffset;
  56. F32 mDistanceFade;
  57. bool mShowFrame;
  58. bool mShowFill;
  59. bool mShowLabelFrame;
  60. bool mShowLabelFill;
  61. Point2I mLabelPadding;
  62. protected:
  63. // ZOD: Team coloring
  64. //void drawName( Point2I offset, const char *buf, F32 opacity);
  65. void drawName( Point2I offset, const char *buf, F32 opacity, ColorF color);
  66. //> ZOD: End edit
  67. public:
  68. GuiShapeNameHud();
  69. // GuiControl
  70. virtual void onRender(Point2I offset, const RectI &updateRect);
  71. static void initPersistFields();
  72. DECLARE_CONOBJECT( GuiShapeNameHud );
  73. DECLARE_CATEGORY( "Gui Game" );
  74. DECLARE_DESCRIPTION( "Displays name and damage of ShapeBase objects in its bounds.\n"
  75. "Must be a child of a GuiTSCtrl and a server connection must be present." );
  76. };
  77. //-----------------------------------------------------------------------------
  78. IMPLEMENT_CONOBJECT(GuiShapeNameHud);
  79. ConsoleDocClass( GuiShapeNameHud,
  80. "@brief Displays name and damage of ShapeBase objects in its bounds. Must be a child of a GuiTSCtrl and a server connection must be present.\n\n"
  81. "This control displays the name and damage value of all named ShapeBase objects on the client. "
  82. "The name and damage of objects within the control's display area are overlayed above the object.\n\n"
  83. "This GUI control must be a child of a TSControl, and a server connection and control object must be present. "
  84. "This is a stand-alone control and relies only on the standard base GuiControl.\n\n"
  85. "@tsexample\n"
  86. "\n new GuiShapeNameHud()"
  87. "{\n"
  88. " fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  89. " frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  90. " textColor = \"1.0 1.0 1.0 1.0\"; // Solid white text Color\n"
  91. " showFill = \"true\";\n"
  92. " showFrame = \"true\";\n"
  93. " labelFillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  94. " labelFrameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  95. " showLabelFill = \"true\";\n"
  96. " showLabelFrame = \"true\";\n"
  97. " verticalOffset = \"0.15\";\n"
  98. " distanceFade = \"15.0\";\n"
  99. "};\n"
  100. "@endtsexample\n\n"
  101. "@ingroup GuiGame\n"
  102. );
  103. GuiShapeNameHud::GuiShapeNameHud()
  104. {
  105. mFillColor.set( 0.25f, 0.25f, 0.25f, 0.25f );
  106. mFrameColor.set( 0, 1, 0, 1 );
  107. mLabelFillColor.set( 0.25f, 0.25f, 0.25f, 0.25f );
  108. mLabelFrameColor.set( 0, 1, 0, 1 );
  109. mTextColor.set( 0, 1, 0, 1 );
  110. //> ZOD: Team coloring
  111. mEnemyTextColor.set( 1, 0, 0, 1 );
  112. mNeutralTextColor.set( 1, 1, 1, 1 );
  113. //> ZOD: End addition
  114. mShowFrame = mShowFill = true;
  115. mShowLabelFrame = mShowLabelFill = false;
  116. mVerticalOffset = 0.5f;
  117. mDistanceFade = 0.1f;
  118. mLabelPadding.set(0, 0);
  119. }
  120. void GuiShapeNameHud::initPersistFields()
  121. {
  122. addGroup("Colors");
  123. addField( "fillColor", TypeColorF, Offset( mFillColor, GuiShapeNameHud ), "Standard color for the background of the control." );
  124. addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiShapeNameHud ), "Color for the control's frame." );
  125. addField( "textColor", TypeColorF, Offset( mTextColor, GuiShapeNameHud ), "Color for the text on this control." );
  126. addField( "labelFillColor", TypeColorF, Offset( mLabelFillColor, GuiShapeNameHud ), "Color for the background of each shape name label." );
  127. addField( "labelFrameColor", TypeColorF, Offset( mLabelFrameColor, GuiShapeNameHud ), "Color for the frames around each shape name label." );
  128. // ZOD: Team coloring
  129. addField( "enemyTextColor", TypeColorF, Offset( mEnemyTextColor, GuiShapeNameHud ), "Color for enemy shapes." );
  130. //> ZOD: End addition
  131. addField( "neutralTextColor", TypeColorF, Offset( mNeutralTextColor, GuiShapeNameHud ), "Color for neutral shapes." );
  132. endGroup("Colors");
  133. addGroup("Misc");
  134. addField( "showFill", TypeBool, Offset( mShowFill, GuiShapeNameHud ), "If true, we draw the background color of the control." );
  135. addField( "showFrame", TypeBool, Offset( mShowFrame, GuiShapeNameHud ), "If true, we draw the frame of the control." );
  136. addField( "showLabelFill", TypeBool, Offset( mShowLabelFill, GuiShapeNameHud ), "If true, we draw a background for each shape name label." );
  137. addField( "showLabelFrame", TypeBool, Offset( mShowLabelFrame, GuiShapeNameHud ), "If true, we draw a frame around each shape name label." );
  138. addField( "labelPadding", TypePoint2I, Offset( mLabelPadding, GuiShapeNameHud ), "The padding (in pixels) between the label text and the frame." );
  139. addField( "verticalOffset", TypeF32, Offset( mVerticalOffset, GuiShapeNameHud ), "Amount to vertically offset the control in relation to the ShapeBase object in focus." );
  140. addField( "distanceFade", TypeF32, Offset( mDistanceFade, GuiShapeNameHud ), "Visibility distance (how far the player must be from the ShapeBase object in focus) for this control to render." );
  141. endGroup("Misc");
  142. Parent::initPersistFields();
  143. }
  144. //----------------------------------------------------------------------------
  145. /// Core rendering method for this control.
  146. ///
  147. /// This method scans through all the current client ShapeBase objects.
  148. /// If one is named, it displays the name and damage information for it.
  149. ///
  150. /// Information is offset from the center of the object's bounding box,
  151. /// unless the object is a PlayerObjectType, in which case the eye point
  152. /// is used.
  153. ///
  154. /// @param updateRect Extents of control.
  155. void GuiShapeNameHud::onRender( Point2I, const RectI &updateRect)
  156. {
  157. // Background fill first
  158. if (mShowFill)
  159. GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
  160. // Must be in a TS Control
  161. GuiTSCtrl *parent = dynamic_cast<GuiTSCtrl*>(getParent());
  162. if (!parent) return;
  163. // Must have a connection and control object
  164. GameConnection* conn = GameConnection::getConnectionToServer();
  165. if (!conn) return;
  166. //> ZOD: Team coloring
  167. //GameBase * control = dynamic_cast<GameBase*>(conn->getControlObject());
  168. ShapeBase * control = dynamic_cast<ShapeBase*>(conn->getControlObject());
  169. //< ZOD: End edit
  170. if (!control) return;
  171. // Get control camera info
  172. MatrixF cam;
  173. Point3F camPos;
  174. VectorF camDir;
  175. conn->getControlCameraTransform(0,&cam);
  176. cam.getColumn(3, &camPos);
  177. cam.getColumn(1, &camDir);
  178. F32 camFovCos;
  179. conn->getControlCameraFov(&camFovCos);
  180. camFovCos = mCos(mDegToRad(camFovCos) / 2);
  181. // Visible distance info & name fading
  182. F32 visDistance = gClientSceneGraph->getVisibleDistance();
  183. F32 visDistanceSqr = visDistance * visDistance;
  184. F32 fadeDistance = visDistance * mDistanceFade;
  185. // Collision info. We're going to be running LOS tests and we
  186. // don't want to collide with the control object.
  187. static U32 losMask = TerrainObjectType | ShapeBaseObjectType | StaticObjectType;
  188. control->disableCollision();
  189. //> ZOD: Team coloring
  190. ColorF renderColor;
  191. const char *fof = NULL;
  192. char buf[64];
  193. //< ZOD: End addition
  194. // All ghosted objects are added to the server connection group,
  195. // so we can find all the shape base objects by iterating through
  196. // our current connection.
  197. for (SimSetIterator itr(conn); *itr; ++itr) {
  198. ShapeBase* shape = dynamic_cast< ShapeBase* >(*itr);
  199. if ( shape ) {
  200. if (shape != control && shape->getShapeName())
  201. {
  202. //> ZOD: If cloaked, early out
  203. if ( shape->getCloakedState() )
  204. continue;
  205. // Target pos to test, if it's a player run the LOS to his eye
  206. // point, otherwise we'll grab the generic box center.
  207. Point3F shapePos;
  208. if (shape->getTypeMask() & PlayerObjectType)
  209. {
  210. MatrixF eye;
  211. // Use the render eye transform, otherwise we'll see jittering
  212. shape->getRenderEyeTransform(&eye);
  213. eye.getColumn(3, &shapePos);
  214. }
  215. else
  216. {
  217. // Use the render transform instead of the box center
  218. // otherwise it'll jitter.
  219. MatrixF srtMat = shape->getRenderTransform();
  220. srtMat.getColumn(3, &shapePos);
  221. }
  222. VectorF shapeDir = shapePos - camPos;
  223. // Test to see if it's in range
  224. F32 shapeDist = shapeDir.lenSquared();
  225. if (shapeDist == 0 || shapeDist > visDistanceSqr)
  226. continue;
  227. shapeDist = mSqrt(shapeDist);
  228. // Test to see if it's within our viewcone, this test doesn't
  229. // actually match the viewport very well, should consider
  230. // projection and box test.
  231. shapeDir.normalize();
  232. F32 dot = mDot(shapeDir, camDir);
  233. if (dot < camFovCos)
  234. continue;
  235. // Test to see if it's behind something, and we want to
  236. // ignore anything it's mounted on when we run the LOS.
  237. RayInfo info;
  238. shape->disableCollision();
  239. SceneObject *mount = shape->getObjectMount();
  240. if (mount)
  241. mount->disableCollision();
  242. bool los = !gClientContainer.castRay(camPos, shapePos,losMask, &info);
  243. shape->enableCollision();
  244. if (mount)
  245. mount->enableCollision();
  246. if (!los)
  247. continue;
  248. // Project the shape pos into screen space and calculate
  249. // the distance opacity used to fade the labels into the
  250. // distance.
  251. Point3F projPnt;
  252. shapePos.z += mVerticalOffset;
  253. if (!parent->project(shapePos, &projPnt))
  254. continue;
  255. F32 opacity = (shapeDist < fadeDistance)? 1.0:
  256. 1.0 - (shapeDist - fadeDistance) / (visDistance - fadeDistance);
  257. //> ZOD: Team coloring
  258. S32 myId = control->getTeamId();
  259. S32 targetId = shape->getTeamId();
  260. if(targetId == 0)
  261. {
  262. fof = "N";
  263. renderColor = mNeutralTextColor;
  264. }
  265. else if(myId != targetId)
  266. {
  267. fof = "E";
  268. renderColor = mEnemyTextColor;
  269. }
  270. else
  271. {
  272. fof = "F";
  273. renderColor = mTextColor;
  274. }
  275. // Append the distance from the shape to the name
  276. dSprintf(buf,sizeof(buf), "%s : %gm", shape->getShapeName(), mFloor(shapeDist));
  277. drawName(Point2I((S32)projPnt.x, (S32)projPnt.y), buf, opacity, renderColor);
  278. //< ZOD: End addition
  279. // Render the shape's name
  280. //drawName(Point2I((S32)projPnt.x, (S32)projPnt.y),shape->getShapeName(),opacity);
  281. }
  282. }
  283. }
  284. // Restore control object collision
  285. control->enableCollision();
  286. // ZOD: Waypoints
  287. SimSet *WayPointSet = Sim::getWayPointSet();
  288. SimSet::iterator i;
  289. for(i = WayPointSet->begin(); i != WayPointSet->end(); i++)
  290. {
  291. WayPoint *way = (WayPoint *) (*i);
  292. S32 myId = control->getTeamId();
  293. S32 wayId = way->getTeamId();
  294. Point3F wayPos;
  295. MatrixF srtMat = way->getTransform();
  296. srtMat.getColumn(3, &wayPos);
  297. VectorF wayDir = wayPos - control->getPosition();//camPos;
  298. F32 wayDist = wayDir.lenSquared();
  299. if (wayDist == 0)
  300. continue;
  301. wayDist = mSqrt(wayDist);
  302. Point3F projPnt;
  303. wayPos.z += mVerticalOffset;
  304. if (!parent->project(wayPos, &projPnt))
  305. continue;
  306. fof = way->mName;
  307. if(wayId == 0)
  308. renderColor = mNeutralTextColor;
  309. else if(myId != wayId)
  310. renderColor = mEnemyTextColor;
  311. else
  312. renderColor = mTextColor;
  313. dSprintf(buf,sizeof(buf), "%s : %gm", fof, mFloor(wayDist));
  314. drawName(Point2I((S32)projPnt.x, (S32)projPnt.y), buf, renderColor.alpha, renderColor);
  315. }
  316. // ZOD: End addition
  317. // Border last
  318. if (mShowFrame)
  319. GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
  320. }
  321. //----------------------------------------------------------------------------
  322. /// Render object names.
  323. ///
  324. /// Helper function for GuiShapeNameHud::onRender
  325. ///
  326. /// @param offset Screen coordinates to render name label. (Text is centered
  327. /// horizontally about this location, with bottom of text at
  328. /// specified y position.)
  329. /// @param name String name to display.
  330. /// @param opacity Opacity of name (a fraction).
  331. void GuiShapeNameHud::drawName(Point2I offset, const char *name, F32 opacity, ColorF mTextColor)
  332. {
  333. F32 width = mProfile->mFont->getStrWidth((const UTF8 *)name) + mLabelPadding.x * 2;
  334. F32 height = mProfile->mFont->getHeight() + mLabelPadding.y * 2;
  335. Point2I extent = Point2I(width, height);
  336. // Center the name
  337. offset.x -= width / 2;
  338. offset.y -= height / 2;
  339. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  340. // Background fill first
  341. if (mShowLabelFill)
  342. drawUtil->drawRectFill(RectI(offset, extent), mLabelFillColor);
  343. // Deal with opacity and draw.
  344. mTextColor.alpha = opacity;
  345. drawUtil->setBitmapModulation(mTextColor);
  346. drawUtil->drawText(mProfile->mFont, offset + mLabelPadding, name);
  347. drawUtil->clearBitmapModulation();
  348. // Border last
  349. if (mShowLabelFrame)
  350. drawUtil->drawRect(RectI(offset, extent), mLabelFrameColor);
  351. }