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,
85  HEART_BADGE = 1024,
87  REDDOT_BADGE = 2048
88  };
89 
90 
91  enum Property
92  {
93  PROP_ID = 100,
94  PROP_PROPORTION,
95  PROP_WIDTH,
96  PROP_HEIGHT,
97  PROP_CHILD_WIDTH,
98  PROP_CHILD_HEIGHT,
99  PROP_WORD_WRAP,
100  PROP_ALTERNATE_BG,
101  PROP_LINE_HEIGHT,
102  //PROP_GROW_WITH_TEXT, // yet unused
103  PROP_X,
104  PROP_Y,
105  PROP_LAYOUT,
106  PROP_ALIGN,
107  // PROP_TEXT, // this one is a bit special, can't go along others since it's wide strings
108  PROP_ICON,
109  PROP_FOCUS_ICON,
110  PROP_TEXT_ALIGN,
111  PROP_TEXT_VALIGN,
112  PROP_MIN_VALUE,
113  PROP_MAX_VALUE,
114  PROP_MAX_WIDTH,
115  PROP_MAX_HEIGHT,
116  PROP_SQUARE,
117  PROP_EXTEND_LABEL,
118  PROP_LABELS_LOCATION,
119  PROP_MAX_ROWS,
120  PROP_WRAP_AROUND,
121  PROP_DIV_PADDING,
122  PROP_KEEP_SELECTION,
123  PROP_CUSTOM_RATIO,
124  PROP_ICON_ALIGN,
125  PROP_COLOR_SLIDER, // this one is used to identify if a gauge is used as the color slider
126  };
127 
128  bool isWithinATextBox();
129  void setWithinATextBox(bool in);
130 
147  {
148  protected:
149  unsigned int m_magic_number;
150 
151  // Used to limit event callback on this widget based on input type
152  // At the moment used for continue button in kart selection
153  std::bitset<Input::IT_LAST + 1> m_active_event_callback;
154 
155  // FIXME: find better ways than hackish "friend"?
156  friend class EventHandler;
157  friend class Screen;
158  friend class Skin;
159  friend class RibbonWidget;
160  friend class SpinnerWidget;
161  friend class ProgressBarWidget;
162  friend class DynamicRibbonWidget;
163  friend class LayoutManager;
164  friend class ModalDialog;
165  friend class AbstractTopLevelContainer;
166 
168  int m_absolute_x, m_absolute_y, m_absolute_w, m_absolute_h;
169  int m_absolute_reverse_x, m_absolute_reverse_y;
170  float m_relative_x, m_relative_y, m_relative_w, m_relative_h;
171 
174  irr::core::stringw m_text;
175 
178 
184  bool m_selected[MAX_PLAYER_COUNT];
185 
191 
198  virtual EventPropagation rightPressed(const int playerID) { return EVENT_BLOCK; }
199 
206  virtual EventPropagation leftPressed (const int playerID) { return EVENT_BLOCK; }
207 
214  virtual EventPropagation upPressed(const int playerID) { return EVENT_BLOCK; }
215 
222  virtual EventPropagation downPressed(const int playerID) { return EVENT_BLOCK; }
223 
227  virtual EventPropagation mouseHovered(Widget* child, const int playerID) { return EVENT_BLOCK; }
228 
231  virtual EventPropagation focused(const int playerID) { setWithinATextBox(false); return EVENT_LET; }
232 
234  virtual void unfocused(const int playerID, Widget* new_focus) { }
235 
239  irr::gui::IGUIElement* m_parent;
240 
244  irr::gui::IGUIElement* m_element;
245 
246 
250  int m_id;
251 
255  bool m_player_focus[MAX_PLAYER_COUNT];
256 
259 
261  WidgetType m_type;
262 
270 
272  int m_badges;
273 
276 
279 
282 
283  bool m_bottom_bar;
284  bool m_top_bar;
285 
288 
289  bool m_has_tooltip;
290  irr::core::stringw m_tooltip_text;
291 
294 
297 
298  public:
299 
308 
314 
318 
322 
326  int m_x, m_y, m_w, m_h;
327 
330 
333 
341 
350  std::map<Property, std::string> m_properties;
351 
352  Widget(WidgetType type, bool reserve_id = false);
353  virtual ~Widget();
354 
358  virtual void setEventCallbackActive(Input::InputType type, bool active);
359 
363  virtual bool isEventCallbackActive(Input::InputType type) const;
364 
369  void setParent(irr::gui::IGUIElement* parent);
370 
377  virtual void setVisible(bool visible);
378 
388  virtual void setCollapsed(bool collapsed, Screen* calling_screen = NULL);
389 
399  virtual void setCollapsed(bool collapsed, int uncollapsed_height, Screen* calling_screen = NULL);
400 
402  bool isVisible() const;
403 
405  bool isCollapsed() const { return m_is_collapsed; }
406 
407  bool isActivated() const;
408 
409  virtual EventPropagation onActivationInput(const int playerID) { return EVENT_LET; }
410 
414  void resetCoordinates();
415 
419  virtual void resize();
420 
424  virtual void move(const int x, const int y, const int w, const int h);
425 
431  bool isSelected(const int playerID) const { return m_selected[playerID]; }
432  void setSelected(const int playerID, bool state) { m_selected[playerID] = state;}
433 
434  bool isBottomBar() const { return m_bottom_bar; }
435  bool isTopBar () const { return m_top_bar; }
436 
446  virtual void setActive(bool active=true);
447 
460  template<typename T> T* getIrrlichtElement()
461  {
462  #if HAVE_RTT
463  T* out = dynamic_cast<T*>(m_element);
464  return out;
465  #else
466  return static_cast<T*>(m_element);
467  #endif
468  }
469 
473  template<typename T> const T* getIrrlichtElement() const
474  {
475  #if HAVE_RTT
476  T* out = dynamic_cast<T*>(m_element);
477  return out;
478  #else
479  return static_cast<T*>(m_element);
480  #endif
481  }
482 
486  irr::gui::IGUIElement* getIrrlichtElement() { return m_element; }
487 
488  void moveIrrlichtElement();
489  bool isSameIrrlichtWidgetAs(const Widget* ref) const { return m_element == ref->m_element; }
490 
511  virtual void setText(const irr::core::stringw &s);
512 
514  const irr::core::stringw &getText() const {return m_text; }
515 
517  WidgetType getType() const { return m_type; }
518 
523  int getID() const { return m_id; }
524 
527  std::string getName() { return m_properties[PROP_ID]; }
528 
530  bool isFocusable() const { return m_focusable; }
531 
532  void setFocusable(bool f) { m_focusable = f; }
533 
547  void setFocusForPlayer(const int playerID);
548 
554  bool isFocusedForPlayer(const int playerID);
555 
557  void unsetFocusForPlayer(const int playerID);
558 
573  static void resetIDCounters();
574 
578  static int getNewID();
579 
583  static int getNewNoFocusID();
584 
591  static bool isFocusableId(const int id);
592 
606  const PtrVector<Widget>& getChildren() const { return m_children; }
607 
609 
620  bool deleteChild(const char* id);
621 
637  virtual void update(float delta) { }
638 
643  virtual EventPropagation transmitEvent(Widget* w,
644  const std::string& originator,
645  const int playerID)
646  { return EVENT_LET; }
647 
652  virtual void add();
653 
657  virtual void elementRemoved();
658 
659  bool searchInsideMe() const { return m_check_inside_me; }
660 
663  virtual int getWidthNeededAroundLabel() const { return 0; }
664 
667  virtual int getHeightNeededAroundLabel() const { return 0; }
668 
684  void setBadge(BadgeType badge_bit)
685  {
686  m_badges |= int(badge_bit);
687  }
688 
693  void unsetBadge(BadgeType badge_bit)
694  {
695  m_badges &= (~int(badge_bit));
696  }
697 
702  {
703  m_badges = 0;
704  }
705 
710  int getBadges() const
711  {
712  return m_badges;
713  }
714 
725  bool hasTooltip() const { return m_has_tooltip; }
726 
728  irr::core::stringw getTooltipText() const { return m_tooltip_text; }
729 
730  void setTooltip(irr::core::stringw s) { m_tooltip_text = s; m_has_tooltip = true; }
731 
736  bool ok() const { return (m_magic_number == 0xCAFEC001); }
737 
739  virtual EventPropagation onClick() { return EVENT_LET; }
740  virtual irr::core::dimension2di getDimension() const { return irr::core::dimension2di(m_w, m_h); }
741  };
742 
743 
744 }
745 #endif
Represents a GUI widgets container.
Definition: abstract_top_level_container.hpp:43
An extended version of RibbonWidget, with more capabilities.
Definition: dynamic_ribbon_widget.hpp:82
Class to handle irrLicht events (GUI and input as well)
Definition: event_handler.hpp:66
Definition: layout_manager.hpp:32
Abstract base class representing a modal dialog.
Definition: modaldialog.hpp:56
A progress bar widget.
Definition: progress_bar_widget.hpp:36
A static text/icons/tabs bar widget.
Definition: ribbon_widget.hpp:66
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:142
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:280
A spinner or gauge widget (to select numbers / percentages).
Definition: spinner_widget.hpp:44
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
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:234
int m_absolute_x
Used during loading, by the layout engine.
Definition: widget.hpp:168
static bool isFocusableId(const int id)
get whether the given ID represents an ID of a widget that can be focused
Definition: widget.cpp:224
bool m_is_visible
A flag to indicate whether this widget should be visible or not.
Definition: widget.hpp:278
int m_tab_up_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:321
virtual EventPropagation leftPressed(const int playerID)
called when left key is pressed and focus is on widget.
Definition: widget.hpp:206
virtual void add()
Create and add the irrLicht widget(s) associated with this object.
Definition: widget.cpp:244
const PtrVector< Widget > & getChildren() const
Definition: widget.hpp:606
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:328
int m_tab_down_root
Instead of searching for widget IDs smaller/greater than that of this object, navigation through widg...
Definition: widget.hpp:317
void setFocusForPlayer(const int playerID)
Focus the widget for the given player.
Definition: widget.cpp:259
int getBadges() const
Get which badges are currently on this widget.
Definition: widget.hpp:710
virtual bool isEventCallbackActive(Input::InputType type) const
Return if there should be an event callback for this widget based on input type.
Definition: widget.cpp:137
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:643
virtual void move(const int x, const int y, const int w, const int h)
Move the widget to the given position.
Definition: widget.cpp:315
bool deleteChild(const char *id)
removes and deletes the child with the given PROP_ID
Definition: widget.cpp:186
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:236
const T * getIrrlichtElement() const
Get the underlying irrLicht GUI element, casted to the right type; const version.
Definition: widget.hpp:473
virtual void update(float delta)
Override in children to possibly receive updates (you may need to register to them first)
Definition: widget.hpp:637
PtrVector< Widget > m_children
If this widget has any children, they go here.
Definition: widget.hpp:269
bool m_deactivated
A simple flag that can be raised to deactivate this widget.
Definition: widget.hpp:275
bool m_focusable
Set to false if widget is something that should not receive focus.
Definition: widget.hpp:281
virtual EventPropagation rightPressed(const int playerID)
called when right key is pressed and focus is on widget.
Definition: widget.hpp:198
virtual EventPropagation onClick()
Gets called when the widget is active and got clicked.
Definition: widget.hpp:739
int m_reserved_id
Used in two cases : 1) For 'placeholder' divisions; at the time the layout is created,...
Definition: widget.hpp:340
void unsetBadge(BadgeType badge_bit)
removes a particular bade from this widget, if it had it.
Definition: widget.hpp:693
int getID() const
Get the irrlicht widget ID attributed to this widget.
Definition: widget.hpp:523
void resetAllBadges()
sets this widget to have no badge
Definition: widget.hpp:701
T * getIrrlichtElement()
Get the underlying irrLicht GUI element, casted to the right type.
Definition: widget.hpp:460
int m_badge_x_shift
If a badge wouldn't look too pretty on the very side of the widget.
Definition: widget.hpp:287
virtual void setVisible(bool visible)
Sets the widget (and its children, if any) visible or not.
Definition: widget.cpp:358
bool m_is_collapsed
A flag to indicate whether this widget got collapsed.
Definition: widget.hpp:296
bool m_supports_multiplayer
Whether this widget supports multiplayer interaction (i.e.
Definition: widget.hpp:313
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:307
irr::gui::IGUIElement * m_parent
An irrlicht parent (most often used to put widgets in dialogs)
Definition: widget.hpp:239
virtual void setCollapsed(bool collapsed, Screen *calling_screen=NULL)
Sets the widget (and its children, if any) collapsed or not.
Definition: widget.cpp:375
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:174
int m_id
numerical ID used by irrLicht to identify this widget (not the same as the string identificator speci...
Definition: widget.hpp:250
bool m_title_font
When true, this widget shall use a bigger and more colourful font.
Definition: widget.hpp:177
virtual void setText(const irr::core::stringw &s)
Sets the text of a widget from a wchar_t.
Definition: widget.cpp:143
irr::gui::IGUIElement * getIrrlichtElement()
Get the underlying irrLicht GUI element.
Definition: widget.hpp:486
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:663
virtual void elementRemoved()
Called when irrLicht widgets cleared.
Definition: widget.cpp:152
std::string getName()
Get the name used for this widget in the GUI files or set in the code Use only for debugging.
Definition: widget.hpp:527
virtual EventPropagation focused(const int playerID)
override in children if you need to know when the widget is focused.
Definition: widget.hpp:231
static int getNewNoFocusID()
Provides a new unique ID on each call, for widgets that can not be focused.
Definition: widget.cpp:218
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:667
int m_badges
A bitmask of which badges to show, if any; choices are *_BADGE, defined above.
Definition: widget.hpp:272
bool m_show_bounding_box
Whether to show a bounding box around this widget (used for sections)
Definition: widget.hpp:329
bool m_selected[MAX_PLAYER_COUNT]
Can be used in children to indicate whether a widget is selected or not.
Definition: widget.hpp:184
void setBadge(BadgeType badge_bit)
adds a particular badge to this widget.
Definition: widget.hpp:684
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:227
int m_uncollapsed_height
height of the widget before it was collapsed (only set if widget got collapsed)
Definition: widget.hpp:293
irr::core::stringw getTooltipText() const
Only call if hasTooltip() returned true.
Definition: widget.hpp:728
bool m_reserve_id
Whether to reserve an ID in 'm_reserved_id' when widget is added.
Definition: widget.hpp:258
std::map< Property, std::string > m_properties
A map that holds values for all specified widget properties (in the XML file)
Definition: widget.hpp:350
bool m_player_focus[MAX_PLAYER_COUNT]
Usually, only one widget at a time can be focused.
Definition: widget.hpp:255
WidgetType m_type
Type of this widget.
Definition: widget.hpp:261
static int getNewID()
Provides a new unique ID on each call, for widgets that can be focused.
Definition: widget.cpp:214
irr::gui::IGUIElement * m_element
IrrLicht widget created to represent this object.
Definition: widget.hpp:244
virtual void resize()
Call to resize the widget after its coordinates are updated.
Definition: widget.cpp:306
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:431
void resetCoordinates()
Call to reset its coordinates before they are updated by layout manager.
Definition: widget.cpp:119
const irr::core::stringw & getText() const
Returns the text of a widget.
Definition: widget.hpp:514
bool m_is_bounding_box_round
Only used if m_show_bounding_box is true.
Definition: widget.hpp:332
virtual void setEventCallbackActive(Input::InputType type, bool active)
Allow (or not) an event callback for this widget based on input type.
Definition: widget.cpp:131
int m_x
Coordinates of the widget once added (the difference between those x/h and PROP_WIDTH/PROP_HEIGHT is ...
Definition: widget.hpp:326
virtual void setActive(bool active=true)
Sets an widget to be either activated or deactivated (i.e.
Definition: widget.cpp:172
virtual EventPropagation downPressed(const int playerID)
called when down key is pressed and focus is on widget.
Definition: widget.hpp:222
bool isCollapsed() const
Returns whether the element is collapsed (through setCollapsed).
Definition: widget.hpp:405
void unsetFocusForPlayer(const int playerID)
Internal method, do not call it.
Definition: widget.cpp:284
bool isFocusedForPlayer(const int playerID)
Find whether this widget is focused by a given player.
Definition: widget.cpp:297
virtual EventPropagation upPressed(const int playerID)
called when up key is pressed and focus is on widget.
Definition: widget.hpp:214
bool isVisible() const
Returns if the element is visible.
Definition: widget.cpp:336
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:190
bool isFocusable() const
Get whether this object is allowed to receive focus.
Definition: widget.hpp:530
WidgetType getType() const
Definition: widget.hpp:517
Definition: ptr_vector.hpp:44
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33
BadgeType
Definition: widget.hpp:63
@ HEART_BADGE
A heart badge, to indicate e.g.
Definition: widget.hpp:85
@ GAMEPAD_BADGE
A gamepad icon.
Definition: widget.hpp:73
@ BAD_BADGE
display a red mark badge on the widget, useful e.g.
Definition: widget.hpp:69
@ KEYBOARD_BADGE
A keyboard icon.
Definition: widget.hpp:75
@ OK_BADGE
display a green check on a widget, useful e.g.
Definition: widget.hpp:67
@ REDDOT_BADGE
A red dot badge, to indicate there's something new inside.
Definition: widget.hpp:87
@ TROPHY_BADGE
display a trophy badge on the widget, useful e.g.
Definition: widget.hpp:71
@ LOCKED_BADGE
display a lock on the widget, to mean a certain game feature is locked
Definition: widget.hpp:65
@ DOWN_BADGE
A down arrow badge to indicate new addons for downloading.
Definition: widget.hpp:83
@ ANCHOR_BADGE
A anchor badge to indicate that this player receives a handicap.
Definition: widget.hpp:81
@ ZIPPER_BADGE
A zipper badge to indicate that this player receives a boost.
Definition: widget.hpp:79
@ LOADING_BADGE
An hourglass badge to indicate loading.
Definition: widget.hpp:77