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
15#include <span>
16#include <string_view>
17#include <vector>
18
19namespace VoltMod
20{
21
22/**
23 * @brief The `custom_hud_layout` entity behind a @ref Screen: spawning, removal, and every write.
24 *
25 * The entity is networked to its owner alone. Text and classes go to its global state, which a client
26 * shows whatever pawn it is watching, and the cursor to the owner's own per-player state, so a screen
27 * keeps working while its owner is dead or spectating.
28 *
29 * Writes never spawn; @ref EnsureSpawned does. Unchanged values are skipped through @ref WriteCache.
30 */
32{
33public:
34 /** Every reference must outlive this. @p owner is the player slot the screen belongs to. It is in
35 * @p live for as long as it exists. */
36 ScreenEntity(EntitySystem& entities, SlotEvents& slots, Visibility& visibility, const NetChannels& clients,
37 std::vector<const ScreenEntity*>& live, LayoutPath layout, int owner);
39
40 ScreenEntity(const ScreenEntity&) = delete;
42
43 bool Exists() const;
44
45 int Owner() const noexcept { return _owner; }
46
47 /** Whether the cursor was last shown and the entity is still there. */
48 bool HasCursor() const;
49
50 /** Spawn, or respawn after the player list changed, until the entity covers its owner. */
51 bool EnsureSpawned(int slot);
52
53 /** Remove the entity and forget everything written to it. Safe to call again. */
54 void Remove();
55
56 void WriteText(int slot, std::string_view variable, std::string_view value);
57 void WriteClass(int slot, std::string_view elementId, std::string_view className, bool on);
58 /** Turns `<block>--<name>` on, and off only the other @p names the cache holds on: a new entity has none. */
59 void WriteModifier(int slot, std::string_view elementId, std::string_view block,
60 std::span<const std::string_view> names, std::string_view name);
61 void WriteCursor(int slot, bool shown);
62
63private:
64 /** Whether a write naming @p slot is for this screen: its owner or @ref EveryoneSlot. */
65 bool Accepts(int slot) const noexcept { return slot == _owner || slot == EveryoneSlot; }
66
67 /** On failure, forget the writes so the next redraw resends them, and log the first one. */
68 void CheckWrite(const Status& status, std::string_view what);
69
70 Status Spawn();
71 bool SpawnOrWarn();
72 bool Covers() const;
73
74 // ScreenEntitySetters.cpp: the entity's state and the game's own setters.
75 int PlayerStateCount() const;
76 /** The entity, or why it cannot be written to right now. */
77 Result<CEntityInstance*> ResolveWritable() const;
78
79 Status SendText(std::string_view variable, std::string_view value);
80 Status SendClass(std::string_view elementId, std::string_view className, bool on);
81 Status SendCursor(bool shown);
82
83 EntitySystem& _entities;
84 Visibility& _visibility;
85 const NetChannels& _clients;
86 std::vector<const ScreenEntity*>& _live;
87 LayoutPath _layout;
88 int _owner;
89
90 EntityRef _entity;
91 /** Per-player state is sized at spawn, so a later slot is only reachable through a new entity. */
92 bool _playersChangedSinceSpawn = true;
93 WriteCache _written;
94 /** Cleared with the entity, so a respawn starts without the cursor. */
95 bool _cursor = false;
96
97 /** Declared last: their handlers touch the members above. */
98 Subscription _slotChanges;
99 Subscription _fullConnects;
100};
101
102} // 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.
Per-client network access: latency, userids, usercmd status and the client's replicated userinfo cvar...
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)
int Owner() const noexcept
void WriteModifier(int slot, std::string_view elementId, std::string_view block, std::span< const std::string_view > names, std::string_view name)
ScreenEntity(EntitySystem &entities, SlotEvents &slots, Visibility &visibility, const NetChannels &clients, std::vector< const ScreenEntity * > &live, LayoutPath layout, int owner)
void WriteCursor(int slot, bool shown)
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Which clients receive which entities, filtered after CheckTransmit.
std::expected< void, Error > Status
Definition Result.hpp:67
std::expected< T, Error > Result
Definition Result.hpp:64
constexpr int EveryoneSlot
Definition Slot.hpp:10