# ClickableWidgetHelper

`com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper`

**Since:** 1.0.5

Wraps a game widget so scripts can inspect and control it; other widget helpers
 extend this base. Use the `Widgets` types, such as `Widgets.Button` and
 `Widgets.Slider`, to identify a specific widget helper.  
 
 Each helper refers to its wrapped widget, so changes through a helper affect that
 widget. Screens may return new helpers for the same widget on separate queries.  
 
 Tooltips and `zIndex` are held by the helper for JsMacros' rendering; the wrapped
 widget state comes from the game.  
 
 A script builds its own widgets with a builder rather than by hand: `
 IScreen.buttonBuilder()` and its siblings hand back a builder whose `build()`
 puts the widget on the screen and gives the helper to go on with. Because opening a
 screen clears earlier elements, build widgets inside `IScreen.setOnInit`.  
 ```js
// a button on a screen of the script's own
const screen = Hud.createScreen("a screen", true);  
screen.setOnInit(JavaWrapper.methodToJava(function (self) {
  self.buttonBuilder()
     .pos(10, 20)
     .size(100, 20)
     .message("press me")
     .action(JavaWrapper.methodToJava(function (btn) {
       // the callback is given the button, so it can change the button
       btn.setLabel("pressed");  
       btn.setActive(false);  
     }))
     .build();  

  // and the same button widget, wrapped as the screen hands it over
  for (const widget of self.getButtonWidgets()) {
    if (!(widget instanceof Widgets.Button)) continue;  
    Chat.log(`button ${widget.getLabel()} at ${widget.getX()}, ${widget.getY()}`);  
  }
}));  
Hud.openScreen(screen);  
 ```

Available in scripts as `Widgets.Clickable`.

## Type Parameters

- `B`: the concrete helper type, used for fluent method returns
- `T`: the wrapped Minecraft widget type

## Constructors

### new ClickableWidgetHelper {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_constructor-T-}

```js
new ClickableWidgetHelper(T extends AbstractWidget btn)
```

Wraps `btn` with z-index `0` and an empty tooltip list.

**Parameters:**
- `btn`: the widget to wrap

### new ClickableWidgetHelper {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_constructor-T-int-}

```js
new ClickableWidgetHelper(T extends AbstractWidget btn, int zIndex)
```

Wraps `btn` with the supplied helper draw-ordering value and an empty tooltip list.

**Parameters:**
- `btn`: the widget to wrap
- `zIndex`: the helper's draw-ordering value

## Fields

### tooltips {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_tooltips}

```js
tooltips: List<Component>
```

Mutable tooltip components held by this helper; initially empty.

### zIndex {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_zIndex}

```js
zIndex: int
```

The value used by this helper's screen for widget draw ordering.

## Methods

### addTooltip {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_addTooltip-Object-}

```js
addTooltip(Object tooltip): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

appends a tooltip, preserving insertion order. [TextBuilder](../../classes/TextBuilder.md) and
 [TextHelper](../TextHelper.md) values retain styling; other values are converted with
 `toString()` to plain text.

**Parameters:**
- `tooltip`: the tooltip to add

**Returns:** self for chaining.

**Since:** 1.8.4

### click {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_click-}

```js
click(): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

sends a left-button press and release at the widget's top-left coordinates. Off the
 client thread, waits for the queued input task to run; on the client thread it runs
 immediately.

**Returns:** self for chaining.

**Since:** 1.3.1

### click {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_click-boolean-}

```js
click(boolean await): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

sends a left-button press and release at the widget's top-left coordinates. Off the
 client thread, queues the input task; `await` controls whether this call
 waits for that task. On the client thread, the input runs immediately and the flag
 has no effect.

**Parameters:**
- `await`: should wait for button to finish clicking.

**Returns:** self for chaining.

**Since:** 1.3.1

### clickedOn {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_clickedOn-IScreen-}

```js
static clickedOn(IScreen screen): void
```

Marks the next release to be skipped on a container screen. Other screen types
 are unchanged.  
 ```js
const screen = Hud.createScreen("a screen", true);  
screen.setOnInit(JavaWrapper.methodToJava(function (self) {
  self.buttonBuilder()
     .pos(10, 20)
     .size(100, 20)
     .message("press me")
     .action(JavaWrapper.methodToJava(function (btn) {
       Widgets.Clickable.clickedOn(self);  
     }))
     .build();  
}));  
Hud.openScreen(screen);  
 ```

**Parameters:**
- `screen`: the screen the widget belongs to

### getActive {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getActive-}

```js
getActive(): boolean
```

returns the widget's active flag. Inactive widgets reject mouse clicks; this flag
 does not indicate whether the button's action will succeed.

**Returns:** button clickable state.

**Since:** 1.0.5

### getHeight {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getHeight-}

```js
getHeight(): int
```

Returns the widget's height in pixels.

**Returns:** the height of the button in pixels.

**Since:** 1.8.4

### getLabel {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getLabel-}

```js
getLabel(): TextHelper
```

returns the button's label as a text wrapper, preserving its styling. Use
 `getStringStripFormatting()` on the result for plain text without formatting.

**Returns:** current button text.

**Since:** 1.2.3

**Note:** renamed fro `getText` in 1.3.1

### getParentHeight {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getParentHeight-}

```js
getParentHeight(): int
```

Returns the open screen's height for parent-relative alignment. Requires an open
 screen.

**Returns:** the height of the parent element

**Since:** 1.0.5

### getParentWidth {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getParentWidth-}

```js
getParentWidth(): int
```

Returns the open screen's width for parent-relative alignment. Requires an open
 screen.

**Returns:** the width of the parent element

**Since:** 1.0.5

### getScaledHeight {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getScaledHeight-}

```js
getScaledHeight(): int
```

Returns the widget height used by alignment; this helper applies no scaling.

**Returns:** the scaled height of the widget

**Since:** 1.0.5

### getScaledLeft {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getScaledLeft-}

```js
getScaledLeft(): int
```

Returns the widget's `x` coordinate; this helper applies no scaling.

**Returns:** the position of the scaled element's left side

**Since:** 1.0.5

### getScaledTop {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getScaledTop-}

```js
getScaledTop(): int
```

Returns the widget's top edge; this class has no scaling, so it is its `y` coordinate.

**Returns:** the top edge in screen-coordinate pixels

**Since:** 1.0.5

### getScaledWidth {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getScaledWidth-}

```js
getScaledWidth(): int
```

Returns the widget width used by alignment; this helper applies no scaling.

**Returns:** the scaled width of the widget

**Since:** 1.0.5

### getTooltips {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getTooltips-}

```js
getTooltips(): List<TextHelper>
```

Returns wrappers for the current tooltips in insertion order. The returned list is
 independent; modifying it does not change the widget.

**Returns:** a copy of the tooltips.

**Since:** 1.8.4

### getWidth {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getWidth-}

```js
getWidth(): int
```

Returns the widget's width in pixels.

**Returns:** the width of the button in pixels.

**Since:** 1.0.5

### getX {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getX-}

```js
getX(): int
```

Returns the widget's left edge in screen-coordinate pixels.

**Returns:** the `x` coordinate of the button.

**Since:** 1.0.5

### getY {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getY-}

```js
getY(): int
```

Returns the widget's top edge in screen-coordinate pixels.

**Returns:** the `y` coordinate of the button.

**Since:** 1.0.5

### getZIndex {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_getZIndex-}

```js
getZIndex(): int
```

Returns this helper's z-index, used by its screen for widget draw ordering.

**Returns:** the z-index of this widget

**Since:** 1.0.5

### moveTo {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_moveTo-int-int-}

```js
moveTo(int x, int y): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

Moves the widget to the given screen-coordinate pixel position without changing its size.  
 This is equivalent to [setPos(int, int)](#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setPos-int-int-) and supports the alignment methods.

**Parameters:**
- `x`: the new x position
- `y`: the new y position

**Returns:** self for chaining.

**Since:** 1.0.5

### removeTooltip {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_removeTooltip-int-}

```js
removeTooltip(int index): boolean
```

Removes the tooltip at the zero-based index. An invalid index returns `false`;  
 duplicate tooltips can be distinguished by their positions.

**Parameters:**
- `index`: the index of the tooltip to remove

**Returns:** `true` if the tooltip was removed successfully, `false` otherwise.

**Since:** 1.8.4

### removeTooltip {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_removeTooltip-TextHelper-}

```js
removeTooltip(TextHelper tooltip): boolean
```

Removes the first tooltip whose underlying text component equals the supplied
 component. Returns `false` if no match exists.

**Parameters:**
- `tooltip`: the tooltip to remove

**Returns:** `true` if the tooltip was removed successfully, `false` otherwise.

**Since:** 1.8.4

### render {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_render-GuiGraphics-int-int-float-}

```js
render(GuiGraphics drawContext, int mouseX, int mouseY, float delta): void
```

Renders the wrapped widget and requests tooltip rendering when the pointer is over
 it and its tooltip list is non-empty.

**Parameters:**
- `drawContext`: the graphics the widget is drawn into
- `mouseX`: the pointer's position on the x axis
- `mouseY`: the pointer's position on the y axis
- `delta`: how far through the current tick the drawing is

**Since:** 1.0.5

### setActive {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setActive-boolean-}

```js
setActive(boolean t): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

sets the widget's active flag, controlling whether it accepts mouse clicks.

**Parameters:**
- `t`: whether the button should be clickable

**Returns:** self for chaining.

**Since:** 1.0.5

### setLabel {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setLabel-TextHelper-}

```js
setLabel(TextHelper helper): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

Sets the label from the supplied text component, preserving its styling.

**Parameters:**
- `helper`: the styled text to show on the button

**Returns:** self for chaining.

**Since:** 1.3.1

### setPos {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setPos-int-int-}

```js
setPos(int x, int y): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

Moves the widget to the given screen-coordinate pixel position without changing
 its size.

**Parameters:**
- `x`: the new left edge
- `y`: the new top edge

**Returns:** self for chaining.

**Since:** 1.0.5

### setTooltip {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setTooltip-Object[]-}

```js
setTooltip(Object... tooltips): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

replaces the helper's tooltip list. [TextBuilder](../../classes/TextBuilder.md) and [TextHelper](../TextHelper.md)
 values retain styling; other values are converted with `toString()` to plain
 text. The tooltip list is drawn while the widget is hovered.

**Parameters:**
- `tooltips`: the tooltips to set

**Returns:** self for chaining.

**Since:** 1.8.4

### setWidth {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_setWidth-int-}

```js
setWidth(int width): B extends ClickableWidgetHelper<B, T extends AbstractWidget>
```

sets the widget's width in pixels without changing its height.

**Parameters:**
- `width`: the new width in pixels

**Returns:** self for chaining.

**Since:** 1.0.5

### toString {#com.jsmacrosce.jsmacros.client.api.helper.screen.ClickableWidgetHelper_toString-}

```js
toString(): String
```

Returns a diagnostic string containing the widget message as plain text.

**Returns:** a diagnostic string containing the widget message
