main.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 PickingToy::create( %this )
  23. {
  24. // Configure the toy.
  25. PickingToy.PickType = Point;
  26. PickingToy.PickMode = Any;
  27. PickingToy.NotPickedAlpha = 0.2;
  28. PickingToy.PickAreaSize = 10;
  29. PickingToy.RayStart = "0 30";
  30. // Add the configuration options.
  31. addSelectionOption( "Point,Area,Circle,Ray", "Pick Type", 4, "setPickType", true, "Selects the picking type." );
  32. addSelectionOption( "Any,OOBB,AABB,Collision", "Pick Mode", 4, "setPickMode", false, "Selects the picking mode." );
  33. // Force-on debug options.
  34. setAABBOption( true );
  35. setOOBBOption( true );
  36. setCollisionOption( true );
  37. // Reset the toy.
  38. PickingToy.reset();
  39. }
  40. //-----------------------------------------------------------------------------
  41. function PickingToy::destroy( %this )
  42. {
  43. // Force-off debug options.
  44. setAABBOption( false );
  45. setOOBBOption( false );
  46. setCollisionOption( false );
  47. }
  48. //-----------------------------------------------------------------------------
  49. function PickingToy::reset( %this )
  50. {
  51. // Clear the scene.
  52. SandboxScene.clear();
  53. // Create background.
  54. %this.createBackground();
  55. // Create target.
  56. %this.createTarget();
  57. // Create pick cursor.
  58. %this.createPickCursor();
  59. // Create the ray-cast overlay.
  60. %this.createRaycastOverlay();
  61. }
  62. //-----------------------------------------------------------------------------
  63. function PickingToy::createBackground( %this )
  64. {
  65. // Create the sprite.
  66. %object = SandboxScene.create( Sprite );
  67. %object.BodyType = static;
  68. %object.Position = "0 0";
  69. %object.Size = "100 75";
  70. %object.SceneLayer = 31;
  71. %object.Image = "ToyAssets:highlightBackground";
  72. %object.BlendColor = SlateGray;
  73. %object.PickingAllowed = false;
  74. }
  75. //-----------------------------------------------------------------------------
  76. function PickingToy::createTarget( %this )
  77. {
  78. // Create the sprite.
  79. %object = SandboxScene.create( Sprite );
  80. %object.Size = 40;
  81. %object.Angle = -30;
  82. %object.Image = "ToyAssets:Tiles";
  83. %object.Frame = 0;
  84. %object.setBlendAlpha( PickingToy.NotPickedAlpha );
  85. // Create some collision shapes.
  86. %object.createCircleCollisionShape( 10, "-20 -20" );
  87. %object.createPolygonBoxCollisionShape( "20 20", "20 20" );
  88. // Set the target object.
  89. PickingToy.TargetObject = %object;
  90. }
  91. //-----------------------------------------------------------------------------
  92. function PickingToy::createPickCursor( %this )
  93. {
  94. // Create the sprite.
  95. %object = SandboxScene.create( Sprite );
  96. %object.Size = PickingToy.PickAreaSize;
  97. %object.BlendColor = Red;
  98. %object.PickingAllowed = false;
  99. if ( PickingToy.PickType $= "point" || PickingToy.PickType $= "ray" )
  100. {
  101. %object.Image = "ToyAssets:CrossHair1";
  102. }
  103. else if ( PickingToy.PickType $= "area" )
  104. {
  105. %object.Image = "ToyAssets:Blank";
  106. }
  107. else if ( PickingToy.PickType $= "circle" )
  108. {
  109. %object.Image = "ToyAssets:BlankCircle";
  110. }
  111. // Set the cursor.
  112. PickingToy.CursorObject = %object;
  113. }
  114. //-----------------------------------------------------------------------------
  115. function PickingToy::createRaycastOverlay( %this )
  116. {
  117. // Finish if not in ray mode.
  118. if ( PickingToy.PickType !$= "ray" )
  119. return;
  120. // Create the sprite.
  121. %object = SandboxScene.create( ShapeVector );
  122. %object.Size = "1 1";
  123. %object.PickingAllowed = false;
  124. %object.IsCircle = false;
  125. %object.FillMode = false;
  126. %object.LineColor = Red;
  127. // Set the ray-cast overlay object.
  128. PickingToy.RaycastOverlay = %object;
  129. }
  130. //-----------------------------------------------------------------------------
  131. function PickingToy::onTouchDown(%this, %touchID, %worldPosition)
  132. {
  133. // Update cursor position.
  134. PickingToy.CursorObject.Position = %worldPosition;
  135. // Are we in ray mode?
  136. if ( PickingToy.PickType $= "ray" )
  137. {
  138. // Yes, so update the ray geometry.
  139. PickingToy.RaycastOverlay.PolyList = PickingToy.RayStart SPC %worldPosition;
  140. }
  141. // Handle picking mode appropriately.
  142. switch$( PickingToy.PickType )
  143. {
  144. case "point":
  145. %picked = SandboxScene.pickPoint( %worldPosition, "", "", PickingToy.PickMode );
  146. case "area":
  147. %halfSize = PickingToy.PickAreaSize * 0.5;
  148. %lower = (%worldPosition._0 - %halfSize) SPC(%worldPosition._1 - %halfSize);
  149. %upper = (%worldPosition._0 + %halfSize) SPC(%worldPosition._1 + %halfSize);
  150. %picked = SandboxScene.pickArea( %lower, %upper, "", "", PickingToy.PickMode );
  151. case "ray":
  152. %picked = SandboxScene.pickRay( PickingToy.RayStart, %worldPosition, "", "", PickingToy.PickMode );
  153. case "circle":
  154. %halfSize = PickingToy.PickAreaSize * 0.5;
  155. %picked = SandboxScene.pickCircle( %worldPosition, %halfSize, "", "", PickingToy.PickMode );
  156. }
  157. // Fetch pick count.
  158. %pickCount = %picked.Count;
  159. // See if the target object is amongst those picked.
  160. for( %i = 0; %i < %pickCount; %i++ )
  161. {
  162. // If this is the target object then make it opaque.
  163. if ( getWord( %picked, %i ) == PickingToy.TargetObject )
  164. {
  165. PickingToy.TargetObject.setBlendAlpha( 1.0 );
  166. return;
  167. }
  168. }
  169. // Target not picked so make it transparent.
  170. PickingToy.TargetObject.setBlendAlpha( 0.25 );
  171. }
  172. //-----------------------------------------------------------------------------
  173. function PickingToy::onTouchDragged(%this, %touchID, %worldPosition)
  174. {
  175. // Update cursor position.
  176. PickingToy.CursorObject.Position = %worldPosition;
  177. // Are we in ray mode?
  178. if ( PickingToy.PickType $= "ray" )
  179. {
  180. // Yes, so update the ray geometry.
  181. PickingToy.RaycastOverlay.PolyList = PickingToy.RayStart SPC %worldPosition;
  182. }
  183. // Handle picking mode appropriately.
  184. switch$( PickingToy.PickType )
  185. {
  186. case "point":
  187. %picked = SandboxScene.pickPoint( %worldPosition, "", "", PickingToy.PickMode );
  188. case "area":
  189. %halfSize = PickingToy.PickAreaSize * 0.5;
  190. %lower = (%worldPosition._0 - %halfSize) SPC(%worldPosition._1 - %halfSize);
  191. %upper = (%worldPosition._0 + %halfSize) SPC(%worldPosition._1 + %halfSize);
  192. %picked = SandboxScene.pickArea( %lower, %upper, "", "", PickingToy.PickMode );
  193. case "ray":
  194. %picked = SandboxScene.pickRay( PickingToy.RayStart, %worldPosition, "", "", PickingToy.PickMode );
  195. case "circle":
  196. %halfSize = PickingToy.PickAreaSize * 0.5;
  197. %picked = SandboxScene.pickCircle( %worldPosition, %halfSize, "", "", PickingToy.PickMode );
  198. }
  199. // Fetch pick count.
  200. %pickCount = %picked.Count;
  201. // See if the target object is amongst those picked.
  202. for( %i = 0; %i < %pickCount; %i++ )
  203. {
  204. // If this is the target object then make it opaque.
  205. if ( getWord( %picked, %i ) == PickingToy.TargetObject )
  206. {
  207. PickingToy.TargetObject.setBlendAlpha( 1.0 );
  208. return;
  209. }
  210. }
  211. // Target not picked so make it transparent.
  212. PickingToy.TargetObject.setBlendAlpha( 0.25 );
  213. }
  214. //-----------------------------------------------------------------------------
  215. function PickingToy::setPickMode( %this, %value )
  216. {
  217. PickingToy.PickMode = %value;
  218. }
  219. //-----------------------------------------------------------------------------
  220. function PickingToy::setPickType( %this, %value )
  221. {
  222. PickingToy.PickType = %value;
  223. }