TGUI  0.8.8
ChildWindow.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_CHILD_WINDOW_HPP
27 #define TGUI_CHILD_WINDOW_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Container.hpp>
32 #include <TGUI/Widgets/Button.hpp>
33 #include <TGUI/Renderers/ChildWindowRenderer.hpp>
34 #include <limits>
35 
37 
38 namespace tgui
39 {
43  class TGUI_API ChildWindow : public Container
44  {
45  public:
46 
47  typedef std::shared_ptr<ChildWindow> Ptr;
48  typedef std::shared_ptr<const ChildWindow> ConstPtr;
49 
50 
52  enum class TitleAlignment
53  {
54  Left,
55  Center,
56  Right
57  };
58 
59 
62  {
63  None = 0,
64  Close = 1 << 0,
65  Maximize = 1 << 1,
66  Minimize = 1 << 2
67  };
68 
70  // Default constructor
72  ChildWindow(const sf::String& title = "", unsigned int titleButtons = TitleButton::Close);
73 
74 
81  static ChildWindow::Ptr create(const sf::String& title = "", unsigned int titleButtons = TitleButton::Close);
82 
83 
93 
94 
100  const ChildWindowRenderer* getSharedRenderer() const;
101 
108  const ChildWindowRenderer* getRenderer() const;
109 
110 
123  void setPosition(const Layout2d& position) override;
124  using Widget::setPosition;
125 
126 
135  void setSize(const Layout2d& size) override;
136  using Widget::setSize;
137 
138 
147  Vector2f getFullSize() const override;
148 
149 
160 
161 
171 
172 
183 
184 
194 
195 
202  void setTitle(const sf::String& title);
203 
204 
211  const sf::String& getTitle() const;
212 
213 
219  void setTitleTextSize(unsigned int size);
220 
221 
226  unsigned int getTitleTextSize() const;
227 
228 
236 
237 
245 
246 
259  void setTitleButtons(unsigned int buttons);
260 
261 
268  unsigned int getTitleButtons() const;
269 
270 
276  void close();
277 
278 
287  void destroy();
288 
289 
296  void setResizable(bool resizable = true);
297 
298 
305  bool isResizable() const;
306 
307 
314  void setPositionLocked(bool positionLocked = true);
315 
316 
321  bool isPositionLocked() const;
322 
323 
331  void setKeepInParent(bool enabled = true);
332 
333 
341  bool isKeptInParent() const;
342 
343 
351  Vector2f getChildWidgetsOffset() const override;
352 
353 
359  void setParent(Container* parent) override;
360 
361 
368  bool mouseOnWidget(Vector2f pos) const override;
369 
373  void leftMousePressed(Vector2f pos) override;
374 
378  void leftMouseReleased(Vector2f pos) override;
379 
383  void rightMousePressed(Vector2f pos) override;
384 
388  void rightMouseReleased(Vector2f pos) override;
389 
393  void mouseMoved(Vector2f pos) override;
394 
398  void keyPressed(const sf::Event::KeyEvent& event) override;
399 
403  void mouseNoLongerOnWidget() override;
404 
408  void leftMouseButtonNoLongerDown() override;
409 
410 
418  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
419 
420 
422  protected:
423 
425  // Updates the title bar texture, text and buttons after the title bar height has changed.
427  void updateTitleBarHeight();
428 
429 
439  Signal& getSignal(std::string signalName) override;
440 
441 
448  void rendererChanged(const std::string& property) override;
449 
450 
454  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
455 
456 
460  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
461 
462 
464  // Makes a copy of the widget
466  Widget::Ptr clone() const override
467  {
468  return std::make_shared<ChildWindow>(*this);
469  }
470 
471 
473  public:
474 
475  Signal onMousePress = {"MousePressed"};
476  SignalChildWindow onClose = {"Closed"};
477  SignalChildWindow onMinimize = {"Minimized"};
478  SignalChildWindow onMaximize = {"Maximized"};
479  SignalChildWindow onEscapeKeyPressed = {"EscapeKeyPressed"};
480 
481 
483  protected:
484 
485  enum ResizeDirection
486  {
487  ResizeNone = 0,
488  ResizeLeft = 1,
489  ResizeTop = 2,
490  ResizeRight = 4,
491  ResizeBottom = 8
492  };
493 
494 
496  protected:
497 
498  Text m_titleText;
499  Vector2f m_draggingPosition;
500  Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
501  Vector2f m_minimumSize = {0, 0};
502  TitleAlignment m_titleAlignment = TitleAlignment::Center;
503  unsigned int m_titleButtons = TitleButton::Close;
504  unsigned int m_titleTextSize = 0;
505 
506  CopiedSharedPtr<Button> m_closeButton;
507  CopiedSharedPtr<Button> m_minimizeButton;
508  CopiedSharedPtr<Button> m_maximizeButton;
509 
510  bool m_mouseDownOnTitleBar = false;
511  bool m_keepInParent = false;
512 
513  bool m_positionLocked = false;
514  bool m_resizable = false;
515  int m_resizeDirection = ResizeNone;
516 
517  Sprite m_spriteTitleBar;
518  Sprite m_spriteBackground;
519 
520  // Cached renderer properties
521  Borders m_bordersCached;
522  Color m_borderColorCached;
523  Color m_borderColorFocusedCached;
524  Color m_titleColorCached;
525  Color m_titleBarColorCached;
526  Color m_backgroundColorCached;
527  float m_titleBarHeightCached = 20;
528  float m_borderBelowTitleBarCached = 0;
529  float m_distanceToSideCached = 0;
530  float m_paddingBetweenButtonsCached = 0;
531  float m_minimumResizableBorderWidthCached = 5;
532  bool m_showTextOnTitleButtonsCached = false;
533 
535  };
536 
538 }
539 
541 
542 #endif // TGUI_CHILD_WINDOW_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
void destroy()
Destroys the window.
Container widget.
Definition: Container.hpp:48
void close()
Try to close the window.
TitleButton
Title buttons (use bitwise OR to combine)
Definition: ChildWindow.hpp:62
static ChildWindow::Ptr create(const sf::String &title="", unsigned int titleButtons=TitleButton::Close)
Creates a new child window widget.
const sf::String & getTitle() const
Returns the title that is displayed in the title bar of the child window.
void setPosition(const Layout2d &position) override
Sets the position of the widget.
void setTitle(const sf::String &title)
Changes the title that is displayed in the title bar of the child window.
void setResizable(bool resizable=true)
Changes whether the child window can be resized by dragging its borders or not.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:505
ChildWindowRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setTitleAlignment(TitleAlignment alignment)
Changes the title alignment.
Vector2f getMinimumSize() const
Returns the minimum size of the child window.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void setKeepInParent(bool enabled=true)
Sets the child window to be kept inside its parent.
bool isResizable() const
Checks whether the child window can be resized by dragging its borders or not.
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Class to store the position or size of a widget.
Definition: Layout.hpp:260
TitleAlignment
Title alignments, possible options for the setTitleAlignment function.
Definition: ChildWindow.hpp:53
void setMaximumSize(Vector2f size)
Sets the maximum size of the child window.
void setTitleTextSize(unsigned int size)
Changes the character size of the title.
bool isKeptInParent() const
Tells whether the child window is kept inside its parent.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChildWindow.hpp:466
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition: ChildWindow.hpp:48
ChildWindowRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Vector2f getChildWidgetsOffset() const override
Returns the distance between the position of the container and a widget that would be drawn inside th...
static ChildWindow::Ptr copy(ChildWindow::ConstPtr childWindow)
Makes a copy of another child window.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
unsigned int getTitleButtons() const
Returns the title bar buttons.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setMinimumSize(Vector2f size)
Sets the minimum size of the child window.
Child window widget.
Definition: ChildWindow.hpp:44
Definition: ChildWindowRenderer.hpp:37
TitleAlignment getTitleAlignment() const
Returns the title alignment.
Definition: Vector2f.hpp:39
void setSize(const Layout2d &size) override
Changes the size of the child window.
Vector2f getMaximumSize() const
Returns the maximum size of the child window.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Vector2f getFullSize() const override
Returns the size of the full child window.
std::shared_ptr< ChildWindow > Ptr
Shared widget pointer.
Definition: ChildWindow.hpp:47
void setPositionLocked(bool positionLocked=true)
Changes whether the child window can be moved by dragging its title bar or not.
void setTitleButtons(unsigned int buttons)
Changes the title buttons.
unsigned int getTitleTextSize() const
Returns the character size of the title.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
bool isPositionLocked() const
Checks whether the child window can be moved by dragging its title bar or not.