MenuOptionBase
Class MenuOptionBase
Namespace: SwiftlyS2.Core.Menus.OptionsBase
Assembly: SwiftlyS2.CS2.dll
Provides a base implementation for menu options with event-driven behavior.
public abstract class MenuOptionBase : IMenuOption, IDisposableInheritance
Derived
- ButtonMenuOption
- ChoiceMenuOption
- InputMenuOption
- ProgressBarMenuOption
- SelectorMenuOption<T>
- SliderMenuOption
- SubmenuMenuOption
- TextMenuOption
- ToggleMenuOption
Implements
Inherited Members
- object.Equals(object?)
- object.Equals(object?, object?)
- object.GetHashCode()
- object.GetType()
- object.MemberwiseClone()
- object.ReferenceEquals(object?, object?)
- object.ToString()
Constructors
MenuOptionBase()
Creates an instance of SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.
protected MenuOptionBase()Using the parameterless constructor will not enable dynamic text updating features. Derived classes should override the SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.GetDisplayText(SwiftlyS2.Shared.Players.IPlayer%2cSystem.Int32) method to implement custom text style changes.
MenuOptionBase(int, int)
Creates an instance of SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase with dynamic text updating capabilities.
protected MenuOptionBase(int updateIntervalMs, int pauseIntervalMs)- updateIntervalMs int — The interval in milliseconds between text updates.
- pauseIntervalMs int — The pause duration in milliseconds before starting the next text update cycle.
Values less than 1/64f*1000 milliseconds (approximately 15.6ms) are meaningless, as the refresh rate would be higher than the game's frame interval. Both parameters will be automatically clamped to this minimum value.
Properties
BindingText
Gets or sets a function that dynamically provides the text content for this menu option.
public Func<string?>? BindingText { get; set; }When this property is set to a non-null function, the SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.Text property's getter will invoke it to retrieve the current text value on each access. This enables automatic tracking of external string sources. The function can return null to indicate that it wants to fall back to the static SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.Text value, or return an empty string to display nothing.
Setting this property to null will revert to using only the static SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.Text value stored in the backing field.
Example usage:
string myText = "Hello";
option.BindingText = () => myText;
myText = "World"; // option.Text now returns "World"
// Return null to use fallback Text
option.BindingText = () => condition ? playerName : null;CloseAfterClick
Gets or sets a value indicating whether the menu should be closed after handling the click.
public bool CloseAfterClick { get; init; }Comment
Gets or sets the comment content displayed for this menu option.
public string Comment { get; set; }This is a global property. Changing it will affect what all players see.
Enabled
Gets or sets a value indicating whether this option can be interacted with.
public bool Enabled { get; set; }This is a global property. Changing it will affect what all players see.
LineCount
Gets the number of lines this option requests to occupy in the menu.
public virtual int LineCount { get; }MaxWidth
The maximum display width for menu option text in relative units.
public float MaxWidth { get; set; }Menu
Gets or sets the menu that this option belongs to.
public IMenuAPI? Menu { get; }This property will be null until the option is added to a menu via SwiftlyS2.Shared.Menus.IMenuAPI.AddOption(SwiftlyS2.Shared.Menus.IMenuOption). When implementing custom menu options, avoid accessing this property in the constructor as it will not be set yet.
PlaySound
Gets or sets a value indicating whether a sound should play when this option is selected.
public bool PlaySound { get; set; }Tag
Gets or sets an object that contains data about this option.
public object? Tag { get; set; }Text
Gets or sets the text content displayed for this menu option.
public string Text { get; set; }This is a global property. Changing it will affect what all players see.
When SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.BindingText is set to a non-null function, the getter will invoke that function to retrieve the current text value dynamically. If the function returns null, it falls back to this property's static value. Otherwise, it returns the static value stored in the backing field.
Setting this property directly will update the static fallback value without clearing any SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.BindingText binding. This allows SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.BindingText and static text to coexist, with SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.BindingText taking priority. The function is allowed to return an empty string, which will be displayed as-is.
TextSize
Gets or sets the text size for this option.
public MenuOptionTextSize TextSize { get; set; }TextStyle
Gets or sets the text overflow style for this option.
public MenuOptionTextStyle TextStyle { get; set; }Visible
Gets or sets a value indicating whether this option is visible in the menu.
public bool Visible { get; set; }This is a global property. Changing it will affect what all players see.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public virtual void Dispose()~MenuOptionBase()
protected ~MenuOptionBase()GetDisplayText(IPlayer, int)
Gets the display text for this option as it should appear to the specified player.
public virtual string GetDisplayText(IPlayer player, int displayLine = 0)- player IPlayer — The player requesting the display text.
- displayLine int — The display line index of the option.
- string — The formatted display text for the option.
- When
LineCount=1: ThedisplayLineparameter is not needed; return the HTML-formatted string directly.displayLine=0: Return all contentdisplayLine=1: Return only the first line contentdisplayLine=2: Return only the second line content- And so on...
GetEnabled(IPlayer)
Determines whether this option is enabled for the specified player.
public virtual bool GetEnabled(IPlayer player)- player IPlayer — The player to check enabled state for.
- bool — True if the option is enabled for the player; otherwise, false.
GetVisible(IPlayer)
Determines whether this option is visible to the specified player.
public virtual bool GetVisible(IPlayer player)- player IPlayer — The player to check visibility for.
- bool — True if the option is visible to the player; otherwise, false.
IsClickTaskCompleted(IPlayer)
Determines whether the click task for the specified player is completed.
public virtual bool IsClickTaskCompleted(IPlayer player)- player IPlayer — The player to check.
- bool — True if the click task is completed; otherwise, false.
OnClickAsync(IPlayer)
Handles the click action for this option.
public virtual ValueTask OnClickAsync(IPlayer player)- player IPlayer — The player who clicked the option.
- ValueTask — A task that represents the asynchronous operation.
OnValidatingAsync(IPlayer)
Validates whether the specified player can interact with this option.
public virtual ValueTask<bool> OnValidatingAsync(IPlayer player)- player IPlayer — The player to validate.
- ValueTask<bool> — A task that represents the asynchronous operation. The task result is true if validation succeeds; otherwise, false.
PauseTextAnimation()
Pauses the dynamic text animation.
public virtual void PauseTextAnimation()ResumeTextAnimation()
Resumes the dynamic text animation.
public virtual void ResumeTextAnimation()SetEnabled(IPlayer, bool)
Sets the enabled state of this option for a specific player.
public virtual void SetEnabled(IPlayer player, bool enabled)- player IPlayer — The player to set enabled state for.
- enabled bool — True to enable the option for the player; false to disable it.
The per-player enabled state has lower priority than the global SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.Enabled property.
SetVisible(IPlayer, bool)
Sets the visibility of this option for a specific player.
public virtual void SetVisible(IPlayer player, bool visible)- player IPlayer — The player to set visibility for.
- visible bool — True to make the option visible to the player; false to hide it.
The per-player visibility has lower priority than the global SwiftlyS2.Core.Menus.OptionsBase.MenuOptionBase.Visible property.
AfterFormat
Occurs after HTML markup is assembled, allowing customization of the final HTML output.
public event EventHandler<MenuOptionFormattingEventArgs>? AfterFormatBeforeFormat
Occurs before HTML markup is assembled, allowing customization of the text content.
public event EventHandler<MenuOptionFormattingEventArgs>? BeforeFormatClick
Occurs when the option is clicked by a player.
public event AsyncEventHandler<MenuOptionClickEventArgs>? ClickEnabledChanged
Occurs when the enabled state of the option changes.
public event EventHandler<MenuOptionEventArgs>? EnabledChangedTextChanged
Occurs when the text of the option changes.
public event EventHandler<MenuOptionEventArgs>? TextChangedValidating
Occurs before a click is processed, allowing validation and cancellation.
public event EventHandler<MenuOptionValidatingEventArgs>? ValidatingVisibilityChanged
Occurs when the visibility of the option changes.
public event EventHandler<MenuOptionEventArgs>? VisibilityChanged