Skip to content

@lands.io/mod-sdk / ModActionButton

Class: ModActionButton

Abstract base class for mod-defined action buttons. Buttons appear in the game's attack menu.

Example

class DonateButton extends ModActionButton {
  label = 'Donate';
  intentName = 'donate';

  shouldShow(context: ModButtonContext): boolean {
    return context.myPlayer !== null;
  }

  getPayload(context: ModButtonContext) {
    return { amount: 1000 };
  }
}
// Note: Icon is loaded from icon.svg in the button folder

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new ModActionButton(): ModActionButton

Returns

ModActionButton

Properties

cost

Optional cost: number

Cost to display on the button (optional)


disabledFillColor

Optional disabledFillColor: string


disabledStrokeColor

Optional disabledStrokeColor: string


fillColor

Optional fillColor: string


hoverFillColor

Optional hoverFillColor: string


hoverStrokeColor

Optional hoverStrokeColor: string


id

Optional id: string

Unique ID for this button


intentName

Abstract intentName: string

The intent name used when creating the ModIntent


label

Abstract label: string

The label text for the button


strokeColor

Optional strokeColor: string

Methods

getCooldownProgress

getCooldownProgress(context): undefined | number

Optional: report cooldown progress for this button.

Return a number between 0 and 1 while on cooldown, where 0 means "just used" and 1 means "ready". Return undefined if this button has no cooldown or cooldown is not applicable for the current context.

Parameters

Name Type
context ModButtonContext

Returns

undefined | number


getNotificationMessage

getNotificationMessage(context, payload): undefined | string

Get the notification message to display when this button is clicked. Override this to provide custom notification text based on context and payload. Return undefined to show no notification.

Parameters

Name Type
context ModButtonContext
payload unknown

Returns

undefined | string

Example

getNotificationMessage(context: ModButtonContext, payload: unknown): string {
  const { myPlayer } = context;
  const troops = Math.floor((myPlayer?.troops() || 0) * 0.2);
  return `Donated ${troops} troops!`;
}

getPayload

getPayload(context): unknown

Get the payload to send with the intent (default: empty object)

Parameters

Name Type
context ModButtonContext

Returns

unknown


isDisabled

isDisabled(context): boolean

Whether the button is disabled (shown but not clickable)

Parameters

Name Type
context ModButtonContext

Returns

boolean


shouldShow

shouldShow(context): boolean

Determines whether this button should be shown

Parameters

Name Type
context ModButtonContext

Returns

boolean


Source Code

View full implementation
/**
 * Abstract base class for mod-defined action buttons.
 * Buttons appear in the game's attack menu.
 *
 * @example
 * ```typescript
 * class DonateButton extends ModActionButton {
 *   label = 'Donate';
 *   intentName = 'donate';
 *
 *   shouldShow(context: ModButtonContext): boolean {
 *     return context.myPlayer !== null;
 *   }
 *
 *   getPayload(context: ModButtonContext) {
 *     return { amount: 1000 };
 *   }
 * }
 * // Note: Icon is loaded from icon.svg in the button folder
 * ```
 */
export abstract class ModActionButton {
  /** Unique ID for this button */
  id?: string;

  /** The label text for the button */
  abstract label: string;

  /** The intent name used when creating the ModIntent */
  abstract intentName: string;

  /** Determines whether this button should be shown */
  abstract shouldShow(context: ModButtonContext): boolean;

  /** Get the payload to send with the intent (default: empty object) */
  getPayload(context: ModButtonContext): unknown {
    return {};
  }

  /** Cost to display on the button (optional) */
  cost?: number;

  /** Whether the button is disabled (shown but not clickable) */
  isDisabled(context: ModButtonContext): boolean {
    return false;
  }

  /**
   * Get the notification message to display when this button is clicked.
   * Override this to provide custom notification text based on context and payload.
   * Return undefined to show no notification.
   * 
   * @example
   * ```typescript
   * getNotificationMessage(context: ModButtonContext, payload: unknown): string {
   *   const { myPlayer } = context;
   *   const troops = Math.floor((myPlayer?.troops() || 0) * 0.2);
   *   return `Donated ${troops} troops!`;
   * }
   * ```
   */
  getNotificationMessage(context: ModButtonContext, payload: unknown): string | undefined {
    return undefined; // No notification by default
  }

  /**
   * Optional: report cooldown progress for this button.
   *
   * Return a number between 0 and 1 while on cooldown, where 0 means "just used"
   * and 1 means "ready". Return undefined if this button has no cooldown or
   * cooldown is not applicable for the current context.
   */
  getCooldownProgress(context: ModButtonContext): number | undefined {
    return undefined;
  }

  // Styling properties - all optional with defaults
  fillColor?: string;
  disabledFillColor?: string;
  strokeColor?: string;
  disabledStrokeColor?: string;
  hoverFillColor?: string;
  hoverStrokeColor?: string;
}