HelloGUI.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2008-2021 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 "Sample.h"
  24. namespace Urho3D
  25. {
  26. class Window;
  27. }
  28. /// A simple 'HelloWorld' GUI created purely from code.
  29. /// This sample demonstrates:
  30. /// - Creation of controls and building a UI hierarchy
  31. /// - Loading UI style from XML and applying it to controls
  32. /// - Handling of global and per-control events
  33. /// For more advanced users (beginners can skip this section):
  34. /// - Dragging UIElements
  35. /// - Displaying tooltips
  36. /// - Accessing available Events data (eventData)
  37. class HelloGUI : public Sample
  38. {
  39. URHO3D_OBJECT(HelloGUI, Sample);
  40. public:
  41. /// Construct.
  42. explicit HelloGUI(Context* context);
  43. /// Setup after engine initialization and before running the main loop.
  44. void Start() override;
  45. protected:
  46. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  47. String GetScreenJoystickPatchString() const override { return
  48. "<patch>"
  49. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  50. " <attribute name=\"Is Visible\" value=\"false\" />"
  51. " </add>"
  52. "</patch>";
  53. }
  54. private:
  55. /// Create and initialize a Window control.
  56. void InitWindow();
  57. /// Create and add various common controls for demonstration purposes.
  58. void InitControls();
  59. /// Create a draggable fish button.
  60. void CreateDraggableFish();
  61. /// Handle drag begin for the fish button.
  62. void HandleDragBegin(StringHash eventType, VariantMap& eventData);
  63. /// Handle drag move for the fish button.
  64. void HandleDragMove(StringHash eventType, VariantMap& eventData);
  65. /// Handle drag end for the fish button.
  66. void HandleDragEnd(StringHash eventType, VariantMap& eventData);
  67. /// Handle any UI control being clicked.
  68. void HandleControlClicked(StringHash eventType, VariantMap& eventData);
  69. /// Handle close button pressed and released.
  70. void HandleClosePressed(StringHash eventType, VariantMap& eventData);
  71. /// The Window.
  72. SharedPtr<Window> window_;
  73. /// The UI's root UIElement.
  74. SharedPtr<UIElement> uiRoot_;
  75. /// Remembered drag begin position.
  76. IntVector2 dragBeginPosition_;
  77. };