SuperTuxKart
widget.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 //
3 // Copyright (C) 2009-2015 Marianne Gagnon
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 #ifndef HEADER_WIDGET_HPP
21 #define HEADER_WIDGET_HPP
22 
23 #include <irrString.h>
24 namespace irr
25 {
26  namespace gui { class IGUIElement; }
27 }
28 #include <map>
29 
30 #include "guiengine/event_handler.hpp"
31 #include "guiengine/skin.hpp"
32 #include "utils/constants.hpp"
33 #include "utils/ptr_vector.hpp"
34 
35 namespace GUIEngine
36 {
37 
38  class DynamicRibbonWidget;
39  class Screen;
40 
41  enum WidgetType
42  {
43  WTYPE_NONE = -1,
44  WTYPE_RIBBON,
45  WTYPE_SPINNER,
46  WTYPE_BUTTON,
47  WTYPE_ICON_BUTTON,
48  WTYPE_CHECKBOX,
49  WTYPE_LABEL,
50  WTYPE_BUBBLE,
51  WTYPE_SPACER,
52  WTYPE_DIV,
53  WTYPE_DYNAMIC_RIBBON,
54  WTYPE_MODEL_VIEW,
55  WTYPE_LIST,
56  WTYPE_TEXTBOX,
57  WTYPE_PROGRESS,
58  WTYPE_RATINGBAR
59  };
60 
61  enum BadgeType
62  {
66  OK_BADGE = 2,
68  BAD_BADGE = 4,
78  ZIPPER_BADGE = 128,
81  };
82 
83 
84  enum Property
85  {
86  PROP_ID = 100,
87  PROP_PROPORTION,
88  PROP_WIDTH,
89  PROP_HEIGHT,
90  PROP_CHILD_WIDTH,
91  PROP_CHILD_HEIGHT,
92  PROP_WORD_WRAP,
93  PROP_ALTERNATE_BG,
94  PROP_LINE_HEIGHT,
95  //PROP_GROW_WITH_TEXT, // yet unused
96  PROP_X,
97  PROP_Y,
98  PROP_LAYOUT,
99  PROP_ALIGN,
100  // PROP_TEXT, // this one is a bit special, can't go along others since it's wide strings
101  PROP_ICON,
102  PROP_FOCUS_ICON,
103  PROP_TEXT_ALIGN,
104  PROP_TEXT_VALIGN,
105  PROP_MIN_VALUE,
106  PROP_MAX_VALUE,
107  PROP_MAX_WIDTH,
108  PROP_MAX_HEIGHT,
109  PROP_SQUARE,
110  PROP_EXTEND_LABEL,
111  PROP_LABELS_LOCATION,
112  PROP_MAX_ROWS,
113  PROP_WRAP_AROUND,
114  PROP_DIV_PADDING,
115  PROP_KEEP_SELECTION,
116  PROP_CUSTOM_RATIO,
117  PROP_ICON_ALIGN,
118  };
119 
120  bool isWithinATextBox();
121  void setWithinATextBox(bool in);
122 
139  {
140  protected:
141  unsigned int m_magic_number;
142 
143  // FIXME: find better ways than hackish "friend"?
144  friend class EventHandler;
145  friend class Screen;
146  friend class Skin;
147  friend class RibbonWidget;
148  friend class SpinnerWidget;
149  friend class ProgressBarWidget;
150  friend class DynamicRibbonWidget;
151  friend class LayoutManager;
152  friend class ModalDialog;
153  friend class AbstractTopLevelContainer;
154 
156  int m_absolute_x, m_absolute_y, m_absolute_w, m_absolute_h;
157  int m_absolute_reverse_x, m_absolute_reverse_y;
158  float m_relative_x, m_relative_y, m_relative_w, m_relative_h;
159 
162  irr::core::stringw m_text;
163 
166 
172  bool m_selected[MAX_PLAYER_COUNT];
173 
179 
186  virtual EventPropagation rightPressed(const int playerID) { return EVENT_BLOCK; }
187 
194  virtual EventPropagation leftPressed (const int playerID) { return EVENT_BLOCK; }
195 
202  virtual EventPropagation upPressed(const int playerID) { return EVENT_BLOCK; }
203 
210  virtual EventPropagation downPressed(const int playerID) { return EVENT_BLOCK; }
211 
215  virtual EventPropagation mouseHovered(Widget* child, const int playerID) { return EVENT_BLOCK; }
216 
219  virtual EventPropagation focused(const int playerID) { setWithinATextBox(false); return EVENT_LET; }
220 
222  virtual void unfocused(const int playerID, Widget* new_focus) { }
223 
227  irr::gui::IGUIElement* m_parent;
228 
232  irr::gui::IGUIElement* m_element;
233 
234 
238  int m_id;
239 
243  bool m_player_focus[MAX_PLAYER_COUNT];
244 
247 
249  WidgetType m_type;
250 
258 
260  int m_badges;
261 
264 
267 
270 
271  bool m_bottom_bar;
272  bool m_top_bar;
273 
276 
277  bool m_has_tooltip;
278  irr::core::stringw m_tooltip_text;
279 
282 
285 
286  public:
287 
296 
302 
306 
310 
314  int m_x, m_y, m_w, m_h;
315 
318 
321 
329 
338  std::map<Property, std::string> m_properties;
339 
340  Widget(WidgetType type, bool reserve_id = false);
341  virtual ~Widget();
342 
347  void setParent(irr::gui::IGUIElement* parent);
348 
355  virtual void setVisible(bool visible);
356 
366  virtual void setCollapsed(bool collapsed, Screen* calling_screen = NULL);
367 
377  virtual void setCollapsed(bool collapsed, int uncollapsed_height, Screen* calling_screen = NULL);
378 
380  bool isVisible() const;
381 
383  bool isCollapsed() const { return m_is_collapsed; }
384 
385  bool isActivated() const;
386 
387  virtual EventPropagation onActivationInput(const int playerID) { return EVENT_LET; }
388 
392  virtual void move(const int x, const int y, const int w, const int h);
393 
399  bool isSelected(const int playerID) const { return m_selected[playerID]; }
400  void setSelected(const int playerID, bool state) { m_selected[playerID] = state;}
401 
402  bool isBottomBar() const { return m_bottom_bar; }
403  bool isTopBar () const { return m_top_bar; }
404 
414  virtual void setActive(bool active=true);
415 
428  template<typename T> T* getIrrlichtElement()
429  {
430  #if HAVE_RTT
431  T* out = dynamic_cast<T*>(m_element);
432  return out;
433  #else
434  return static_cast<T*>(m_element);
435  #endif
436  }
437 
441  template<typename T> const T* getIrrlichtElement() const
442  {
443  #if HAVE_RTT
444  T* out = dynamic_cast<T*>(m_element);
445  return out;
446  #else
447  return static_cast<T*>(m_element);
448  #endif
449  }
450 
454  irr::gui::IGUIElement* getIrrlichtElement() { return m_element; }
455 
456  void moveIrrlichtElement();
457  bool isSameIrrlichtWidgetAs(const Widget* ref) const { return m_element == ref->m_element; }
458 
479  virtual void setText(const irr::core::stringw &s);
480 
482  const irr::core::stringw &getText() const {return m_text; }
483 
485  WidgetType getType() const { return m_type; }
486 
491  int getID() const { return m_id; }
492 
494  bool isFocusable() const { return m_focusable; }
495 
496  void setFocusable(bool f) { m_focusable = f; }
497 
511  void setFocusForPlayer(const int playerID);
512 
518  bool isFocusedForPlayer(const int playerID);
519 
521  void unsetFocusForPlayer(const int playerID);
522 
537  static void resetIDCounters();
538 
542  static int getNewID();
543 
547  static int getNewNoFocusID();
548 
555  static bool isFocusableId(const int id);
556 
570  const PtrVector<Widget>& getChildren() const { return m_children; }
571 
573 
584  bool deleteChild(const char* id);
585 
601  virtual void update(float delta) { }
602 
607  virtual EventPropagation transmitEvent(Widget* w,
608  const std::string& originator,
609  const int playerID)
610  { return EVENT_LET; }
611 
616  virtual void add();
617 
621  virtual void elementRemoved();
622 
623  bool searchInsideMe() const { return m_check_inside_me; }
624 
627  virtual int getWidthNeededAroundLabel() const { return 0; }
628 
631  virtual int getHeightNeededAroundLabel() const { return 0; }
632 
648  void setBadge(BadgeType badge_bit)
649  {
650  m_badges |= int(badge_bit);
651  }
652 
657  void unsetBadge(BadgeType badge_bit)
658  {
659  m_badges &= (~int(badge_bit));
660  }
661 
666  {
667  m_badges = 0;
668  }
669 
674  int getBadges() const
675  {
676  return m_badges;
677  }
678 
689  bool hasTooltip() const { return m_has_tooltip; }
690 
692  irr::core::stringw getTooltipText() const { return m_tooltip_text; }
693 
694  void setTooltip(irr::core::stringw s) { m_tooltip_text = s; m_has_tooltip = true; }
695 
700  bool ok() const { return (m_magic_number == 0xCAFEC001); }
701 
703  virtual EventPropagation onClick() { return EVENT_LET; }
704  virtual irr::core::dimension2di getDimension() const { return irr::core::dimension2di(m_w, m_h); }
705  };
706 
707 
708 }
709 #endif
A spinner or gauge widget (to select numbers / percentages).
Definition: spinner_widget.hpp:39
static bool isFocusableId(const int id)
get whether the given ID represents an ID of a widget that can be focused
Definition: widget.cpp:205
bool isFocusedForPlayer(const int playerID)
Find whether this widget is focused by a given player.
Definition: widget.cpp:278
bool m_reserve_id
Whether to reserve an ID in 'm_reserved_id' when widget is added.
Definition: widget.hpp:246
An extended version of RibbonWidget, with more capabilities.
Definition: dynamic_ribbon_widget.hpp:81
int m_uncollapsed_height
height of the widget before it was collapsed (only set if widget got collapsed)
Definition: widget.hpp:281
bool m_player_focus[MAX_PLAYER_COUNT]
Usually, only one widget at a time can be focused.
Definition: widget.hpp:243
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
int m_badge_x_shift
If a badge wouldn't look too pretty on the very side of the widget.
Definition: widget.hpp:275
virtual void move(const int x, const int y, const int w, const int h)
Call to resize/move the widget.
Definition: widget.cpp:287
display a lock on the widget, to mean a certain game feature is locked
Definition: widget.hpp:64
bool m_is_visible
A flag to indicate whether this widget should be visible or not.
Definition: widget.hpp:266
virtual EventPropagation rightPressed(const int playerID)
called when right key is pressed and focus is on widget.
Definition: widget.hpp:186
void setParent(irr::gui::IGUIElement *parent)
Set the irrlicht widget to be used as parent of this widget next time Widget::add() is invoked on thi...
Definition: widget.cpp:302
virtual EventPropagation leftPressed(const int playerID)
called when left key is pressed and focus is on widget.
Definition: widget.hpp:194
virtual EventPropagation onClick()
Gets called when the widget is active and got clicked.
Definition: widget.hpp:703
int getID() const
Get the irrlicht widget ID attributed to this widget.
Definition: widget.hpp:491
Widget * m_event_handler
This is set to NULL by default; set to something else in a widget to mean that events happening on th...
Definition: widget.hpp:295
virtual void update(float delta)
Override in children to possibly receive updates (you may need to register to them first)
Definition: widget.hpp:601
bool isSelected(const int playerID) const
Get whether this widget is selected (only makes sense in some cases where a widget is part of a bigge...
Definition: widget.hpp:399
bool m_focusable
Set to false if widget is something that should not receive focus.
Definition: widget.hpp:269
BadgeType
Definition: widget.hpp:61
static void resetIDCounters()
When switching to a new screen, this function will be called to reset ID counters (so we start again ...
Definition: widget.cpp:217
static int getNewNoFocusID()
Provides a new unique ID on each call, for widgets that can not be focused.
Definition: widget.cpp:199
A keyboard icon.
Definition: widget.hpp:74
Represents a GUI widgets container.
Definition: abstract_top_level_container.hpp:42
bool m_selected[MAX_PLAYER_COUNT]
Can be used in children to indicate whether a widget is selected or not.
Definition: widget.hpp:172
bool m_show_bounding_box
Whether to show a bounding box around this widget (used for sections)
Definition: widget.hpp:317
virtual void setText(const irr::core::stringw &s)
Sets the text of a widget from a wchar_t.
Definition: widget.cpp:124
int m_absolute_x
Used during loading, by the layout engine.
Definition: widget.hpp:156
bool isVisible() const
Returns if the element is visible.
Definition: widget.cpp:310
A zipper badge to indicate that this player receives a boost.
Definition: widget.hpp:78
virtual void setCollapsed(bool collapsed, Screen *calling_screen=NULL)
Sets the widget (and its children, if any) collapsed or not.
Definition: widget.cpp:349
int m_tab_up_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:309
const irr::core::stringw & getText() const
Returns the text of a widget.
Definition: widget.hpp:482
int m_reserved_id
Used in two cases : 1) For 'placeholder' divisions; at the time the layout is created,...
Definition: widget.hpp:328
void unsetFocusForPlayer(const int playerID)
Internal method, do not call it.
Definition: widget.cpp:265
Class to handle irrLicht events (GUI and input as well)
Definition: event_handler.hpp:65
virtual int getWidthNeededAroundLabel() const
When inferring widget size from its label length, this method will be called to if/how much space mus...
Definition: widget.hpp:627
void setFocusForPlayer(const int playerID)
Focus the widget for the given player.
Definition: widget.cpp:240
A gamepad icon.
Definition: widget.hpp:72
virtual void elementRemoved()
Called when irrLicht widgets cleared.
Definition: widget.cpp:133
bool m_is_bounding_box_round
Only used if m_show_bounding_box is true.
Definition: widget.hpp:320
const T * getIrrlichtElement() const
Get the underlying irrLicht GUI element, casted to the right type; const version.
Definition: widget.hpp:441
irr::core::stringw getTooltipText() const
Only call if hasTooltip() returned true.
Definition: widget.hpp:692
display a trophy badge on the widget, useful e.g.
Definition: widget.hpp:70
void resetAllBadges()
sets this widget to have no badge
Definition: widget.hpp:665
virtual EventPropagation upPressed(const int playerID)
called when up key is pressed and focus is on widget.
Definition: widget.hpp:202
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:265
const PtrVector< Widget > & getChildren() const
Definition: widget.hpp:570
T * getIrrlichtElement()
Get the underlying irrLicht GUI element, casted to the right type.
Definition: widget.hpp:428
A anchor badge to indicate that this player receives a handicap.
Definition: widget.hpp:80
bool m_check_inside_me
Whether to descend in the children of this widget when searching a widget from its ID or name.
Definition: widget.hpp:178
int m_badges
A bitmask of which badges to show, if any; choices are *_BADGE, defined above.
Definition: widget.hpp:260
int m_tab_down_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:305
irr::gui::IGUIElement * m_parent
An irrlicht parent (most often used to put widgets in dialogs)
Definition: widget.hpp:227
A static text/icons/tabs bar widget.
Definition: ribbon_widget.hpp:59
int m_id
numerical ID used by irrLicht to identify this widget (not the same as the string identificator speci...
Definition: widget.hpp:238
virtual EventPropagation mouseHovered(Widget *child, const int playerID)
used when you set eventSupervisors - see m_event_handler explainations below called when one of a wid...
Definition: widget.hpp:215
bool deleteChild(const char *id)
removes and deletes the child with the given PROP_ID
Definition: widget.cpp:167
bool isCollapsed() const
Returns whether the element is collapsed (through setCollapsed).
Definition: widget.hpp:383
bool m_supports_multiplayer
Whether this widget supports multiplayer interaction (i.e.
Definition: widget.hpp:301
virtual void unfocused(const int playerID, Widget *new_focus)
override in children if you need to know when the widget is unfocused.
Definition: widget.hpp:222
std::map< Property, std::string > m_properties
A map that holds values for all specified widget properties (in the XML file)
Definition: widget.hpp:338
int getBadges() const
Get which badges are currently on this widget.
Definition: widget.hpp:674
int m_x
Coordinates of the widget once added (the difference between those x/h and PROP_WIDTH/PROP_HEIGHT is ...
Definition: widget.hpp:314
Definition: ptr_vector.hpp:43
Represents a single GUI screen.
Definition: screen.hpp:94
In order to avoid calculating render information every frame, it's stored in a SkinWidgetContainer fo...
Definition: skin.hpp:141
void unsetBadge(BadgeType badge_bit)
removes a particular bade from this widget, if it had it.
Definition: widget.hpp:657
virtual void setVisible(bool visible)
Sets the widget (and its children, if any) visible or not.
Definition: widget.cpp:332
bool m_title_font
When true, this widget shall use a bigger and more colourful font.
Definition: widget.hpp:165
virtual int getHeightNeededAroundLabel() const
When inferring widget size from its label length, this method will be called to if/how much space mus...
Definition: widget.hpp:631
virtual EventPropagation focused(const int playerID)
override in children if you need to know when the widget is focused.
Definition: widget.hpp:219
PtrVector< Widget > m_children
If this widget has any children, they go here.
Definition: widget.hpp:257
bool m_deactivated
A simple flag that can be raised to deactivate this widget.
Definition: widget.hpp:263
WidgetType m_type
Type of this widget.
Definition: widget.hpp:249
virtual EventPropagation transmitEvent(Widget *w, const std::string &originator, const int playerID)
All widgets, including their parents (m_event_handler) will be notified on event through this call.
Definition: widget.hpp:607
bool isFocusable() const
Get whether this object is allowed to receive focus.
Definition: widget.hpp:494
virtual void add()
Create and add the irrLicht widget(s) associated with this object.
Definition: widget.cpp:225
Definition: layout_manager.hpp:31
display a green check on a widget, useful e.g.
Definition: widget.hpp:66
WidgetType getType() const
Definition: widget.hpp:485
display a red mark badge on the widget, useful e.g.
Definition: widget.hpp:68
irr::gui::IGUIElement * m_element
IrrLicht widget created to represent this object.
Definition: widget.hpp:232
static int getNewID()
Provides a new unique ID on each call, for widgets that can be focused.
Definition: widget.cpp:195
virtual EventPropagation downPressed(const int playerID)
called when down key is pressed and focus is on widget.
Definition: widget.hpp:210
Abstract base class representing a modal dialog.
Definition: modaldialog.hpp:55
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:138
A progress bar widget.
Definition: progress_bar_widget.hpp:35
bool m_is_collapsed
A flag to indicate whether this widget got collapsed.
Definition: widget.hpp:284
virtual void setActive(bool active=true)
Sets an widget to be either activated or deactivated (i.e.
Definition: widget.cpp:153
irr::core::stringw m_text
PROP_TEXT is a special case : since it can be translated it can't go in the map above,...
Definition: widget.hpp:162
An hourglass badge to indicate loading.
Definition: widget.hpp:76
void setBadge(BadgeType badge_bit)
adds a particular badge to this widget.
Definition: widget.hpp:648
irr::gui::IGUIElement * getIrrlichtElement()
Get the underlying irrLicht GUI element.
Definition: widget.hpp:454