VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ScreenEntity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Ui/LayoutPath.hpp"
4#include "Ui/WriteCache.hpp"
5
14#include <string_view>
15
16namespace VoltMod
17{
18
19/**
20 * @brief The `custom_hud_layout` entity behind a @ref Screen: spawning, removal, and every write.
21 *
22 * A shared screen writes the layout's global state (@ref EveryoneSlot) or one player's state. The
23 * client shows per-player state for the pawn it is *watching*, so a player screen instead writes
24 * text and classes to the global state of an entity only its owner receives, and writes the cursor
25 * to the owner's own slot. That is what keeps a menu working while its owner is dead or spectating.
26 *
27 * Writes never spawn; @ref EnsureSpawned does. Unchanged values are skipped through @ref WriteCache.
28 */
30{
31public:
32 /** Every reference must outlive this. @p owner is a player slot, or @ref EveryoneSlot for a shared screen. */
35
36 ScreenEntity(const ScreenEntity&) = delete;
38
39 bool Exists() const;
40
41 /** Spawn, or respawn after the player list changed, until the entity covers @p slot. */
42 bool EnsureSpawned(int slot);
43
44 /** Remove the entity and forget everything written to it. Safe to call again. */
45 void Remove();
46
47 void WriteText(int slot, std::string_view variable, std::string_view value);
48 void WriteClass(int slot, std::string_view elementId, std::string_view className, bool on);
49 void WriteCursor(int slot, bool shown);
50
51private:
52 bool IsForPlayer() const noexcept { return _owner != EveryoneSlot; }
53
54 /** The slot a write is cached under, which the cursor is also sent to. A player screen accepts
55 * only its owner or @ref EveryoneSlot. */
56 Result<int> CacheSlotFor(int slot) const;
57
58 /** The slot text and classes are sent to: global on a player screen. */
59 int ContentSlotFor(int cacheSlot) const noexcept;
60
61 Status Spawn();
62 bool SpawnOrWarn();
63
64 int PlayerStateCount() const;
65 bool Covers(int slot) const;
66
67 /** The entity, or why @p engineSlot cannot be written to right now. */
68 Result<CEntityInstance*> EntityForWrite(int engineSlot) const;
69
70 Status SendText(int engineSlot, std::string_view variable, std::string_view value);
71 Status SendClass(int engineSlot, std::string_view elementId, std::string_view className, bool on);
72 Status SendCursor(int engineSlot, bool shown);
73
74 /** On failure, forget the write so the next redraw resends it, and log the first one per slot. */
75 void CheckWrite(int cacheSlot, const Status& status, std::string_view what);
76
77 EntitySystem& _entities;
78 Visibility& _visibility;
79 LayoutPath _layout;
80 int _owner;
81
82 EntityRef _entity;
83 /** Per-player state is sized at spawn, so a later slot is only reachable through a new entity. */
84 bool _playersChangedSinceSpawn = true;
85 WriteCache _written;
86
87 /** Declared last: their handlers touch the members above. */
88 Subscription _slotChanges;
89 Subscription _fullConnects;
90};
91
92} // namespace VoltMod
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
A layout resource the client will accept, checked before anything is spawned.
The custom_hud_layout entity behind a Screen: spawning, removal, and every write.
ScreenEntity & operator=(const ScreenEntity &)=delete
ScreenEntity(const ScreenEntity &)=delete
void WriteText(int slot, std::string_view variable, std::string_view value)
void WriteClass(int slot, std::string_view elementId, std::string_view className, bool on)
bool EnsureSpawned(int slot)
void WriteCursor(int slot, bool shown)
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Owns one registration and releases it on destruction.
Which clients receive which entities, filtered after CheckTransmit.
What each player, or everyone, was last sent on one screen, so an unchanged write is skipped.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::expected< void, Error > Status
Definition Result.hpp:67
constexpr int EveryoneSlot
Definition Slot.hpp:10
std::expected< T, Error > Result
Definition Result.hpp:64