TGUI 1.13
Loading...
Searching...
No Matches
MenuWidgetBase.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_MENU_WIDGET_BASE_HPP
26#define TGUI_MENU_WIDGET_BASE_HPP
27
28#include <TGUI/Renderers/MenuWidgetBaseRenderer.hpp>
29#include <TGUI/Text.hpp>
30#include <TGUI/Widget.hpp>
31
33
34namespace tgui
35{
37
43 class TGUI_API MenuWidgetBase : public Widget
44 {
45 public:
48 {
49 String text;
50 bool enabled;
51 std::vector<GetMenusElement> menuItems;
52 };
53
54 public:
56 // Copy constructor
58 MenuWidgetBase(const MenuWidgetBase& other);
59
61 // Move constructor
63 MenuWidgetBase(MenuWidgetBase&& other) noexcept;
64
66 // Copy assignment operator
68 MenuWidgetBase& operator=(const MenuWidgetBase& other);
69
71 // Move assignment operator
73 MenuWidgetBase& operator=(MenuWidgetBase&& other) noexcept;
74
79 [[nodiscard]] MenuWidgetBaseRenderer* getSharedRenderer() override;
80 [[nodiscard]] const MenuWidgetBaseRenderer* getSharedRenderer() const override;
81
87 [[nodiscard]] MenuWidgetBaseRenderer* getRenderer() override;
88
92 virtual void closeMenu() = 0;
93
95
96 protected:
97 struct Menu
98 {
99 Text text;
100 bool enabled = true;
101 int selectedMenuItem = -1;
102 std::vector<Menu> menuItems;
103 };
104
106
107 protected:
113 MenuWidgetBase(const char* typeName, bool initRenderer);
114
124 [[nodiscard]] Signal& getSignal(String signalName) override;
125
131 void rendererChanged(const String& property) override;
132
135 void createMenu(std::vector<Menu>& menus, const String& text);
136
140 [[nodiscard]] Menu* findMenuItemParent(const std::vector<String>& hierarchy,
141 unsigned int parentIndex,
142 std::vector<Menu>& menus,
143 bool createParents);
144
148 [[nodiscard]] const Menu* findMenuItemParent(const std::vector<String>& hierarchy,
149 unsigned int parentIndex,
150 const std::vector<Menu>& menus) const;
151
154 [[nodiscard]] Menu* findMenuItem(const std::vector<String>& hierarchy, std::vector<Menu>& menus);
155
158 [[nodiscard]] const Menu* findMenuItem(const std::vector<String>& hierarchy, const std::vector<Menu>& menus) const;
159
162 void loadMenus(const std::unique_ptr<DataIO::Node>& node, std::vector<Menu>& menus);
163
166 void closeSubMenus(std::vector<Menu>& menus, int& selectedMenu);
167
169 void updateMenuTextColor(Menu& menu, bool selected);
170
172 void updateTextColors(std::vector<Menu>& menus, int selectedMenu);
173
175 void updateTextOpacity(std::vector<Menu>& menus);
176
178 void updateTextFont(std::vector<Menu>& menus);
179
182 [[nodiscard]] float calculateMenuWidth(const Menu& menu) const;
183
186 [[nodiscard]] float getMenuItemHeight(const Menu& menuItem) const;
187
190 [[nodiscard]] float calculateOpenMenuHeight(const std::vector<Menu>& menuItems) const;
191
193 [[nodiscard]] Vector2f calculateSubmenuOffset(
194 const Menu& menu,
195 float globalLeftPos,
196 float menuWidth,
197 float subMenuWidth,
198 bool& openSubMenuToRight) const;
199
201 [[nodiscard]] bool isMouseOnTopOfMenu(Vector2f menuPos, Vector2f mousePos, bool openSubMenuToRight, const Menu& menu, float menuWidth) const;
202
204 [[nodiscard]] bool findMenuItemBelowMouse(
205 Vector2f menuPos,
206 Vector2f mousePos,
207 bool openSubMenuToRight,
208 Menu& menu,
209 float menuWidth,
210 Menu** resultMenu,
211 std::size_t* resultSelectedMenuItem);
212
214 static bool isSeparator(const Menu& menuItem);
215
217 static bool removeMenuImpl(const std::vector<String>& hierarchy,
218 bool removeParentsWhenEmpty,
219 unsigned int parentIndex,
220 std::vector<Menu>& menus);
221
223 static bool removeSubMenusImpl(const std::vector<String>& hierarchy, unsigned int parentIndex, std::vector<Menu>& menus);
224
226 static void setTextSizeImpl(std::vector<Menu>& menus, unsigned int textSize);
227
229 static std::vector<GetMenusElement> getMenusImpl(const std::vector<Menu>& menus);
230
232 static void saveMenus(const std::unique_ptr<DataIO::Node>& parentNode, const std::vector<Menu>& menus);
233
236 void drawMenu(BackendRenderTarget& target,
237 RenderStates states,
238 const Menu& menu,
239 float menuWidth,
240 float globalLeftPos,
241 bool openSubMenuToRight) const;
242
244 void leftMouseReleasedOnMenu(const Menu* menu);
245
247 bool isMouseOnOpenMenu(Vector2f pos, const Menu& menu, Vector2f menuOffset) const;
248
250 void mouseMovedOnMenu(Vector2f pos, Menu& menu, Vector2f menuOffset);
251
253 void deselectDeepestItem(Menu* menu);
254
256 void drawOpenMenu(BackendRenderTarget& target, RenderStates states, const Menu& menu, Vector2f menuOffset) const;
257
262 virtual void emitMenuItemClick(const std::vector<String>& hierarchy) = 0;
263
268 [[nodiscard]] virtual float getDefaultMenuItemHeight() const = 0;
269
274 virtual void leftMouseReleasedOnMenu() = 0;
275
282 [[nodiscard]] virtual bool isMouseOnOpenMenu(Vector2f pos) const = 0;
283
289 virtual void mouseMovedOnMenu(Vector2f pos) = 0;
290
295 virtual void deselectDeepestItem() = 0;
296
303 virtual void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const = 0;
304
306
307 public:
313
315
316 protected:
317 std::shared_ptr<OpenMenuPlaceholder> m_openMenuPlaceholder;
318
319 float m_minimumMenuWidth = 125;
320
321 Sprite m_spriteItemBackground;
322 Sprite m_spriteSelectedItemBackground;
323
324 // Cached renderer properties
325 Color m_backgroundColorCached;
326 Color m_selectedBackgroundColorCached;
327 Color m_textColorCached;
328 Color m_selectedTextColorCached;
329 Color m_textColorDisabledCached;
330 Color m_separatorColorCached = Color::Black;
331 float m_separatorThicknessCached = 1;
332 float m_separatorVerticalPaddingCached = 0;
333 float m_separatorSidePaddingCached = 0;
334 float m_distanceToSideCached = 0;
335
336 bool m_invertedMenuDirection = false; // Only used by MenuBar
337
338 friend class OpenMenuPlaceholder;
339 };
340
345 class OpenMenuPlaceholder : public Widget
346 {
347 public:
348 // Instances of this class can't be copied
349 OpenMenuPlaceholder(const OpenMenuPlaceholder&) = delete;
350 OpenMenuPlaceholder& operator=(const OpenMenuPlaceholder&) = delete;
351
356 explicit OpenMenuPlaceholder(MenuWidgetBase* menuWidget);
357
365 [[nodiscard]] Vector2f getFullSize() const override;
366
374 [[nodiscard]] Vector2f getWidgetOffset() const override;
375
380 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
381
388 void draw(BackendRenderTarget& target, RenderStates states) const override;
389
393 void leftMouseButtonNoLongerDown() override;
394
398 void rightMousePressed(Vector2f pos) override;
399
403 void mouseMoved(Vector2f pos) override;
404
408 void keyPressed(const Event::KeyEvent& event) override;
409
411 // Makes a copy of the widget
413 [[nodiscard]] Widget::Ptr clone() const override;
414
416
417 private:
418 MenuWidgetBase* m_menuWidget;
419 bool m_mouseWasOnMenuWidget = true; // When a menu opens then the mouse will be on top of the menu bar
420 };
421
422 using MenuBarMenuPlaceholder TGUI_DEPRECATED("Use OpenMenuPlaceholder instead") = OpenMenuPlaceholder;
423
425} // namespace tgui
426
428
429#endif // TGUI_MENU_WIDGET_BASE_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
static const Color Black
Black predefined color.
Definition Color.hpp:244
Renderer for the MenuBar widget.
Definition MenuWidgetBaseRenderer.hpp:38
Base class for MenuBar and ContextMenu.
Definition MenuWidgetBase.hpp:44
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
virtual void closeMenu()=0
Closes the menu if it was open.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
SignalItemHierarchy onMenuItemClick
Definition MenuWidgetBase.hpp:312
MenuWidgetBaseRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
MenuWidgetBaseRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
MenuWidgetBase(const char *typeName, bool initRenderer)
Constructor.
Widget that is added to a container when the user clicks on the menu bar. This widget will be added i...
Definition MenuWidgetBase.hpp:346
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of an open me...
Vector2f getFullSize() const override
Returns the entire size that the widget is using.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
OpenMenuPlaceholder(MenuWidgetBase *menuWidget)
Constructor.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1094
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:94
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
KeyPressed event parameters.
Definition Event.hpp:166
Used for return value of getMenus.
Definition MenuWidgetBase.hpp:48
Definition MenuWidgetBase.hpp:98
States used for drawing.
Definition RenderStates.hpp:38