supertux
item_horizontalmenu.hpp
1 // SuperTux
2 // Copyright (C) 2022-2023 Vankata453
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (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, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_GUI_ITEM_HORIZONTALMENU_HPP
18 #define HEADER_SUPERTUX_GUI_ITEM_HORIZONTALMENU_HPP
19 
20 #include "gui/menu_item.hpp"
21 
22 #include <vector>
23 
24 #include "math/rectf.hpp"
25 #include "video/surface_ptr.hpp"
26 
27 class DrawingContext;
28 
29 class ItemHorizontalMenu final : public MenuItem
30 {
31 protected:
32  static float s_max_width;
33 
34  static const float s_width_offset;
35  static const float s_menu_width_offset;
36  static const float s_item_spacing;
37 
38  static const float s_icon_y;
39 
40 public:
41  struct Item
42  {
43  const int id;
44  const std::string text;
45  const std::string description;
46  const SurfacePtr icon;
47  };
48 
49 protected:
50  std::vector<Item> m_items;
51  int m_selected_item;
52  int m_item_range_begin;
53  int m_item_range_end;
54 
55  float m_width;
56  const float m_height;
57  const float m_min_item_width;
58  Rectf m_rect;
59 
60 public:
61  ItemHorizontalMenu(int id, float height, float min_item_width = -1.f);
62 
63  Item& get_selected_item();
64 
65  void add_item(const std::string& text, const std::string& description,
66  const std::string& icon_file, int id = -1);
67 
68  void draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) override;
69  void draw_item(DrawingContext& context, const Item& item, bool active,
70  const Vector& pos, const float& item_width);
71 
72  void process_action(const MenuAction& action) override;
73  void event(const SDL_Event& ev) override;
74 
75  void on_window_resize() override;
76 
77  bool changes_width() const override { return true; }
78  bool select_blink() const override { return false; }
79 
80  float get_distance() const override { return 10.f; }
81 
82  int get_width() const override { return static_cast<int>(m_width); }
83  int get_height() const override { return static_cast<int>(m_height); }
84 
85 protected:
86  void calculate_width(int begin = 0);
87  void calculate_rect(const Vector& pos);
88 
89  // Navigation functions
90  void go_left();
91  void go_right();
92 
93 private:
94  float get_item_width(const std::string& text) const;
95 
96 private:
97  ItemHorizontalMenu(const ItemHorizontalMenu&) = delete;
98  ItemHorizontalMenu& operator=(const ItemHorizontalMenu&) = delete;
99 };
100 
101 #endif
102 
103 /* EOF */
bool select_blink() const override
Returns true when the item should have a blink effect, provided by the menu, when active...
Definition: item_horizontalmenu.hpp:78
int get_width() const override
Returns the minimum width of the menu item.
Definition: item_horizontalmenu.hpp:82
void draw(DrawingContext &context, const Vector &pos, int menu_width, bool active) override
Draws the menu item.
Definition: item_horizontalmenu.cpp:125
int get_height() const override
Returns height of menu item.
Definition: item_horizontalmenu.hpp:83
bool changes_width() const override
Returns true when the width must be recalculated when an action is processed.
Definition: item_horizontalmenu.hpp:77
Definition: menu_item.hpp:23
Definition: item_horizontalmenu.hpp:29
float get_distance() const override
Returns the distance between the items above and below the current menu item.
Definition: item_horizontalmenu.hpp:80
Definition: rectf.hpp:31
void event(const SDL_Event &ev) override
Processes the given event.
Definition: item_horizontalmenu.cpp:213
void process_action(const MenuAction &action) override
Processes the menu action.
Definition: item_horizontalmenu.cpp:195
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Definition: item_horizontalmenu.hpp:41