SuperTuxKart
ribbon_widget.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2009-2015 Marianne Gagnon
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 
19 
20 #ifndef HEADER_RIBBON_HPP
21 #define HEADER_RIBBON_HPP
22 
23 #include <irrString.h>
24 
25 #include "guiengine/widget.hpp"
26 #include "guiengine/widgets/icon_button_widget.hpp"
27 #include "utils/cpp2011.hpp"
28 #include "utils/leak_check.hpp"
29 #include "utils/ptr_vector.hpp"
30 
31 #include <map>
32 
33 namespace irr
34 {
35  namespace gui { class IGUIButton; class IGUIStaticText; }
36 }
37 
38 
39 namespace GUIEngine
40 {
43  {
48  };
49 
52  {
53  FLIP_NO, // For non-tab ribbons
54  FLIP_UP_LEFT, // For horizontal tabs it goes up vertical ones it goes left
55  FLIP_DOWN_RIGHT // For horizontal tabs it goes down vertical ones it goes right
56  };
57 
65  class RibbonWidget : public Widget
66  {
67  public:
69  {
70  public:
71  virtual ~IRibbonListener(){}
72  virtual void onRibbonWidgetScroll(const int delta_x) = 0;
73  virtual void onRibbonWidgetFocus(RibbonWidget* emitter,
74  const int playerID) = 0;
75  virtual void onSelectionChange() = 0;
76  };
77 
78  private:
79  friend class DynamicRibbonWidget;
80  friend class EventHandler;
81 
82  int m_selection[MAX_PLAYER_COUNT];
83 
86 
89 
93  void updateSelection();
94 
96  virtual EventPropagation rightPressed(const int playerID=0) OVERRIDE;
97  virtual EventPropagation leftPressed (const int playerID=0) OVERRIDE;
98  virtual EventPropagation upPressed (const int playerID=0) OVERRIDE;
99  virtual EventPropagation downPressed (const int playerID=0) OVERRIDE;
100  EventPropagation moveToNextItem(const bool horizontally, const bool reverse, const int playerID);
101  EventPropagation propagationType(const bool horizontally);
102  void selectNextActiveWidget(const bool horizontally, const bool reverse,
103  const int playerID, const int old_selection);
104  virtual EventPropagation mouseHovered(Widget* child,
105  const int playerID) OVERRIDE;
106  virtual EventPropagation transmitEvent(Widget* w,
107  const std::string& originator,
108  const int playerID=0) OVERRIDE;
109  virtual EventPropagation focused(const int playerID) OVERRIDE;
110  virtual void unfocused(const int playerID, Widget* new_focus) OVERRIDE;
111 
112  virtual EventPropagation onClick() OVERRIDE;
113 
114  PtrVector<irr::gui::IGUIStaticText, REF> m_labels;
115 
116  IRibbonListener* m_listener;
117  PtrVector<Widget> m_active_children;
118  std::map<int, std::pair<irr::gui::IGUIStaticText*, irr::gui::IGUIButton*> > m_child_data;
119  public:
120 
121  LEAK_CHECK()
122 
128  static const char NO_ITEM_ID[];
129 
134 
136  virtual ~RibbonWidget();
137 
138  virtual void add() OVERRIDE;
139 
144  void setListener(IRibbonListener* listener) { m_listener = listener; }
145  // --------------------------------------------------------------------
149  // --------------------------------------------------------------------
152  // --------------------------------------------------------------------
154  int getActiveChildrenNumber(const int playerID) const
155  { return m_active_children.size(); }
156  // --------------------------------------------------------------------
158  int getSelection(const int playerID) const
159  { return m_selection[playerID]; }
160  // --------------------------------------------------------------------
162  const std::string& getSelectionIDString(const int playerID);
163  // --------------------------------------------------------------------
165  irr::core::stringw getSelectionText(const int playerID)
166  {
167  const int selection = m_selection[playerID];
168  if (selection < 0 || selection >= int(m_children.size())) return "";
169  return m_children[selection].m_text;
170  }
171  // --------------------------------------------------------------------
172 
174  void setSelection(const int i, const int playerID)
175  { m_selection[playerID] = i; updateSelection(); }
176 
178  void setFirstSelection(const int playerID, bool reverse_order = false);
179 
181  void setLastSelection(const int playerID)
182  { setFirstSelection(playerID, true); }
183 
185  void select(std::string item, const int playerID);
186 
194  void setLabel(const unsigned int id, irr::core::stringw new_name);
195 
196  void setItemVisible(const unsigned int id, bool visible);
197 
198  void setFlip(RibbonFlip direction);
199 
201  int findItemNamed(const char* internalName);
202 
204  GUIEngine::Widget * findWidgetNamed(const char* interalName);
205 
212  void addTextChild(const core::stringw& text, const std::string &id);
213 
214 
220  void addIconChild(const core::stringw& text, const std::string &id,
221  const int w, const int h, const std::string &icon,
222  const IconButtonWidget::IconPathType iconPathType=
224 
229  void clearAllChildren();
230 
235  void removeChildNamed(const char* name);
236 
237  PtrVector<Widget>& getRibbonChildren() { return m_children; }
238 
239  virtual EventPropagation onActivationInput(const int playerID) OVERRIDE;
240 
241  virtual void resize() OVERRIDE;
242  };
243 
244 }
245 
246 #endif
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
IconPathType
Definition: icon_button_widget.hpp:68
@ ICON_PATH_TYPE_RELATIVE
relative to the data dir
Definition: icon_button_widget.hpp:71
Definition: ribbon_widget.hpp:69
A static text/icons/tabs bar widget.
Definition: ribbon_widget.hpp:66
virtual EventPropagation rightPressed(const int playerID=0) OVERRIDE
Callbacks.
Definition: ribbon_widget.cpp:299
int getSelection(const int playerID) const
Returns the numerical ID of the selected item within the ribbon.
Definition: ribbon_widget.hpp:158
static const char NO_ITEM_ID[]
Internal identifier of filler items that are added in a ribbon widget to filllines when the number of...
Definition: ribbon_widget.hpp:128
virtual EventPropagation onClick() OVERRIDE
Gets called when the widget is active and got clicked.
Definition: ribbon_widget.cpp:519
virtual void resize() OVERRIDE
Call to resize the widget after its coordinates are updated.
Definition: ribbon_widget.cpp:696
virtual EventPropagation downPressed(const int playerID=0) OVERRIDE
called when down key is pressed and focus is on widget.
Definition: ribbon_widget.cpp:311
const std::string & getSelectionIDString(const int playerID)
Returns the string ID (internal name) of the selection.
Definition: ribbon_widget.cpp:528
void setSelection(const int i, const int playerID)
Sets the ID of the selected item within the ribbon.
Definition: ribbon_widget.hpp:174
void setLabel(const unsigned int id, irr::core::stringw new_name)
This method can be used to rename an item.
Definition: ribbon_widget.cpp:631
virtual EventPropagation transmitEvent(Widget *w, const std::string &originator, const int playerID=0) OVERRIDE
All widgets, including their parents (m_event_handler) will be notified on event through this call.
Definition: ribbon_widget.cpp:589
int findItemNamed(const char *internalName)
Returns the ID of the item, or -1 if not found.
Definition: ribbon_widget.cpp:675
GUIEngine::Widget * findWidgetNamed(const char *interalName)
Returns the the widget, or NULL if not found.
Definition: ribbon_widget.cpp:687
Widget * m_mouse_focus
Contains which element within the ribbon is currently focused by player 0 (used by the skin to show m...
Definition: ribbon_widget.hpp:133
virtual EventPropagation focused(const int playerID) OVERRIDE
override in children if you need to know when the widget is focused.
Definition: ribbon_widget.cpp:432
void updateSelection()
Each item within the ribbon holds a flag saying whether it is selected or not.
Definition: ribbon_widget.cpp:544
virtual void add() OVERRIDE
Create and add the irrLicht widget(s) associated with this object.
Definition: ribbon_widget.cpp:76
RibbonType m_ribbon_type
The type of this ribbon (toolbar, combo, tabs, vertical tabs)
Definition: ribbon_widget.hpp:85
RibbonFlip getRibbonFlip() const
Returns the flip direction of thin ribbon.
Definition: ribbon_widget.hpp:151
virtual void unfocused(const int playerID, Widget *new_focus) OVERRIDE
override in children if you need to know when the widget is unfocused.
Definition: ribbon_widget.cpp:470
void addTextChild(const core::stringw &text, const std::string &id)
Dynamically (at runtime) add a text item to this ribbon.
Definition: ribbon_widget.cpp:194
RibbonType getRibbonType() const
Returns the type of this ribbon (see the GUI module overview page for detailed descriptions)
Definition: ribbon_widget.hpp:148
virtual EventPropagation upPressed(const int playerID=0) OVERRIDE
called when up key is pressed and focus is on widget.
Definition: ribbon_widget.cpp:317
RibbonFlip m_ribbon_flip
The flip direction of this ribbon.
Definition: ribbon_widget.hpp:88
irr::core::stringw getSelectionText(const int playerID)
Returns the user-visible text of the selection.
Definition: ribbon_widget.hpp:165
void addIconChild(const core::stringw &text, const std::string &id, const int w, const int h, const std::string &icon, const IconButtonWidget::IconPathType iconPathType=IconButtonWidget::ICON_PATH_TYPE_RELATIVE)
Dynamically (at runtime) add an icon item to this ribbon.
Definition: ribbon_widget.cpp:208
void setFirstSelection(const int playerID, bool reverse_order=false)
Sets the first active item within the ribbon as selected.
Definition: ribbon_widget.cpp:260
virtual EventPropagation mouseHovered(Widget *child, const int playerID) OVERRIDE
used when you set eventSupervisors - see m_event_handler explainations below called when one of a wid...
Definition: ribbon_widget.cpp:484
void removeChildNamed(const char *name)
clear one child from this ribbon
Definition: ribbon_widget.cpp:241
virtual EventPropagation leftPressed(const int playerID=0) OVERRIDE
called when left key is pressed and focus is on widget.
Definition: ribbon_widget.cpp:305
int getActiveChildrenNumber(const int playerID) const
Returns the number of active items within the ribbon.
Definition: ribbon_widget.hpp:154
void setListener(IRibbonListener *listener)
Sets a listener that will be notified of changes on this ribbon.
Definition: ribbon_widget.hpp:144
void clearAllChildren()
clear all children of this ribbon (likely because new ones will be added soon after)
Definition: ribbon_widget.cpp:231
void setLastSelection(const int playerID)
Sets the last active item within the ribbon as selected.
Definition: ribbon_widget.hpp:181
void select(std::string item, const int playerID)
Select an item in the ribbon by its internal name.
Definition: ribbon_widget.cpp:282
void selectNextActiveWidget(const bool horizontally, const bool reverse, const int playerID, const int old_selection)
Move to the next child widget in the requested direction.
Definition: ribbon_widget.cpp:388
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
PtrVector< Widget > m_children
If this widget has any children, they go here.
Definition: widget.hpp:269
Definition: ptr_vector.hpp:44
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33
RibbonFlip
Filp directions of ribbons.
Definition: ribbon_widget.hpp:52
RibbonType
Types of ribbons.
Definition: ribbon_widget.hpp:43
@ RIBBON_VERTICAL_TABS
a vertical tab bar
Definition: ribbon_widget.hpp:47
@ RIBBON_TOOLBAR
a row of individual buttons
Definition: ribbon_widget.hpp:45
@ RIBBON_TABS
a tab bar
Definition: ribbon_widget.hpp:46
@ RIBBON_COMBO
select one item out of many, like in a combo box
Definition: ribbon_widget.hpp:44