VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PlayerScreens.cpp
Go to the documentation of this file.
1#include "Ui/LayoutPath.hpp"
2
6#include <string_view>
7#include <utility>
8
9namespace VoltMod
10{
11
12/** The root element id of @p layout; a layout @ref ScreenManager::Create refuses never shows anyway. */
13static std::string RootElement(std::string_view layout)
14{
15 auto path = LayoutPath::Parse(layout);
16 return path ? std::string(path->Name()) : std::string(layout);
17}
18
19PlayerScreens::PlayerScreens(ScreenManager& screens, std::string layout)
20 : _screens(screens), _layout(std::move(layout)), _root(RootElement(_layout))
21{}
22
24{
25 if (!IsValidSlot(slot))
26 {
27 return _empty;
28 }
29
30 std::optional<Screen>& screen = _created[slot];
31 if (!screen)
32 {
33 auto created = _screens.Create(_layout, slot);
34 // Kept empty rather than retried: what Create refuses for lasts the whole load.
35 if (!created)
36 {
37 Log::Warn("Screen '{}': no player screen for slot {} ({}).", _layout, slot, created.error().Detail);
38 }
39 screen = created ? std::move(*created) : Screen();
40 }
41 return *screen;
42}
43
45{
46 if (!IsValidSlot(slot) || !_created[slot])
47 {
48 return nullptr;
49 }
50 return &*_created[slot];
51}
52
54{
55 Screen& screen = For(slot);
56 return screen.EnsureSpawned(slot) ? &screen : nullptr;
57}
58
60{
61 Screen* screen = FindSpawned(slot);
62 if (!screen)
63 {
64 return nullptr;
65 }
66 screen->SetHidden(slot, _root, false);
67 screen->ShowCursor(slot, true);
68 return screen;
69}
70
72{
73 Screen* screen = Find(slot);
74 if (!screen || !*screen)
75 {
76 return;
77 }
78 screen->ShowCursor(slot, false);
79 screen->Remove();
80}
81
82bool PlayerScreens::IsOpen(int slot) const
83{
84 return IsValidSlot(slot) && _created[slot] && _created[slot]->HasCursor();
85}
86
87} // namespace VoltMod
static Result< LayoutPath > Parse(std::string_view layout)
Screen & For(int slot)
Screen * FindSpawned(int slot)
Screen * Open(int slot)
Screen * Find(int slot)
bool IsOpen(int slot) const
PlayerScreens(ScreenManager &screens, std::string layout)
Create Screen objects and report their button presses.
A spawned Panorama layout created by ScreenManager.
Definition Screen.hpp:23
void ShowCursor(int slot, bool shown)
Definition Screen.cpp:64
void SetHidden(int slot, std::string_view elementId, bool hidden)
Definition Screen.cpp:45
bool EnsureSpawned(int slot)
Definition Screen.cpp:24
void Remove()
Definition Screen.cpp:72
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
static std::string RootElement(std::string_view layout)