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.

Closed for you if the owning plugin unloads while it is open. The menu object and its option handlers live inside that plugin's library, and the next key press would go looking for them there.
ParameterTypeDescription
ownerPluginIdPlugin the menu belongs to
playerCCSPlayerController*Target player
menuCenterHtmlMenu*Menu to open

GetActiveMenu

virtual IMenuInstance* GetActiveMenu(CCSPlayerController* player) = 0;

Gets active menu instance for player.

ParameterTypeDescription
playerCCSPlayerController*Target player

Returns IMenuInstance* — Pointer to active menu instance or nullptr

CloseActiveMenu

virtual void CloseActiveMenu(CCSPlayerController* player) = 0;

Closes active menu for player.

ParameterTypeDescription
playerCCSPlayerController*Target player

OnKeyPress

virtual void OnKeyPress(CCSPlayerController* player, int key) = 0;

Handles key press input.

ParameterTypeDescription
playerCCSPlayerController*Player who pressed key
keyintPressed 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.

ParameterTypeDescription
ownerPluginIdPlugin the menu belongs to (whose unload closes it)
playerCCSPlayerController*Target player
instanceIMenuInstance*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.

ParameterType
menuIMenu*

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.

ParameterType
playerCCSPlayerController*
menuIMenu*

GetOpenMenu

virtual IMenu* GetOpenMenu(CCSPlayerController* player) = 0;

The menu @p player is looking at (the submenu they are in), or nullptr.

ParameterType
playerCCSPlayerController*

ChatMenuOption

Represents a single menu option.

Contains display text, state and callback logic.

Fields

FieldTypeDescription
Textstd::stringDisplay text
OnSelectstd::function<void(CCSPlayerController*, ChatMenuOption&)>Callback executed on select
DisabledEvaluatorstd::function<bool(CCSPlayerController*)>Optional dynamic disabled evaluator, asked for the player it is shown to
TextEvaluatorstd::function<std::string(CCSPlayerController*)>Text worked out for the player it is shown to (replaces Text)
VisibleEvaluatorstd::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.

ParameterTypeDescription
tstd::stringNew 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.

ParameterTypeDescription
aPostSelectActionBehavior 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.

ParameterTypeDescription
vboolTrue to enable exit button

IBaseMenu

Base implementation of IMenu.

Inherits IMenu

Fields

Constructors

explicit IBaseMenu(std::string title);

Constructs a menu.

Title

const std::string& Title() const override;

SetTitle

void SetTitle(std::string t) override;
ParameterType
tstd::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.

ParameterTypeDescription
aPostSelectActionBehavior to apply

GetExitButton

bool GetExitButton() const override;

SetExitButton

void SetExitButton(bool v) override;

Sets exit button state.

ParameterTypeDescription
vboolTrue 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);
ParameterType
countint

GetTimeout

int GetTimeout() const;

Seconds after which the menu closes (MenuEndReason::Timeout); 0 = never.

SetTimeout

void SetTimeout(int seconds);
ParameterType
secondsint

SetDynamicTitle

void SetDynamicTitle(std::function<std::string(CCSPlayerController*)> title);

A title worked out per player (what the default DisplayTitle shows).

SetEndHandler

void SetEndHandler(std::function<void(CCSPlayerController*, MenuEndReason)> handler);

What the default OnMenuEnd calls.

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().

ParameterType
playerCCSPlayerController*

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().

ParameterType
playerCCSPlayerController*
optionChatMenuOption&
indexint

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.

ParameterType
playerCCSPlayerController*
reasonMenuEndReason

AddSubmenu

ChatMenuOption& AddSubmenu(std::string display, IBaseMenu* submenu, bool disabled = false);

An option that opens @p submenu.

ParameterTypeDefault
displaystd::string
submenuIBaseMenu*
disabledboolfalse

AddText

ChatMenuOption& AddText(std::string text);

A line of text that cannot be picked and takes no number.

ParameterType
textstd::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.

AddMenuOption

ChatMenuOption& AddMenuOption(std::string display, std::function<void(CCSPlayerController*, ChatMenuOption&)> onSelect, bool disabled = false);

Adds a menu option.

ParameterTypeDefaultDescription
displaystd::stringDisplay text
onSelectstd::function<void(CCSPlayerController*, ChatMenuOption&)>Callback executed on selection
disabledboolfalseWhether 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.

ParameterTypeDefaultDescription
optionTextstd::stringDisplay text
actionstd::function<void(CCSPlayerController*, ChatMenuOption&)>Callback executed on selection
disabledboolfalseWhether option is disabled
closebooltrueWhether menu closes after select
disabledEvaluatorstd::function<bool(CCSPlayerController*)>nullptrOptional 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.

ParameterTypeDescription
canSelectstd::function<bool(CCSPlayerController*)>Function that checks if player can select
onSelectstd::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.

ParameterTypeDescription
playerCCSPlayerController*Player who triggered input
keyintPressed 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)

ParameterType
reasonMenuEndReason

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)

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

FieldType
TitleColorstd::string
EnabledColorstd::string
DisabledColorstd::string
PrevPageColorstd::string
NextPageColorstd::string
CloseColorstd::string

Constructors

explicit CenterHtmlMenu(std::string title);

Constructs center HTML menu.

Enums

PostSelectAction

enum class PostSelectAction

Action executed after selecting a menu option.

NameDescription
CloseClose menu after selection
ResetReset menu to first page
NothingDo nothing
enum class MenuEndReason

Why a menu went away for a player (IBaseMenu::OnMenuEnd).

NameDescription
SelectedAn option closed it (CloseOnSelect, or PostSelectAction::Close)
ExitThe player pressed Exit
ReplacedAnother menu was opened over it
ClosedCloseActiveMenu / CloseMenu
DisconnectedThe player left
TimeoutThe menu's timeout ran out

Macros

MacroExpands 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)

On this page