VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CenterHtmlMenu.hpp
Go to the documentation of this file.
1#pragma once
2
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <string>
21#include <string_view>
22
23namespace VoltMod
24{
25
26/**
27 * @brief Menus drawn as center HTML and driven with W/S/A/D/E/R. It needs no client addon, so
28 * `runtime.Menus` falls back to it for a player another surface cannot draw for.
29 */
30class CenterHtmlMenu final : public MenuSurface
31{
32public:
33 /** All must outlive the menu. */
45
46 explicit CenterHtmlMenu(const Services& services);
47
48 bool OpenSession(int slot, std::shared_ptr<Menu> menu, MenuOptions options) override;
49 void Open(int slot, std::shared_ptr<Menu> menu) override;
50 bool IsOpen(int slot) const override;
51 void Close(int slot) override;
52 void CloseAll(int slot) override;
53 void CloseAll(int slot, std::string_view replyKey) override;
54 void Prompt(int slot, std::string prompt, std::function<bool(int slot, std::string_view text)> callback) override;
55 std::string Translate(int slot, std::string_view key, std::string_view fallback) const override;
56
57private:
58 /** Presses closer together than this are ignored. */
59 static constexpr int64_t PressGapMs = 200;
60 /** Rows can show live values, so an idle page is rebuilt this often. */
61 static constexpr int64_t RenderIntervalMs = 250;
62
63 /** A player's body as a page was sent to it; the game drops the page when this changes. */
64 struct Body
65 {
67 bool Alive = false;
68 Team TeamNum = Team::None;
69
70 bool operator==(const Body&) const = default;
71 };
72
73 /** What one player was last sent. */
74 struct Page
75 {
76 std::string Html;
77 int64_t RenderedAt = 0;
78 int64_t SentAt = 0;
79 Body SentTo;
80 };
81
82 /** One player's place in the menu on top. */
83 struct Cursor
84 {
85 int Selected = 0;
86 uint64_t PrevButtons = 0;
87 int64_t LastInputTime = 0;
88 Page LastSent;
89 };
90
91 /** Push @p menu onto an open session and start the per-frame work. */
92 void Push(int slot, std::shared_ptr<Menu> menu);
93
94 /** Put the cursor back on the first selectable row of whatever is now on top. */
95 void ResetCursor(int slot);
96
97 /** Send the menu on top, or its chat prompt. Rebuilt when @p changed; sent when it differs or
98 * the last send is old. */
99 void Draw(int slot, bool changed);
100 std::string Render(int slot, Menu& menu);
101 Body CurrentBody(int slot) const;
102
103 void OnGameFrame();
104
105 // CenterHtmlControls.cpp
106 /** The W/S/A/D/E/R controls for @p slot. True when a press was consumed. */
107 bool ReadKeys(int slot);
108 /** Move @p slot's cursor to row @p index, applying the value pending on the row it leaves. */
109 void Select(int slot, int index);
110 bool RunKey(int slot, uint64_t pressed);
111 void MoveCursor(int slot, int step);
112 void JumpPage(int slot, int delta);
113
114 Services _services;
115 /** What every menu surface shares. */
116 MenuStack _stack;
117 PerSlot<Cursor> _cursors;
118 /** Declared last: per-frame delivery drops before the state it touches. */
119 Subscription _onFrame;
120};
121
122} // namespace VoltMod
bool OpenSession(int slot, std::shared_ptr< Menu > menu, MenuOptions options) override
void CloseAll(int slot) override
void Open(int slot, std::shared_ptr< Menu > menu) override
CenterHtmlMenu(const Services &services)
void Close(int slot) override
bool IsOpen(int slot) const override
void Prompt(int slot, std::string prompt, std::function< bool(int slot, std::string_view text)> callback) override
std::string Translate(int slot, std::string_view key, std::string_view fallback) const override
Per-player pending-prompt registry for menu free-text input.
Definition ChatInput.hpp:27
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
Holding players still while a menu is open, for every menu surface.
MenuSurface(const MenuSurface &)=delete
Send chat, center print, center HTML, and alert messages.
Definition Messages.hpp:30
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
The one permission and targeting gate, plus the plugin's reply callback.
Definition Policy.hpp:41
One-shot delays and repeating timers, run on the game thread.
Definition Scheduler.hpp:18
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
VoltMod::Translations & Translations