ISource2ModMenus
Interface and base implementation for menu system.
Provides functionality for:
- Defining menus and menu options
- Handling per-player menu instances
- Processing menu navigation and input
A menu is an object of the plugin's: a CenterHtmlMenu filled with options, or a class derived from it that overrides the handlers (DisplayTitle, OnOptionSelected, OnMenuEnd). Options can be fixed or worked out per player (text, disabled, visible), open a submenu (Back returns), or be plain text lines. Keys: the numbered options of the page, 7 Back (previous page, else the parent menu), 8 Next, 9 Exit -- so 1-6 options a page.
class ShopMenu : public CenterHtmlMenu
{
public:
ShopMenu() : CenterHtmlMenu("Shop")
{
AddSubmenu("Weapons", &m_weapons);
AddDynamicOption([](CCSPlayerController* p) { return "Credits: " + std::to_string(Credits(p)); }, {})
.Disabled = true;
}
void OnMenuEnd(CCSPlayerController* player, MenuEndReason reason) override { ... }
private:
CenterHtmlMenu m_weapons{"Weapons"};
};Interface for menu system management.
Interface id ISource2ModMenus007 (SOURCE2MOD_MENUS_INTERFACE) · Global g_pSource2ModMenus · Header source2mod/ISource2ModMenus.h
Types
PluginId
using PluginId = int;Methods
OpenCenterHtmlMenu
virtual void OpenCenterHtmlMenu(PluginId owner, CCSPlayerController* player, CenterHtmlMenu* menu) = 0;Opens a CenterHtmlMenu for a player.
| Parameter | Type | Description |
|---|---|---|
owner | PluginId | Plugin the menu belongs to |
player | CCSPlayerController* | Target player |
menu | CenterHtmlMenu* | Menu to open |
GetActiveMenu
virtual IMenuInstance* GetActiveMenu(CCSPlayerController* player) = 0;Gets active menu instance for player.
| Parameter | Type | Description |
|---|---|---|
player | CCSPlayerController* | Target player |
Returns IMenuInstance* — Pointer to active menu instance or nullptr
CloseActiveMenu
virtual void CloseActiveMenu(CCSPlayerController* player) = 0;Closes active menu for player.
| Parameter | Type | Description |
|---|---|---|
player | CCSPlayerController* | Target player |
OnKeyPress
virtual void OnKeyPress(CCSPlayerController* player, int key) = 0;Handles key press input.
| Parameter | Type | Description |
|---|---|---|
player | CCSPlayerController* | Player who pressed key |
key | int | Pressed key (1-9) |
OpenMenu
virtual void OpenMenu(PluginId owner, CCSPlayerController* player, IMenuInstance* instance) = 0;Opens a menu instance another library built.
The instance is drawn, refreshed (OnFrame) and fed the keys by the core like its own, and Destroy()ed when closed, replaced, the player leaves or owner unloads. This is how the CustomhudManager_s2t plugin serves its HudMenu (ICustomhudManager::OpenMenu); a plugin with a screen of its own can do the same.
| Parameter | Type | Description |
|---|---|---|
owner | PluginId | Plugin the menu belongs to (whose unload closes it) |
player | CCSPlayerController* | Target player |
instance | IMenuInstance* | Built with new by the caller; the core owns it from here |
CloseMenu
virtual void CloseMenu(IMenu* menu) = 0;Closes a menu for everyone who has it open, also where it is a submenu they are in (MenuEndReason::Closed). Call it before destroying a menu that may be open.
| Parameter | Type |
|---|---|
menu | IMenu* |
IsMenuOpen
virtual bool IsMenuOpen(CCSPlayerController* player, IMenu* menu) = 0;Whether @p player has @p menu open, as the root or as the submenu they are in or came through.
| Parameter | Type |
|---|---|
player | CCSPlayerController* |
menu | IMenu* |
GetOpenMenu
virtual IMenu* GetOpenMenu(CCSPlayerController* player) = 0;The menu @p player is looking at (the submenu they are in), or nullptr.
| Parameter | Type |
|---|---|
player | CCSPlayerController* |
ChatMenuOption
Represents a single menu option.
Contains display text, state and callback logic.
Fields
| Field | Type | Description |
|---|---|---|
Text | std::string | Display text |
OnSelect | std::function<void(CCSPlayerController*, ChatMenuOption&)> | Callback executed on select |
DisabledEvaluator | std::function<bool(CCSPlayerController*)> | Optional dynamic disabled evaluator, asked for the player it is shown to |
TextEvaluator | std::function<std::string(CCSPlayerController*)> | Text worked out for the player it is shown to (replaces Text) |
VisibleEvaluator | std::function<bool(CCSPlayerController*)> | Whether it shows for the player; not shown (and takes no number) when false |
IMenu
Base interface for menu definitions.
Title
virtual const std::string& Title() const = 0;Gets menu title.
Returns const std::string& — Reference to menu title string
SetTitle
virtual void SetTitle(std::string t) = 0;Sets the menu title.
| Parameter | Type | Description |
|---|---|---|
t | std::string | New title |
Options
virtual std::vector<ChatMenuOption>& Options() = 0;
virtual const std::vector<ChatMenuOption>& Options() const = 0;Gets menu options.
Gets menu options (const).
Returns std::vector<ChatMenuOption>& — Reference to options vector
GetPostSelectAction
virtual PostSelectAction GetPostSelectAction() const = 0;Gets post-select behavior.
Returns PostSelectAction — PostSelectAction value
SetPostSelectAction
virtual void SetPostSelectAction(PostSelectAction a) = 0;Sets post-select behavior.
| Parameter | Type | Description |
|---|---|---|
a | PostSelectAction | Behavior to apply after selection |
GetExitButton
virtual bool GetExitButton() const = 0;Gets exit button state.
Returns bool — True if exit button is enabled
SetExitButton
virtual void SetExitButton(bool v) = 0;Sets exit button state.
| Parameter | Type | Description |
|---|---|---|
v | bool | True to enable exit button |
IBaseMenu
Base implementation of IMenu.
Inherits IMenu
Fields
| Field | Type |
|---|---|
s_canSelect | std::function<bool(CCSPlayerController*)> |
s_onSelect | std::function<void(CCSPlayerController*)> |
Constructors
explicit IBaseMenu(std::string title);Constructs a menu.
Title
const std::string& Title() const override;SetTitle
void SetTitle(std::string t) override;| Parameter | Type |
|---|---|
t | std::string |
Options
std::vector<ChatMenuOption>& Options() override;
const std::vector<ChatMenuOption>& Options() const override;ClearOptions
void ClearOptions();Drops every option, for a menu that is rebuilt in place.
GetPostSelectAction
PostSelectAction GetPostSelectAction() const override;SetPostSelectAction
void SetPostSelectAction(PostSelectAction a) override;Sets post-select behavior.
| Parameter | Type | Description |
|---|---|---|
a | PostSelectAction | Behavior to apply |
GetExitButton
bool GetExitButton() const override;SetExitButton
void SetExitButton(bool v) override;Sets exit button state.
| Parameter | Type | Description |
|---|---|---|
v | bool | True to enable exit button |
GetItemsPerPage
int GetItemsPerPage() const;Options a page, 1-6 (keys 7, 8, 9 are Back, Next, Exit); default 5.
SetItemsPerPage
void SetItemsPerPage(int count);| Parameter | Type |
|---|---|
count | int |
GetTimeout
int GetTimeout() const;Seconds after which the menu closes (MenuEndReason::Timeout); 0 = never.
SetTimeout
void SetTimeout(int seconds);| Parameter | Type |
|---|---|
seconds | int |
SetDynamicTitle
void SetDynamicTitle(std::function<std::string(CCSPlayerController*)> title);A title worked out per player (what the default DisplayTitle shows).
| Parameter | Type |
|---|---|
title | std::function<std::string(CCSPlayerController*)> |
SetEndHandler
void SetEndHandler(std::function<void(CCSPlayerController*, MenuEndReason)> handler);What the default OnMenuEnd calls.
| Parameter | Type |
|---|---|
handler | std::function<void(CCSPlayerController*, MenuEndReason)> |
DisplayTitle
virtual std::string DisplayTitle(CCSPlayerController* player);The title shown to a player. Override for a title of your own; the default is the dynamic title if set, else Title().
| Parameter | Type |
|---|---|
player | CCSPlayerController* |
OnOptionSelected
virtual void OnOptionSelected(CCSPlayerController* player, ChatMenuOption& option, int index);An option was picked (not a submenu, not text). The default runs its OnSelect. @p index is its index in Options().
| Parameter | Type |
|---|---|
player | CCSPlayerController* |
option | ChatMenuOption& |
index | int |
OnMenuEnd
virtual void OnMenuEnd(CCSPlayerController* player, MenuEndReason reason);The menu (opened as the root) went away for a player. The default calls the end handler. Not called when the owning plugin unloads.
| Parameter | Type |
|---|---|
player | CCSPlayerController* |
reason | MenuEndReason |
AddSubmenu
ChatMenuOption& AddSubmenu(std::string display, IBaseMenu* submenu, bool disabled = false);An option that opens @p submenu.
| Parameter | Type | Default |
|---|---|---|
display | std::string | |
submenu | IBaseMenu* | |
disabled | bool | false |
AddText
ChatMenuOption& AddText(std::string text);A line of text that cannot be picked and takes no number.
| Parameter | Type |
|---|---|
text | std::string |
AddDynamicOption
ChatMenuOption& AddDynamicOption(std::function<std::string(CCSPlayerController*)> text, std::function<void(CCSPlayerController*, ChatMenuOption&)> onSelect, std::function<bool(CCSPlayerController*)> disabledEvaluator = nullptr, std::function<bool(CCSPlayerController*)> visibleEvaluator = nullptr);An option whose text, and optionally disabled / visible state, is worked out per player.
| Parameter | Type | Default |
|---|---|---|
text | std::function<std::string(CCSPlayerController*)> | |
onSelect | std::function<void(CCSPlayerController*, ChatMenuOption&)> | |
disabledEvaluator | std::function<bool(CCSPlayerController*)> | nullptr |
visibleEvaluator | std::function<bool(CCSPlayerController*)> | nullptr |
AddMenuOption
ChatMenuOption& AddMenuOption(std::string display, std::function<void(CCSPlayerController*, ChatMenuOption&)> onSelect, bool disabled = false);Adds a menu option.
| Parameter | Type | Default | Description |
|---|---|---|---|
display | std::string | Display text | |
onSelect | std::function<void(CCSPlayerController*, ChatMenuOption&)> | Callback executed on selection | |
disabled | bool | false | Whether option is disabled |
Returns ChatMenuOption& — Reference to created option
AddMenuOptionWithCooldown
ChatMenuOption& AddMenuOptionWithCooldown(std::string optionText, std::function<void(CCSPlayerController*, ChatMenuOption&)> action, bool disabled = false, bool close = true, std::function<bool(CCSPlayerController*)> disabledEvaluator = nullptr);Adds a menu option with cooldown support.
| Parameter | Type | Default | Description |
|---|---|---|---|
optionText | std::string | Display text | |
action | std::function<void(CCSPlayerController*, ChatMenuOption&)> | Callback executed on selection | |
disabled | bool | false | Whether option is disabled |
close | bool | true | Whether menu closes after select |
disabledEvaluator | std::function<bool(CCSPlayerController*)> | nullptr | Optional dynamic disabled evaluator |
Returns ChatMenuOption& — Reference to created option
SetCooldownHandlers
static void SetCooldownHandlers(std::function<bool(CCSPlayerController*)> canSelect, std::function<void(CCSPlayerController*)> onSelect);Sets global cooldown handlers.
| Parameter | Type | Description |
|---|---|---|
canSelect | std::function<bool(CCSPlayerController*)> | Function that checks if player can select |
onSelect | std::function<void(CCSPlayerController*)> | Function called after selection |
IMenuInstance
Runtime instance of a menu bound to a player.
Display
virtual void Display() = 0;Displays the menu.
OnKeyPress
virtual void OnKeyPress(CCSPlayerController* player, int key) = 0;Handles key press input.
| Parameter | Type | Description |
|---|---|---|
player | CCSPlayerController* | Player who triggered input |
key | int | Pressed key (1-9) |
Reset
virtual void Reset();Resets menu state.
Close
virtual void Close() = 0;Closes the menu.
OnFrame
virtual void OnFrame();Once a frame while this is the player's open menu.
The default redraws, which is what a center HTML menu needs (it fades). A menu drawn on a HUD layout overrides it to refresh only when something changed.
AcceptsKeys
virtual bool AcceptsKeys() const;Whether the number keys (chat triggers, binds) pick options.
A menu the player clicks instead returns false and the core drops the key presses while it is open.
Destroy
virtual void Destroy();Destroys the instance.
The core calls this, never delete, when the menu is closed, replaced or the player leaves -- so an instance a plugin built (OpenMenu) is freed by the library that allocated it.
OnEnd
virtual void OnEnd(MenuEndReason reason);The menu is going away for this player, just before Destroy(); not called when the owning plugin unloads. (ISource2ModMenus006)
| Parameter | Type |
|---|---|
reason | MenuEndReason |
KeyMask
virtual uint32_t KeyMask() const;The number keys the menu does something with right now, as a bit mask (bit 1 = key 1 ... bit 9 = key 9). A key outside it is left to the game: with no binds, Source2Mod reads the keys from the weapon slots, and only the keys in the mask are taken. The default takes 1-9. (ISource2ModMenus007)
Menu
IMenu* Menu() const;Gets underlying menu.
Returns IMenu* — Pointer to menu
Player
CCSPlayerController* Player();Gets owning player.
Returns CCSPlayerController* — Pointer to player
CenterHtmlMenu
Styled center HTML menu.
Inherits IBaseMenu
Fields
| Field | Type |
|---|---|
TitleColor | std::string |
EnabledColor | std::string |
DisabledColor | std::string |
PrevPageColor | std::string |
NextPageColor | std::string |
CloseColor | std::string |
Constructors
explicit CenterHtmlMenu(std::string title);Constructs center HTML menu.
Enums
PostSelectAction
enum class PostSelectActionAction executed after selecting a menu option.
| Name | Description |
|---|---|
Close | Close menu after selection |
Reset | Reset menu to first page |
Nothing | Do nothing |
MenuEndReason
enum class MenuEndReasonWhy a menu went away for a player (IBaseMenu::OnMenuEnd).
| Name | Description |
|---|---|
Selected | An option closed it (CloseOnSelect, or PostSelectAction::Close) |
Exit | The player pressed Exit |
Replaced | Another menu was opened over it |
Closed | CloseActiveMenu / CloseMenu |
Disconnected | The player left |
Timeout | The menu's timeout ran out |
Macros
| Macro | Expands to |
|---|---|
OPEN_CENTER_HTML_MENU(player, menu) | g_pSource2ModMenus->OpenCenterHtmlMenu(g_PluginID, player, menu) |
GET_ACTIVE_MENU(player) | g_pSource2ModMenus->GetActiveMenu(player) |
CLOSE_ACTIVE_MENU(player) | g_pSource2ModMenus->CloseActiveMenu(player) |
MENU_ON_KEY_PRESS(player, key) | g_pSource2ModMenus->OnKeyPress(player, key) |