manipulation.cs 15 KB

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