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 <bitset>
29 #include <map>
30 
31 #include "guiengine/event_handler.hpp"
32 #include "guiengine/skin.hpp"
33 #include "utils/constants.hpp"
34 #include "utils/ptr_vector.hpp"
35 
36 namespace GUIEngine
37 {
38 
39  class DynamicRibbonWidget;
40  class Screen;
41 
42  enum WidgetType
43  {
44  WTYPE_NONE = -1,
45  WTYPE_RIBBON,
46  WTYPE_SPINNER,
47  WTYPE_BUTTON,
48  WTYPE_ICON_BUTTON,
49  WTYPE_CHECKBOX,
50  WTYPE_LABEL,
51  WTYPE_BUBBLE,
52  WTYPE_SPACER,
53  WTYPE_DIV,
54  WTYPE_DYNAMIC_RIBBON,
55  WTYPE_MODEL_VIEW,
56  WTYPE_LIST,
57  WTYPE_TEXTBOX,
58  WTYPE_PROGRESS,
59  WTYPE_RATINGBAR
60  };
61 
62  enum BadgeType
63  {
67  OK_BADGE = 2,
69  BAD_BADGE = 4,
79  ZIPPER_BADGE = 128,
81  ANCHOR_BADGE = 256,
83  DOWN_BADGE = 512
84  };
85 
86 
87  enum Property
88  {
89  PROP_ID = 100,
90  PROP_PROPORTION,
91  PROP_WIDTH,
92  PROP_HEIGHT,
93  PROP_CHILD_WIDTH,
94  PROP_CHILD_HEIGHT,
95  PROP_WORD_WRAP,
96  PROP_ALTERNATE_BG,
97  PROP_LINE_HEIGHT,
98  //PROP_GROW_WITH_TEXT, // yet unused
99  PROP_X,
100  PROP_Y,
101  PROP_LAYOUT,
102  PROP_ALIGN,
103  // PROP_TEXT, // this one is a bit special, can't go along others since it's wide strings
104  PROP_ICON,
105  PROP_FOCUS_ICON,
106  PROP_TEXT_ALIGN,
107  PROP_TEXT_VALIGN,
108  PROP_MIN_VALUE,
109  PROP_MAX_VALUE,
110  PROP_MAX_WIDTH,
111  PROP_MAX_HEIGHT,
112  PROP_SQUARE,
113  PROP_EXTEND_LABEL,
114  PROP_LABELS_LOCATION,
115  PROP_MAX_ROWS,
116  PROP_WRAP_AROUND,
117  PROP_DIV_PADDING,
118  PROP_KEEP_SELECTION,
119  PROP_CUSTOM_RATIO,
120  PROP_ICON_ALIGN,
121  PROP_COLOR_SLIDER, // this one is used to identify if a gauge is used as the color slider
122  };
123 
124  bool isWithinATextBox();
125  void setWithinATextBox(bool in);
126 
143  {
144  protected:
145  unsigned int m_magic_number;
146 
147  // Used to limit event callback on this widget based on input type
148  // At the moment used for continue button in kart selection
149  std::bitset<Input::IT_LAST + 1> m_active_event_callback;
150 
151  // FIXME: find better ways than hackish "friend"?
152  friend class EventHandler;
153  friend class Screen;
154  friend class Skin;
155  friend class RibbonWidget;
156  friend class SpinnerWidget;
157  friend class ProgressBarWidget;
158  friend class DynamicRibbonWidget;
159  friend class LayoutManager;
160  friend class ModalDialog;
161  friend class AbstractTopLevelContainer;
162 
164  int m_absolute_x, m_absolute_y, m_absolute_w, m_absolute_h;
165  int m_absolute_reverse_x, m_absolute_reverse_y;
166  float m_relative_x, m_relative_y, m_relative_w, m_relative_h;
167 
170  irr::core::stringw m_text;
171 
174 
180  bool m_selected[MAX_PLAYER_COUNT];
181 
187 
194  virtual EventPropagation rightPressed(const int playerID) { return EVENT_BLOCK; }
195 
202  virtual EventPropagation leftPressed (const int playerID) { return EVENT_BLOCK; }
203 
210  virtual EventPropagation upPressed(const int playerID) { return EVENT_BLOCK; }
211 
218  virtual EventPropagation downPressed(const int playerID) { return EVENT_BLOCK; }
219 
223  virtual EventPropagation mouseHovered(Widget* child, const int playerID) { return EVENT_BLOCK; }
224 
227  virtual EventPropagation focused(const int playerID) { setWithinATextBox(false); return EVENT_LET; }
228 
230  virtual void unfocused(const int playerID, Widget* new_focus) { }
231 
235  irr::gui::IGUIElement* m_parent;
236 
240  irr::gui::IGUIElement* m_element;
241 
242 
246  int m_id;
247 
251  bool m_player_focus[MAX_PLAYER_COUNT];
252 
255 
257  WidgetType m_type;
258 
266 
268  int m_badges;
269 
272 
275 
278 
279  bool m_bottom_bar;
280  bool m_top_bar;
281 
284 
285  bool m_has_tooltip;
286  irr::core::stringw m_tooltip_text;
287 
290 
293 
294  public:
295 
304 
310 
314 
318 
322  int m_x, m_y, m_w, m_h;
323 
326 
329 
337 
346  std::map<Property, std::string> m_properties;
347 
348  Widget(WidgetType type, bool reserve_id = false);
349  virtual ~Widget();
350 
354  virtual void setEventCallbackActive(Input::InputType type, bool active);
355 
359  virtual bool isEventCallbackActive(Input::InputType type) const;
360 
365  void setParent(irr::gui::IGUIElement* parent);
366 
373  virtual void setVisible(bool visible);
374 
384  virtual void setCollapsed(bool collapsed, Screen* calling_screen = NULL);
385 
395  virtual void setCollapsed(bool collapsed, int uncollapsed_height, Screen* calling_screen = NULL);
396 
398  bool isVisible() const;
399 
401  bool isCollapsed() const { return m_is_collapsed; }
402 
403  bool isActivated() const;
404 
405  virtual EventPropagation onActivationInput(const int playerID) { return EVENT_LET; }
406 
410  virtual void move(const int x, const int y, const int w, const int h);
411 
417  bool isSelected(const int playerID) const { return m_selected[playerID]; }
418  void setSelected(const int playerID, bool state) { m_selected[playerID] = state;}
419 
420  bool isBottomBar() const { return m_bottom_bar; }
421  bool isTopBar () const { return m_top_bar; }
422 
432  virtual void setActive(bool active=true);
433 
446  template<typename T> T* getIrrlichtElement()
447  {
448  #if HAVE_RTT
449  T* out = dynamic_cast<T*>(m_element);
450  return out;
451  #else
452  return static_cast<T*>(m_element);
453  #endif
454  }
455 
459  template<typename T> const T* getIrrlichtElement() const
460  {
461  #if HAVE_RTT
462  T* out = dynamic_cast<T*>(m_element);
463  return out;
464  #else
465  return static_cast<T*>(m_element);
466  #endif
467  }
468 
472  irr::gui::IGUIElement* getIrrlichtElement() { return m_element; }
473 
474  void moveIrrlichtElement();
475  bool isSameIrrlichtWidgetAs(const Widget* ref) const { return m_element == ref->m_element; }
476 
497  virtual void setText(const irr::core::stringw &s);
498 
500  const irr::core::stringw &getText() const {return m_text; }
501 
503  WidgetType getType() const { return m_type; }
504 
509  int getID() const { return m_id; }
510 
512  bool isFocusable() const { return m_focusable; }
513 
514  void setFocusable(bool f) { m_focusable = f; }
515 
529  void setFocusForPlayer(const int playerID);
530 
536  bool isFocusedForPlayer(const int playerID);
537 
539  void unsetFocusForPlayer(const int playerID);
540 
555  static void resetIDCounters();
556 
560  static int getNewID();
561 
565  static int getNewNoFocusID();
566 
573  static bool isFocusableId(const int id);
574 
588  const PtrVector<Widget>& getChildren() const { return m_children; }
589 
590  PtrVector<Widget>& getChildren() { return m_children; }
591 
602  bool deleteChild(const char* id);
603 
619  virtual void update(float delta) { }
620 
625  virtual EventPropagation transmitEvent(Widget* w,
626  const std::string& originator,
627  const int playerID)
628  { return EVENT_LET; }
629 
634  virtual void add();
635 
639  virtual void elementRemoved();
640 
641  bool searchInsideMe() const { return m_check_inside_me; }
642 
645  virtual int getWidthNeededAroundLabel() const { return 0; }
646 
649  virtual int getHeightNeededAroundLabel() const { return 0; }
650 
666  void setBadge(BadgeType badge_bit)
667  {
668  m_badges |= int(badge_bit);
669  }
670 
675  void unsetBadge(BadgeType badge_bit)
676  {
677  m_badges &= (~int(badge_bit));
678  }
679 
684  {
685  m_badges = 0;
686  }
687 
692  int getBadges() const
693  {
694  return m_badges;
695  }
696 
707  bool hasTooltip() const { return m_has_tooltip; }
708 
710  irr::core::stringw getTooltipText() const { return m_tooltip_text; }
711 
712  void setTooltip(irr::core::stringw s) { m_tooltip_text = s; m_has_tooltip = true; }
713 
718  bool ok() const { return (m_magic_number == 0xCAFEC001); }
719 
721  virtual EventPropagation onClick() { return EVENT_LET; }
722  virtual irr::core::dimension2di getDimension() const { return irr::core::dimension2di(m_w, m_h); }
723  };
724 
725 
726 }
727 #endif
A spinner or gauge widget (to select numbers / percentages).
Definition: spinner_widget.hpp:40
bool m_reserve_id
Whether to reserve an ID in &#39;m_reserved_id&#39; when widget is added.
Definition: widget.hpp:254
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:289
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
int m_badge_x_shift
If a badge wouldn&#39;t look too pretty on the very side of the widget.
Definition: widget.hpp:283
display a lock on the widget, to mean a certain game feature is locked
Definition: widget.hpp:65
bool m_is_visible
A flag to indicate whether this widget should be visible or not.
Definition: widget.hpp:274
virtual EventPropagation rightPressed(const int playerID)
called when right key is pressed and focus is on widget.
Definition: widget.hpp:194
virtual EventPropagation leftPressed(const int playerID)
called when left key is pressed and focus is on widget.
Definition: widget.hpp:202
virtual EventPropagation onClick()
Gets called when the widget is active and got clicked.
Definition: widget.hpp:721
int getID() const
Get the irrlicht widget ID attributed to this widget.
Definition: widget.hpp:509
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:303
virtual void update(float delta)
Override in children to possibly receive updates (you may need to register to them first) ...
Definition: widget.hpp:619
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:417
bool m_focusable
Set to false if widget is something that should not receive focus.
Definition: widget.hpp:277
BadgeType
Definition: widget.hpp:62
Definition: three_d_animation.hpp:32
A keyboard icon.
Definition: widget.hpp:75
Represents a GUI widgets container.
Definition: abstract_top_level_container.hpp:42
bool m_show_bounding_box
Whether to show a bounding box around this widget (used for sections)
Definition: widget.hpp:325
int m_absolute_x
Used during loading, by the layout engine.
Definition: widget.hpp:164
A zipper badge to indicate that this player receives a boost.
Definition: widget.hpp:79
int m_tab_up_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:317
const irr::core::stringw & getText() const
Returns the text of a widget.
Definition: widget.hpp:500
int m_reserved_id
Used in two cases : 1) For &#39;placeholder&#39; divisions; at the time the layout is created, there is nothing to place there yet, but we know there eventually will.
Definition: widget.hpp:336
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:645
A gamepad icon.
Definition: widget.hpp:73
bool m_is_bounding_box_round
Only used if m_show_bounding_box is true.
Definition: widget.hpp:328
const T * getIrrlichtElement() const
Get the underlying irrLicht GUI element, casted to the right type; const version. ...
Definition: widget.hpp:459
irr::core::stringw getTooltipText() const
Only call if hasTooltip() returned true.
Definition: widget.hpp:710
display a trophy badge on the widget, useful e.g.
Definition: widget.hpp:71
void resetAllBadges()
sets this widget to have no badge
Definition: widget.hpp:683
virtual EventPropagation upPressed(const int playerID)
called when up key is pressed and focus is on widget.
Definition: widget.hpp:210
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:268
const PtrVector< Widget > & getChildren() const
Definition: widget.hpp:588
T * getIrrlichtElement()
Get the underlying irrLicht GUI element, casted to the right type.
Definition: widget.hpp:446
bool isFocusedForPlayer(const Widget *w, const unsigned int playerID)
Returns whether given the widget is currently focused by given player.
Definition: engine.cpp:805
A anchor badge to indicate that this player receives a handicap.
Definition: widget.hpp:81
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:186
int m_badges
A bitmask of which badges to show, if any; choices are *_BADGE, defined above.
Definition: widget.hpp:268
int m_tab_down_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:313
irr::gui::IGUIElement * m_parent
An irrlicht parent (most often used to put widgets in dialogs)
Definition: widget.hpp:235
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:246
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:223
bool isCollapsed() const
Returns whether the element is collapsed (through setCollapsed).
Definition: widget.hpp:401
bool m_supports_multiplayer
Whether this widget supports multiplayer interaction (i.e.
Definition: widget.hpp:309
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:230
std::map< Property, std::string > m_properties
A map that holds values for all specified widget properties (in the XML file)
Definition: widget.hpp:346
int getBadges() const
Get which badges are currently on this widget.
Definition: widget.hpp:692
int m_x
Coordinates of the widget once added (the difference between those x/h and PROP_WIDTH/PROP_HEIGHT is ...
Definition: widget.hpp:322
Definition: ptr_vector.hpp:43
Represents a single GUI screen.
Definition: screen.hpp:96
In order to avoid calculating render information every frame, it&#39;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:675
bool m_title_font
When true, this widget shall use a bigger and more colourful font.
Definition: widget.hpp:173
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:649
virtual EventPropagation focused(const int playerID)
override in children if you need to know when the widget is focused.
Definition: widget.hpp:227
PtrVector< Widget > m_children
If this widget has any children, they go here.
Definition: widget.hpp:265
A down arrow badge to indicate new addons for downloading.
Definition: widget.hpp:83
bool m_deactivated
A simple flag that can be raised to deactivate this widget.
Definition: widget.hpp:271
WidgetType m_type
Type of this widget.
Definition: widget.hpp:257
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:625
bool isFocusable() const
Get whether this object is allowed to receive focus.
Definition: widget.hpp:512
Definition: layout_manager.hpp:31
display a green check on a widget, useful e.g.
Definition: widget.hpp:67
WidgetType getType() const
Definition: widget.hpp:503
display a red mark badge on the widget, useful e.g.
Definition: widget.hpp:69
irr::gui::IGUIElement * m_element
IrrLicht widget created to represent this object.
Definition: widget.hpp:240
virtual EventPropagation downPressed(const int playerID)
called when down key is pressed and focus is on widget.
Definition: widget.hpp:218
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:142
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:292
irr::core::stringw m_text
PROP_TEXT is a special case : since it can be translated it can&#39;t go in the map above, which uses narrow strings.
Definition: widget.hpp:170
An hourglass badge to indicate loading.
Definition: widget.hpp:77
void setBadge(BadgeType badge_bit)
adds a particular badge to this widget.
Definition: widget.hpp:666
irr::gui::IGUIElement * getIrrlichtElement()
Get the underlying irrLicht GUI element.
Definition: widget.hpp:472