Sample.h 3.5 KB

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