SuperTuxKart
dynamic_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_RIBBONGRID_HPP
21 #define HEADER_RIBBONGRID_HPP
22 
23 #include <irrString.h>
24 
25 #include <algorithm>
26 
27 #include "guiengine/widget.hpp"
28 #include "guiengine/widgets/ribbon_widget.hpp"
29 #include "utils/leak_check.hpp"
30 #include "utils/ptr_vector.hpp"
31 
32 namespace GUIEngine
33 {
34  class IconButtonWidget;
35 
41  {
42  public:
43  virtual ~DynamicRibbonHoverListener() {}
44  virtual void onSelectionChanged(DynamicRibbonWidget* theWidget,
45  const std::string& selectionID,
46  const irr::core::stringw& selectionText,
47  const int playerID) = 0;
48  };
49 
52  {
53  irr::core::stringw m_user_name;
54  std::string m_code_name;
55  std::string m_sshot_file;
56  IconButtonWidget::IconPathType m_image_path_type;
57 
58  bool m_animated;
60  std::vector<std::string> m_all_images;
61  float m_curr_time;
62  float m_time_per_frame;
63 
64  unsigned int m_badges;
65  };
66 
68  {
69  void* data;
70  void (*callback)(void* data);
71  };
72 
82  {
83  friend class RibbonWidget;
84 
87 
88  virtual ~DynamicRibbonWidget();
89 
92  irr::gui::IGUIStaticText* m_label;
93 
98 
101 
106 
109 
111  std::vector<ItemDescription> m_items;
112 
115 
118 
120  int m_child_width, m_child_height;
121 
125  int m_col_amount;
126 
129 
132 
139  std::vector<int> m_ids;
140 
142  bool m_combo;
143 
144  /* reference pointers only, the actual instances are owned by m_children */
145  IconButtonWidget* m_left_widget;
146  IconButtonWidget* m_right_widget;
147 
149  RibbonWidget* getSelectedRibbon(const int playerID);
150 
153 
155  void updateLabel(RibbonWidget* from_this_ribbon=NULL);
156 
161  void propagateSelection();
162 
164  EventPropagation focused(const int playerID);
165 
167  void buildInternalStructure();
168 
170  void scroll(int x_delta, bool evenIfDeactivated = false);
171 
173  int m_selected_item[MAX_PLAYER_COUNT];
174 
176  virtual void add();
177  virtual EventPropagation mouseHovered(Widget* child, const int playerID);
178  virtual EventPropagation transmitEvent(Widget* w, const std::string& originator, const int playerID);
179 
180  bool findItemInRows(const char* name, int* p_row, int* p_id);
181 
182  int m_item_count_hint;
183 
184  float getFontScale(int icon_width) const;
185  void setLabelSize(const irr::core::stringw& text);
186  irr::core::stringw getUserName(const irr::core::stringw& user_name) const;
187 
193 
196 
198  unsigned int m_max_label_length;
199 
200  DynamicRibbonScrollCallback m_scroll_callback;
201  public:
202 
203  LEAK_CHECK()
204 
205 
210  DynamicRibbonWidget(const bool combo, const bool multi_row);
211 
215 
225  void addItem( const irr::core::stringw& user_name, const std::string& code_name,
226  const std::string& image_file, const unsigned int badge=0,
227  IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
228 
240  void addAnimatedItem( const irr::core::stringw& user_name, const std::string& code_name,
241  const std::vector<std::string>& image_files, const float time_per_frame,
242  const unsigned int badge=0,
243  IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
244 
247  void clearItems();
248  void setBadge(const std::string &name, BadgeType badge);
249 
251  template<typename Compare>
252  void sortItems(Compare comp)
253  {
254  std::sort(m_items.begin(), m_items.end(), comp);
255  }
256 
264 
266  EventPropagation rightPressed(const int playerID);
267 
269  EventPropagation leftPressed(const int playerID);
270 
273  void updateItemDisplay();
274 
276  const std::string& getSelectionIDString(const int playerID);
277 
279  irr::core::stringw getSelectionText(const int playerID);
280 
282  const std::vector<ItemDescription>& getItems() const { return m_items; }
283 
290  bool setSelection(int item_id, const int playerID, const bool focusIt, bool evenIfDeactivated=false);
291 
297  bool setSelection(const std::string &item_codename,
298  const int playerID, const bool focusIt,
299  bool evenIfDeactivated=false);
300 
302  virtual void elementRemoved();
303 
305  virtual void onRibbonWidgetScroll(const int delta_x);
306 
308  virtual void onRibbonWidgetFocus(RibbonWidget* emitter, const int playerID);
309 
311  virtual void onSelectionChange(){}
312 
313  virtual void setText(const irr::core::stringw& text);
314 
315  virtual void update(float delta);
316 
319  void setItemCountHint(int hint) { m_item_count_hint = hint; }
320 
322  void setMaxLabelLength(int length) { m_max_label_length = length; }
323 
324  void registerScrollCallback(void (*callback)(void* data), void* data)
325  {
326  m_scroll_callback.callback = callback;
327  m_scroll_callback.data = data;
328  }
329  };
330 
331 }
332 
333 #endif
std::vector< std::string > m_all_images
used instead of 'm_sshot_file' if m_animated is true
Definition: dynamic_ribbon_widget.hpp:60
bool m_scrolling_enabled
Whether there are more items than can fit in a single screen; arrows will then appear on each side of...
Definition: dynamic_ribbon_widget.hpp:105
void addItem(const irr::core::stringw &user_name, const std::string &code_name, const std::string &image_file, const unsigned int badge=0, IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE)
Dynamically add an item to the ribbon's list of items (will not be visible until you call 'updateItem...
Definition: dynamic_ribbon_widget.cpp:464
irr::gui::ScalableFont * m_font
Font used to write the labels, can be scaled down depending on the length of the text.
Definition: dynamic_ribbon_widget.hpp:192
void setItemCountHint(int hint)
Set approximately how many items are expected to be in this ribbon; will help the layout algorithm ne...
Definition: dynamic_ribbon_widget.hpp:319
An extended version of RibbonWidget, with more capabilities.
Definition: dynamic_ribbon_widget.hpp:81
PtrVector< RibbonWidget, REF > m_rows
Reference pointers only, the actual instances are owned by m_children.
Definition: dynamic_ribbon_widget.hpp:214
int m_label_height
Height of ONE label text line (if label is multiline only one line is measured here).
Definition: dynamic_ribbon_widget.hpp:97
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
unsigned int m_max_label_length
Max length of a label, in characters.
Definition: dynamic_ribbon_widget.hpp:198
std::vector< ItemDescription > m_items
List of items in the ribbon.
Definition: dynamic_ribbon_widget.hpp:111
int m_selected_item[MAX_PLAYER_COUNT]
Used for combo ribbons, to contain the ID of the currently selected item for each player.
Definition: dynamic_ribbon_widget.hpp:173
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: dynamic_ribbon_widget.cpp:717
void registerHoverListener(DynamicRibbonHoverListener *listener)
Register a listener to be notified of selection changes within the ribbon.
Definition: dynamic_ribbon_widget.cpp:627
const std::vector< ItemDescription > & getItems() const
Returns a read-only list of items added to this ribbon.
Definition: dynamic_ribbon_widget.hpp:282
EventPropagation focused(const int playerID)
Callback called widget is focused.
Definition: dynamic_ribbon_widget.cpp:737
int m_max_label_width
Max width of a label, in pixels.
Definition: dynamic_ribbon_widget.hpp:195
bool setSelection(int item_id, const int playerID, const bool focusIt, bool evenIfDeactivated=false)
Select an item from its numerical ID.
Definition: dynamic_ribbon_widget.cpp:1119
virtual void update(float delta)
Override in children to possibly receive updates (you may need to register to them first)
Definition: dynamic_ribbon_widget.cpp:1058
void addAnimatedItem(const irr::core::stringw &user_name, const std::string &code_name, const std::vector< std::string > &image_files, const float time_per_frame, const unsigned int badge=0, IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE)
Dynamically add an animated item to the ribbon's list of items (will not be visible until you call 'u...
Definition: dynamic_ribbon_widget.cpp:483
IconPathType
Definition: icon_button_widget.hpp:63
std::vector< int > m_ids
irrlicht relies on consecutive IDs to perform keyboard navigation between widgets.
Definition: dynamic_ribbon_widget.hpp:139
bool m_combo
Whether this is a "combo" style ribbon grid widget.
Definition: dynamic_ribbon_widget.hpp:142
int m_scroll_offset
Current scroll offset within items.
Definition: dynamic_ribbon_widget.hpp:117
BadgeType
Definition: widget.hpp:61
bool m_animated_contents
Whether this ribbon contains at least one animated item.
Definition: dynamic_ribbon_widget.hpp:100
bool m_has_label
Used for ribbon grids that have a label at the bottom.
Definition: dynamic_ribbon_widget.hpp:91
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: dynamic_ribbon_widget.cpp:683
The description of an item added to a DynamicRibbonWidget.
Definition: dynamic_ribbon_widget.hpp:51
irr::core::stringw getSelectionText(const int playerID)
Get the user-visible text of the selected item.
Definition: dynamic_ribbon_widget.cpp:585
virtual void setText(const irr::core::stringw &text)
Sets the text of a widget from a wchar_t.
Definition: dynamic_ribbon_widget.cpp:766
int m_arrows_w
Width of the scrolling arrows on each side.
Definition: dynamic_ribbon_widget.hpp:114
A button widget with an icon and optionnaly a label beneath.
Definition: icon_button_widget.hpp:43
virtual void onRibbonWidgetScroll(const int delta_x)
callback from IRibbonListener
Definition: dynamic_ribbon_widget.cpp:759
void sortItems(Compare comp)
Sort the list of items with a given comparator.
Definition: dynamic_ribbon_widget.hpp:252
int m_child_width
Width and height of children as declared in the GUI file.
Definition: dynamic_ribbon_widget.hpp:120
void scroll(int x_delta, bool evenIfDeactivated=false)
Call this to scroll within a scrollable ribbon.
Definition: dynamic_ribbon_widget.cpp:805
EventPropagation leftPressed(const int playerID)
Called when left key is pressed.
Definition: dynamic_ribbon_widget.cpp:660
RibbonWidget * getRowContaining(Widget *w)
Returns the row.
Definition: dynamic_ribbon_widget.cpp:596
void clearItems()
Clears all items added through 'addItem'.
Definition: dynamic_ribbon_widget.cpp:516
bool m_multi_row
Whether this ribbon can have multiple rows (i.e.
Definition: dynamic_ribbon_widget.hpp:131
Definition: scalable_font.hpp:34
Definition: dynamic_ribbon_widget.hpp:67
void updateLabel(RibbonWidget *from_this_ribbon=NULL)
Updates the visible label to match the currently selected item.
Definition: dynamic_ribbon_widget.cpp:885
const std::string & getSelectionIDString(const int playerID)
Get the internal name (ID) of the selected item.
Definition: dynamic_ribbon_widget.cpp:574
EventPropagation rightPressed(const int playerID)
Called when right key is pressed.
Definition: dynamic_ribbon_widget.cpp:632
A static text/icons/tabs bar widget.
Definition: ribbon_widget.hpp:59
int m_needed_cols
The total number of columns given item count and row count (even counting not visible with current sc...
Definition: dynamic_ribbon_widget.hpp:128
void buildInternalStructure()
Removes all previously added contents icons, and re-adds them (calculating the new amount)
Definition: dynamic_ribbon_widget.cpp:320
int m_previous_item_count
Used to keep track of item count changes.
Definition: dynamic_ribbon_widget.hpp:108
int m_row_amount
Number of rows and columns.
Definition: dynamic_ribbon_widget.hpp:124
Definition: ribbon_widget.hpp:62
Definition: ptr_vector.hpp:43
PtrVector< DynamicRibbonHoverListener > m_hover_listeners
A list of all listeners that registered to be notified on hover/selection.
Definition: dynamic_ribbon_widget.hpp:86
void updateItemDisplay()
Updates icons/labels given current items and scrolling offset, taking care of resizing the dynamic ri...
Definition: dynamic_ribbon_widget.cpp:916
void propagateSelection()
Even though the ribbon grid widget looks like a grid, it is really a vertical stack of independant ri...
Definition: dynamic_ribbon_widget.cpp:843
void setMaxLabelLength(int length)
Set max length of displayed text.
Definition: dynamic_ribbon_widget.hpp:322
Even if you have a ribbon that only acts on click/enter, you may wish to know which item is currently...
Definition: dynamic_ribbon_widget.hpp:40
RibbonWidget * getSelectedRibbon(const int playerID)
Returns the currently selected row.
Definition: dynamic_ribbon_widget.cpp:609
virtual void add()
Callbacks.
Definition: dynamic_ribbon_widget.cpp:108
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:138
virtual void onSelectionChange()
callback from IRibbonListener
Definition: dynamic_ribbon_widget.hpp:311
virtual void onRibbonWidgetFocus(RibbonWidget *emitter, const int playerID)
callback from IRibbonListener
Definition: dynamic_ribbon_widget.cpp:776
virtual void elementRemoved()
Callback from parent class Widget.
Definition: dynamic_ribbon_widget.cpp:557