gameTSCtrl.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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 char* screenPosition, const char* worldPosition, const char* 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. "@see GuiTSCtrl\n\n"
  199. );
  200. GameTSCtrl::GameTSCtrl()
  201. {
  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::makeScriptCall(const char *func, const GuiEvent &evt) const
  223. {
  224. // write screen position
  225. char *sp = Con::getArgBuffer(32);
  226. dSprintf(sp, 32, "%d %d", evt.mousePoint.x, evt.mousePoint.y);
  227. // write world position
  228. char *wp = Con::getArgBuffer(32);
  229. Point3F camPos;
  230. mLastCameraQuery.cameraMatrix.getColumn(3, &camPos);
  231. dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z);
  232. // write click vector
  233. char *vec = Con::getArgBuffer(32);
  234. Point3F fp(evt.mousePoint.x, evt.mousePoint.y, 1.0);
  235. Point3F ray;
  236. unproject(fp, &ray);
  237. ray -= camPos;
  238. ray.normalizeSafe();
  239. dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z);
  240. Con::executef( (SimObject*)this, func, sp, wp, vec );
  241. }
  242. //------------------------------------------------------------------------------
  243. void GameTSCtrl::sendMouseEvent(const char *name, const GuiEvent &event)
  244. {
  245. // write screen position
  246. char* sp = Con::getArgBuffer(32);
  247. dSprintf(sp, 32, "%d %d", event.mousePoint.x, event.mousePoint.y);
  248. // write world position
  249. char* wp = Con::getArgBuffer(32);
  250. Point3F camPos;
  251. mLastCameraQuery.cameraMatrix.getColumn(3, &camPos);
  252. dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z);
  253. // write click vector
  254. char* vec = Con::getArgBuffer(32);
  255. Point3F fp(event.mousePoint.x, event.mousePoint.y, 1.0);
  256. Point3F ray;
  257. unproject(fp, &ray);
  258. ray -= camPos;
  259. ray.normalizeSafe();
  260. dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z);
  261. if (dStricmp(name, "onMouseDown") == 0)
  262. onMouseDown_callback(sp, wp, vec);
  263. else if (dStricmp(name, "onRightMouseDown") == 0)
  264. onRightMouseDown_callback(sp, wp, vec);
  265. else if (dStricmp(name, "onMiddleMouseDown") == 0)
  266. onMiddleMouseDown_callback(sp, wp, vec);
  267. else if (dStricmp(name, "onMouseUp") == 0)
  268. onMouseUp_callback(sp, wp, vec);
  269. else if (dStricmp(name, "onRightMouseUp") == 0)
  270. onRightMouseUp_callback(sp, wp, vec);
  271. else if (dStricmp(name, "onMiddleMouseUp") == 0)
  272. onMiddleMouseUp_callback(sp, wp, vec);
  273. else if (dStricmp(name, "onMouseDragged") == 0)
  274. onMouseDragged_callback(sp, wp, vec);
  275. else if (dStricmp(name, "onRightMouseDragged") == 0)
  276. onRightMouseDragged_callback(sp, wp, vec);
  277. else if (dStricmp(name, "onMiddleMouseDragged") == 0)
  278. onMiddleMouseDragged_callback(sp, wp, vec);
  279. else if (dStricmp(name, "onMouseWheelUp") == 0)
  280. onMouseWheelUp_callback(sp, wp, vec);
  281. else if (dStricmp(name, "onMouseWheelDown") == 0)
  282. onMouseWheelDown_callback(sp, wp, vec);
  283. else if (dStricmp(name, "onMouseMove") == 0)
  284. onMouseMove_callback(sp, wp, vec);
  285. }
  286. void GameTSCtrl::onMouseDown(const GuiEvent &evt)
  287. {
  288. Parent::onMouseDown(evt);
  289. sendMouseEvent("onMouseDown", evt);
  290. }
  291. void GameTSCtrl::onRightMouseDown(const GuiEvent &evt)
  292. {
  293. Parent::onRightMouseDown(evt);
  294. sendMouseEvent("onRightMouseDown", evt);
  295. }
  296. void GameTSCtrl::onMiddleMouseDown(const GuiEvent &evt)
  297. {
  298. Parent::onMiddleMouseDown(evt);
  299. sendMouseEvent("onMiddleMouseDown", evt);
  300. }
  301. void GameTSCtrl::onMouseUp(const GuiEvent &evt)
  302. {
  303. Parent::onMouseUp(evt);
  304. sendMouseEvent("onMouseUp", evt);
  305. }
  306. void GameTSCtrl::onRightMouseUp(const GuiEvent &evt)
  307. {
  308. Parent::onRightMouseUp(evt);
  309. sendMouseEvent("onRightMouseUp", evt);
  310. }
  311. void GameTSCtrl::onMiddleMouseUp(const GuiEvent &evt)
  312. {
  313. Parent::onMiddleMouseUp(evt);
  314. sendMouseEvent("onMiddleMouseUp", evt);
  315. }
  316. void GameTSCtrl::onMouseDragged(const GuiEvent &evt)
  317. {
  318. Parent::onMouseDragged(evt);
  319. sendMouseEvent("onMouseDragged", evt);
  320. }
  321. void GameTSCtrl::onRightMouseDragged(const GuiEvent &evt)
  322. {
  323. Parent::onRightMouseDragged(evt);
  324. sendMouseEvent("onRightMouseDragged", evt);
  325. }
  326. void GameTSCtrl::onMiddleMouseDragged(const GuiEvent &evt)
  327. {
  328. Parent::onMiddleMouseDragged(evt);
  329. sendMouseEvent("onMiddleMouseDragged", evt);
  330. }
  331. bool GameTSCtrl::onMouseWheelUp(const GuiEvent &evt)
  332. {
  333. sendMouseEvent("onMouseWheelUp", evt);
  334. return Parent::onMouseWheelUp(evt);
  335. }
  336. bool GameTSCtrl::onMouseWheelDown(const GuiEvent &evt)
  337. {
  338. sendMouseEvent("onMouseWheelDown", evt);
  339. return Parent::onMouseWheelDown(evt);
  340. }
  341. void GameTSCtrl::onMouseMove(const GuiEvent &evt)
  342. {
  343. if(gSnapLine)
  344. return;
  345. MatrixF mat;
  346. Point3F vel;
  347. if ( GameGetCameraTransform(&mat, &vel) )
  348. {
  349. Point3F pos;
  350. mat.getColumn(3,&pos);
  351. Point3F screenPoint((F32)evt.mousePoint.x, (F32)evt.mousePoint.y, -1.0f);
  352. Point3F worldPoint;
  353. if (unproject(screenPoint, &worldPoint)) {
  354. Point3F vec = worldPoint - pos;
  355. lineTestStart = pos;
  356. vec.normalizeSafe();
  357. lineTestEnd = pos + vec * 1000;
  358. }
  359. }
  360. sendMouseEvent("onMouseMove", evt);
  361. }
  362. void GameTSCtrl::onRender(Point2I offset, const RectI &updateRect)
  363. {
  364. // check if should bother with a render
  365. GameConnection * con = GameConnection::getConnectionToServer();
  366. bool skipRender = !con || (con->getWhiteOut() >= 1.f) || (con->getDamageFlash() >= 1.f) || (con->getBlackOut() >= 1.f);
  367. if(!skipRender || true)
  368. Parent::onRender(offset, updateRect);
  369. }
  370. //--------------------------------------------------------------------------
  371. DefineEngineFunction(snapToggle, void, (),,
  372. "@brief Prevents mouse movement from being processed\n\n"
  373. "In the source, whenever a mouse move event occurs "
  374. "GameTSCtrl::onMouseMove() is called. Whenever snapToggle() "
  375. "is called, it will flag a variable that can prevent this "
  376. "from happening: gSnapLine. This variable is not exposed to "
  377. "script, so you need to call this function to trigger it.\n\n"
  378. "@tsexample\n"
  379. "// Snapping is off by default, so we will toggle\n"
  380. "// it on first:\n"
  381. "PlayGui.snapToggle();\n\n"
  382. "// Mouse movement should be disabled\n"
  383. "// Let's turn it back on\n"
  384. "PlayGui.snapToggle();\n"
  385. "@endtsexample\n\n"
  386. "@ingroup GuiGame")
  387. {
  388. gSnapLine = !gSnapLine;
  389. }
  390. //
  391. //ConsoleFunction( snapToggle, void, 1, 1, "()" )
  392. //{
  393. // gSnapLine = !gSnapLine;
  394. //}