Sample.cs 11 KB

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