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 
130 
133 
136 
143  std::vector<int> m_ids;
144 
146  bool m_combo;
147 
148  /* reference pointers only, the actual instances are owned by m_children */
149  IconButtonWidget* m_left_widget;
150  IconButtonWidget* m_right_widget;
151 
153  RibbonWidget* getSelectedRibbon(const int playerID);
154 
157 
159  void updateLabel(RibbonWidget* from_this_ribbon=NULL);
160 
165  void propagateSelection();
166 
168  EventPropagation focused(const int playerID);
169 
172  float estimateRowScore(const int rowCount, const int width, const int height,
173  const float iconAspectRatio, const int maxIcons, float* heightRatio,
174  float capSize = 0.5f);
175 
177  void buildInternalStructure();
178 
180  void scroll(int x_delta, bool evenIfDeactivated = false);
181 
183  int m_selected_item[MAX_PLAYER_COUNT];
184 
186  virtual void add();
187  virtual EventPropagation mouseHovered(Widget* child, const int playerID);
188  virtual EventPropagation transmitEvent(Widget* w, const std::string& originator, const int playerID);
189 
190  bool findItemInRows(const char* name, int* p_row, int* p_id);
191 
192  int m_item_count_hint;
193 
194  float getFontScale(int icon_width) const;
195  void setLabelSize(const irr::core::stringw& text);
196  irr::core::stringw getUserName(const irr::core::stringw& user_name) const;
197 
198  void updateForResizing();
204 
207 
209  unsigned int m_max_label_length;
210 
211  DynamicRibbonScrollCallback m_scroll_callback;
212  public:
213 
214  LEAK_CHECK()
215 
216 
221  DynamicRibbonWidget(const bool combo, const bool multi_row);
222 
226 
236  void addItem( const irr::core::stringw& user_name, const std::string& code_name,
237  const std::string& image_file, const unsigned int badge=0,
238  IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
239 
251  void addAnimatedItem( const irr::core::stringw& user_name, const std::string& code_name,
252  const std::vector<std::string>& image_files, const float time_per_frame,
253  const unsigned int badge=0,
254  IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
255 
258  void clearItems();
259  void setBadge(const std::string &name, BadgeType badge);
260 
262  template<typename Compare>
263  void sortItems(Compare comp)
264  {
265  std::sort(m_items.begin(), m_items.end(), comp);
266  }
267 
275 
277  EventPropagation rightPressed(const int playerID);
278 
280  EventPropagation leftPressed(const int playerID);
281 
284  void updateItemDisplay();
285 
287  const std::string& getSelectionIDString(const int playerID);
288 
290  irr::core::stringw getSelectionText(const int playerID);
291 
293  const std::vector<ItemDescription>& getItems() const { return m_items; }
294 
301  bool setSelection(int item_id, const int playerID, const bool focusIt, bool evenIfDeactivated=false);
302 
308  bool setSelection(const std::string &item_codename,
309  const int playerID, const bool focusIt,
310  bool evenIfDeactivated=false);
311 
313  virtual void elementRemoved();
314 
316  virtual void onRibbonWidgetScroll(const int delta_x);
317 
319  virtual void onRibbonWidgetFocus(RibbonWidget* emitter, const int playerID);
320 
322  virtual void onSelectionChange(){}
323 
324  virtual void setText(const irr::core::stringw& text);
325 
326  virtual void update(float delta);
327 
328  virtual void resize();
329 
332  void setItemCountHint(int hint) { m_item_count_hint = hint; }
333 
335  void setMaxLabelLength(int length) { m_max_label_length = length; }
336 
337  void registerScrollCallback(void (*callback)(void* data), void* data)
338  {
339  m_scroll_callback.callback = callback;
340  m_scroll_callback.data = data;
341  }
342  };
343 
344 }
345 
346 #endif
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:41
An extended version of RibbonWidget, with more capabilities.
Definition: dynamic_ribbon_widget.hpp:82
void scroll(int x_delta, bool evenIfDeactivated=false)
Call this to scroll within a scrollable ribbon.
Definition: dynamic_ribbon_widget.cpp:897
void updateItemDisplay()
Updates icons/labels given current items and scrolling offset, taking care of resizing the dynamic ri...
Definition: dynamic_ribbon_widget.cpp:1006
int m_max_label_width
Max width of a label, in pixels.
Definition: dynamic_ribbon_widget.hpp:206
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:937
bool m_animated_contents
Whether this ribbon contains at least one animated item.
Definition: dynamic_ribbon_widget.hpp:100
void registerHoverListener(DynamicRibbonHoverListener *listener)
Register a listener to be notified of selection changes within the ribbon.
Definition: dynamic_ribbon_widget.cpp:727
void setMaxLabelLength(int length)
Set max length of displayed text.
Definition: dynamic_ribbon_widget.hpp:335
PtrVector< RibbonWidget, REF > m_rows
Reference pointers only, the actual instances are owned by m_children.
Definition: dynamic_ribbon_widget.hpp:225
void clearItems()
Clears all items added through 'addItem'.
Definition: dynamic_ribbon_widget.cpp:616
int m_row_amount
Number of rows and columns.
Definition: dynamic_ribbon_widget.hpp:124
EventPropagation rightPressed(const int playerID)
Called when right key is pressed.
Definition: dynamic_ribbon_widget.cpp:732
irr::core::stringw getSelectionText(const int playerID)
Get the user-visible text of the selected item.
Definition: dynamic_ribbon_widget.cpp:685
EventPropagation focused(const int playerID)
Callback called widget is focused.
Definition: dynamic_ribbon_widget.cpp:829
int m_scroll_offset
Current scroll offset within items.
Definition: dynamic_ribbon_widget.hpp:117
RibbonWidget * getSelectedRibbon(const int playerID)
Returns the currently selected row.
Definition: dynamic_ribbon_widget.cpp:709
RibbonWidget * getRowContaining(Widget *w)
Returns the row.
Definition: dynamic_ribbon_widget.cpp:696
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:583
PtrVector< DynamicRibbonHoverListener > m_hover_listeners
A list of all listeners that registered to be notified on hover/selection.
Definition: dynamic_ribbon_widget.hpp:86
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
const std::vector< ItemDescription > & getItems() const
Returns a read-only list of items added to this ribbon.
Definition: dynamic_ribbon_widget.hpp:293
std::vector< ItemDescription > m_items
List of items in the ribbon.
Definition: dynamic_ribbon_widget.hpp:111
void sortItems(Compare comp)
Sort the list of items with a given comparator.
Definition: dynamic_ribbon_widget.hpp:263
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:768
const std::string & getSelectionIDString(const int playerID)
Get the internal name (ID) of the selected item.
Definition: dynamic_ribbon_widget.cpp:674
unsigned int m_max_label_length
Max length of a label, in characters.
Definition: dynamic_ribbon_widget.hpp:209
bool m_combo
Whether this is a "combo" style ribbon grid widget.
Definition: dynamic_ribbon_widget.hpp:146
virtual void add()
Callbacks.
Definition: dynamic_ribbon_widget.cpp:84
virtual void onRibbonWidgetScroll(const int delta_x)
callback from IRibbonListener
Definition: dynamic_ribbon_widget.cpp:851
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:183
EventPropagation leftPressed(const int playerID)
Called when left key is pressed.
Definition: dynamic_ribbon_widget.cpp:760
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:203
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:564
float estimateRowScore(const int rowCount, const int width, const int height, const float iconAspectRatio, const int maxIcons, float *heightRatio, float capSize=0.5f)
Computes a score based on multiple icon properties (used to estimate the best number of rows)
Definition: dynamic_ribbon_widget.cpp:243
bool m_multi_row
Whether this ribbon can have multiple rows (i.e.
Definition: dynamic_ribbon_widget.hpp:135
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:1208
float m_size_ratio
The ratio between the targeted child height and the maximum height that would fit with with the curre...
Definition: dynamic_ribbon_widget.hpp:129
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:132
int m_child_width
Width and height of children as declared in the GUI file.
Definition: dynamic_ribbon_widget.hpp:120
bool m_has_label
Used for ribbon grids that have a label at the bottom.
Definition: dynamic_ribbon_widget.hpp:91
virtual void onSelectionChange()
callback from IRibbonListener
Definition: dynamic_ribbon_widget.hpp:322
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:802
virtual void onRibbonWidgetFocus(RibbonWidget *emitter, const int playerID)
callback from IRibbonListener
Definition: dynamic_ribbon_widget.cpp:868
void buildInternalStructure()
Removes all previously added contents icons, and re-adds them (calculating the new amount)
Definition: dynamic_ribbon_widget.cpp:318
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:332
int m_previous_item_count
Used to keep track of item count changes.
Definition: dynamic_ribbon_widget.hpp:108
virtual void resize()
Call to resize the widget after its coordinates are updated.
Definition: dynamic_ribbon_widget.cpp:168
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:1147
void updateLabel(RibbonWidget *from_this_ribbon=NULL)
Updates the visible label to match the currently selected item.
Definition: dynamic_ribbon_widget.cpp:975
std::vector< int > m_ids
irrlicht relies on consecutive IDs to perform keyboard navigation between widgets.
Definition: dynamic_ribbon_widget.hpp:143
int m_arrows_w
Width of the scrolling arrows on each side.
Definition: dynamic_ribbon_widget.hpp:114
virtual void elementRemoved()
Callback from parent class Widget.
Definition: dynamic_ribbon_widget.cpp:657
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
virtual void setText(const irr::core::stringw &text)
Sets the text of a widget from a wchar_t.
Definition: dynamic_ribbon_widget.cpp:858
A button widget with an icon and optionnaly a label beneath.
Definition: icon_button_widget.hpp:44
IconPathType
Definition: icon_button_widget.hpp:68
Definition: ribbon_widget.hpp:69
A static text/icons/tabs bar widget.
Definition: ribbon_widget.hpp:66
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
Definition: ptr_vector.hpp:44
Definition: scalable_font.hpp:35
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33
BadgeType
Definition: widget.hpp:63
Definition: dynamic_ribbon_widget.hpp:68
The description of an item added to a DynamicRibbonWidget.
Definition: dynamic_ribbon_widget.hpp:52
std::vector< std::string > m_all_images
used instead of 'm_sshot_file' if m_animated is true
Definition: dynamic_ribbon_widget.hpp:60