input.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. function LeapToy::initializeInput( %this )
  23. {
  24. // General toy action map
  25. new ActionMap(ToyMap);
  26. // Gesture action map for sandbox
  27. new ActionMap(GestureMap);
  28. // Absolute palm rotation/pos action map for breakout game
  29. new ActionMap(BreakoutMap);
  30. // Absolute finger positioning for finger demo
  31. new ActionMap(FingerMap);
  32. // action map for the builder demo game
  33. new ActionMap(BuilderMap);
  34. // Create keyboard bindings
  35. ToyMap.bindObj(keyboard, tab, toggleCursorMode, %this);
  36. ToyMap.bindObj(keyboard, escape, showToolBox, %this);
  37. // Help text binding
  38. ToyMap.bindObj(keyboard, h, toggleHelpTextScene, %this);
  39. // Debugging keybinds
  40. ToyMap.bindObj(keyboard, space, simulateCircle, %this);
  41. ToyMap.bindObj(keyboard, x, simulateKeytap, %this);
  42. ToyMap.bindObj(keyboard, z, showParticle, %this);
  43. ToyMap.bindObj(keyboard, a, movePaddleLeft, %this);
  44. ToyMap.bindObj(keyboard, d, movePaddleRight, %this);
  45. // Create Leap Motion gesture bindings
  46. GestureMap.bindObj(leapdevice, circleGesture, reactToCircleGesture, %this);
  47. GestureMap.bindObj(leapdevice, screenTapGesture, reactToScreenTapGesture, %this);
  48. GestureMap.bindObj(leapdevice, swipeGesture, reactToSwipeGesture, %this);
  49. GestureMap.bindObj(leapdevice, keyTapGesture, reactToKeyTapGesture, %this);
  50. // Create the Leap Motion hand/finger bindings
  51. BreakoutMap.bindObj(leapdevice, leapHandRot, "D", %this.handRotDeadzone, trackHandRotation, %this);
  52. FingerMap.bindObj(leapdevice, leapFingerPos, trackFingerPos, %this);
  53. // Create Leap Motion Builder Demo bindings
  54. BuilderMap.bindObj(leapdevice, circleGesture, reactToCircleBuilder, %this);
  55. BuilderMap.bindObj(leapdevice, screenTapGesture, reactToScreenTapBuilder, %this);
  56. BuilderMap.bindObj(leapdevice, swipeGesture, reactToSwipeBuilder, %this);
  57. BuilderMap.bindObj(leapdevice, keyTapGesture, reactToKeyTapBuilder, %this);
  58. BuilderMap.bindObj(leapdevice, leapFingerPos, trackFingerPosBuilder, %this);
  59. // Push the LeapMap to the stack, making it active
  60. ToyMap.push();
  61. // Initialize the Leap Motion manager
  62. initLeapMotionManager();
  63. enableLeapMotionManager(true);
  64. enableLeapCursorControl(true);
  65. configureLeapGesture("Gesture.Circle.MinProgress", 1);
  66. configureLeapGesture("Gesture.ScreenTap.MinForwardVelocity", 0.1);
  67. configureLeapGesture("Gesture.ScreenTap.MinDistance", 0.1);
  68. configureLeapGesture("Gesture.KeyTap.MinDownVelocity", 20);
  69. configureLeapGesture("Gesture.KeyTap.MinDistance", 1.0);
  70. }
  71. //-----------------------------------------------------------------------------
  72. function LeapToy::destroyInput(%this)
  73. {
  74. FingerMap.pop();
  75. FingerMap.delete();
  76. BreakoutMap.pop();
  77. BreakoutMap.delete();
  78. GestureMap.pop();
  79. GestureMap.delete();
  80. ToyMap.pop();
  81. ToyMap.delete();
  82. BuilderMap.pop();
  83. BuilderMap.delete();
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Called when the user makes a circle with their finger(s)
  87. //
  88. // %id - Finger ID, based on the order the finger was added to the tracking
  89. // %progress - How much of the circle has been completed
  90. // %radius - Radius of the circle created by the user's motion
  91. // %isClockwise - Toggle based on the direction the user made the circle
  92. // %state - State of the gesture progress: 1: Started, 2: Updated, 3: Stopped
  93. function LeapToy::reactToCircleGesture(%this, %id, %progress, %radius, %isClockwise, %state)
  94. {
  95. if (!%this.enableCircleGesture)
  96. return;
  97. if (%progress > 0 && %state == 3)
  98. {
  99. %this.grabObjectsInCircle();
  100. %this.schedule(300, "hideCircleSprite");
  101. }
  102. else if (%progress > 0 && %state != 3)
  103. {
  104. %this.showCircleSprite(%radius/5, %isClockwise);
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Called when the user makes a swipe with their finger(s)
  109. //
  110. // %id - Finger ID, based on the order the finger was added to the tracking
  111. // %state - State of the gesture progress: 1: Started, 2: Updated, 3: Stopped
  112. // %direction - 3 point vector based on the direction the finger swiped
  113. // %speed - How fast the user's finger moved. Values will be quite large
  114. function LeapToy::reactToSwipeGesture(%this, %id, %state, %direction, %speed)
  115. {
  116. if (!%this.enableSwipeGesture)
  117. return;
  118. %worldPosition = SandboxWindow.getWorldPoint(Canvas.getCursorPos());
  119. if (isLeapCursorControlled())
  120. %worldPosition = "0 0";
  121. %this.createAsteroid(%worldPosition, %direction, %speed);
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Called when the user makes a screen tap gesture with their finger(s)
  125. //
  126. // %id - Finger ID, based on the order the finger was added to the tracking
  127. // %position - 3 point vector based on where the finger was located in "Leap Space"
  128. // %direction - 3 point vector based on the direction the finger motion
  129. function LeapToy::reactToScreenTapGesture(%this, %id, %position, %direction)
  130. {
  131. if (!%this.enableScreenTapGesture)
  132. return;
  133. %control = Canvas.getMouseControl();
  134. %control.performClick();
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Called when the user makes a key tap gesture with their finger(s)
  138. //
  139. // %id - Finger ID, based on the order the finger was added to the tracking
  140. // %position - 3 point vector based on where the finger was located in "Leap Space"
  141. // %direction - 3 point vector based on the direction the finger tap
  142. function LeapToy::reactToKeyTapGesture(%this, %id, %position, %direction)
  143. {
  144. if (!%this.enableKeyTapGesture)
  145. return;
  146. %this.deleteSelectedObjects();
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Constantly polling callback based on the palm position of a hand
  150. //
  151. // %id - Ordered hand ID based on when it was added to the tracking device
  152. // %position - 3 point vector based on where the hand is located in "Leap Space"
  153. function LeapToy::trackHandPosition(%this, %id, %position)
  154. {
  155. echo("Hand " @ %id @ " - x:" SPC %position._0 SPC "y:" SPC %position._1 SPC "z:" SPC %position._2);
  156. }
  157. //-----------------------------------------------------------------------------
  158. // Constantly polling callback based on the palm rotation of a hand
  159. //
  160. // %id - Ordered hand ID based on when it was added to the tracking device
  161. // %rotation - 3 point vector based on the hand's rotation: "yaw pitch roll"
  162. function LeapToy::trackHandRotation(%this, %id, %rotation)
  163. {
  164. if (isLeapCursorControlled() || !%this.enableHandRotation)
  165. return;
  166. // Grab the values. Only going to use roll in this demo
  167. %roll = %rotation._2;
  168. %this.movePaddle(%roll*-1);
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Constantly polling callback based on the finger position on a hand
  172. // %id - Ordered hand ID based on when it was added to the tracking device
  173. // %position - 3 point vector based on where the finger is located in "Leap Space"
  174. function LeapToy::trackFingerPos(%this, %ids, %fingersX, %fingersY, %fingersZ)
  175. {
  176. if (!%this.enableFingerTracking)
  177. return;
  178. for(%i = 0; %i < getWordCount(%ids); %i++)
  179. {
  180. %id = getWord(%ids, %i);
  181. %position = getWord(%fingersX, %i) SPC getWord(%fingersY, %i) SPC getWord(%fingersZ, %i);
  182. %screenPosition = getPointFromProjection(%position);
  183. //%screenPosition = getPointFromIntersection(%id);
  184. %worldPosition = SandboxWindow.getWorldPoint(%screenPosition);
  185. %this.showFingerCircle(%id, %worldPosition);
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Flips a switch to activate/deactivate cursor control by the Leap Motion Manager
  190. function LeapToy::toggleCursorMode( %this, %val )
  191. {
  192. if (!%val)
  193. return;
  194. if (isLeapCursorControlled())
  195. enableLeapCursorControl(false);
  196. else
  197. enableLeapCursorControl(true);
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Shows the Sandbox tool box
  201. function LeapToy::showToolBox( %this, %val )
  202. {
  203. if (%val)
  204. toggleToolbox(true);
  205. }
  206. //-----------------------------------------------------------------------------
  207. // DEBUGGING FUNCTIONS
  208. function LeapToy::simulateCircle( %this, %val)
  209. {
  210. if (%val)
  211. {
  212. %this.grabObjectsInCircle(2);
  213. }
  214. }
  215. function LeapToy::simulateKeyTap( %this, %val )
  216. {
  217. if (%val)
  218. {
  219. %this.deleteSelectedObjects();
  220. }
  221. }
  222. function LeapToy::showParticle(%this)
  223. {
  224. %worldPosition = SandboxWindow.getWorldPoint(Canvas.getCursorPos());
  225. %particlePlayer = new ParticlePlayer();
  226. %particlePlayer.BodyType = static;
  227. %particlePlayer.SetPosition( %worldPosition );
  228. %particlePlayer.SceneLayer = 0;
  229. %particlePlayer.ParticleInterpolation = true;
  230. %particlePlayer.Particle = "LeapToy:blockFadeParticle";
  231. %particlePlayer.SizeScale = 1;
  232. SandboxScene.add( %particlePlayer );
  233. %particlePlayer.setLifeTime(3);
  234. }
  235. function LeapToy::movePaddleLeft(%this, %val)
  236. {
  237. if (%val)
  238. {
  239. %this.paddle.setLinearVelocity(%this.PaddleSpeed*-1, 0);
  240. }
  241. else
  242. {
  243. %this.paddle.setLinearVelocity(0, 0);
  244. }
  245. }
  246. function LeapToy::movePaddleRight(%this, %val)
  247. {
  248. if (%val)
  249. {
  250. %this.paddle.setLinearVelocity(%this.PaddleSpeed, 0);
  251. }
  252. else
  253. {
  254. %this.paddle.setLinearVelocity(0, 0);
  255. }
  256. }
  257. function LeapToy::simulateFingerPositions(%this)
  258. {
  259. %numFingers = getRandom(0, 9);
  260. setRandomSeed(-1);
  261. %randomPosition = getRandom(-10, 10) SPC getRandom(-10, 10);
  262. for (%i = 0; %i < %numFingers; %i++)
  263. {
  264. %this.showFingerCircle(%i, %randomPosition);
  265. }
  266. }
  267. // DEBUGGING FUNCTIONS
  268. //-----------------------------------------------------------------------------