afxGuiTextHud.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  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 "afx/arcaneFX.h"
  25. #include "gui/3d/guiTSControl.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "T3D/gameBase/gameConnection.h"
  28. #include "T3D/shapeBase.h"
  29. #include "afx/ui/afxGuiTextHud.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. Vector<afxGuiTextHud::HudTextSpec> afxGuiTextHud::text_items;
  32. IMPLEMENT_CONOBJECT(afxGuiTextHud);
  33. ConsoleDocClass( afxGuiTextHud,
  34. "@brief Similar to GuiShapeNameHud, afxGuiTextHud displays ShapeBase object names but "
  35. "also allows Gui Text effects.\n\n"
  36. "@ingroup GuiGame\n"
  37. );
  38. afxGuiTextHud::afxGuiTextHud()
  39. {
  40. mFillColor.set( 0.25f, 0.25f, 0.25f, 0.25f );
  41. mFrameColor.set( 0, 0, 0, 1 );
  42. mTextColor.set( 0, 1, 0, 1 );
  43. mShowFill = false;
  44. mShowFrame = true;
  45. mVerticalOffset = 0.5f;
  46. mDistanceFade = 0.1f;
  47. mLabelAllShapes = false;
  48. mEnableControlObjectOcclusion = false;
  49. }
  50. void afxGuiTextHud::initPersistFields()
  51. {
  52. addGroup("Colors");
  53. addField( "fillColor", TypeColorF, Offset( mFillColor, afxGuiTextHud ),
  54. "...");
  55. addField( "frameColor", TypeColorF, Offset( mFrameColor, afxGuiTextHud ),
  56. "...");
  57. addField( "textColor", TypeColorF, Offset( mTextColor, afxGuiTextHud ),
  58. "...");
  59. endGroup("Colors");
  60. addGroup("Misc");
  61. addField( "showFill", TypeBool, Offset( mShowFill, afxGuiTextHud ),
  62. "...");
  63. addField( "showFrame", TypeBool, Offset( mShowFrame, afxGuiTextHud ),
  64. "...");
  65. addField( "verticalOffset", TypeF32, Offset( mVerticalOffset, afxGuiTextHud ),
  66. "...");
  67. addField( "distanceFade", TypeF32, Offset( mDistanceFade, afxGuiTextHud ),
  68. "...");
  69. addField( "labelAllShapes", TypeBool, Offset( mLabelAllShapes, afxGuiTextHud ),
  70. "...");
  71. addField( "enableControlObjectOcclusion", TypeBool, Offset( mEnableControlObjectOcclusion, afxGuiTextHud ),
  72. "...");
  73. endGroup("Misc");
  74. Parent::initPersistFields();
  75. }
  76. //----------------------------------------------------------------------------
  77. /// Core rendering method for this control.
  78. ///
  79. /// This method scans through all the current client ShapeBase objects.
  80. /// If one is named, it displays the name and damage information for it.
  81. ///
  82. /// Information is offset from the center of the object's bounding box,
  83. /// unless the object is a PlayerObjectType, in which case the eye point
  84. /// is used.
  85. ///
  86. /// @param updateRect Extents of control.
  87. void afxGuiTextHud::onRender( Point2I, const RectI &updateRect)
  88. {
  89. // Background fill first
  90. if (mShowFill)
  91. GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor.toColorI());
  92. // Must be in a TS Control
  93. GuiTSCtrl *parent = dynamic_cast<GuiTSCtrl*>(getParent());
  94. if (!parent) return;
  95. // Must have a connection and control object
  96. GameConnection* conn = GameConnection::getConnectionToServer();
  97. if (!conn)
  98. return;
  99. GameBase * control = dynamic_cast<GameBase*>(conn->getControlObject());
  100. if (!control)
  101. return;
  102. // Get control camera info
  103. MatrixF cam;
  104. Point3F camPos;
  105. VectorF camDir;
  106. conn->getControlCameraTransform(0,&cam);
  107. cam.getColumn(3, &camPos);
  108. cam.getColumn(1, &camDir);
  109. F32 camFovCos;
  110. conn->getControlCameraFov(&camFovCos);
  111. camFovCos = mCos(mDegToRad(camFovCos) / 2);
  112. // Visible distance info & name fading
  113. F32 visDistance = gClientSceneGraph->getVisibleDistance();
  114. F32 visDistanceSqr = visDistance * visDistance;
  115. F32 fadeDistance = visDistance * mDistanceFade;
  116. // Collision info. We're going to be running LOS tests and we
  117. // don't want to collide with the control object.
  118. static U32 losMask = TerrainObjectType | TerrainLikeObjectType | ShapeBaseObjectType;
  119. if (!mEnableControlObjectOcclusion)
  120. control->disableCollision();
  121. if (mLabelAllShapes)
  122. {
  123. // This section works just like GuiShapeNameHud and renders labels for
  124. // all the shapes.
  125. // All ghosted objects are added to the server connection group,
  126. // so we can find all the shape base objects by iterating through
  127. // our current connection.
  128. for (SimSetIterator itr(conn); *itr; ++itr)
  129. {
  130. ///if ((*itr)->getTypeMask() & ShapeBaseObjectType)
  131. ///{
  132. ShapeBase* shape = dynamic_cast<ShapeBase*>(*itr);
  133. if ( shape ) {
  134. if (shape != control && shape->getShapeName())
  135. {
  136. // Target pos to test, if it's a player run the LOS to his eye
  137. // point, otherwise we'll grab the generic box center.
  138. Point3F shapePos;
  139. if (shape->getTypeMask() & PlayerObjectType)
  140. {
  141. MatrixF eye;
  142. // Use the render eye transform, otherwise we'll see jittering
  143. shape->getRenderEyeTransform(&eye);
  144. eye.getColumn(3, &shapePos);
  145. }
  146. else
  147. {
  148. // Use the render transform instead of the box center
  149. // otherwise it'll jitter.
  150. MatrixF srtMat = shape->getRenderTransform();
  151. srtMat.getColumn(3, &shapePos);
  152. }
  153. VectorF shapeDir = shapePos - camPos;
  154. // Test to see if it's in range
  155. F32 shapeDist = shapeDir.lenSquared();
  156. if (shapeDist == 0 || shapeDist > visDistanceSqr)
  157. continue;
  158. shapeDist = mSqrt(shapeDist);
  159. // Test to see if it's within our viewcone, this test doesn't
  160. // actually match the viewport very well, should consider
  161. // projection and box test.
  162. shapeDir.normalize();
  163. F32 dot = mDot(shapeDir, camDir);
  164. if (dot < camFovCos)
  165. continue;
  166. // Test to see if it's behind something, and we want to
  167. // ignore anything it's mounted on when we run the LOS.
  168. RayInfo info;
  169. shape->disableCollision();
  170. SceneObject *mount = shape->getObjectMount();
  171. if (mount)
  172. mount->disableCollision();
  173. bool los = !gClientContainer.castRay(camPos, shapePos,losMask, &info);
  174. shape->enableCollision();
  175. if (mount)
  176. mount->enableCollision();
  177. if (!los)
  178. continue;
  179. // Project the shape pos into screen space and calculate
  180. // the distance opacity used to fade the labels into the
  181. // distance.
  182. Point3F projPnt;
  183. shapePos.z += mVerticalOffset;
  184. if (!parent->project(shapePos, &projPnt))
  185. continue;
  186. F32 opacity = (shapeDist < fadeDistance)? 1.0:
  187. 1.0 - (shapeDist - fadeDistance) / (visDistance - fadeDistance);
  188. // Render the shape's name
  189. drawName(Point2I((S32)projPnt.x, (S32)projPnt.y),shape->getShapeName(),opacity);
  190. }
  191. }
  192. }
  193. }
  194. // This section renders all text added by afxGuiText effects.
  195. for (S32 i = 0; i < text_items.size(); i++)
  196. {
  197. HudTextSpec* spec = &text_items[i];
  198. if (spec->text && spec->text[0] != '\0')
  199. {
  200. VectorF shapeDir = spec->pos - camPos;
  201. // do range test
  202. F32 shapeDist = shapeDir.lenSquared();
  203. if (shapeDist == 0 || shapeDist > visDistanceSqr)
  204. continue;
  205. shapeDist = mSqrt(shapeDist);
  206. // Test to see if it's within our viewcone, this test doesn't
  207. // actually match the viewport very well, should consider
  208. // projection and box test.
  209. shapeDir.normalize();
  210. F32 dot = mDot(shapeDir, camDir);
  211. if (dot < camFovCos)
  212. continue;
  213. // Test to see if it's behind something, and we want to
  214. // ignore anything it's mounted on when we run the LOS.
  215. RayInfo info;
  216. if (spec->obj)
  217. spec->obj->disableCollision();
  218. bool los = !gClientContainer.castRay(camPos, spec->pos, losMask, &info);
  219. if (spec->obj)
  220. spec->obj->enableCollision();
  221. if (!los)
  222. continue;
  223. // Project the shape pos into screen space.
  224. Point3F projPnt;
  225. if (!parent->project(spec->pos, &projPnt))
  226. continue;
  227. // Calculate the distance opacity used to fade text into the distance.
  228. F32 opacity = (shapeDist < fadeDistance)? 1.0 : 1.0 - (shapeDist - fadeDistance) / (25.0f);
  229. if (opacity > 0.01f)
  230. drawName(Point2I((S32)projPnt.x, (S32)projPnt.y), spec->text, opacity, &spec->text_clr);
  231. }
  232. }
  233. // Restore control object collision
  234. if (!mEnableControlObjectOcclusion)
  235. control->enableCollision();
  236. // Border last
  237. if (mShowFrame)
  238. GFX->getDrawUtil()->drawRect(updateRect, mFrameColor.toColorI());
  239. reset();
  240. }
  241. //----------------------------------------------------------------------------
  242. /// Render object names.
  243. ///
  244. /// Helper function for GuiShapeNameHud::onRender
  245. ///
  246. /// @param offset Screen coordinates to render name label. (Text is centered
  247. /// horizontally about this location, with bottom of text at
  248. /// specified y position.)
  249. /// @param name String name to display.
  250. /// @param opacity Opacity of name (a fraction).
  251. void afxGuiTextHud::drawName(Point2I offset, const char *name, F32 opacity, LinearColorF* color)
  252. {
  253. // Center the name
  254. offset.x -= mProfile->mFont->getStrWidth((const UTF8 *)name) / 2;
  255. offset.y -= mProfile->mFont->getHeight();
  256. // Deal with opacity and draw.
  257. LinearColorF draw_color = (color) ? *color : mTextColor;
  258. draw_color.alpha *= opacity;
  259. GFX->getDrawUtil()->setBitmapModulation(draw_color.toColorI());
  260. GFX->getDrawUtil()->drawText(mProfile->mFont, offset, name);
  261. GFX->getDrawUtil()->clearBitmapModulation();
  262. }
  263. void afxGuiTextHud::addTextItem(const Point3F& pos, const char* text, LinearColorF& color, SceneObject* obj)
  264. {
  265. if (!text || text[0] == '\0')
  266. return;
  267. HudTextSpec spec;
  268. spec.pos = pos;
  269. spec.text = text;
  270. spec.text_clr = color;
  271. spec.obj = obj;
  272. text_items.push_back(spec);
  273. }
  274. void afxGuiTextHud::reset()
  275. {
  276. text_items.clear();
  277. }
  278. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//