gameTSCtrl.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "T3D/gameTSCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "T3D/gameBase/gameBase.h"
  25. #include "T3D/gameBase/gameConnection.h"
  26. #include "T3D/shapeBase.h"
  27. #include "T3D/gameFunctions.h"
  28. #include "console/engineAPI.h"
  29. //---------------------------------------------------------------------------
  30. // Debug stuff:
  31. Point3F lineTestStart = Point3F(0, 0, 0);
  32. Point3F lineTestEnd = Point3F(0, 1000, 0);
  33. Point3F lineTestIntersect = Point3F(0, 0, 0);
  34. bool gSnapLine = false;
  35. //----------------------------------------------------------------------------
  36. // Class: GameTSCtrl
  37. //----------------------------------------------------------------------------
  38. IMPLEMENT_CONOBJECT(GameTSCtrl);
  39. // See Torque manual (.CHM) for more information
  40. ConsoleDocClass( GameTSCtrl,
  41. "@brief The main 3D viewport for a Torque 3D game.\n\n"
  42. "@ingroup Gui3D\n");
  43. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  44. "@brief Callback that occurs whenever the left mouse button is pressed while in this control.\n\n"
  45. "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n"
  46. "@param worldPosition Position of world when mouse was pressed during this callback.\n\n"
  47. "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n"
  48. "@tsexample\n"
  49. "// Mouse was pressed down in this control, causing the callback\n"
  50. "GameTSCtrl::onMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n"
  51. "{\n"
  52. " // Code to call when a mouse event occurs.\n"
  53. "}\n"
  54. "@endtsexample\n\n"
  55. );
  56. IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  57. "@brief Callback that occurs whenever the right mouse button is pressed while in this control.\n\n"
  58. "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n"
  59. "@param worldPosition Position of world when mouse was pressed during this callback.\n\n"
  60. "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n"
  61. "@tsexample\n"
  62. "// Mouse was pressed down in this control, causing the callback\n"
  63. "GameTSCtrl::onRightMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n"
  64. "{\n"
  65. " // Code to call when a mouse event occurs.\n"
  66. "}\n"
  67. "@endtsexample\n\n"
  68. );
  69. IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  70. "@brief Callback that occurs whenever the middle mouse button is pressed while in this control.\n\n"
  71. "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n"
  72. "@param worldPosition Position of world when mouse was pressed during this callback.\n\n"
  73. "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n"
  74. "@tsexample\n"
  75. "// Mouse was pressed down in this control, causing the callback\n"
  76. "GameTSCtrl::onMiddleMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n"
  77. "{\n"
  78. " // Code to call when a mouse event occurs.\n"
  79. "}\n"
  80. "@endtsexample\n\n"
  81. );
  82. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  83. "@brief Callback that occurs whenever the left mouse button is released while in this control.\n\n"
  84. "@param screenPosition Position of screen when mouse was released during this callback.\n\n"
  85. "@param worldPosition Position of world when mouse was released during this callback.\n\n"
  86. "@param clickVector Vector for the click when mouse was released during this callback.\n\n"
  87. "@tsexample\n"
  88. "// Mouse was released down in this control, causing the callback\n"
  89. "GameTSCtrl::onMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n"
  90. "{\n"
  91. " // Code to call when a mouse event occurs.\n"
  92. "}\n"
  93. "@endtsexample\n\n"
  94. );
  95. IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  96. "@brief Callback that occurs whenever the right mouse button is released while in this control.\n\n"
  97. "@param screenPosition Position of screen when mouse was released during this callback.\n\n"
  98. "@param worldPosition Position of world when mouse was released during this callback.\n\n"
  99. "@param clickVector Vector for the click when mouse was released during this callback.\n\n"
  100. "@tsexample\n"
  101. "// Mouse was released down in this control, causing the callback\n"
  102. "GameTSCtrl::onRightMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n"
  103. "{\n"
  104. " // Code to call when a mouse event occurs.\n"
  105. "}\n"
  106. "@endtsexample\n\n"
  107. );
  108. IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  109. "@brief Callback that occurs whenever the middle mouse button is released while in this control.\n\n"
  110. "@param screenPosition Position of screen when mouse was released during this callback.\n\n"
  111. "@param worldPosition Position of world when mouse was released during this callback.\n\n"
  112. "@param clickVector Vector for the click when mouse was released during this callback.\n\n"
  113. "@tsexample\n"
  114. "// Mouse was released down in this control, causing the callback\n"
  115. "GameTSCtrl::onMiddleMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n"
  116. "{\n"
  117. " // Code to call when a mouse event occurs.\n"
  118. "}\n"
  119. "@endtsexample\n\n"
  120. );
  121. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  122. "@brief Callback that occurs whenever the left mouse button is dragged while in this control.\n\n"
  123. "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n"
  124. "@param worldPosition Position of world when mouse was dragged during this callback.\n\n"
  125. "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n"
  126. "@tsexample\n"
  127. "// Mouse was dragged down in this control, causing the callback\n"
  128. "GameTSCtrl::onMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n"
  129. "{\n"
  130. " // Code to call when a mouse event occurs.\n"
  131. "}\n"
  132. "@endtsexample\n\n"
  133. );
  134. IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  135. "@brief Callback that occurs whenever the right mouse button is dragged while in this control.\n\n"
  136. "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n"
  137. "@param worldPosition Position of world when mouse was dragged during this callback.\n\n"
  138. "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n"
  139. "@tsexample\n"
  140. "// Mouse was dragged down in this control, causing the callback\n"
  141. "GameTSCtrl::onRightMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n"
  142. "{\n"
  143. " // Code to call when a mouse event occurs.\n"
  144. "}\n"
  145. "@endtsexample\n\n"
  146. );
  147. IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  148. "@brief Callback that occurs whenever the middle mouse button is dragged while in this control.\n\n"
  149. "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n"
  150. "@param worldPosition Position of world when mouse was dragged during this callback.\n\n"
  151. "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n"
  152. "@tsexample\n"
  153. "// Mouse was dragged down in this control, causing the callback\n"
  154. "GameTSCtrl::onMiddleMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n"
  155. "{\n"
  156. " // Code to call when a mouse event occurs.\n"
  157. "}\n"
  158. "@endtsexample\n\n"
  159. );
  160. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  161. "@brief Callback that occurs whenever the mouse wheel up is pressed while in this control.\n\n"
  162. "@param screenPosition Position of screen when mouse wheel up was pressed during this callback.\n\n"
  163. "@param worldPosition Position of world when mouse wheel up was pressed during this callback.\n\n"
  164. "@param clickVector Vector for the click when mouse wheel up was pressed during this callback.\n\n"
  165. "@tsexample\n"
  166. "// Mouse wheel up was pressed in this control, causing the callback\n"
  167. "GameTSCtrl::onMouseWheelUp(%this,%screenPosition,%worldPosition,%clickVector)\n"
  168. "{\n"
  169. " // Code to call when a mouse event occurs.\n"
  170. "}\n"
  171. "@endtsexample\n\n"
  172. );
  173. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  174. "@brief Callback that occurs whenever the mouse wheel down is pressed while in this control.\n\n"
  175. "@param screenPosition Position of screen when mouse wheel down was pressed during this callback.\n\n"
  176. "@param worldPosition Position of world when mouse wheel down was pressed during this callback.\n\n"
  177. "@param clickVector Vector for the click when mouse wheel down was pressed during this callback.\n\n"
  178. "@tsexample\n"
  179. "// Mouse wheel down was pressed in this control, causing the callback\n"
  180. "GameTSCtrl::onMouseWheelDown(%this,%screenPosition,%worldPosition,%clickVector)\n"
  181. "{\n"
  182. " // Code to call when a mouse event occurs.\n"
  183. "}\n"
  184. "@endtsexample\n\n"
  185. );
  186. IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector),
  187. "@brief Callback that occurs whenever the mouse is moved (without dragging) while in this control.\n\n"
  188. "@param screenPosition Position of screen when mouse was moved during this callback.\n\n"
  189. "@param worldPosition Position of world when mouse was moved during this callback.\n\n"
  190. "@param clickVector Vector for the click when mouse was moved during this callback.\n\n"
  191. "@tsexample\n"
  192. "// Mouse was moved down in this control, causing the callback\n"
  193. "GameTSCtrl::onMouseMove(%this,%screenPosition,%worldPosition,%clickVector)\n"
  194. "{\n"
  195. " // Code to call when a mouse event occurs.\n"
  196. "}\n"
  197. "@endtsexample\n\n"
  198. );
  199. GameTSCtrl::GameTSCtrl()
  200. {
  201. mFrameTime = PlatformTimer::create();
  202. }
  203. //---------------------------------------------------------------------------
  204. bool GameTSCtrl::onAdd()
  205. {
  206. if ( !Parent::onAdd() )
  207. return false;
  208. return true;
  209. }
  210. //---------------------------------------------------------------------------
  211. bool GameTSCtrl::processCameraQuery(CameraQuery *camq)
  212. {
  213. GameUpdateCameraFov();
  214. return GameProcessCameraQuery(camq);
  215. }
  216. //---------------------------------------------------------------------------
  217. void GameTSCtrl::renderWorld(const RectI &updateRect)
  218. {
  219. GameRenderWorld();
  220. }
  221. //------------------------------------------------------------------------------
  222. void GameTSCtrl::sendMouseEvent(const char *name, const GuiEvent &event)
  223. {
  224. // write screen position
  225. Point2I screenPosition = event.mousePoint;
  226. // write world position
  227. Point3F worldPosition;
  228. mLastCameraQuery.cameraMatrix.getColumn(3, &worldPosition);
  229. // write click vector
  230. Point3F fp(event.mousePoint.x, event.mousePoint.y, 1.0);
  231. Point3F clickVector;
  232. unproject(fp, &clickVector);
  233. clickVector -= worldPosition;
  234. clickVector.normalizeSafe();
  235. if (dStricmp(name, "onMouseDown") == 0)
  236. onMouseDown_callback(screenPosition, worldPosition, clickVector);
  237. else if (dStricmp(name, "onRightMouseDown") == 0)
  238. onRightMouseDown_callback(screenPosition, worldPosition, clickVector);
  239. else if (dStricmp(name, "onMiddleMouseDown") == 0)
  240. onMiddleMouseDown_callback(screenPosition, worldPosition, clickVector);
  241. else if (dStricmp(name, "onMouseUp") == 0)
  242. onMouseUp_callback(screenPosition, worldPosition, clickVector);
  243. else if (dStricmp(name, "onRightMouseUp") == 0)
  244. onRightMouseUp_callback(screenPosition, worldPosition, clickVector);
  245. else if (dStricmp(name, "onMiddleMouseUp") == 0)
  246. onMiddleMouseUp_callback(screenPosition, worldPosition, clickVector);
  247. else if (dStricmp(name, "onMouseDragged") == 0)
  248. onMouseDragged_callback(screenPosition, worldPosition, clickVector);
  249. else if (dStricmp(name, "onRightMouseDragged") == 0)
  250. onRightMouseDragged_callback(screenPosition, worldPosition, clickVector);
  251. else if (dStricmp(name, "onMiddleMouseDragged") == 0)
  252. onMiddleMouseDragged_callback(screenPosition, worldPosition, clickVector);
  253. else if (dStricmp(name, "onMouseWheelUp") == 0)
  254. onMouseWheelUp_callback(screenPosition, worldPosition, clickVector);
  255. else if (dStricmp(name, "onMouseWheelDown") == 0)
  256. onMouseWheelDown_callback(screenPosition, worldPosition, clickVector);
  257. else if (dStricmp(name, "onMouseMove") == 0)
  258. onMouseMove_callback(screenPosition, worldPosition, clickVector);
  259. }
  260. void GameTSCtrl::onMouseDown(const GuiEvent &evt)
  261. {
  262. Parent::onMouseDown(evt);
  263. sendMouseEvent("onMouseDown", evt);
  264. }
  265. void GameTSCtrl::onRightMouseDown(const GuiEvent &evt)
  266. {
  267. Parent::onRightMouseDown(evt);
  268. sendMouseEvent("onRightMouseDown", evt);
  269. }
  270. void GameTSCtrl::onMiddleMouseDown(const GuiEvent &evt)
  271. {
  272. Parent::onMiddleMouseDown(evt);
  273. sendMouseEvent("onMiddleMouseDown", evt);
  274. }
  275. void GameTSCtrl::onMouseUp(const GuiEvent &evt)
  276. {
  277. Parent::onMouseUp(evt);
  278. sendMouseEvent("onMouseUp", evt);
  279. }
  280. void GameTSCtrl::onRightMouseUp(const GuiEvent &evt)
  281. {
  282. Parent::onRightMouseUp(evt);
  283. sendMouseEvent("onRightMouseUp", evt);
  284. }
  285. void GameTSCtrl::onMiddleMouseUp(const GuiEvent &evt)
  286. {
  287. Parent::onMiddleMouseUp(evt);
  288. sendMouseEvent("onMiddleMouseUp", evt);
  289. }
  290. void GameTSCtrl::onMouseDragged(const GuiEvent &evt)
  291. {
  292. Parent::onMouseDragged(evt);
  293. if (mFrameTime && mFrameTime->getElapsedMs() > 16)
  294. {
  295. sendMouseEvent("onMouseDragged", evt);
  296. mFrameTime->reset();
  297. }
  298. }
  299. void GameTSCtrl::onRightMouseDragged(const GuiEvent &evt)
  300. {
  301. Parent::onRightMouseDragged(evt);
  302. if (mFrameTime && mFrameTime->getElapsedMs() > 16)
  303. {
  304. sendMouseEvent("onRightMouseDragged", evt);
  305. mFrameTime->reset();
  306. }
  307. }
  308. void GameTSCtrl::onMiddleMouseDragged(const GuiEvent &evt)
  309. {
  310. Parent::onMiddleMouseDragged(evt);
  311. if (mFrameTime && mFrameTime->getElapsedMs() > 16)
  312. {
  313. sendMouseEvent("onMiddleMouseDragged", evt);
  314. mFrameTime->reset();
  315. }
  316. }
  317. bool GameTSCtrl::onMouseWheelUp(const GuiEvent &evt)
  318. {
  319. sendMouseEvent("onMouseWheelUp", evt);
  320. return Parent::onMouseWheelUp(evt);
  321. }
  322. bool GameTSCtrl::onMouseWheelDown(const GuiEvent &evt)
  323. {
  324. sendMouseEvent("onMouseWheelDown", evt);
  325. return Parent::onMouseWheelDown(evt);
  326. }
  327. void GameTSCtrl::onMouseMove(const GuiEvent &evt)
  328. {
  329. if(gSnapLine)
  330. return;
  331. MatrixF mat;
  332. Point3F vel;
  333. if ( GameGetCameraTransform(&mat, &vel) )
  334. {
  335. Point3F pos;
  336. mat.getColumn(3,&pos);
  337. Point3F screenPoint((F32)evt.mousePoint.x, (F32)evt.mousePoint.y, -1.0f);
  338. Point3F worldPoint;
  339. if (unproject(screenPoint, &worldPoint)) {
  340. Point3F vec = worldPoint - pos;
  341. lineTestStart = pos;
  342. vec.normalizeSafe();
  343. lineTestEnd = pos + vec * 1000;
  344. }
  345. }
  346. if (mFrameTime->getElapsedMs() > 16)
  347. {
  348. sendMouseEvent("onMouseMove", evt);
  349. mFrameTime->reset();
  350. }
  351. }
  352. void GameTSCtrl::onRender(Point2I offset, const RectI &updateRect)
  353. {
  354. // check if should bother with a render
  355. GameConnection * con = GameConnection::getConnectionToServer();
  356. bool skipRender = !con || (con->getWhiteOut() >= 1.f) || (con->getDamageFlash() >= 1.f) || (con->getBlackOut() >= 1.f);
  357. if(!skipRender || true)
  358. Parent::onRender(offset, updateRect);
  359. }
  360. //--------------------------------------------------------------------------
  361. DefineEngineFunction(snapToggle, void, (),,
  362. "@brief Prevents mouse movement from being processed\n\n"
  363. "In the source, whenever a mouse move event occurs "
  364. "GameTSCtrl::onMouseMove() is called. Whenever snapToggle() "
  365. "is called, it will flag a variable that can prevent this "
  366. "from happening: gSnapLine. This variable is not exposed to "
  367. "script, so you need to call this function to trigger it.\n\n"
  368. "@tsexample\n"
  369. "// Snapping is off by default, so we will toggle\n"
  370. "// it on first:\n"
  371. "PlayGui.snapToggle();\n\n"
  372. "// Mouse movement should be disabled\n"
  373. "// Let's turn it back on\n"
  374. "PlayGui.snapToggle();\n"
  375. "@endtsexample\n\n"
  376. "@ingroup GuiGame")
  377. {
  378. gSnapLine = !gSnapLine;
  379. }
  380. //
  381. //ConsoleFunction( snapToggle, void, 1, 1, "()" )
  382. //{
  383. // gSnapLine = !gSnapLine;
  384. //}