gameTSCtrl.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. GameTSCtrl::GameTSCtrl()
  44. {
  45. }
  46. //---------------------------------------------------------------------------
  47. bool GameTSCtrl::onAdd()
  48. {
  49. if ( !Parent::onAdd() )
  50. return false;
  51. return true;
  52. }
  53. //---------------------------------------------------------------------------
  54. bool GameTSCtrl::processCameraQuery(CameraQuery *camq)
  55. {
  56. GameUpdateCameraFov();
  57. return GameProcessCameraQuery(camq);
  58. }
  59. //---------------------------------------------------------------------------
  60. void GameTSCtrl::renderWorld(const RectI &updateRect)
  61. {
  62. GameRenderWorld();
  63. }
  64. //---------------------------------------------------------------------------
  65. void GameTSCtrl::makeScriptCall(const char *func, const GuiEvent &evt) const
  66. {
  67. // write screen position
  68. char *sp = Con::getArgBuffer(32);
  69. dSprintf(sp, 32, "%d %d", evt.mousePoint.x, evt.mousePoint.y);
  70. // write world position
  71. char *wp = Con::getArgBuffer(32);
  72. Point3F camPos;
  73. mLastCameraQuery.cameraMatrix.getColumn(3, &camPos);
  74. dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z);
  75. // write click vector
  76. char *vec = Con::getArgBuffer(32);
  77. Point3F fp(evt.mousePoint.x, evt.mousePoint.y, 1.0);
  78. Point3F ray;
  79. unproject(fp, &ray);
  80. ray -= camPos;
  81. ray.normalizeSafe();
  82. dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z);
  83. Con::executef( (SimObject*)this, func, sp, wp, vec );
  84. }
  85. void GameTSCtrl::onMouseDown(const GuiEvent &evt)
  86. {
  87. Parent::onMouseDown(evt);
  88. if( isMethod( "onMouseDown" ) )
  89. makeScriptCall( "onMouseDown", evt );
  90. }
  91. void GameTSCtrl::onRightMouseDown(const GuiEvent &evt)
  92. {
  93. Parent::onRightMouseDown(evt);
  94. if( isMethod( "onRightMouseDown" ) )
  95. makeScriptCall( "onRightMouseDown", evt );
  96. }
  97. void GameTSCtrl::onMiddleMouseDown(const GuiEvent &evt)
  98. {
  99. Parent::onMiddleMouseDown(evt);
  100. if( isMethod( "onMiddleMouseDown" ) )
  101. makeScriptCall( "onMiddleMouseDown", evt );
  102. }
  103. void GameTSCtrl::onMouseUp(const GuiEvent &evt)
  104. {
  105. Parent::onMouseUp(evt);
  106. if( isMethod( "onMouseUp" ) )
  107. makeScriptCall( "onMouseUp", evt );
  108. }
  109. void GameTSCtrl::onRightMouseUp(const GuiEvent &evt)
  110. {
  111. Parent::onRightMouseUp(evt);
  112. if( isMethod( "onRightMouseUp" ) )
  113. makeScriptCall( "onRightMouseUp", evt );
  114. }
  115. void GameTSCtrl::onMiddleMouseUp(const GuiEvent &evt)
  116. {
  117. Parent::onMiddleMouseUp(evt);
  118. if( isMethod( "onMiddleMouseUp" ) )
  119. makeScriptCall( "onMiddleMouseUp", evt );
  120. }
  121. void GameTSCtrl::onMouseMove(const GuiEvent &evt)
  122. {
  123. if(gSnapLine)
  124. return;
  125. MatrixF mat;
  126. Point3F vel;
  127. if ( GameGetCameraTransform(&mat, &vel) )
  128. {
  129. Point3F pos;
  130. mat.getColumn(3,&pos);
  131. Point3F screenPoint((F32)evt.mousePoint.x, (F32)evt.mousePoint.y, -1.0f);
  132. Point3F worldPoint;
  133. if (unproject(screenPoint, &worldPoint)) {
  134. Point3F vec = worldPoint - pos;
  135. lineTestStart = pos;
  136. vec.normalizeSafe();
  137. lineTestEnd = pos + vec * 1000;
  138. }
  139. }
  140. }
  141. void GameTSCtrl::onRender(Point2I offset, const RectI &updateRect)
  142. {
  143. // check if should bother with a render
  144. GameConnection * con = GameConnection::getConnectionToServer();
  145. bool skipRender = !con || (con->getWhiteOut() >= 1.f) || (con->getDamageFlash() >= 1.f) || (con->getBlackOut() >= 1.f);
  146. if(!skipRender || true)
  147. Parent::onRender(offset, updateRect);
  148. }
  149. //--------------------------------------------------------------------------
  150. DefineEngineFunction(snapToggle, void, (),,
  151. "@brief Prevents mouse movement from being processed\n\n"
  152. "In the source, whenever a mouse move event occurs "
  153. "GameTSCtrl::onMouseMove() is called. Whenever snapToggle() "
  154. "is called, it will flag a variable that can prevent this "
  155. "from happening: gSnapLine. This variable is not exposed to "
  156. "script, so you need to call this function to trigger it.\n\n"
  157. "@tsexample\n"
  158. "// Snapping is off by default, so we will toggle\n"
  159. "// it on first:\n"
  160. "PlayGui.snapToggle();\n\n"
  161. "// Mouse movement should be disabled\n"
  162. "// Let's turn it back on\n"
  163. "PlayGui.snapToggle();\n"
  164. "@endtsexample\n\n"
  165. "@ingroup GuiGame")
  166. {
  167. gSnapLine = !gSnapLine;
  168. }
  169. //
  170. //ConsoleFunction( snapToggle, void, 1, 1, "()" )
  171. //{
  172. // gSnapLine = !gSnapLine;
  173. //}