VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
MenuRouter.cpp
Go to the documentation of this file.
1
#include <
VoltMod/Menu/MenuRouter.hpp
>
2
#include <utility>
3
4
namespace
VoltMod
5
{
6
7
MenuRouter::MenuRouter
(
MenuSurface
&
fallback
) : _fallback(
fallback
) {}
8
9
Subscription
MenuRouter::Prefer
(
MenuSurface
&
surface
)
10
{
11
_preferred = &
surface
;
12
return
Subscription
([
this
, &
surface
] {
13
// A later Prefer replaced this one; leave that in place.
14
if
(_preferred == &
surface
)
15
{
16
_preferred =
nullptr
;
17
}
18
});
19
}
20
21
bool
MenuRouter::OpenSession
(
int
slot
, std::shared_ptr<Menu>
menu
,
MenuOptions
options
)
22
{
23
// One session per player: a menu left on the other surface would stay open and frozen.
24
if
(_preferred)
25
{
26
_preferred->
CloseAll
(
slot
);
27
}
28
_fallback.
CloseAll
(
slot
);
29
30
if
(_preferred && _preferred->
OpenSession
(
slot
,
menu
,
options
))
31
{
32
return
true
;
33
}
34
return
_fallback.
OpenSession
(
slot
, std::move(
menu
),
options
);
35
}
36
37
void
MenuRouter::Open
(
int
slot
, std::shared_ptr<Menu>
menu
)
38
{
39
if
(!
IsOpen
(
slot
))
40
{
41
OpenSession
(
slot
, std::move(
menu
), {});
42
return
;
43
}
44
45
SessionOf(
slot
).
Open
(
slot
, std::move(
menu
));
46
}
47
48
bool
MenuRouter::IsOpen
(
int
slot
)
const
49
{
50
return
(_preferred && _preferred->
IsOpen
(
slot
)) || _fallback.
IsOpen
(
slot
);
51
}
52
53
void
MenuRouter::Close
(
int
slot
)
54
{
55
SessionOf(
slot
).
Close
(
slot
);
56
}
57
58
void
MenuRouter::CloseAll
(
int
slot
)
59
{
60
SessionOf(
slot
).
CloseAll
(
slot
);
61
}
62
63
void
MenuRouter::CloseAll
(
int
slot
, std::string_view
replyKey
)
64
{
65
SessionOf(
slot
).
CloseAll
(
slot
,
replyKey
);
66
}
67
68
void
MenuRouter::Prompt
(
int
slot
, std::string
prompt
, std::function<
bool
(
int
, std::string_view)>
callback
)
69
{
70
SessionOf(
slot
).
Prompt
(
slot
, std::move(
prompt
), std::move(
callback
));
71
}
72
73
std::string
MenuRouter::Translate
(
int
slot
, std::string_view
key
, std::string_view
fallback
)
const
74
{
75
return
SessionOf(
slot
).
Translate
(
slot
,
key
,
fallback
);
76
}
77
78
MenuSurface
& MenuRouter::SessionOf(
int
slot
)
const
79
{
80
return
_preferred && _preferred->
IsOpen
(
slot
) ? *_preferred : _fallback;
81
}
82
83
}
// namespace VoltMod
MenuRouter.hpp
VoltMod::MenuRouter::OpenSession
bool OpenSession(int slot, std::shared_ptr< Menu > menu, MenuOptions options) override
Definition
MenuRouter.cpp:21
VoltMod::MenuRouter::CloseAll
void CloseAll(int slot) override
Definition
MenuRouter.cpp:58
VoltMod::MenuRouter::Close
void Close(int slot) override
Definition
MenuRouter.cpp:53
VoltMod::MenuRouter::Prefer
Subscription Prefer(MenuSurface &surface)
Definition
MenuRouter.cpp:9
VoltMod::MenuRouter::IsOpen
bool IsOpen(int slot) const override
Definition
MenuRouter.cpp:48
VoltMod::MenuRouter::Open
void Open(int slot, std::shared_ptr< Menu > menu) override
Definition
MenuRouter.cpp:37
VoltMod::MenuRouter::Prompt
void Prompt(int slot, std::string prompt, std::function< bool(int slot, std::string_view text)> callback) override
Definition
MenuRouter.cpp:68
VoltMod::MenuRouter::MenuRouter
MenuRouter(MenuSurface &fallback)
Definition
MenuRouter.cpp:7
VoltMod::MenuRouter::Translate
std::string Translate(int slot, std::string_view key, std::string_view fallback) const override
Definition
MenuRouter.cpp:73
VoltMod::MenuSurface
Definition
MenuModel.hpp:73
VoltMod::MenuSurface::Close
virtual void Close(int slot)=0
VoltMod::MenuSurface::Open
virtual void Open(int slot, std::shared_ptr< Menu > menu)=0
VoltMod::MenuSurface::OpenSession
virtual bool OpenSession(int slot, std::shared_ptr< Menu > menu, MenuOptions options)=0
VoltMod::MenuSurface::Prompt
virtual void Prompt(int slot, std::string prompt, std::function< bool(int slot, std::string_view text)> callback)=0
VoltMod::MenuSurface::Translate
virtual std::string Translate(int slot, std::string_view key, std::string_view fallback) const =0
VoltMod::MenuSurface::IsOpen
virtual bool IsOpen(int slot) const =0
VoltMod::MenuSurface::CloseAll
virtual void CloseAll(int slot)=0
VoltMod::Subscription
Owns one registration and releases it on destruction.
Definition
Subscription.hpp:34
VoltMod
Definition
Options.hpp:13
VoltMod::ReadFile
static std::string ReadFile(const std::filesystem::path &path)
Definition
Loader.cpp:56
VoltMod::MenuOptions
Definition
MenuModel.hpp:61
src
Menu
MenuRouter.cpp
Generated by
1.9.8