# C API: Player editor module (sona_editor.h)

An optional library, `sona_editor`: the player-facing editor, the same screens as 360sona.com's, drawn by the SDK for keyboard, gamepad, mouse and touch. Include `sona_editor.h` and link `sona_editor`. The guide: [Player editor](../../guides/editor.md).

## sona_editor_defaults

```c
void sona_editor_defaults(sona_editor_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_editor_options *` | The [`sona_editor_options`](../fields.md#sona_editor_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_editor_options x_editor_options;
memset(&x_editor_options, 0, sizeof x_editor_options);
x_editor_options.size = sizeof x_editor_options;
sona_editor_defaults(&x_editor_options);
```

`size` set, 1280 x 720 (`sona_editor_resize` changes it), seed 1, everything else zero.

## sona_editor_theme_defaults

```c
void sona_editor_theme_defaults(sona_editor_theme *t, uint32_t which);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_editor_theme *` | The [`sona_editor_theme`](../fields.md#sona_editor_theme) to fill with the defaults | required |
| `which` | `uint32_t` | Which built-in theme | - |

**Returns** Nothing.

**Example**:

```c
sona_editor_theme x_editor_theme;
memset(&x_editor_theme, 0, sizeof x_editor_theme);
x_editor_theme.size = sizeof x_editor_theme;
sona_editor_theme_defaults(&x_editor_theme, 0);
```

`which`: `SONA_EDITOR_THEME_LIGHT` (360sona.com's editor) or _DARK (the console slate).

## sona_editor_open

```c
sona_result sona_editor_open(sona_ctx *ctx, const sona_editor_options *o, const sona_editor_theme *theme, sona_editor **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `o` | `const sona_editor_options *` | The options, a [`sona_editor_options`](../fields.md#sona_editor_options) | required |
| `theme` | `const sona_editor_theme *` | A [`sona_editor_theme`](../fields.md#sona_editor_theme) | required |
| `out` | `sona_editor **` | Receives the new editor | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

`theme` NULL: the light (site) theme. `SONA_E_ARGUMENT` without a catalogue, for MODE_WARDROBE without a start manifest, or a start manifest that does not parse; `SONA_E_CORRUPT` for font bytes that do not load.

## sona_editor_close

```c
void sona_editor_close(sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |

**Returns** Nothing.

Removes its avatars from the ctx.

## sona_editor_set_theme

```c
sona_result sona_editor_set_theme(sona_editor *ed, const sona_editor_theme *theme);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `theme` | `const sona_editor_theme *` | A [`sona_editor_theme`](../fields.md#sona_editor_theme) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Changes the colours and sizes from the next frame. NULL: the light (site) theme.

## sona_editor_resize

```c
sona_result sona_editor_resize(sona_editor *ed, uint32_t width, uint32_t height);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `width` | `uint32_t` | The picture's width in pixels | - |
| `height` | `uint32_t` | The picture's height in pixels | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

The editor's size in pixels, the size of the target it is drawn into.

## sona_editor_feed

```c
sona_result sona_editor_feed(sona_editor *ed, const sona_editor_input *in);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `in` | `const sona_editor_input *` | A [`sona_editor_input`](../fields.md#sona_editor_input) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

One input event (`SONA_EDITOR_IN_*`): a gamepad button or axis, a key, a typed character for the search box, a pointer or touch, or the wheel.

## sona_editor_update

```c
uint32_t sona_editor_update(sona_editor *ed, double dt);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `dt` | `double` | Seconds since the last call | - |

**Returns** `uint32_t`.

Advances dt seconds: stick repeat, animations, avatar builds and (unless HOST_UPDATES) `sona_update` on the ctx. Returns `SONA_EDITOR_RUNNING`, _DONE or _CANCELLED (and keeps returning it).

## sona_editor_status

```c
uint32_t sona_editor_status(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `uint32_t`.

`SONA_EDITOR_RUNNING`, `_DONE` or `_CANCELLED`, as the last `sona_editor_update` returned.

## sona_editor_result

```c
sona_result sona_editor_result(const sona_editor *ed, uint8_t out[SONA_MANIFEST_BYTES]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `out` | `uint8_t [SONA_MANIFEST_BYTES]` | Receives the manifest's 1,000 bytes | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

The avatar as it stands (at any time): `SONA_MANIFEST_BYTES`. After CANCELLED it is the start manifest.

## sona_editor_set_search

```c
sona_result sona_editor_set_search(sona_editor *ed, const char *text);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `text` | `const char *` | The search text, UTF-8; NULL clears it | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

The search box of the wardrobe and the item screens, for hosts with an on-screen keyboard (UTF-8; NULL clears).

## sona_editor_set_screen

```c
sona_result sona_editor_set_screen(sona_editor *ed, uint32_t screen);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `screen` | `uint32_t` | A `SONA_EDITOR_SCREEN_*` | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Moves to a screen of the mode (a deprecated id to the screen that took its place). `SONA_E_ARGUMENT` for one the mode does not have, `SONA_EDITOR_SCREEN_LOOKS` in `SONA_EDITOR_MODE_FULL` among them.

## sona_editor_screen_get

```c
uint32_t sona_editor_screen_get(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `uint32_t`.

The screen shown now.

## sona_editor_screens

```c
uint32_t sona_editor_screens(const sona_editor *ed, uint32_t *out, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `out` | `uint32_t *` | Receives the values | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

The mode's screens in tab order: writes up to cap of them (out may be NULL) and returns how many there are.

## sona_editor_busy

```c
int sona_editor_busy(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `int`.

1 while an avatar is still building (the shown one or a change on its way); thumbnails do not count. A change that fails to build leaves the last avatar that did on screen; when the first one fails, nothing shows and busy is 0.

## sona_editor_build_failures

```c
uint32_t sona_editor_build_failures(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `uint32_t`.

How many avatar builds failed since open (the screen reader is told each time).

## sona_editor_avatar

```c
sona_avatar *sona_editor_avatar(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `sona_avatar *`.

The avatar on the editor's stage now, or NULL while the first one builds. It is the editor's: read it or hide it,
never remove it. Each choice in the editor makes a new one, so ask again each frame. A host that draws the avatar
layer itself uses it to fade the stage's avatar in and out.

## sona_editor_undo

```c
int sona_editor_undo(sona_editor *ed, int redo);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `redo` | `int` | 1 for redo, 0 for undo | - |

**Returns** `int`.

Undoes the last change (`redo` 0) or redoes the last undone one (`redo` 1), for a host that draws the stage's own
undo and redo buttons. Returns 1 when it stepped, 0 when there was nothing to step to. The editor keeps 32 steps.

## sona_editor_can_undo

```c
int sona_editor_can_undo(const sona_editor *ed, int redo);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `redo` | `int` | 1 for redo, 0 for undo | - |

**Returns** `int`.

1 when `sona_editor_undo` with the same `redo` would step: use it to grey out the buttons.

## sona_editor_zoom

```c
double sona_editor_zoom(sona_editor *ed, double set);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `set` | `double` | A matched set's index | - |

**Returns** `double`.

The stage's zoom: 1 is the editor's own framing, from 0.6 to 3. A `set` above 0 sets it (clamped to that range);
0 only reads it. Returns the zoom now.

## sona_editor_turn

```c
void sona_editor_turn(sona_editor *ed, double degrees);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `degrees` | `double` | How far to turn the avatar, degrees | - |

**Returns** Nothing.

Turns the avatar on the stage by `degrees`, as a drag across the stage does.

## sona_editor_view

```c
sona_result sona_editor_view(const sona_editor *ed, sona_view *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `out` | `sona_view *` | Receives the [`sona_view`](../fields.md#sona_view) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

The camera the avatar layer is drawn with (framed into avatar_rect of the full target).

## sona_editor_draw

```c
sona_result sona_editor_draw(sona_editor *ed, sona_renderer *r, const sona_target *t, const sona_draw_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `r` | `sona_renderer *` | The renderer | required |
| `t` | `const sona_target *` | Where the frame lands, a [`sona_target`](../fields.md#sona_target) | required |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | `NULL`: the defaults |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

The editor into t: `sona_draw` with the editor's view and the theme's background, without the credit (the UI draws it), and the UI layer over it as r's overlay (rasterised on the CPU where it changed, uploaded where it changed, blended by the renderer; r has no overlay again afterwards); then up to two pending thumbnails (`sona_item_preview_snapshot` and `sona_snapshot` on r). opts NULL: defaults; its clear and the NO_CREDIT flag are the editor's. The UI is drawn at the editor's size over the whole target: resize the editor to the target.

## sona_editor_draw_avatar

```c
sona_result sona_editor_draw_avatar(sona_editor *ed, sona_renderer *r, const sona_target *t, const sona_draw_options *opts);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `r` | `sona_renderer *` | The renderer | required |
| `t` | `const sona_target *` | Where the frame lands, a [`sona_target`](../fields.md#sona_target) | required |
| `opts` | `const sona_draw_options *` | The options, a [`sona_draw_options`](../fields.md#sona_draw_options) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT` ([what each means](../../troubleshooting.md#error-codes)).

The avatar layer alone (and the thumbnails), for a host that draws the UI layer with its own 2D pass.

## sona_editor_thumbnails

```c
uint32_t sona_editor_thumbnails(sona_editor *ed, sona_renderer *r, uint32_t budget);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `r` | `sona_renderer *` | The renderer | required |
| `budget` | `uint32_t` | How many pictures to draw this call | - |

**Returns** `uint32_t`.

Renders up to budget pending thumbnails on r, for hosts that draw the avatar layer with their own `sona_draw`.

## sona_editor_draw_data_get

```c
sona_result sona_editor_draw_data_get(sona_editor *ed, sona_editor_draw_data *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `out` | `sona_editor_draw_data *` | Receives the [`sona_editor_draw_data`](../fields.md#sona_editor_draw_data) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

The UI layer as of the last `sona_editor_update`. Pointers stay valid until the next update, resize or close.

## sona_editor_texture_get

```c
sona_result sona_editor_texture_get(sona_editor *ed, uint32_t id, sona_editor_texture *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `id` | `uint32_t` | The id, as the call that made it gave it | - |
| `out` | `sona_editor_texture *` | Receives the [`sona_editor_texture`](../fields.md#sona_editor_texture) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

A texture the draw data names by id, with its pixels (straight RGBA8, owned by the editor) and the rectangle that changed since an older revision, for a host that draws the UI layer itself.

## sona_editor_render_ui

```c
sona_result sona_editor_render_ui(sona_editor *ed, uint8_t *rgba, uint32_t width, uint32_t height, uint32_t stride, int over);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `rgba` | `uint8_t *` | Receives straight RGBA8, row 0 at the top | required |
| `width` | `uint32_t` | The picture's width in pixels | - |
| `height` | `uint32_t` | The picture's height in pixels | - |
| `stride` | `uint32_t` | Bytes from one row to the next | - |
| `over` | `int` | 1 to blend over what `rgba` holds, 0 to replace it | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

The UI layer rasterised on the CPU into width x height straight RGBA8 (stride in bytes, 0: width * 4). over 1: blended over what rgba holds (a snapshot of the avatar layer); 0: cleared to transparent first.

## sona_editor_snapshot

```c
sona_result sona_editor_snapshot(sona_editor *ed, sona_renderer *r, uint8_t *rgba);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `r` | `sona_renderer *` | The renderer | required |
| `rgba` | `uint8_t *` | Receives straight RGBA8, row 0 at the top | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CONTEXT`, `SONA_E_READBACK` ([what each means](../../troubleshooting.md#error-codes)).

The whole picture into rgba (width * height * 4 of the editor's size): `sona_snapshot` of the avatar layer, then render_ui over it. For screenshots, tests and servers.

## sona_editor_control_count

```c
uint32_t sona_editor_control_count(const sona_editor *ed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |

**Returns** `uint32_t`.

Every control on screen, in reading order, and the focused one.

## sona_editor_control_get

```c
sona_result sona_editor_control_get(const sona_editor *ed, uint32_t index, sona_editor_control *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_editor_control *` | Receives the [`sona_editor_control`](../fields.md#sona_editor_control) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

Control `index` in reading order: its id, role, flags, rectangle, value, place in its group, label, value text and hint ("Wear it"), for a screen reader or a test.

## sona_editor_focus_get

```c
sona_result sona_editor_focus_get(const sona_editor *ed, sona_editor_control *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `const sona_editor *` | The editor | required |
| `out` | `sona_editor_control *` | Receives the [`sona_editor_control`](../fields.md#sona_editor_control) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

`SONA_E_NOT_FOUND` when nothing has focus (pointer-only use).

## sona_editor_announcement

```c
uint32_t sona_editor_announcement(sona_editor *ed, char *out, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ed` | `sona_editor *` | The editor | required |
| `out` | `char *` | Receives the text, NUL-terminated | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

What a screen reader should say since the last call ("Wardrobe, tab, 6 of 6", "Blue Hoodie, 3 of 120, worn"): copies it (UTF-8, NUL-terminated, cut at cap) and returns its length; 0 when nothing changed.

## sona_editor_struct_size

```c
uint32_t sona_editor_struct_size(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A public struct's name | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_editor_struct_size("sona_config");
printf("sona_editor_struct_size: %lld\n", (long long)x_got);
```

sizeof a public struct of this header by name, for the bindings' layout checks; 0 when unknown.

## sona_editor_struct_offset

```c
uint32_t sona_editor_struct_offset(const char *struct_dot_field);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `struct_dot_field` | `const char *` | `"struct.field"` | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_editor_struct_offset("example");
printf("sona_editor_struct_offset: %lld\n", (long long)x_got);
```

offsetof a field by "struct.field"; `UINT32_MAX` when unknown.
