Sample.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Globalization;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. public class Sample : NETScriptObject
  29. {
  30. protected const float PixelSize = 0.01f;
  31. protected const float TouchSensitivity = 2;
  32. protected float Yaw { get; set; }
  33. protected float Pitch { get; set; }
  34. protected bool TouchEnabled { get; set; }
  35. protected Node CameraNode { get; set; }
  36. protected Sample() { }
  37. static readonly Random random = new Random();
  38. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  39. public static float NextRandom() { return (float)random.NextDouble(); }
  40. /// Return a random float between 0.0 and range, inclusive from both ends.
  41. public static float NextRandom(float range) { return (float)random.NextDouble() * range; }
  42. /// Return a random float between min and max, inclusive from both ends.
  43. public static float NextRandom(float min, float max) { return (float)((random.NextDouble() * (max - min)) + min); }
  44. /// Return a random integer between min and max - 1.
  45. public static int NextRandom(int min, int max) { return random.Next(min, max); }
  46. public virtual void Start()
  47. {
  48. SubscribeToEvent<UpdateEvent>((e) => { Update(e.TimeStep); });
  49. SubscribeToEvent<KeyDownEvent>(HandleKeyDown);
  50. }
  51. protected virtual void Update(float timeStep)
  52. {
  53. MoveCameraByTouches(timeStep);
  54. }
  55. /// <summary>
  56. /// Move camera for 2D samples
  57. /// </summary>
  58. protected void SimpleMoveCamera2D(float timeStep)
  59. {
  60. // Movement speed as world units per second
  61. const float moveSpeed = 4.0f;
  62. var input = GetSubsystem<Input>();
  63. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  64. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitY * moveSpeed * timeStep);
  65. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitY * moveSpeed * timeStep);
  66. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  67. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  68. if (input.GetKeyDown(Constants.KEY_PAGEUP))
  69. {
  70. Camera camera = CameraNode.GetComponent<Camera>();
  71. camera.Zoom = camera.Zoom * 1.01f;
  72. }
  73. if (input.GetKeyDown(Constants.KEY_PAGEDOWN))
  74. {
  75. Camera camera = CameraNode.GetComponent<Camera>();
  76. camera.Zoom = camera.Zoom * 0.99f;
  77. }
  78. }
  79. /// <summary>
  80. /// Move camera for 3D samples
  81. /// </summary>
  82. protected void SimpleMoveCamera3D(float timeStep, float moveSpeed = 10.0f)
  83. {
  84. const float mouseSensitivity = .1f;
  85. var input = GetSubsystem<Input>();
  86. var mouseMove = input.MouseMove;
  87. Yaw += mouseSensitivity * mouseMove.X;
  88. Pitch += mouseSensitivity * mouseMove.Y;
  89. Pitch = MathHelper.Clamp(Pitch, -90, 90);
  90. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  91. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitZ * moveSpeed * timeStep);
  92. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitZ * moveSpeed * timeStep);
  93. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  94. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  95. }
  96. protected void MoveCameraByTouches(float timeStep)
  97. {
  98. if (!TouchEnabled || CameraNode == null)
  99. return;
  100. var input = GetSubsystem<Input>();
  101. for (uint i = 0, num = input.NumTouches; i < num; ++i)
  102. {
  103. var delta = input.GetTouchDelta(i);
  104. if (delta.X != 0 || delta.Y != 0)
  105. {
  106. var camera = CameraNode.GetComponent<Camera>();
  107. if (camera == null)
  108. return;
  109. var graphics = GetSubsystem<Graphics>();
  110. Yaw += TouchSensitivity * camera.Fov / graphics.Height * delta.X;
  111. Pitch += TouchSensitivity * camera.Fov / graphics.Height * delta.Y;
  112. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  113. }
  114. else
  115. {
  116. //var cursor = UI.Cursor;
  117. //if (cursor != null && cursor.Visible)
  118. // cursor.Position = state.Position;
  119. }
  120. }
  121. }
  122. protected void SimpleCreateInstructionsWithWasd(string extra = "")
  123. {
  124. SimpleCreateInstructions("Use WASD keys and mouse/touch to move" + extra);
  125. }
  126. protected void SimpleCreateInstructions(string text = "")
  127. {
  128. var view = new UIView();
  129. var layout = new UILayout();
  130. layout.Rect = view.Rect;
  131. layout.LayoutPosition = UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_RIGHT_BOTTOM;
  132. layout.LayoutDistributionPosition = UI_LAYOUT_DISTRIBUTION_POSITION.UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM;
  133. var fontDesc = new UIFontDescription();
  134. fontDesc.Id = "Vera";
  135. fontDesc.Size = 18;
  136. var label = new UIEditField();
  137. label.FontDescription = fontDesc;
  138. label.ReadOnly = true;
  139. label.Multiline = true;
  140. label.AdaptToContentSize = true;
  141. label.Text = text;
  142. layout.AddChild(label);
  143. view.AddChild(layout);
  144. }
  145. void CreateLogo()
  146. {
  147. }
  148. void SetWindowAndTitleIcon()
  149. {
  150. }
  151. void CreateConsoleAndDebugHud()
  152. {
  153. }
  154. void HandleKeyDown(KeyDownEvent e)
  155. {
  156. switch (e.Key)
  157. {
  158. case Constants.KEY_ESCAPE:
  159. GetSubsystem<Engine>().Exit();
  160. return;
  161. case Constants.KEY_F1:
  162. GetSubsystem<UI>().ToggleConsole();
  163. return;
  164. case Constants.KEY_F2:
  165. GetSubsystem<UI>().ToggleDebugHud();
  166. return;
  167. }
  168. var renderer = GetSubsystem<Renderer>();
  169. switch (e.Key)
  170. {
  171. case Constants.KEY_1:
  172. var quality = renderer.TextureQuality;
  173. ++quality;
  174. if (quality > 2)
  175. quality = 0;
  176. renderer.TextureQuality = quality;
  177. break;
  178. case Constants.KEY_2:
  179. var mquality = renderer.MaterialQuality;
  180. ++mquality;
  181. if (mquality > 2)
  182. mquality = 0;
  183. renderer.MaterialQuality = mquality;
  184. break;
  185. case Constants.KEY_3:
  186. renderer.SpecularLighting = !renderer.SpecularLighting;
  187. break;
  188. case Constants.KEY_4:
  189. renderer.DrawShadows = !renderer.DrawShadows;
  190. break;
  191. case Constants.KEY_5:
  192. var shadowMapSize = renderer.ShadowMapSize;
  193. shadowMapSize *= 2;
  194. if (shadowMapSize > 2048)
  195. shadowMapSize = 512;
  196. renderer.ShadowMapSize = shadowMapSize;
  197. break;
  198. // shadow depth and filtering quality
  199. case Constants.KEY_6:
  200. var q = (int)renderer.ShadowQuality++;
  201. if (q > 3)
  202. q = 0;
  203. renderer.ShadowQuality = (ShadowQuality)q;
  204. break;
  205. // occlusion culling
  206. case Constants.KEY_7:
  207. var o = !(renderer.MaxOccluderTriangles > 0);
  208. renderer.MaxOccluderTriangles = o ? 5000 : 0;
  209. break;
  210. // instancing
  211. case Constants.KEY_8:
  212. renderer.DynamicInstancing = !renderer.DynamicInstancing;
  213. break;
  214. case Constants.KEY_9:
  215. Image screenshot = new Image();
  216. GetSubsystem<Graphics>().TakeScreenShot(screenshot);
  217. screenshot.SavePNG(GetSubsystem<FileSystem>().ProgramDir + $"Data/Screenshot_{GetType().Name}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture)}.png");
  218. break;
  219. }
  220. }
  221. void InitTouchInput()
  222. {
  223. TouchEnabled = true;
  224. }
  225. }
  226. }