malena
Why malena
Quick start
Tutorials
API docs
GitHub
GitHub
Tutorials
Loading...
Searching...
No Matches
Graphics
Controls
MenuItem.h
Go to the documentation of this file.
1
//
2
// MenuItem.h
3
//
4
5
#ifndef MALENA_MENUITEM_H
6
#define MALENA_MENUITEM_H
7
8
#pragma once
9
10
#include <
Malena/Core/malena_export.h
>
11
#include <
SFML/Graphics/Texture.hpp
>
12
#include <functional>
13
#include <string>
14
#include <vector>
15
16
namespace
ml
17
{
48
struct
MenuItem
49
{
50
std::string
label
;
51
std::string
shortcut
;
52
const
sf::Texture
*
icon
=
nullptr
;
53
bool
checked
=
false
;
54
bool
enabled
=
true
;
55
bool
separator
=
false
;
56
std::function<void()>
action
;
57
std::vector<MenuItem>
submenu
;
58
59
// ── Factory helpers ───────────────────────────────────────────────────
60
62
static
MenuItem
item
(
const
std::string&
label
,
63
std::function<
void
()>
action
= {},
64
const
std::string&
shortcut
=
""
,
65
const
sf::Texture
*
icon
=
nullptr
)
66
{
67
MenuItem
m;
68
m.
label
=
label
;
69
m.
action
= std::move(
action
);
70
m.
shortcut
=
shortcut
;
71
m.
icon
=
icon
;
72
return
m;
73
}
74
76
static
MenuItem
chk
(
const
std::string&
label
,
77
bool
& ref,
78
std::function<
void
()> onChange = {})
79
{
80
MenuItem
m;
81
m.
label
=
label
;
82
m.
checked
= ref;
83
m.
action
= [&ref, cb = std::move(onChange)]{
84
ref = !ref;
85
if
(cb) cb();
86
};
87
return
m;
88
}
89
91
static
MenuItem
sub
(
const
std::string&
label
,
92
std::vector<MenuItem> items)
93
{
94
MenuItem
m;
95
m.
label
=
label
;
96
m.
submenu
= std::move(items);
97
return
m;
98
}
99
101
static
MenuItem
sep
()
102
{
103
MenuItem
m;
104
m.
separator
=
true
;
105
return
m;
106
}
107
109
static
MenuItem
disabled
(
const
std::string&
label
)
110
{
111
MenuItem
m;
112
m.
label
=
label
;
113
m.
enabled
=
false
;
114
return
m;
115
}
116
117
[[nodiscard]]
bool
hasSubmenu
()
const
{
return
!
submenu
.empty(); }
118
};
119
120
}
// namespace ml
121
122
#endif
// MALENA_MENUITEM_H
Texture.hpp
sf::Texture
malena_export.h
ml
Definition
Component.h:22
ml::MenuItem
A single entry in a MenuBar dropdown or submenu.
Definition
MenuItem.h:49
ml::MenuItem::checked
bool checked
Definition
MenuItem.h:53
ml::MenuItem::chk
static MenuItem chk(const std::string &label, bool &ref, std::function< void()> onChange={})
Checkable item that toggles a bool reference.
Definition
MenuItem.h:76
ml::MenuItem::sep
static MenuItem sep()
Horizontal divider line.
Definition
MenuItem.h:101
ml::MenuItem::enabled
bool enabled
Definition
MenuItem.h:54
ml::MenuItem::item
static MenuItem item(const std::string &label, std::function< void()> action={}, const std::string &shortcut="", const sf::Texture *icon=nullptr)
Standard clickable item.
Definition
MenuItem.h:62
ml::MenuItem::icon
const sf::Texture * icon
Definition
MenuItem.h:52
ml::MenuItem::action
std::function< void()> action
Definition
MenuItem.h:56
ml::MenuItem::shortcut
std::string shortcut
Definition
MenuItem.h:51
ml::MenuItem::sub
static MenuItem sub(const std::string &label, std::vector< MenuItem > items)
Item that opens a submenu.
Definition
MenuItem.h:91
ml::MenuItem::submenu
std::vector< MenuItem > submenu
Definition
MenuItem.h:57
ml::MenuItem::separator
bool separator
Definition
MenuItem.h:55
ml::MenuItem::hasSubmenu
bool hasSubmenu() const
Definition
MenuItem.h:117
ml::MenuItem::label
std::string label
Definition
MenuItem.h:50
ml::MenuItem::disabled
static MenuItem disabled(const std::string &label)
Greyed-out non-interactive label.
Definition
MenuItem.h:109