Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
BaseWidget.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Development Kit"
3 // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
4 
5 #pragma once
6 
7 #ifndef NDK_BASEWIDGET_HPP
8 #define NDK_BASEWIDGET_HPP
9 
10 #include <NDK/Prerequesites.hpp>
11 #include <NDK/Entity.hpp>
12 #include <NDK/EntityOwner.hpp>
13 #include <NDK/World.hpp>
14 #include <Nazara/Graphics/Sprite.hpp>
15 #include <Nazara/Platform/Event.hpp>
16 #include <Nazara/Platform/Mouse.hpp>
17 #include <Nazara/Utility/Node.hpp>
18 #include <limits>
19 
20 namespace Ndk
21 {
22  class Canvas;
23 
24  class NDK_API BaseWidget : public Nz::Node
25  {
26  friend Canvas;
27 
28  public:
29  struct Padding;
30 
31  BaseWidget(BaseWidget* parent);
32  BaseWidget(const BaseWidget&) = delete;
33  BaseWidget(BaseWidget&&) = default;
34  virtual ~BaseWidget();
35 
36  template<typename T, typename... Args> T* Add(Args&&... args);
37  inline void AddChild(std::unique_ptr<BaseWidget>&& widget);
38 
39  inline void Center();
40  inline void CenterHorizontal();
41  inline void CenterVertical();
42 
43  inline void Destroy();
44 
45  void EnableBackground(bool enable);
46 
47  //virtual BaseWidget* Clone() const = 0;
48 
49  inline const Nz::Color& GetBackgroundColor() const;
50  inline Canvas* GetCanvas();
51  inline Nz::SystemCursor GetCursor() const;
52  inline const Padding& GetPadding() const;
53  inline Nz::Vector2f GetContentOrigin() const;
54  inline const Nz::Vector2f& GetContentSize() const;
55  inline Nz::Vector2f GetSize() const;
56 
57  inline bool IsVisible() const;
58 
59  void GrabKeyboard();
60 
61  virtual void ResizeToContent() = 0;
62 
63  void SetBackgroundColor(const Nz::Color& color);
64  void SetCursor(Nz::SystemCursor systemCursor);
65  inline void SetContentSize(const Nz::Vector2f& size);
66  inline void SetPadding(float left, float top, float right, float bottom);
67  void SetSize(const Nz::Vector2f& size);
68 
69  void Show(bool show = true);
70 
71  BaseWidget& operator=(const BaseWidget&) = delete;
72  BaseWidget& operator=(BaseWidget&&) = default;
73 
74  struct Padding
75  {
76  float left;
77  float top;
78  float right;
79  float bottom;
80  };
81 
82  protected:
83  const EntityHandle& CreateEntity();
84  void DestroyEntity(Entity* entity);
85  virtual void Layout();
86  void InvalidateNode() override;
87 
88  virtual void OnKeyPressed(const Nz::WindowEvent::KeyEvent& key);
89  virtual void OnKeyReleased(const Nz::WindowEvent::KeyEvent& key);
90  virtual void OnMouseEnter();
91  virtual void OnMouseMoved(int x, int y, int deltaX, int deltaY);
92  virtual void OnMouseButtonPress(int x, int y, Nz::Mouse::Button button);
93  virtual void OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button);
94  virtual void OnMouseExit();
95  virtual void OnParentResized(const Nz::Vector2f& newSize);
96  virtual void OnTextEntered(char32_t character, bool repeated);
97 
98  private:
99  inline BaseWidget();
100 
101  inline void DestroyChild(BaseWidget* widget);
102  void DestroyChildren();
103  inline bool IsRegisteredToCanvas() const;
104  inline void NotifyParentResized(const Nz::Vector2f& newSize);
105  void RegisterToCanvas();
106  inline void UpdateCanvasIndex(std::size_t index);
107  void UnregisterFromCanvas();
108 
109  static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max();
110 
111  std::size_t m_canvasIndex;
112  std::vector<EntityOwner> m_entities;
113  std::vector<std::unique_ptr<BaseWidget>> m_children;
114  Canvas* m_canvas;
115  EntityOwner m_backgroundEntity;
116  Padding m_padding;
117  WorldHandle m_world;
118  Nz::Color m_backgroundColor;
119  Nz::SpriteRef m_backgroundSprite;
120  Nz::SystemCursor m_cursor;
121  Nz::Vector2f m_contentSize;
122  BaseWidget* m_widgetParent;
123  bool m_visible;
124  };
125 }
126 
127 #include <NDK/BaseWidget.inl>
128 
129 #endif // NDK_BASEWIDGET_HPP
TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so...
Definition: Algorithm.hpp:12
NDK class that represents an entity in a world.
Definition: Entity.hpp:28
Core class that represents a color.
Definition: Color.hpp:18
NDK class that represents the owner of the entity and so its lifetime.
Definition: EntityOwner.hpp:14
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
Abstract class serving as a base class for all widgets.
Definition: BaseWidget.hpp:24