Sample.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 UIView UIView { get; set; }
  37. protected Sample()
  38. {
  39. UIView = SampleSelector.UIView;
  40. }
  41. protected void Exit() { GetSubsystem<Engine>().Exit(); }
  42. static readonly Random random = new Random();
  43. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  44. public static float NextRandom() { return (float)random.NextDouble(); }
  45. /// Return a random float between 0.0 and range, inclusive from both ends.
  46. public static float NextRandom(float range) { return (float)random.NextDouble() * range; }
  47. /// Return a random float between min and max, inclusive from both ends.
  48. public static float NextRandom(float min, float max) { return (float)((random.NextDouble() * (max - min)) + min); }
  49. /// Return a random integer between min and max - 1.
  50. public static int NextRandom(int min, int max) { return random.Next(min, max); }
  51. public virtual void Start()
  52. {
  53. SubscribeToEvent<UpdateEvent>((e) => { Update(e.TimeStep); });
  54. SubscribeToEvent<KeyDownEvent>(HandleKeyDown);
  55. }
  56. protected virtual void Update(float timeStep)
  57. {
  58. MoveCameraByTouches(timeStep);
  59. }
  60. /// <summary>
  61. /// Move camera for 2D samples
  62. /// </summary>
  63. protected void SimpleMoveCamera2D(float timeStep)
  64. {
  65. // Movement speed as world units per second
  66. const float moveSpeed = 4.0f;
  67. var input = GetSubsystem<Input>();
  68. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  69. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitY * moveSpeed * timeStep);
  70. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitY * moveSpeed * timeStep);
  71. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  72. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  73. if (input.GetKeyDown(Constants.KEY_PAGEUP))
  74. {
  75. Camera camera = CameraNode.GetComponent<Camera>();
  76. camera.Zoom = camera.Zoom * 1.01f;
  77. }
  78. if (input.GetKeyDown(Constants.KEY_PAGEDOWN))
  79. {
  80. Camera camera = CameraNode.GetComponent<Camera>();
  81. camera.Zoom = camera.Zoom * 0.99f;
  82. }
  83. }
  84. /// <summary>
  85. /// Move camera for 3D samples
  86. /// </summary>
  87. protected void SimpleMoveCamera3D(float timeStep, float moveSpeed = 10.0f)
  88. {
  89. const float mouseSensitivity = .1f;
  90. var input = GetSubsystem<Input>();
  91. var mouseMove = input.MouseMove;
  92. Yaw += mouseSensitivity * mouseMove.X;
  93. Pitch += mouseSensitivity * mouseMove.Y;
  94. Pitch = MathHelper.Clamp(Pitch, -90, 90);
  95. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  96. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitZ * moveSpeed * timeStep);
  97. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitZ * moveSpeed * timeStep);
  98. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  99. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  100. }
  101. protected void MoveCameraByTouches(float timeStep)
  102. {
  103. if (!TouchEnabled || CameraNode == null)
  104. return;
  105. var input = GetSubsystem<Input>();
  106. for (uint i = 0, num = input.NumTouches; i < num; ++i)
  107. {
  108. var delta = input.GetTouchDelta(i);
  109. if (delta.X != 0 || delta.Y != 0)
  110. {
  111. var camera = CameraNode.GetComponent<Camera>();
  112. if (camera == null)
  113. return;
  114. var graphics = GetSubsystem<Graphics>();
  115. Yaw += TouchSensitivity * camera.Fov / graphics.Height * delta.X;
  116. Pitch += TouchSensitivity * camera.Fov / graphics.Height * delta.Y;
  117. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  118. }
  119. else
  120. {
  121. //var cursor = UI.Cursor;
  122. //if (cursor != null && cursor.Visible)
  123. // cursor.Position = state.Position;
  124. }
  125. }
  126. }
  127. protected void SimpleCreateInstructionsWithWasd(string extra = "")
  128. {
  129. SimpleCreateInstructions("Use WASD keys and mouse/touch to move" + extra);
  130. }
  131. protected void SimpleCreateInstructions(string text = "")
  132. {
  133. var layout = new UILayout();
  134. layout.Rect = UIView.Rect;
  135. layout.LayoutPosition = UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_RIGHT_BOTTOM;
  136. layout.LayoutDistributionPosition = UI_LAYOUT_DISTRIBUTION_POSITION.UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM;
  137. var fontDesc = new UIFontDescription();
  138. fontDesc.Id = "Vera";
  139. fontDesc.Size = 18;
  140. var label = new UIEditField();
  141. label.FontDescription = fontDesc;
  142. label.ReadOnly = true;
  143. label.Multiline = true;
  144. label.AdaptToContentSize = true;
  145. label.Text = text;
  146. layout.AddChild(label);
  147. UIView.AddChild(layout);
  148. }
  149. void CreateLogo()
  150. {
  151. }
  152. void SetWindowAndTitleIcon()
  153. {
  154. }
  155. void CreateConsoleAndDebugHud()
  156. {
  157. }
  158. protected void BackToSelector()
  159. {
  160. GetSubsystem<Input>().SetMouseVisible(true);
  161. UnsubscribeFromAllEvents();
  162. var renderer = GetSubsystem<Renderer>();
  163. for (uint i = 0; i < renderer.NumViewports; i++)
  164. {
  165. renderer.SetViewport(i, null);
  166. }
  167. SampleSelector.sampleRef = null;
  168. SampleSelector.UIView.DeleteAllChildren();
  169. new SampleSelector();
  170. }
  171. void HandleKeyDown(KeyDownEvent e)
  172. {
  173. var renderer = GetSubsystem<Renderer>();
  174. switch (e.Key)
  175. {
  176. case Constants.KEY_ESCAPE:
  177. BackToSelector();
  178. return;
  179. case Constants.KEY_F1:
  180. GetSubsystem<UI>().ToggleConsole();
  181. return;
  182. case Constants.KEY_F2:
  183. GetSubsystem<UI>().ToggleDebugHud();
  184. return;
  185. }
  186. switch (e.Key)
  187. {
  188. case Constants.KEY_1:
  189. var quality = renderer.TextureQuality;
  190. ++quality;
  191. if (quality > 2)
  192. quality = 0;
  193. renderer.TextureQuality = quality;
  194. break;
  195. case Constants.KEY_2:
  196. var mquality = renderer.MaterialQuality;
  197. ++mquality;
  198. if (mquality > 2)
  199. mquality = 0;
  200. renderer.MaterialQuality = mquality;
  201. break;
  202. case Constants.KEY_3:
  203. renderer.SpecularLighting = !renderer.SpecularLighting;
  204. break;
  205. case Constants.KEY_4:
  206. renderer.DrawShadows = !renderer.DrawShadows;
  207. break;
  208. case Constants.KEY_5:
  209. var shadowMapSize = renderer.ShadowMapSize;
  210. shadowMapSize *= 2;
  211. if (shadowMapSize > 2048)
  212. shadowMapSize = 512;
  213. renderer.ShadowMapSize = shadowMapSize;
  214. break;
  215. // shadow depth and filtering quality
  216. case Constants.KEY_6:
  217. var q = (int)renderer.ShadowQuality++;
  218. if (q > 3)
  219. q = 0;
  220. renderer.ShadowQuality = (ShadowQuality)q;
  221. break;
  222. // occlusion culling
  223. case Constants.KEY_7:
  224. var o = !(renderer.MaxOccluderTriangles > 0);
  225. renderer.MaxOccluderTriangles = o ? 5000 : 0;
  226. break;
  227. // instancing
  228. case Constants.KEY_8:
  229. renderer.DynamicInstancing = !renderer.DynamicInstancing;
  230. break;
  231. case Constants.KEY_9:
  232. Image screenshot = new Image();
  233. GetSubsystem<Graphics>().TakeScreenShot(screenshot);
  234. screenshot.SavePNG(GetSubsystem<FileSystem>().ProgramDir + $"Data/Screenshot_{GetType().Name}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture)}.png");
  235. break;
  236. }
  237. }
  238. void InitTouchInput()
  239. {
  240. TouchEnabled = true;
  241. }
  242. }
  243. }