Sample.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Urho3D/Engine/Application.h>
  24. #include <Urho3D/Input/Input.h>
  25. namespace Urho3D
  26. {
  27. class Node;
  28. class Scene;
  29. class Sprite;
  30. }
  31. // All Urho3D classes reside in namespace Urho3D
  32. using namespace Urho3D;
  33. const float TOUCH_SENSITIVITY = 2.0f;
  34. /// Sample class, as framework for all samples.
  35. /// - Initialization of the Urho3D engine (in Application class)
  36. /// - Modify engine parameters for windowed mode and to show the class name as title
  37. /// - Create Urho3D logo at screen
  38. /// - Set custom window title and icon
  39. /// - Create Console and Debug HUD, and use F1 and F2 key to toggle them
  40. /// - Toggle rendering options from the keys 1-8
  41. /// - Take screenshot with key 9
  42. /// - Handle Esc key down to hide Console or exit application
  43. /// - Init touch input on mobile platform using screen joysticks (patched for each individual sample)
  44. class Sample : public Application
  45. {
  46. // Enable type information.
  47. URHO3D_OBJECT(Sample, Application);
  48. public:
  49. /// Construct.
  50. explicit Sample(Context* context);
  51. /// Setup before engine initialization. Modifies the engine parameters.
  52. void Setup() override;
  53. /// Setup after engine initialization. Creates the logo, console & debug HUD.
  54. void Start() override;
  55. /// Cleanup after the main loop. Called by Application.
  56. void Stop() override;
  57. protected:
  58. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  59. virtual String GetScreenJoystickPatchString() const { return String::EMPTY; }
  60. /// Initialize touch input on mobile platform.
  61. void InitTouchInput();
  62. /// Initialize mouse mode on non-web platform.
  63. void InitMouseMode(MouseMode mode);
  64. /// Control logo visibility.
  65. void SetLogoVisible(bool enable);
  66. /// Logo sprite.
  67. SharedPtr<Sprite> logoSprite_;
  68. /// Scene.
  69. SharedPtr<Scene> scene_;
  70. /// Camera scene node.
  71. SharedPtr<Node> cameraNode_;
  72. /// Camera yaw angle.
  73. float yaw_;
  74. /// Camera pitch angle.
  75. float pitch_;
  76. /// Flag to indicate whether touch input has been enabled.
  77. bool touchEnabled_;
  78. /// Mouse mode option to use in the sample.
  79. MouseMode useMouseMode_;
  80. private:
  81. /// Create logo.
  82. void CreateLogo();
  83. /// Set custom window Title & Icon
  84. void SetWindowTitleAndIcon();
  85. /// Create console and debug HUD.
  86. void CreateConsoleAndDebugHud();
  87. /// Handle request for mouse mode on web platform.
  88. void HandleMouseModeRequest(StringHash eventType, VariantMap& eventData);
  89. /// Handle request for mouse mode change on web platform.
  90. void HandleMouseModeChange(StringHash eventType, VariantMap& eventData);
  91. /// Handle key down event to process key controls common to all samples.
  92. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  93. /// Handle key up event to process key controls common to all samples.
  94. void HandleKeyUp(StringHash eventType, VariantMap& eventData);
  95. /// Handle scene update event to control camera's pitch and yaw for all samples.
  96. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  97. /// Handle touch begin event to initialize touch input on desktop platform.
  98. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  99. /// Screen joystick index for navigational controls (mobile platforms only).
  100. unsigned screenJoystickIndex_;
  101. /// Screen joystick index for settings (mobile platforms only).
  102. unsigned screenJoystickSettingsIndex_;
  103. /// Pause flag.
  104. bool paused_;
  105. };
  106. #include "Sample.inl"