TGUI  0.8.8
Gui.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_GUI_HPP
27 #define TGUI_GUI_HPP
28 
29 
30 #include <TGUI/Container.hpp>
31 #include <SFML/Graphics/RenderWindow.hpp>
32 #include <queue>
33 
35 
36 namespace tgui
37 {
42  class TGUI_API Gui
43  {
44  public:
45 
52  Gui();
53 
54 #if SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR < 5
55  Gui(sf::RenderWindow& window);
64 #endif
65 
74  Gui(sf::RenderTarget& target);
75 
76 
78  // Deleted copy constructor
80  Gui(const Gui& copy) = delete;
81 
82 
84  // Deleted assignment operator overload
86  Gui& operator=(const Gui& right) = delete;
87 
88 #if SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR < 5
89  void setTarget(sf::RenderWindow& window);
96 #endif
97 
104  void setTarget(sf::RenderTarget& target);
105 
106 
113  sf::RenderTarget* getTarget() const;
114 
115 
122  void setView(const sf::View& view);
123 
124 
131  const sf::View& getView() const;
132 
133 
145  bool handleEvent(sf::Event event);
146 
147 
148 #ifndef TGUI_REMOVE_DEPRECATED_CODE
149  TGUI_DEPRECATED("Use setTabKeyUsageEnabled instead") void enableTabKeyUsage();
153 
154 
158  TGUI_DEPRECATED("Use setTabKeyUsageEnabled instead") void disableTabKeyUsage();
159 #endif
160 
164  void setTabKeyUsageEnabled(bool enabled);
165 
166 
170  bool isTabKeyUsageEnabled() const;
171 
172 
177  void draw();
178 
179 
191  GuiContainer::Ptr getContainer() const;
192 
193 
200  void setFont(const Font& font);
201 
202 
209  std::shared_ptr<sf::Font> getFont() const;
210 
211 
218  const std::vector<Widget::Ptr>& getWidgets() const;
219 
220 #ifndef TGUI_REMOVE_DEPRECATED_CODE
221  TGUI_DEPRECATED("Use getWidgets() and Widget::getWidgetName instead") const std::vector<sf::String> getWidgetNames() const;
228 #endif
229 
239  void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
240 
241 
255  Widget::Ptr get(const sf::String& widgetName) const;
256 
257 
272  template <class T>
273  typename T::Ptr get(const sf::String& widgetName) const
274  {
275  return m_container->get<T>(widgetName);
276  }
277 
278 
285  bool remove(const Widget::Ptr& widget);
286 
287 
293 
294 
304 
305 
315 
316 
317 #ifndef TGUI_REMOVE_DEPRECATED_CODE
318  TGUI_DEPRECATED("Use Widget::setWidgetName instead") bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
328 
329 
338  TGUI_DEPRECATED("Use Widget::getWidgetName instead") std::string getWidgetName(const Widget::Ptr& widget) const;
339 #endif
340 
346 
347 
353 
354 
359 
360 
366  void moveWidgetToFront(const Widget::Ptr& widget);
367 
368 
374  void moveWidgetToBack(const Widget::Ptr& widget);
375 
376 
382 
383 
390  void setOpacity(float opacity);
391 
392 
399  float getOpacity() const;
400 
401 
410  void setTextSize(unsigned int size);
411 
412 
418  unsigned int getTextSize() const;
419 
420 
427  void loadWidgetsFromFile(const std::string& filename, bool replaceExisting = true);
428 
429 
436  void saveWidgetsToFile(const std::string& filename);
437 
438 
445  void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
446 
447 
454  void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
455 
456 
463  void saveWidgetsToStream(std::stringstream& stream) const;
464 
465 
471  void setDrawingUpdatesTime(bool drawUpdatesTime);
472 
473 
481  bool updateTime();
482 
483 
486  // Updates the internal clock to make animation possible. This function is called automatically by the draw function.
487  // You will thus only need to call it yourself when you are drawing everything manually.
489  bool updateTime(const sf::Time& elapsedTime);
490 
491 
493  private:
494 
496  // Code that has to be run in each constructor
498  void init();
499 
500 
502  protected:
503 
504  // The internal clock which is used for animation of widgets
505  sf::Clock m_clock;
506 
507  // The sfml render target to draw on
508  sf::RenderTarget* m_target;
509  bool m_windowFocused = true;
510 
511  #if SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR < 5
512  // Does m_target contains a sf::RenderWindow?
513  bool m_accessToWindow;
514  #endif
515 
516  // Internal container to store all widgets
517  GuiContainer::Ptr m_container = std::make_shared<GuiContainer>();
518 
519  Widget::Ptr m_visibleToolTip = nullptr;
520  sf::Time m_tooltipTime;
521  bool m_tooltipPossible = false;
522  Vector2f m_lastMousePos;
523 
524  sf::View m_view{{0, 0, 1, 1}};
525 
526  bool m_drawUpdatesTime = true;
527  bool m_TabKeyUsageEnabled = true;
528 
529 
531  };
532 
534 
535 }
536 
538 
539 #endif // TGUI_GUI_HPP
Definition: Font.hpp:43
sf::RenderTarget * getTarget() const
Returns the render target on which the gui is being drawn.
Gui class.
Definition: Gui.hpp:43
T::Ptr get(const sf::String &widgetName) const
Returns a pointer to an earlier created widget.
Definition: Gui.hpp:273
void setTarget(sf::RenderTarget &target)
Sets the target on which the gui should be drawn.
void loadWidgetsFromFile(const std::string &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
Widget::Ptr get(const sf::String &widgetName) const
Returns a pointer to an earlier created widget.
void setTextSize(unsigned int size)
Changes the character size of all existing and future child widgets.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets.
bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
Gui(sf::RenderTarget &target)
Constructs the gui and set the target on which the gui should be drawn.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
void removeAllWidgets()
Removes all widgets that were added to the container.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
GuiContainer::Ptr getContainer() const
Returns the internal container of the Gui.
unsigned int getTextSize() const
Returns the character size for future child widgets (and for existing widgets where the size wasn't c...
void setFont(const Font &font)
Changes the global font.
void setTabKeyUsageEnabled(bool enabled)
When the tab key usage is enabled, pressing tab will focus another widget.
bool focusPreviousWidget()
Focuses the previous widget in the gui.
Gui()
Default constructor.
void uncheckRadioButtons()
Unchecks all the radio buttons.
void setView(const sf::View &view)
Changes the view that is used by the gui.
void saveWidgetsToFile(const std::string &filename)
Saves the child widgets to a text file.
void setOpacity(float opacity)
Changes the opacity of all widgets.
bool focusNextWidget()
Focuses the next widget in the gui.
std::shared_ptr< sf::Font > getFont() const
Returns the global font.
bool handleEvent(sf::Event event)
Passes the event to the widgets.
Wrapper class to store strings.
Definition: String.hpp:119
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
Definition: Vector2f.hpp:39
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
The parent class for every widget.
Definition: Widget.hpp:74
bool updateTime()
Updates the internal clock (for animations and blinking edit cursors)
float getOpacity() const
Returns the opacity of all the widgets.
void unfocusAllWidgets()
Unfocus all the widgets.
void saveWidgetsToStream(std::stringstream &stream) const
Saves this the child widgets to a text file.
const sf::View & getView() const
Returns the view that is currently used by the gui.
void setDrawingUpdatesTime(bool drawUpdatesTime)
Sets whether drawing the gui will automatically update the internal clock or whether the user does it...
bool isTabKeyUsageEnabled() const
Returns whether the tab key usage is enabled (if so, pressing tab will focus another widget)
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
void draw()
Draws all the widgets that were added to the gui.
void add(const Widget::Ptr &widgetPtr, const sf::String &widgetName="")
Adds a widget to the container.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets.