guiShapeNameHud.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. //----------------------------------------------------------------------------
  32. /// Displays name & damage above shape objects.
  33. ///
  34. /// This control displays the name and damage value of all named
  35. /// ShapeBase objects on the client. The name and damage of objects
  36. /// within the control's display area are overlayed above the object.
  37. ///
  38. /// This GUI control must be a child of a TSControl, and a server connection
  39. /// and control object must be present.
  40. ///
  41. /// This is a stand-alone control and relies only on the standard base GuiControl.
  42. class GuiShapeNameHud : public GuiControl {
  43. typedef GuiControl Parent;
  44. // field data
  45. ColorF mFillColor;
  46. ColorF mFrameColor;
  47. ColorF mTextColor;
  48. ColorF mLabelFillColor;
  49. ColorF mLabelFrameColor;
  50. F32 mVerticalOffset;
  51. F32 mDistanceFade;
  52. bool mShowFrame;
  53. bool mShowFill;
  54. bool mShowLabelFrame;
  55. bool mShowLabelFill;
  56. Point2I mLabelPadding;
  57. protected:
  58. void drawName( Point2I offset, const char *buf, F32 opacity);
  59. public:
  60. GuiShapeNameHud();
  61. // GuiControl
  62. virtual void onRender(Point2I offset, const RectI &updateRect);
  63. static void initPersistFields();
  64. DECLARE_CONOBJECT( GuiShapeNameHud );
  65. DECLARE_CATEGORY( "Gui Game" );
  66. DECLARE_DESCRIPTION( "Displays name and damage of ShapeBase objects in its bounds.\n"
  67. "Must be a child of a GuiTSCtrl and a server connection must be present." );
  68. };
  69. //-----------------------------------------------------------------------------
  70. IMPLEMENT_CONOBJECT(GuiShapeNameHud);
  71. ConsoleDocClass( GuiShapeNameHud,
  72. "@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"
  73. "This control displays the name and damage value of all named ShapeBase objects on the client. "
  74. "The name and damage of objects within the control's display area are overlayed above the object.\n\n"
  75. "This GUI control must be a child of a TSControl, and a server connection and control object must be present. "
  76. "This is a stand-alone control and relies only on the standard base GuiControl.\n\n"
  77. "@tsexample\n"
  78. "\n new GuiShapeNameHud()"
  79. "{\n"
  80. " fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  81. " frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  82. " textColor = \"1.0 1.0 1.0 1.0\"; // Solid white text Color\n"
  83. " showFill = \"true\";\n"
  84. " showFrame = \"true\";\n"
  85. " labelFillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
  86. " labelFrameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
  87. " showLabelFill = \"true\";\n"
  88. " showLabelFrame = \"true\";\n"
  89. " verticalOffset = \"0.15\";\n"
  90. " distanceFade = \"15.0\";\n"
  91. "};\n"
  92. "@endtsexample\n\n"
  93. "@ingroup GuiGame\n"
  94. );
  95. /// Default distance for object's information to be displayed.
  96. static const F32 cDefaultVisibleDistance = 500.0f;
  97. GuiShapeNameHud::GuiShapeNameHud()
  98. {
  99. mFillColor.set( 0.25f, 0.25f, 0.25f, 0.25f );
  100. mFrameColor.set( 0, 1, 0, 1 );
  101. mLabelFillColor.set( 0.25f, 0.25f, 0.25f, 0.25f );
  102. mLabelFrameColor.set( 0, 1, 0, 1 );
  103. mTextColor.set( 0, 1, 0, 1 );
  104. mShowFrame = mShowFill = true;
  105. mShowLabelFrame = mShowLabelFill = false;
  106. mVerticalOffset = 0.5f;
  107. mDistanceFade = 0.1f;
  108. mLabelPadding.set(0, 0);
  109. }
  110. void GuiShapeNameHud::initPersistFields()
  111. {
  112. addGroup("Colors");
  113. addField( "fillColor", TypeColorF, Offset( mFillColor, GuiShapeNameHud ), "Standard color for the background of the control." );
  114. addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiShapeNameHud ), "Color for the control's frame." );
  115. addField( "textColor", TypeColorF, Offset( mTextColor, GuiShapeNameHud ), "Color for the text on this control." );
  116. addField( "labelFillColor", TypeColorF, Offset( mLabelFillColor, GuiShapeNameHud ), "Color for the background of each shape name label." );
  117. addField( "labelFrameColor", TypeColorF, Offset( mLabelFrameColor, GuiShapeNameHud ), "Color for the frames around each shape name label." );
  118. endGroup("Colors");
  119. addGroup("Misc");
  120. addField( "showFill", TypeBool, Offset( mShowFill, GuiShapeNameHud ), "If true, we draw the background color of the control." );
  121. addField( "showFrame", TypeBool, Offset( mShowFrame, GuiShapeNameHud ), "If true, we draw the frame of the control." );
  122. addField( "showLabelFill", TypeBool, Offset( mShowLabelFill, GuiShapeNameHud ), "If true, we draw a background for each shape name label." );
  123. addField( "showLabelFrame", TypeBool, Offset( mShowLabelFrame, GuiShapeNameHud ), "If true, we draw a frame around each shape name label." );
  124. addField( "labelPadding", TypePoint2I, Offset( mLabelPadding, GuiShapeNameHud ), "The padding (in pixels) between the label text and the frame." );
  125. addField( "verticalOffset", TypeF32, Offset( mVerticalOffset, GuiShapeNameHud ), "Amount to vertically offset the control in relation to the ShapeBase object in focus." );
  126. 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." );
  127. endGroup("Misc");
  128. Parent::initPersistFields();
  129. }
  130. //----------------------------------------------------------------------------
  131. /// Core rendering method for this control.
  132. ///
  133. /// This method scans through all the current client ShapeBase objects.
  134. /// If one is named, it displays the name and damage information for it.
  135. ///
  136. /// Information is offset from the center of the object's bounding box,
  137. /// unless the object is a PlayerObjectType, in which case the eye point
  138. /// is used.
  139. ///
  140. /// @param updateRect Extents of control.
  141. void GuiShapeNameHud::onRender( Point2I, const RectI &updateRect)
  142. {
  143. // Background fill first
  144. if (mShowFill)
  145. GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
  146. // Must be in a TS Control
  147. GuiTSCtrl *parent = dynamic_cast<GuiTSCtrl*>(getParent());
  148. if (!parent) return;
  149. // Must have a connection and control object
  150. GameConnection* conn = GameConnection::getConnectionToServer();
  151. if (!conn) return;
  152. GameBase * control = dynamic_cast<GameBase*>(conn->getControlObject());
  153. if (!control) return;
  154. // Get control camera info
  155. MatrixF cam;
  156. Point3F camPos;
  157. VectorF camDir;
  158. conn->getControlCameraTransform(0,&cam);
  159. cam.getColumn(3, &camPos);
  160. cam.getColumn(1, &camDir);
  161. F32 camFovCos;
  162. conn->getControlCameraFov(&camFovCos);
  163. camFovCos = mCos(mDegToRad(camFovCos) / 2);
  164. // Visible distance info & name fading
  165. F32 visDistance = gClientSceneGraph->getVisibleDistance();
  166. F32 visDistanceSqr = visDistance * visDistance;
  167. F32 fadeDistance = visDistance * mDistanceFade;
  168. // Collision info. We're going to be running LOS tests and we
  169. // don't want to collide with the control object.
  170. static U32 losMask = TerrainObjectType | ShapeBaseObjectType | StaticObjectType;
  171. control->disableCollision();
  172. // All ghosted objects are added to the server connection group,
  173. // so we can find all the shape base objects by iterating through
  174. // our current connection.
  175. for (SimSetIterator itr(conn); *itr; ++itr) {
  176. ShapeBase* shape = dynamic_cast< ShapeBase* >(*itr);
  177. if ( shape ) {
  178. if (shape != control && shape->getShapeName())
  179. {
  180. // Target pos to test, if it's a player run the LOS to his eye
  181. // point, otherwise we'll grab the generic box center.
  182. Point3F shapePos;
  183. if (shape->getTypeMask() & PlayerObjectType)
  184. {
  185. MatrixF eye;
  186. // Use the render eye transform, otherwise we'll see jittering
  187. shape->getRenderEyeTransform(&eye);
  188. eye.getColumn(3, &shapePos);
  189. }
  190. else
  191. {
  192. // Use the render transform instead of the box center
  193. // otherwise it'll jitter.
  194. MatrixF srtMat = shape->getRenderTransform();
  195. srtMat.getColumn(3, &shapePos);
  196. }
  197. VectorF shapeDir = shapePos - camPos;
  198. // Test to see if it's in range
  199. F32 shapeDist = shapeDir.lenSquared();
  200. if (shapeDist == 0 || shapeDist > visDistanceSqr)
  201. continue;
  202. shapeDist = mSqrt(shapeDist);
  203. // Test to see if it's within our viewcone, this test doesn't
  204. // actually match the viewport very well, should consider
  205. // projection and box test.
  206. shapeDir.normalize();
  207. F32 dot = mDot(shapeDir, camDir);
  208. if (dot < camFovCos)
  209. continue;
  210. // Test to see if it's behind something, and we want to
  211. // ignore anything it's mounted on when we run the LOS.
  212. RayInfo info;
  213. shape->disableCollision();
  214. SceneObject *mount = shape->getObjectMount();
  215. if (mount)
  216. mount->disableCollision();
  217. bool los = !gClientContainer.castRay(camPos, shapePos,losMask, &info);
  218. shape->enableCollision();
  219. if (mount)
  220. mount->enableCollision();
  221. if (!los)
  222. continue;
  223. // Project the shape pos into screen space and calculate
  224. // the distance opacity used to fade the labels into the
  225. // distance.
  226. Point3F projPnt;
  227. shapePos.z += mVerticalOffset;
  228. if (!parent->project(shapePos, &projPnt))
  229. continue;
  230. F32 opacity = (shapeDist < fadeDistance)? 1.0:
  231. 1.0 - (shapeDist - fadeDistance) / (visDistance - fadeDistance);
  232. // Render the shape's name
  233. drawName(Point2I((S32)projPnt.x, (S32)projPnt.y),shape->getShapeName(),opacity);
  234. }
  235. }
  236. }
  237. // Restore control object collision
  238. control->enableCollision();
  239. // Border last
  240. if (mShowFrame)
  241. GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
  242. }
  243. //----------------------------------------------------------------------------
  244. /// Render object names.
  245. ///
  246. /// Helper function for GuiShapeNameHud::onRender
  247. ///
  248. /// @param offset Screen coordinates to render name label. (Text is centered
  249. /// horizontally about this location, with bottom of text at
  250. /// specified y position.)
  251. /// @param name String name to display.
  252. /// @param opacity Opacity of name (a fraction).
  253. void GuiShapeNameHud::drawName(Point2I offset, const char *name, F32 opacity)
  254. {
  255. F32 width = mProfile->mFont->getStrWidth((const UTF8 *)name) + mLabelPadding.x * 2;
  256. F32 height = mProfile->mFont->getHeight() + mLabelPadding.y * 2;
  257. Point2I extent = Point2I(width, height);
  258. // Center the name
  259. offset.x -= width / 2;
  260. offset.y -= height / 2;
  261. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  262. // Background fill first
  263. if (mShowLabelFill)
  264. drawUtil->drawRectFill(RectI(offset, extent), mLabelFillColor);
  265. // Deal with opacity and draw.
  266. mTextColor.alpha = opacity;
  267. drawUtil->setBitmapModulation(mTextColor);
  268. drawUtil->drawText(mProfile->mFont, offset + mLabelPadding, name);
  269. drawUtil->clearBitmapModulation();
  270. // Border last
  271. if (mShowLabelFrame)
  272. drawUtil->drawRect(RectI(offset, extent), mLabelFrameColor);
  273. }