gui3DProjectionCtrl.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //-----------------------------------------------------------------------------
  2. // Gui3DProjectionCtrl
  3. // Doppelganger Inc
  4. // Orion Elenzil 200701
  5. //
  6. // This control is meant to be merely a container for other controls.
  7. // What's neat is that it's easy to 'attach' this control to a point in world-space
  8. // or, more interestingly, to an object such as a player.
  9. //
  10. // Usage:
  11. // * Create the Gui3DProjectionControl - by default it will be at 0, 0, 0.
  12. // * You can change where it's located by setting the field "offsetWorld".
  13. // - note you can specify that right in the .gui file
  14. // * You can attach it to any SceneObject by calling "setAttachedTo()".
  15. //
  16. // Behaviour:
  17. // * If you're attaching it to a player, by default it will center on the player's head.
  18. // * If you attach it to an object, by default it will delete itself if the object is deleted.
  19. // * Doesn't occlude w/r/t 3D objects.
  20. //
  21. // Console Methods:
  22. // * SetAttachedTo(SceneObject)
  23. // * GetAttachedTo()
  24. //
  25. // Params:
  26. // * pointWorld - read/write point in worldspace. read-only if attached to an object.
  27. // * offsetObject - an offset in objectspace. default 0, 0, 0.
  28. // * offsetWorld - an offset in worldspace. default 0, 0, 0.
  29. // * offsetScreen - an offset in screenspace. default 0, 0.
  30. // * hAlign - horizontal alignment. 0 = left, 1 = center, 2 = right. default center.
  31. // * vAlign - vertical alignment. 0 = top, 1 = center, 2 = bottomt. default center.
  32. // * useEyePoint - H & V usage of the eyePoint, if player object. default 0, 1. (ie - use only the vertical component)
  33. // * autoDelete - self-delete when attachedTo object is deleted. default true.
  34. //
  35. // Todo:
  36. // * occlusion - hide the control when its anchor point is occluded.
  37. // * integrate w/ zbuffer - this would actually be a change to the whole GuiControl system.
  38. // * allow attaching to arbitrary nodes in a skeleton.
  39. // * avoid projection when the object is out of the frustum.
  40. //
  41. // oxe 20070111
  42. //-----------------------------------------------------------------------------
  43. #include "console/console.h"
  44. #include "console/consoleTypes.h"
  45. #include "scene/sceneObject.h"
  46. #include "T3D/player.h"
  47. #include "gui/controls/gui3DProjectionCtrl.h"
  48. IMPLEMENT_CONOBJECT(Gui3DProjectionCtrl);
  49. //-----------------------------------------------------------------------------
  50. Gui3DProjectionCtrl::Gui3DProjectionCtrl()
  51. {
  52. mTSCtrl = NULL;
  53. mAttachedTo = NULL;
  54. mAttachedToPlayer = NULL;
  55. mAutoDelete = true;
  56. mHAlign = center;
  57. mVAlign = center;
  58. mUseEyePoint.x = 0;
  59. mUseEyePoint.y = 1;
  60. mPtWorld .set(0, 0, 0);
  61. mPtProj .set(0, 0);
  62. mOffsetObject.set(0, 0, 0);
  63. mOffsetWorld .set(0, 0, 0);
  64. mOffsetScreen.set(0, 0);
  65. }
  66. void Gui3DProjectionCtrl::initPersistFields()
  67. {
  68. Parent::initPersistFields();
  69. addGroup("3DProjection");
  70. addField("pointWorld" , TypePoint3F , Offset(mPtWorld , Gui3DProjectionCtrl));
  71. addField("offsetObject" , TypePoint3F , Offset(mOffsetObject , Gui3DProjectionCtrl));
  72. addField("offsetWorld" , TypePoint3F , Offset(mOffsetWorld , Gui3DProjectionCtrl));
  73. addField("offsetScreen" , TypePoint2I , Offset(mOffsetScreen , Gui3DProjectionCtrl));
  74. addField("hAlign" , TypeS32 , Offset(mHAlign , Gui3DProjectionCtrl));
  75. addField("vAlign" , TypeS32 , Offset(mVAlign , Gui3DProjectionCtrl));
  76. addField("useEyePoint" , TypePoint2I , Offset(mUseEyePoint , Gui3DProjectionCtrl));
  77. addField("autoDelete" , TypeBool , Offset(mAutoDelete , Gui3DProjectionCtrl));
  78. endGroup("3DProjection");
  79. }
  80. void Gui3DProjectionCtrl::onRender(Point2I offset, const RectI &updateRect)
  81. {
  82. doPositioning();
  83. doProjection();
  84. doAlignment();
  85. Parent::onRender(offset, updateRect);
  86. }
  87. void Gui3DProjectionCtrl::resizeDuringRender()
  88. {
  89. doPositioning();
  90. doProjection ();
  91. doAlignment ();
  92. }
  93. bool Gui3DProjectionCtrl::onWake()
  94. {
  95. // walk up the GUI tree until we find a GuiTSCtrl.
  96. mTSCtrl = NULL;
  97. GuiControl* walkCtrl = getParent();
  98. AssertFatal(walkCtrl != NULL, "Gui3DProjectionCtrl::onWake() - NULL parent");
  99. bool doMore = true;
  100. while (doMore)
  101. {
  102. mTSCtrl = dynamic_cast<GuiTSCtrl*>(walkCtrl);
  103. walkCtrl = walkCtrl->getParent();
  104. doMore = (mTSCtrl == NULL) && (walkCtrl != NULL);
  105. }
  106. if (!mTSCtrl)
  107. Con::errorf("Gui3DProjectionCtrl::onWake() - no TSCtrl parent");
  108. return Parent::onWake();
  109. }
  110. void Gui3DProjectionCtrl::onSleep()
  111. {
  112. mTSCtrl = NULL;
  113. return Parent::onSleep();
  114. }
  115. void Gui3DProjectionCtrl::onDeleteNotify(SimObject* obj)
  116. {
  117. // - SimSet assumes that obj is a member of THIS, which in our case ain't true.
  118. // oxe 20070116 - the following doesn't compile on GCC.
  119. // SimSet::Parent::onDeleteNotify(obj);
  120. if (!obj)
  121. {
  122. Con::warnf("Gui3DProjectionCtrl::onDeleteNotify - got NULL");
  123. return;
  124. }
  125. if (obj != mAttachedTo)
  126. {
  127. if (mAttachedTo != NULL)
  128. Con::warnf("Gui3DProjectionCtrl::onDeleteNotify - got unexpected object: %d vs. %d", obj->getId(), mAttachedTo->getId());
  129. return;
  130. }
  131. if (mAutoDelete)
  132. this->deleteObject();
  133. }
  134. //-----------------------------------------------------------------------------
  135. void Gui3DProjectionCtrl::doPositioning()
  136. {
  137. if (mAttachedTo == NULL)
  138. return;
  139. Point3F ptBase; // the regular position of the object.
  140. Point3F ptEye; // the render position of the eye node, if a player object.
  141. Point3F pt; // combination of ptBase and ptEye.
  142. MatrixF mat; // utility
  143. mAttachedTo->getRenderTransform().getColumn(3, &ptBase);
  144. if (mAttachedToPlayer != NULL)
  145. {
  146. mAttachedToPlayer->getRenderEyeTransform(&mat);
  147. mat.getColumn(3, &ptEye);
  148. }
  149. else
  150. {
  151. ptEye = ptBase;
  152. }
  153. // use some components from ptEye but other position from ptBase
  154. pt = ptBase;
  155. if (mUseEyePoint.x != 0)
  156. {
  157. pt.x = ptEye.x;
  158. pt.y = ptEye.y;
  159. }
  160. if (mUseEyePoint.y != 0)
  161. {
  162. pt.z = ptEye.z;
  163. }
  164. // object-space offset
  165. Point3F offsetObj;
  166. QuatF quat(mAttachedTo->getRenderTransform());
  167. quat.mulP(mOffsetObject, &offsetObj);
  168. pt += offsetObj;
  169. // world-space offset
  170. pt += mOffsetWorld;
  171. mPtWorld = pt;
  172. }
  173. void Gui3DProjectionCtrl::doProjection()
  174. {
  175. if (!mTSCtrl)
  176. return;
  177. Point3F pt;
  178. if (!mTSCtrl->project(mPtWorld, &pt))
  179. return;
  180. mPtProj.x = (S32)(pt.x + 0.5f);
  181. mPtProj.y = (S32)(pt.y + 0.5f);
  182. }
  183. void Gui3DProjectionCtrl::doAlignment()
  184. {
  185. // alignment
  186. Point2I offsetAlign;
  187. switch(mHAlign)
  188. {
  189. default:
  190. case center:
  191. offsetAlign.x = -getBounds().extent.x / 2;
  192. break;
  193. case min:
  194. offsetAlign.x = 0;
  195. break;
  196. case max:
  197. offsetAlign.x = -getBounds().extent.x;
  198. break;
  199. }
  200. switch(mVAlign)
  201. {
  202. default:
  203. case center:
  204. offsetAlign.y = -getBounds().extent.y / 2;
  205. break;
  206. case min:
  207. offsetAlign.y = 0;
  208. break;
  209. case max:
  210. offsetAlign.y = -getBounds().extent.y;
  211. break;
  212. }
  213. // projected point
  214. mPtScreen = mPtProj;
  215. // alignment offset
  216. mPtScreen += offsetAlign;
  217. // screen offset
  218. mPtScreen += mOffsetScreen;
  219. // setTrgPosition(mPtScreen);
  220. RectI bounds = getBounds();
  221. bounds.point = mPtScreen;
  222. setBounds(bounds);
  223. }
  224. //-----------------------------------------------------------------------------
  225. void Gui3DProjectionCtrl::setAttachedTo(SceneObject* obj)
  226. {
  227. if (obj == mAttachedTo)
  228. return;
  229. if (mAttachedTo)
  230. clearNotify(mAttachedTo);
  231. mAttachedTo = obj;
  232. mAttachedToPlayer = dynamic_cast<Player*>(obj);
  233. if (mAttachedTo)
  234. deleteNotify(mAttachedTo);
  235. }
  236. DefineEngineMethod(Gui3DProjectionCtrl, setAttachedTo, void, (SceneObject* target), (nullAsType<SceneObject*>()), "(object)")
  237. {
  238. if(target)
  239. object->setAttachedTo(target);
  240. }
  241. DefineEngineMethod(Gui3DProjectionCtrl, getAttachedTo, S32, (),, "()")
  242. {
  243. SceneObject* obj = object->getAttachedTo();
  244. if (!obj)
  245. return 0;
  246. else
  247. return obj->getId();
  248. }