manipulation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. // Sandbox manipulation modes are:
  23. // - Off
  24. // - Pan
  25. // - Pull
  26. Sandbox.ManipulationMode = "off";
  27. // Reset the sandbox pull object.
  28. Sandbox.ManipulationPullMaxForce = 1000;
  29. // Reset the touch events.
  30. Sandbox.InputController = new ScriptObject()
  31. {
  32. class = SandboxInputController;
  33. TouchEventCount = 0;
  34. TouchEventActive[0] = false;
  35. TouchEventActive[1] = false;
  36. TouchEventActive[2] = false;
  37. TouchEventActive[3] = false;
  38. TouchEventActive[4] = false;
  39. LastTouchId = -1;
  40. LatestTouchId = -1;
  41. };
  42. //-----------------------------------------------------------------------------
  43. function SandboxInputController::initialize( %this )
  44. {
  45. // Add sandbox touch gester as an input listener.
  46. SandboxWindow.addInputListener( %this );
  47. }
  48. //-----------------------------------------------------------------------------
  49. function SandboxInputController::onTouchDown(%this, %touchID, %worldPosition)
  50. {
  51. // Finish if the drag mode is off.
  52. if ( Sandbox.ManipulationMode $= "off" )
  53. return;
  54. // Sanity!
  55. if ( %this.TouchEventActive[%touchId] == true )
  56. return;
  57. // Calculate window position.
  58. %windowPosition = SandboxWindow.getWindowPoint( %worldPosition );
  59. // Store the new touch position.
  60. %this.NewTouchPosition[%touchId] = %windowPosition;
  61. // Set the old touch position as new touch position.
  62. %this.OldTouchPosition[%touchId] = %windowPosition;
  63. // Flag event as active.
  64. %this.TouchEventActive[%touchId] = true;
  65. // Insert the new touch Id.
  66. %this.PreviousTouchId = %this.CurrentTouchId;
  67. %this.CurrentTouchId = %touchId;
  68. // Increase event count.
  69. %this.TouchEventCount++;
  70. // Handle "pull" mode.
  71. if ( Sandbox.ManipulationMode $= "pull" )
  72. {
  73. // Reset the pull
  74. Sandbox.ManipulationPullObject[%touchID] = "";
  75. Sandbox.ManipulationPullJointId[%touchID] = "";
  76. // Pick an object.
  77. %picked = SandboxScene.pickPoint( %worldPosition );
  78. // Finish if nothing picked.
  79. if ( %picked $= "" )
  80. return;
  81. // Fetch the pick count.
  82. %pickCount = %picked.Count;
  83. for( %n = 0; %n < %pickCount; %n++ )
  84. {
  85. // Fetch the picked object.
  86. %pickedObject = getWord( %picked, %n );
  87. // Skip if the object is static.
  88. if ( %pickedObject.getBodyType() $= "static" )
  89. continue;
  90. // Set the pull object.
  91. Sandbox.ManipulationPullObject[%touchID] = %pickedObject;
  92. Sandbox.ManipulationPullJointId[%touchID] = SandboxScene.createTargetJoint( %pickedObject, %worldPosition, Sandbox.ManipulationPullMaxForce );
  93. return;
  94. }
  95. return;
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. function SandboxInputController::onTouchUp(%this, %touchID, %worldPosition)
  100. {
  101. // Finish if the drag mode is off.
  102. if ( Sandbox.ManipulationMode $= "off" )
  103. return;
  104. // Sanity!
  105. if ( %this.TouchEventActive[%touchId] == false )
  106. return;
  107. // Reset previous touch.
  108. %this.OldTouchPosition[%touchId] = "";
  109. // Reset current touch.
  110. %this.NewTouchPosition[%touchId] = "";
  111. // Flag event as inactive.
  112. %this.TouchEventActive[%touchId] = false;
  113. // Remove the touch Id.
  114. if ( %this.PreviousTouchId == %touchId )
  115. {
  116. %this.PreviousTouchId = "";
  117. }
  118. if ( %this.CurrentTouchId == %touchId )
  119. {
  120. %this.CurrentTouchId = %this.PreviousTouchId;
  121. %this.PreviousTouchId = "";
  122. }
  123. // Decrease event count.
  124. %this.TouchEventCount--;
  125. // Handle "pull" mode.
  126. if ( Sandbox.ManipulationMode $= "pull" )
  127. {
  128. // Finish if nothing is being pulled.
  129. if ( !isObject(Sandbox.ManipulationPullObject[%touchID]) )
  130. return;
  131. // Reset the pull object.
  132. Sandbox.ManipulationPullObject[%touchID] = "";
  133. // Remove the pull joint.
  134. SandboxScene.deleteJoint( Sandbox.ManipulationPullJointId[%touchID] );
  135. Sandbox.ManipulationPullJointId[%touchID] = "";
  136. return;
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. function SandboxInputController::onTouchDragged(%this, %touchID, %worldPosition)
  141. {
  142. // Finish if the drag mode is off.
  143. if ( Sandbox.ManipulationMode $= "off" )
  144. return;
  145. // Sanity!
  146. if ( %this.TouchEventActive[%touchId] == false )
  147. return;
  148. // Calculate window position.
  149. %windowPosition = SandboxWindow.getWindowPoint( %worldPosition );
  150. // Set the current touch as the previous touch.
  151. %this.OldTouchPosition[%touchId] = %this.NewTouchPosition[%touchId];
  152. // Store the touch event.
  153. %this.NewTouchPosition[%touchId] = %windowPosition;
  154. // Handle "pan" mode.
  155. if ( Sandbox.ManipulationMode $= "pan" )
  156. {
  157. // Fetch the touch event count.
  158. %touchEventCount = Sandbox.InputController.TouchEventCount;
  159. // Do we have a single touch event?
  160. if ( %touchEventCount == 1 )
  161. {
  162. // Yes, so perform pan gesture.
  163. Sandbox.InputController.performPanGesture();
  164. return;
  165. }
  166. // Do we have two event counts?
  167. if ( %touchEventCount == 2 )
  168. {
  169. // Yes, so perform zoom gesture.
  170. Sandbox.InputController.performZoomGesture();
  171. return;
  172. }
  173. }
  174. // Handle "pull" mode.
  175. if ( Sandbox.ManipulationMode $= "pull" )
  176. {
  177. // Finish if nothing is being pulled.
  178. if ( !isObject(Sandbox.ManipulationPullObject[%touchID]) )
  179. return;
  180. // Set a new target for the target joint.
  181. SandboxScene.setTargetJointTarget( Sandbox.ManipulationPullJointId[%touchID], %worldPosition );
  182. return;
  183. }
  184. }
  185. //-----------------------------------------------------------------------------
  186. function SandboxInputController::onTouchMoved(%this, %touchID, %worldPosition)
  187. {
  188. // Finish if the drag mode is off.
  189. if ( Sandbox.ManipulationMode $= "off" )
  190. return;
  191. }
  192. //-----------------------------------------------------------------------------
  193. function SandboxInputController::onMouseWheelUp(%this, %modifier, %mousePoint, %mouseClickCount)
  194. {
  195. // Finish if the drag mode is not "pan".
  196. if ( !Sandbox.ManipulationMode $= "pan" )
  197. return;
  198. // Increase the zoom.
  199. SandboxWindow.setCameraZoom( SandboxWindow.getCameraZoom() + $pref::Sandbox::cameraMouseZoomRate );
  200. }
  201. //-----------------------------------------------------------------------------
  202. function SandboxInputController::onMouseWheelDown(%this, %modifier, %mousePoint, %mouseClickCount)
  203. {
  204. // Finish if the drag mode is not "pan".
  205. if ( !Sandbox.ManipulationMode $= "pan" )
  206. return;
  207. // Increase the zoom.
  208. SandboxWindow.setCameraZoom( SandboxWindow.getCameraZoom() - $pref::Sandbox::cameraMouseZoomRate );
  209. }
  210. //-----------------------------------------------------------------------------
  211. function SandboxInputController::performPanGesture( %this )
  212. {
  213. // Finish if we don't have two touch events.
  214. if ( %this.TouchEventCount != 1 )
  215. return;
  216. // Fetch the last touch event Id.
  217. %touchId = %this.CurrentTouchId;
  218. // Sanity!
  219. if ( %touchId $= "" )
  220. return;
  221. // Calculate pan offset.
  222. %panOffset = Vector2Sub( %this.NewTouchPosition[%touchId], %this.OldTouchPosition[%touchId] );
  223. // Inverse the Y offset.
  224. %panOffset = Vector2InverseY( %panOffset );
  225. // Scale the pan offset by the camera world scale.
  226. %panOffset = Vector2Mult( %panOffset, SandboxWindow.getCameraWorldScale() );
  227. // Update the camera position.
  228. SandboxWindow.setCameraPosition( Vector2Sub( SandboxWindow.getCameraPosition(), %panOffset ) );
  229. }
  230. //-----------------------------------------------------------------------------
  231. function SandboxInputController::performZoomGesture( %this )
  232. {
  233. // Finish if we don't have two touch events.
  234. if ( %this.TouchEventCount != 2 )
  235. return;
  236. // Fetch current and previous touch Ids.
  237. %currentTouchId = %this.CurrentTouchId;
  238. %previousTouchId = %this.PreviousTouchId;
  239. // Finish if we don't have touch Ids active.
  240. if ( !%this.TouchEventActive[%currentTouchId] || !%this.TouchEventActive[%previousTouchId] )
  241. return;
  242. %currentNewPosition = %this.NewTouchPosition[%currentTouchId];
  243. %currentOldPosition = %this.OldTouchPosition[%currentTouchId];
  244. %previousNewPosition = %this.NewTouchPosition[%previousTouchId];
  245. %previousOldPosition = %this.OldTouchPosition[%previousTouchId];
  246. // Calculate the last and current separations.
  247. %lastLength = Vector2Length( Vector2Abs( %currentOldPosition, %previousOldPosition ) );
  248. %currentLength = Vector2Length( Vector2Abs( %currentNewPosition, %previousNewPosition ) );
  249. // Calculate the change in separation length.
  250. %separationDelta = %currentLength - %lastLength;
  251. // Finish if no separation change occurred.
  252. if ( %separationDelta == 0 || %separationDelta $= "" )
  253. return;
  254. // Fetch the camera zoom.
  255. %cameraZoom = SandboxWindow.getCameraZoom();
  256. // Calculate new camera zoom.
  257. %newCameraZoom = %cameraZoom + ( %separationDelta * $pref::Sandbox::cameraTouchZoomRate );
  258. // Change the zoom.
  259. SandboxWindow.setCameraZoom( %newCameraZoom ) ;
  260. }
  261. //-----------------------------------------------------------------------------
  262. function Sandbox::resetManipulationModes( %this )
  263. {
  264. // These control which drag modes are available or not.
  265. Sandbox.ManipulationModeState["off"] = true;
  266. Sandbox.ManipulationModeState["pan"] = false;
  267. Sandbox.ManipulationModeState["pull"] = false;
  268. // Set the sandbox drag mode default.
  269. Sandbox.useManipulation( "off" );
  270. }
  271. //-----------------------------------------------------------------------------
  272. function Sandbox::resetManipulationModes( %this )
  273. {
  274. // These control which drag modes are available or not.
  275. Sandbox.ManipulationModeState["off"] = true;
  276. Sandbox.ManipulationModeState["pan"] = false;
  277. Sandbox.ManipulationModeState["pull"] = false;
  278. // Set the sandbox drag mode default.
  279. Sandbox.useManipulation( "off" );
  280. }
  281. //-----------------------------------------------------------------------------
  282. function Sandbox::allowManipulation( %this, %mode )
  283. {
  284. // Cannot turn-off the "off" manipulation.
  285. if ( %mode $= "off" )
  286. return;
  287. Sandbox.ManipulationModeState[%mode] = true;
  288. }
  289. //-----------------------------------------------------------------------------
  290. function Sandbox::useManipulation( %this, %mode )
  291. {
  292. // Is the drag mode available?
  293. if ( %mode !$= "off" && !Sandbox.ManipulationModeState[%mode] )
  294. return;
  295. // Set the manipulation mode.
  296. Sandbox.ManipulationMode = %mode;
  297. // Set the current mode as text on the button.
  298. if ( isObject(ManipulationModeButton) )
  299. {
  300. // Make the displayed mode more consistent.
  301. if ( %mode $= "off" )
  302. %mode = "Off";
  303. else if ( %mode $= "pan" )
  304. %mode = "Pan";
  305. else if ( %mode $= "pull" )
  306. %mode = "Pull";
  307. // Make the mode consistent when showed.
  308. ManipulationModeButton.Text = %mode;
  309. }
  310. // Reset pulled object and joint.
  311. Sandbox.ManipulationPullObject = "";
  312. if ( Sandbox.ManipulationPullJointId !$= "" && SandboxScene.isJoint(Sandbox.ManipulationPullJointId) )
  313. {
  314. SandboxScene.deleteJoint( Sandbox.ManipulationPullJointId );
  315. Sandbox.ManipulationPullJointId = "";
  316. }
  317. }
  318. //-----------------------------------------------------------------------------
  319. function cycleManipulation( %make )
  320. {
  321. // Finish if being released.
  322. if ( !%make )
  323. return;
  324. // "off" to "pan" transition.
  325. if ( Sandbox.ManipulationMode $= "off" )
  326. {
  327. if ( Sandbox.ManipulationModeState["pan"] )
  328. {
  329. Sandbox.useManipulation("pan");
  330. return;
  331. }
  332. Sandbox.ManipulationMode = "pan";
  333. }
  334. // "pan" to "pull" transition.
  335. if ( Sandbox.ManipulationMode $= "pan" )
  336. {
  337. if ( Sandbox.ManipulationModeState["pull"] )
  338. {
  339. Sandbox.useManipulation("pull");
  340. return;
  341. }
  342. Sandbox.ManipulationMode = "pull";
  343. }
  344. // "pull" to "off" transition.
  345. if ( Sandbox.ManipulationMode $= "pull" )
  346. {
  347. Sandbox.useManipulation("off");
  348. }
  349. }