Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Add extension methods to add local synchronized actions to buttons #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use generic parameter for Action parameter and add detail to summaries.
Banane9 committed May 14, 2023
commit ecf82db910bc2cedf6e714dd935683d6553e7238
82 changes: 38 additions & 44 deletions NeosModLoader/Utility/LocalButtonPressActionExtensions.cs
Original file line number Diff line number Diff line change
@@ -16,46 +16,44 @@ namespace NeosModLoader.Utility
public static class LocalButtonPressActionExtensions
{
/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/> and <paramref name="action"/>.
/// Creates a <see cref="Button"/> using the given <paramref name="text"/> and <paramref name="action"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="text">The text displayed on the button.</param>
/// <param name="action">The action to run when pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton(this UIBuilder builder, in LocaleString text, Action<IButton> action)
{
return builder.Button(text).SetupLocalAction(action);
}
public static Button LocalActionButton(this UIBuilder builder, in LocaleString text, Action<Button> action)
=> builder.Button(text).SetupLocalAction(action);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="icon"/> and <paramref name="action"/>.
/// Creates a <see cref="Button"/> using the given <paramref name="icon"/> and <paramref name="action"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="icon">The icon displayed on the button.</param>
/// <param name="action">The action to run when pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton(this UIBuilder builder, Uri icon, Action<IButton> action)
{
return builder.Button(icon).SetupLocalAction(action);
}
public static Button LocalActionButton(this UIBuilder builder, Uri icon, Action<Button> action)
=> builder.Button(icon).SetupLocalAction(action);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/>,
/// <paramref name="icon"/> and <paramref name="action"/>.
/// <paramref name="icon"/> and <paramref name="action"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="icon">The icon displayed on the button.</param>
/// <param name="text">The text displayed on the button.</param>
/// <param name="action">The action to run when pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton(this UIBuilder builder, Uri icon, LocaleString text, Action<IButton> action)
{
return builder.Button(icon, text).SetupLocalAction(action);
}
public static Button LocalActionButton(this UIBuilder builder, Uri icon, LocaleString text, Action<Button> action)
=> builder.Button(icon, text).SetupLocalAction(action);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/>,
/// <paramref name="icon"/>, tints and <paramref name="action"/>.
/// <paramref name="icon"/>, tints and <paramref name="action"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="icon">The icon displayed on the button.</param>
@@ -64,42 +62,39 @@ public static Button LocalActionButton(this UIBuilder builder, Uri icon, LocaleS
/// <param name="spriteTint">The tint of the icon.</param>
/// <param name="action">The action to run when pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton(this UIBuilder builder, Uri icon, LocaleString text, in color tint, in color spriteTint, Action<IButton> action)
{
return builder.Button(icon, text, tint, spriteTint).SetupLocalAction(action);
}
public static Button LocalActionButton(this UIBuilder builder, Uri icon, LocaleString text, in color tint, in color spriteTint, Action<Button> action)
=> builder.Button(icon, text, tint, spriteTint).SetupLocalAction(action);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.
/// Creates a <see cref="Button"/> using the given <paramref name="text"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="T">The type of the extra argument to pass to the action.</typeparam>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="text">The text displayed on the button.</param>
/// <param name="action">The action to run when pressed.</param>
/// <param name="argument">The extra argument to pass to the action when this button is pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton<T>(this UIBuilder builder, in LocaleString text, Action<IButton, T> action, T argument)
{
return builder.Button(text).SetupLocalAction(action, argument);
}
public static Button LocalActionButton<T>(this UIBuilder builder, in LocaleString text, Action<Button, T> action, T argument)
=> builder.Button(text).SetupLocalAction(action, argument);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="icon"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.
/// Creates a <see cref="Button"/> using the given <paramref name="icon"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="T">The type of the extra argument to pass to the action.</typeparam>
/// <param name="builder">The builder to use for creating the button.</param>
/// <param name="icon">The icon displayed on the button.</param>
/// <param name="action">The action to run when pressed.</param>
/// <param name="argument">The extra argument to pass to the action when this button is pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, Action<IButton, T> action, T argument)
{
return builder.Button(icon).SetupLocalAction(action, argument);
}
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, Action<Button, T> action, T argument)
=> builder.Button(icon).SetupLocalAction(action, argument);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/>,
/// <paramref name="icon"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.
/// <paramref name="icon"/> and <paramref name="action"/> with an extra <paramref name="argument"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="T">The type of the extra argument to pass to the action.</typeparam>
/// <param name="builder">The builder to use for creating the button.</param>
@@ -108,14 +103,13 @@ public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, Acti
/// <param name="action">The action to run when pressed.</param>
/// <param name="argument">The extra argument to pass to the action when this button is pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, LocaleString text, Action<IButton, T> action, T argument)
{
return builder.Button(icon, text).SetupLocalAction(action, argument);
}
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, LocaleString text, Action<Button, T> action, T argument)
=> builder.Button(icon, text).SetupLocalAction(action, argument);

/// <summary>
/// Creates a <see cref="Button"/> using the given <paramref name="text"/>,
/// <paramref name="icon"/>, tints and <paramref name="action"/> with an extra <paramref name="argument"/>.
/// <paramref name="icon"/>, tints and <paramref name="action"/> with an extra <paramref name="argument"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="T">The type of the extra argument to pass to the action.</typeparam>
/// <param name="builder">The builder to use for creating the button.</param>
@@ -126,19 +120,18 @@ public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, Loca
/// <param name="action">The action to run when pressed.</param>
/// <param name="argument">The extra argument to pass to the action when this button is pressed.</param>
/// <returns>The button created by the <see cref="UIBuilder"/>.</returns>
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, LocaleString text, in color tint, in color spriteTint, Action<IButton, T> action, T argument)
{
return builder.Button(icon, text, tint, spriteTint).SetupLocalAction(action, argument);
}
public static Button LocalActionButton<T>(this UIBuilder builder, Uri icon, LocaleString text, in color tint, in color spriteTint, Action<Button, T> action, T argument)
=> builder.Button(icon, text, tint, spriteTint).SetupLocalAction(action, argument);

/// <summary>
/// Sets up an <see cref="IButton"/> with the given <paramref name="action"/>.
/// Sets up an <see cref="IButton"/> with the given <paramref name="action"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="TButton">The specific type of the button.</typeparam>
/// <param name="button">The button to set up with an action.</param>
/// <param name="action">The action to run when pressed.</param>
/// <returns>The unchanged button.</returns>
public static TButton SetupLocalAction<TButton>(this TButton button, Action<IButton> action) where TButton : IButton
public static TButton SetupLocalAction<TButton>(this TButton button, Action<TButton> action) where TButton : IButton
{
var valueField = button.Slot.AttachComponent<ValueField<bool>>().Value;
valueField.OnValueChange += field => action(button);
@@ -150,15 +143,16 @@ public static TButton SetupLocalAction<TButton>(this TButton button, Action<IBut
}

/// <summary>
/// Sets up an <see cref="IButton"/> with the given <paramref name="action"/> and extra <paramref name="argument"/>.
/// Sets up an <see cref="IButton"/> with the given <paramref name="action"/> and extra <paramref name="argument"/>.<br/>
/// The <paramref name="action"/> will be triggerable by anyone, as long as the creating <see cref="User"/> hasn't left the session.
/// </summary>
/// <typeparam name="TButton">The specific type of the button.</typeparam>
/// <typeparam name="TArgument">The type of the extra argument to pass to the action.</typeparam>
/// <param name="button">The button to set up with an action.</param>
/// <param name="action">The action to run when pressed.</param>
/// <param name="argument">The extra argument to pass to the action when this button is pressed.</param>
/// <returns>The unchanged button.</returns>
public static TButton SetupLocalAction<TButton, TArgument>(this TButton button, Action<IButton, TArgument> action, TArgument argument) where TButton : IButton
public static TButton SetupLocalAction<TButton, TArgument>(this TButton button, Action<TButton, TArgument> action, TArgument argument) where TButton : IButton
{
var valueField = button.Slot.AttachComponent<ValueField<bool>>().Value;
valueField.OnValueChange += field => action(button, argument);