51_Urho2DStretchableSprite.as 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Urho2D stretchable sprite example.
  2. // This sample demonstrates:
  3. // - Creating a 2D scene with both static and stretchable sprites
  4. // - Difference in scaling static and stretchable sprites
  5. // - Similarity in otherwise transforming static and stretchable sprites
  6. // - Displaying the scene using the Renderer subsystem
  7. // - Handling keyboard to transform nodes
  8. #include "Scripts/Utilities/Sample.as"
  9. Node@ refSpriteNode = null;
  10. Node@ stretchSpriteNode = null;
  11. int selectTransform = 0;
  12. void Start()
  13. {
  14. // Execute the common startup for samples
  15. SampleStart();
  16. // Create the scene content
  17. CreateScene();
  18. // Create the UI content
  19. CreateInstructions();
  20. // Setup the viewport for displaying the scene
  21. SetupViewport();
  22. // Set the mouse mode to use in the sample
  23. SampleInitMouseMode(MM_FREE);
  24. // Hook up to the frame update events
  25. SubscribeToEvents();
  26. }
  27. void CreateScene()
  28. {
  29. scene_ = Scene();
  30. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  31. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  32. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  33. // optimizing manner
  34. scene_.CreateComponent("Octree");
  35. // Create a scene node for the camera, which we will move around
  36. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  37. cameraNode = scene_.CreateChild("Camera");
  38. // Set an initial position for the camera scene node above the plane
  39. cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
  40. Camera@ camera = cameraNode.CreateComponent("Camera");
  41. camera.orthographic = true;
  42. camera.orthoSize = graphics.height * PIXEL_SIZE;
  43. @refSpriteNode = scene_.CreateChild("regular sprite");
  44. @stretchSpriteNode = scene_.CreateChild("stretchable sprite");
  45. Sprite2D@ sprite = cache.GetResource("Sprite2D", "Urho2D/Stretchable.png");
  46. if (sprite !is null)
  47. {
  48. StaticSprite2D@ staticSprite = refSpriteNode.CreateComponent("StaticSprite2D");
  49. staticSprite.sprite = sprite;
  50. StretchableSprite2D@ stretchSprite = stretchSpriteNode.CreateComponent("StretchableSprite2D");
  51. stretchSprite.sprite = sprite;
  52. stretchSprite.border = IntRect(25, 25, 25, 25);
  53. refSpriteNode.Translate2D(Vector2(-2.0f, 0.0f));
  54. stretchSpriteNode.Translate2D(Vector2(2.0f, 0.0f));
  55. }
  56. }
  57. void CreateInstructions()
  58. {
  59. // Construct new Text object, set string to display and font to use
  60. Text@ instructionText = ui.root.CreateChild("Text");
  61. instructionText.text = "Use WASD keys to transform, Tab key to cycle through\n"
  62. "Scale, Rotate, and Translate transform modes. In Rotate\n"
  63. "mode, combine A/D keys with Ctrl key to rotate about\n"
  64. "the Z axis";
  65. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 12);
  66. // Position the text relative to the screen center
  67. instructionText.horizontalAlignment = HA_CENTER;
  68. instructionText.verticalAlignment = VA_CENTER;
  69. instructionText.SetPosition(0, ui.root.height / 4);
  70. }
  71. void SetupViewport()
  72. {
  73. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  74. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  75. // use, but now we just use full screen and default render path configured in the engine command line options
  76. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  77. renderer.viewports[0] = viewport;
  78. }
  79. void SubscribeToEvents()
  80. {
  81. // Subscribe to Key Up event, to handle individual key presses
  82. SubscribeToEvent("KeyUp", "OnKeyUp");
  83. // Subscribe HandleUpdate() function for processing update events
  84. SubscribeToEvent("Update", "HandleUpdate");
  85. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  86. UnsubscribeFromEvent("SceneUpdate");
  87. }
  88. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  89. {
  90. // Take the frame time step, which is stored as a float
  91. float timeStep = eventData["TimeStep"].GetFloat();
  92. switch (selectTransform)
  93. {
  94. case 0:
  95. ScaleSprites(timeStep);
  96. break;
  97. case 1:
  98. RotateSprites(timeStep);
  99. break;
  100. case 2:
  101. TranslateSprites(timeStep);
  102. break;
  103. default:
  104. log.Error("Bad transform selection: " + selectTransform);
  105. }
  106. }
  107. void OnKeyUp(StringHash eventType, VariantMap& eventData)
  108. {
  109. int key = eventData["Key"].GetInt();
  110. if(key == KEY_TAB)
  111. {
  112. ++selectTransform;
  113. selectTransform %= 3;
  114. }
  115. else if(key == KEY_ESCAPE)
  116. engine.Exit();
  117. }
  118. void TranslateSprites(float timeStep)
  119. {
  120. const float speed = 1.0f;
  121. const bool left = input.keyDown[KEY_A],
  122. right = input.keyDown[KEY_D],
  123. up = input.keyDown[KEY_W],
  124. down = input.keyDown[KEY_S];
  125. if (left || right || up || down)
  126. {
  127. const float quantum = timeStep * speed;
  128. const Vector2 translate = Vector2(
  129. (left ? -quantum : 0.0f) + (right ? quantum : 0.0f),
  130. (down ? -quantum : 0.0f) + (up ? quantum : 0.0f));
  131. refSpriteNode.Translate2D(translate);
  132. stretchSpriteNode.Translate2D(translate);
  133. }
  134. }
  135. void RotateSprites(float timeStep)
  136. {
  137. const float speed = 45.0f;
  138. const bool left = input.keyDown[KEY_A],
  139. right = input.keyDown[KEY_D],
  140. up = input.keyDown[KEY_W],
  141. down = input.keyDown[KEY_S],
  142. ctrl = input.keyDown[KEY_CTRL];
  143. if (left || right || up || down)
  144. {
  145. const float quantum = timeStep * speed;
  146. const float xrot = (up ? -quantum : 0.0f) + (down ? quantum: 0.0f);
  147. const float rot2 = (left ? -quantum: 0.0f) + (right ? quantum : 0.0f);
  148. const Quaternion totalRot = Quaternion(xrot,
  149. ctrl ? 0.0f : rot2,
  150. ctrl ? rot2 : 0.0f);
  151. refSpriteNode.Rotate(totalRot);
  152. stretchSpriteNode.Rotate(totalRot);
  153. }
  154. }
  155. void ScaleSprites(float timeStep)
  156. {
  157. const float speed = 0.5f;
  158. const bool left = input.keyDown[KEY_A],
  159. right = input.keyDown[KEY_D],
  160. up = input.keyDown[KEY_W],
  161. down = input.keyDown[KEY_S];
  162. if (left || right || up || down)
  163. {
  164. const float quantum = timeStep * speed;
  165. const Vector2 scale = Vector2(
  166. 1.0f + (right ? quantum : left ? -quantum : 0.0f),
  167. 1.0f + (up ? quantum : down ? -quantum : 0.0f));
  168. refSpriteNode.Scale2D(scale);
  169. stretchSpriteNode.Scale2D(scale);
  170. }
  171. }
  172. // Create XML patch instructions for screen joystick layout specific to this sample app
  173. String patchInstructions =
  174. "<patch>"
  175. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  176. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">TAB</replace>"
  177. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  178. " <element type=\"Text\">"
  179. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  180. " <attribute name=\"Text\" value=\"TAB\" />"
  181. " </element>"
  182. " </add>"
  183. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  184. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">CTRL</replace>"
  185. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  186. " <element type=\"Text\">"
  187. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  188. " <attribute name=\"Text\" value=\"LCTRL\" />"
  189. " </element>"
  190. " </add>"
  191. "</patch>";