12 std::function<
void(
int slot,
MenuRow& row)> live = {})
14 return [label = std::move(label), kind, enabled = std::move(enabled), live = std::move(live)](
int slot) {
15 MenuRow row{.Label = label,
17 .Enabled = enabled(slot),
32 return [enabled = std::move(enabled), action = std::move(action)](
int slot,
MenuSurface& surface) {
33 if (action && enabled(slot))
35 action(slot, surface);
61 row.
State = get && get(slot);
72 if (!flip || !enabled(slot))
88 std::string value = get ? get(slot) : std::string{};
89 row.
Value = value.empty() ?
"…" : std::move(value);
93 surface.
Prompt(slot, prompt, [set, maxLength](
int s, std::string_view text) {
95 if (maxLength > 0 &&
static_cast<int>(text.size()) > maxLength)
99 return set ? set(s, text) :
true;
116 if (
auto submenu = build(slot))
118 surface.
Open(slot, std::move(submenu));
133 std::function<void(
int slot,
int index)>
Commit;
134 std::optional<ChoiceIndex>
Bind;
144 return std::clamp(index, 0,
static_cast<int>(
Choices.size()) - 1);
151 Bind->Set(slot, index);
167 bool Step(
int slot,
int direction)
179 std::function<
void(
int slot,
int index)> commit, std::optional<ChoiceIndex> bind,
182 auto state = std::make_shared<ChoiceState>(
183 ChoiceState{.Choices = std::move(choices), .Commit = std::move(commit), .Bind = std::move(bind), .Own = index});
187 [state](
int slot,
MenuRow& row) {
189 if (state->Choices.empty())
193 row.
Value = state->Choices[
static_cast<std::size_t
>(state->Selected(slot))];
205 state->Step(slot, +1);
208 .Step = [state, enabled](
int slot,
int direction) {
return enabled(slot) && state->Step(slot, direction); },
214 item.Commit = [state, enabled](
int slot) {
MenuItem ChoiceItem(std::string label, std::vector< std::string > choices, std::function< void(int slot, int index)> commit, std::optional< ChoiceIndex > bind, int index, EnabledCondition enabled, ChoiceApply apply)
static std::function< MenuRow(int)> DescribeRow(std::string label, MenuRowKind kind, EnabledCondition enabled, std::function< void(int slot, MenuRow &row)> live={})
@ Input
Carries a value the player types in chat.
@ Submenu
Opens another menu.
@ Choice
Carries one of several values, stepped left and right.
@ Text
Not selectable; a caption or a summary line.
@ Toggle
Carries an on/off value.
static std::function< void(int, MenuSurface &)> WhenEnabled(EnabledCondition enabled, std::function< void(int, MenuSurface &)> action)
constexpr int WrapIndex(int value, int count)
int Selected(int slot) const
std::optional< ChoiceIndex > Bind
bool Step(int slot, int direction)
void Apply(int slot) const
std::function< void(int slot, int index)> Commit
std::vector< std::string > Choices
void Select(int slot, int index)
std::function< bool(int slot)> Get
std::function< void(int slot)> Flip