Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Widget<S>

A Widget represents a reusable component able to draw to a terminal. These don't have to be used if you prefer to directly write to the terminal itself, but can simplify the process. The abstract class here contains basic shared methods related to Widgets, like adding/removing children, calculating position, etc.

While Malwoden provides several widgets out of the box, this class can be easily extended to make custom widgets of all shapes and sizes.

Type parameters

  • S

Hierarchy

Index

Constructors

constructor

Methods

absoluteToLocal

  • Transforms a given absolute position to one relative to the widget's local origin.

    Parameters

    • absolutePosition: Vector2

      Vector2

    Returns Vector2

    Vector2

addChild

  • addChild<T>(child: T): T
  • Adds a child widget to this widget. Removes any existing parent from the child first.

    Type parameters

    Parameters

    • child: T

      The child widget

    Returns T

    The child widget

cascadeDraw

  • cascadeDraw(): void
  • Calls the draw() method of this widget and all children widgets recursively. If a widget is disabled it will stop the cascade.

    Returns void

cascadeMouseClick

  • Sends a mouse event through a tree of Widgets. If any widget's onMouseClick returns true, the event will be captured and stop cascading.

    Widgets are called in reverse order, so the last one to render will be the first one called for onMouseClick.

    Parameters

    Returns boolean

cascadeUpdate

  • cascadeUpdate(): void
  • Calls update() for this widget and all children widgets recursively. Children added last will be called last. If a widget is disabled, the update will stop there.

    Returns void

clearMouseContext

  • clearMouseContext(): this

clearUpdateFunc

  • clearUpdateFunc(): this
  • Clears the function run on update.

    Returns this

    The Widget

draw

  • draw(): void
  • Draws on to the given terminal using the widget's current state. Will not draw if disabled.

    Returns void

drawGlyph

  • Draws a glyph on the terminal using a position local to the widget.

    Parameters

    • pos: Vector2

      A local position

    • glyph: Glyph

      The glyph to draw

    Returns void

getAbsoluteOrigin

  • Returns a Vector2 relative to true 0,0, which is usually the top left of the terminal.

    Returns Vector2

    • Vector2

getOrigin

  • Gets the local origin. This will be relative to the parent's origin.

    Returns Vector2

    • Vector2

getState

  • getState(): S
  • Gets the current state of the widget. While not a copy, it's recommended to use setState rather than mutate this object.

    Returns S

isDisabled

  • isDisabled(): boolean
  • Whether or not the widget will update/draw

    Returns boolean

    boolean

localToAbsolute

  • Transforms the given local position to an absolute position.

    Parameters

    Returns Vector2

    Vector2

mouseClick

Abstract onDraw

  • onDraw(): void
  • A method to be implemented by widgets to render to the terminal.

    Returns void

onMouseClick

  • Will be fired on click() or cascadeClick(). Return 'true' to capture the event, which will make it no longer cascade to other widgets. Return 'false' to pass the event to other widgets.

    Parameters

    Returns boolean

    boolean - Capture the event

registerMouseContext

removeChild

  • removeChild<T>(child: T): T | undefined
  • Removes a child widget from the parent widget. Clears inherited Terminal/MouseHandler/KeyboardHandler

    Type parameters

    Parameters

    • child: T

      The child widget

    Returns T | undefined

    The child widget if found, undefined otherwise.

setDisabled

  • setDisabled(disabled?: boolean): this
  • Sets the disabled state.

    Parameters

    • Default value disabled: boolean = true

      Default true

    Returns this

    • The Widget

setMouseHandler

  • Sets the mouseHandler for this widget and all children widgets. Will be passed to any children added in the future as well.

    Parameters

    Returns this

    this

setOrigin

  • Sets the local origin of the widget relative to it's parent, then updates the absoluteOrigin of the widget

    Parameters

    • origin: Vector2

      Vector2 - The position relative to its parent

    Returns this

    • The widget

setParent

  • setParent<T>(parent: T): this
  • Adds the widget to a parent.

    Type parameters

    Parameters

    • parent: T

    Returns this

    this - The child widget

setState

  • setState(state: Partial<S>): this
  • Sets the state of the widget. Partial values allowed.

    Parameters

    • state: Partial<S>

      Partial

    Returns this

    • The widget

setTerminal

  • Sets the terminal for this widget and all children widgets. Will be passed to any children added in the future as well.

    Parameters

    Returns this

    this

setUpdateFunc

  • Set a function to run whenever update or cascadeUpdate is called. Generally this is used with closures/currying to transform game state to function state.

    Parameters

    Returns this

    The widget

update

  • update(): void
  • Calls an updateFunc if previously given, and merges it into the widgets state.

    Returns void

Generated using TypeDoc