# Alignable

`com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable`

**Since:** 1.8.4

Provides fluent horizontal and vertical alignment for render elements. Parent-relative
 methods use the overlay parent, or the window when there is no parent; overloads taking
 another `Alignable` position this element against that element. Alignment methods
 move the element and return its concrete self type.  
 ```js
const draw = Hud.createDraw2D();  
const panel = draw.addRect(0, 0, 200, 100, Color.argb(0x80, 0, 0, 0));  
const label = draw.addText("centred", 0, 0, Color.rgb(0xFF, 0xFF, 0xFF), true);  
// the text is put in the middle of the rectangle
label.align(panel, "center", "center");  
draw.register();  
 ```

Available in scripts as `RenderElements.Alignable`.

## Type Parameters

- `B`: the builder class

## Methods

### align {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-String-String-}

```js
align(String horizontal, String vertical): B extends Alignable<B>
```

Aligns this element horizontally and vertically within its parent, with zero offset
 on both axes.

**Parameters:**
- `horizontal`: the horizontal alignment to use
- `vertical`: the vertical alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [align(String, int, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-String-int-String-int-)

### align {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-String-int-String-int-}

```js
align(String horizontal, int horizontalOffset, String vertical, int verticalOffset): B extends Alignable<B>
```

Aligns this element horizontally and vertically within its parent, applying an
 independent offset on each axis.

**Parameters:**
- `horizontal`: the horizontal alignment to use
- `horizontalOffset`: the horizontal offset to use
- `vertical`: the vertical alignment to use
- `verticalOffset`: the vertical offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignHorizontally(String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-String-int-)
- [alignVertically(String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-String-int-)

### align {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-Alignable-String-String-}

```js
align(Alignable<?> other, String horizontal, String vertical): B extends Alignable<B>
```

Aligns this element horizontally and vertically against `other`, with zero
 offset on both axes.

**Parameters:**
- `other`: the element to align to
- `horizontal`: the horizontal alignment to use
- `vertical`: the vertical alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [align(Alignable, String, int, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-Alignable-String-int-String-int-)

### align {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_align-Alignable-String-int-String-int-}

```js
align(Alignable<?> other, String horizontal, int horizontalOffset, String vertical, int verticalOffset): B extends Alignable<B>
```

Aligns this element horizontally and vertically against `other`, applying an
 independent offset on each axis.

**Parameters:**
- `other`: the element to align to
- `horizontal`: the horizontal alignment to use
- `horizontalOffset`: the horizontal offset to use
- `vertical`: the vertical alignment to use
- `verticalOffset`: the vertical offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignHorizontally(Alignable, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-Alignable-String-int-)
- [alignVertically(Alignable, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-Alignable-String-int-)

### alignHorizontally {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-Alignable-String-}

```js
alignHorizontally(Alignable<?> other, String alignment): B extends Alignable<B>
```

Aligns this element horizontally against `other` with zero offset.

**Parameters:**
- `other`: the element to align to
- `alignment`: the alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignHorizontally(Alignable, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-Alignable-String-int-)

### alignHorizontally {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-Alignable-String-int-}

```js
alignHorizontally(Alignable<?> other, String alignment, int offset): B extends Alignable<B>
```

Aligns this element horizontally against `other`. The case-insensitive alignment
 has the form `[left|center|right|x%]On[left|center|right|x%]`; the first part
 selects this element's anchor and the second selects the anchor on `other`.  
 Percentages are whole numbers from 0 to 100. The offset is added to this element's
 resulting x position. Unlike parent-relative alignment, malformed alignments throw.

**Parameters:**
- `other`: the element to align to
- `alignment`: the alignment to use
- `offset`: the offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

### alignHorizontally {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-String-}

```js
alignHorizontally(String alignment): B extends Alignable<B>
```

Aligns this element horizontally within its parent with zero offset.

**Parameters:**
- `alignment`: the alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignHorizontally(String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-String-int-)

### alignHorizontally {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignHorizontally-String-int-}

```js
alignHorizontally(String alignment, int offset): B extends Alignable<B>
```

Aligns this element horizontally within its parent using `left`, `center`,  
 `right`, or an integer percentage from 0 to 100. Percentages position the element
 within the remaining parent width after its own width; the offset is added to its x
 position. Unsupported strings do nothing; a string ending in `%` with a
 non-integer number throws [NumberFormatException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NumberFormatException.html). Out-of-range percentages do nothing.

**Parameters:**
- `alignment`: the alignment to use
- `offset`: the offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

### alignVertically {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-Alignable-String-}

```js
alignVertically(Alignable<?> other, String alignment): B extends Alignable<B>
```

Aligns this element vertically against `other` with zero offset.

**Parameters:**
- `other`: the element to align to
- `alignment`: the alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignVertically(Alignable, String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-Alignable-String-int-)

### alignVertically {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-Alignable-String-int-}

```js
alignVertically(Alignable<?> other, String alignment, int offset): B extends Alignable<B>
```

Aligns this element vertically against `other`. The case-insensitive alignment
 has the form `[top|center|bottom|y%]On[top|center|bottom|y%]`; the first part
 selects this element's anchor and the second selects the anchor on `other`.  
 Percentages are whole numbers from 0 to 100. The offset is added to this element's
 resulting y position. Malformed alignments throw.

**Parameters:**
- `other`: the element to align to
- `alignment`: the alignment to use
- `offset`: the offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

### alignVertically {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-String-}

```js
alignVertically(String alignment): B extends Alignable<B>
```

Aligns this element vertically within its parent with zero offset.

**Parameters:**
- `alignment`: the alignment to use

**Returns:** self for chaining.

**Since:** 1.8.4

**See:**
- [alignVertically(String, int)](#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-String-int-)

### alignVertically {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_alignVertically-String-int-}

```js
alignVertically(String alignment, int offset): B extends Alignable<B>
```

Aligns this element vertically within its parent using `top`, `center`,  
 `bottom`, or an integer percentage from 0 to 100.  
 
 Measured against the parent, which is the overlay the element was added to, or the
 window when it has no parent. `top` is the parent's top edge, `center`
 centres the element in the parent and `bottom` puts the element's bottom edge
 on the parent's bottom edge. A percentage is a share of the space the element does
 not fill, so `0%` is at the top and `100%` is at the bottom.  
 
 A percentage positions the element within the space remaining after its height;  
 the offset is added to the resulting y position. Unsupported strings do nothing.  
 A string ending in `%` with a non-integer number throws
 [NumberFormatException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NumberFormatException.html); out-of-range integers are ignored.

**Parameters:**
- `alignment`: the alignment to use
- `offset`: the offset to use

**Returns:** self for chaining.

**Since:** 1.8.4

### getParentHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getParentHeight-}

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

Returns the height used as the reference by parent-relative alignment.

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

**Since:** 1.8.4

### getParentWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getParentWidth-}

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

Returns the width used as the reference by parent-relative alignment.

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

**Since:** 1.8.4

### getScaledBottom {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledBottom-}

```js
getScaledBottom(): int
```

Returns the bottom edge, calculated as the top edge plus the scaled height.

**Returns:** the position of the scaled element's bottom side.

**Since:** 1.8.4

### getScaledHeight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledHeight-}

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

Returns this element's height after scaling, truncated to an integer; alignment uses
 this height.

**Returns:** the scaled height of the element.

**Since:** 1.8.4

### getScaledLeft {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledLeft-}

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

Returns this element's left edge, used as its x position by alignment methods.

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

**Since:** 1.8.4

### getScaledRight {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledRight-}

```js
getScaledRight(): int
```

Returns the right edge, calculated as the left edge plus the scaled width.

**Returns:** the position of the scaled element's right side.

**Since:** 1.8.4

### getScaledTop {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledTop-}

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

Returns this element's top edge, used as its y position by alignment methods.

**Returns:** the position of the scaled element's top side.

**Since:** 1.8.4

### getScaledWidth {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_getScaledWidth-}

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

Returns the element's width after scaling, truncated to an integer; alignment uses
 this width.

**Returns:** the scaled width of the element.

**Since:** 1.8.4

### moveTo {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_moveTo-int-int-}

```js
moveTo(int x, int y): B extends Alignable<B>
```

Sets this element's position. Implementations determine how its dimensions are
 preserved; the single-axis helpers use this method while retaining the other axis.

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

**Returns:** self for chaining.

**Since:** 1.8.4

### moveToX {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_moveToX-int-}

```js
moveToX(int x): B extends Alignable<B>
```

Moves this element to `x`, retaining its current top coordinate.

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

**Returns:** self for chaining.

**Since:** 1.8.4

### moveToY {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_moveToY-int-}

```js
moveToY(int y): B extends Alignable<B>
```

Moves this element to `y`, retaining its current left coordinate.

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

**Returns:** self for chaining.

**Since:** 1.8.4

### parsePercentage {#com.jsmacrosce.jsmacros.client.api.classes.render.components.Alignable_parsePercentage-String-}

```js
static parsePercentage(String string): int
```

Parses an integer percentage with a trailing `%`. Values outside 0–100 and
 strings without the suffix return `-1`; a suffixed non-integer throws
 [NumberFormatException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NumberFormatException.html).

**Parameters:**
- `string`: the string to parse

**Returns:** the percentage as a whole number from 0 to 100, or `-1` if the string
 has no `%` on the end or the number in front of it is outside that range.

**Since:** 1.8.4
