1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2012 GarageGames, LLC
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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
21 //-----------------------------------------------------------------------------
23 #include "T3D/gameTSCtrl.h"
24 #include "console/consoleTypes.h"
25 #include "T3D/gameBase/gameBase.h"
26 #include "T3D/gameBase/gameConnection.h"
27 #include "T3D/shapeBase.h"
28 #include "T3D/gameFunctions.h"
29 #include "console/engineAPI.h"
31 //---------------------------------------------------------------------------
33 Point3F lineTestStart
= Point3F(0, 0, 0);
34 Point3F lineTestEnd
= Point3F(0, 1000, 0);
35 Point3F lineTestIntersect
= Point3F(0, 0, 0);
36 bool gSnapLine
= false;
38 //----------------------------------------------------------------------------
40 //----------------------------------------------------------------------------
41 IMPLEMENT_CONOBJECT(GameTSCtrl
);
43 // See Torque manual (.CHM) for more information
44 ConsoleDocClass( GameTSCtrl
,
45 "@brief The main 3D viewport for a Torque 3D game.\n\n"
48 GameTSCtrl::GameTSCtrl()
52 //---------------------------------------------------------------------------
53 bool GameTSCtrl::onAdd()
55 if ( !Parent::onAdd() )
61 //---------------------------------------------------------------------------
62 bool GameTSCtrl::processCameraQuery(CameraQuery
*camq
)
64 GameUpdateCameraFov();
65 return GameProcessCameraQuery(camq
);
68 //---------------------------------------------------------------------------
69 void GameTSCtrl::renderWorld(const RectI
&updateRect
)
74 //---------------------------------------------------------------------------
75 void GameTSCtrl::makeScriptCall(const char *func
, const GuiEvent
&evt
) const
77 // write screen position
78 char *sp
= Con::getArgBuffer(32);
79 dSprintf(sp
, 32, "%d %d", evt
.mousePoint
.x
, evt
.mousePoint
.y
);
81 // write world position
82 char *wp
= Con::getArgBuffer(32);
84 mLastCameraQuery
.cameraMatrix
.getColumn(3, &camPos
);
85 dSprintf(wp
, 32, "%g %g %g", camPos
.x
, camPos
.y
, camPos
.z
);
88 char *vec
= Con::getArgBuffer(32);
89 Point3F
fp(evt
.mousePoint
.x
, evt
.mousePoint
.y
, 1.0);
94 dSprintf(vec
, 32, "%g %g %g", ray
.x
, ray
.y
, ray
.z
);
96 Con::executef( (SimObject
*)this, func
, sp
, wp
, vec
);
99 void GameTSCtrl::onMouseDown(const GuiEvent
&evt
)
101 Parent::onMouseDown(evt
);
102 if( isMethod( "onMouseDown" ) )
103 makeScriptCall( "onMouseDown", evt
);
106 void GameTSCtrl::onRightMouseDown(const GuiEvent
&evt
)
108 Parent::onRightMouseDown(evt
);
109 if( isMethod( "onRightMouseDown" ) )
110 makeScriptCall( "onRightMouseDown", evt
);
113 void GameTSCtrl::onMiddleMouseDown(const GuiEvent
&evt
)
115 Parent::onMiddleMouseDown(evt
);
116 if( isMethod( "onMiddleMouseDown" ) )
117 makeScriptCall( "onMiddleMouseDown", evt
);
120 void GameTSCtrl::onMouseUp(const GuiEvent
&evt
)
122 Parent::onMouseUp(evt
);
123 if( isMethod( "onMouseUp" ) )
124 makeScriptCall( "onMouseUp", evt
);
127 void GameTSCtrl::onRightMouseUp(const GuiEvent
&evt
)
129 Parent::onRightMouseUp(evt
);
130 if( isMethod( "onRightMouseUp" ) )
131 makeScriptCall( "onRightMouseUp", evt
);
134 void GameTSCtrl::onMiddleMouseUp(const GuiEvent
&evt
)
136 Parent::onMiddleMouseUp(evt
);
137 if( isMethod( "onMiddleMouseUp" ) )
138 makeScriptCall( "onMiddleMouseUp", evt
);
141 void GameTSCtrl::onMouseMove(const GuiEvent
&evt
)
148 if ( GameGetCameraTransform(&mat
, &vel
) )
151 mat
.getColumn(3,&pos
);
152 Point3F
screenPoint((F32
)evt
.mousePoint
.x
, (F32
)evt
.mousePoint
.y
, -1.0f
);
154 if (unproject(screenPoint
, &worldPoint
)) {
155 Point3F vec
= worldPoint
- pos
;
158 lineTestEnd
= pos
+ vec
* 1000;
163 void GameTSCtrl::onRender(Point2I offset
, const RectI
&updateRect
)
165 // check if should bother with a render
166 GameConnection
* con
= GameConnection::getConnectionToServer();
167 bool skipRender
= !con
|| (con
->getWhiteOut() >= 1.f
) || (con
->getDamageFlash() >= 1.f
) || (con
->getBlackOut() >= 1.f
);
169 if(!skipRender
|| true)
170 Parent::onRender(offset
, updateRect
);
173 //--------------------------------------------------------------------------
175 DefineEngineFunction(snapToggle
, void, (),,
176 "@brief Prevents mouse movement from being processed\n\n"
178 "In the source, whenever a mouse move event occurs "
179 "GameTSCtrl::onMouseMove() is called. Whenever snapToggle() "
180 "is called, it will flag a variable that can prevent this "
181 "from happening: gSnapLine. This variable is not exposed to "
182 "script, so you need to call this function to trigger it.\n\n"
185 "// Snapping is off by default, so we will toggle\n"
187 "PlayGui.snapToggle();\n\n"
188 "// Mouse movement should be disabled\n"
189 "// Let's turn it back on\n"
190 "PlayGui.snapToggle();\n"
195 gSnapLine
= !gSnapLine
;
198 //ConsoleFunction( snapToggle, void, 1, 1, "()" )
200 // gSnapLine = !gSnapLine;