Files
dusk/archive/dusk/ui/widget/uidropdown.h
T
YourWishes 7ee04c78cd Rebuild UI as a scriptable element tree, archive the old system
The old UI system (X-macro static element list, hand-authored C
screens/widgets/focus stack) is archived under archive/ rather than
deleted, since it's a useful reference during the rewrite.

New native UI element pool (src/dusk/ui): a flat UI_ELEMENTS[128] pool
of tagged-union elements (Label, Rectangle, Scripted), a real
persistent parent/child tree (children[8] per element, cycle- and
capacity-checked uiElementSetParent), always-fresh worldX/worldY
(cheap enough to recompute on every read, no dirty-flag cache needed),
and cascading dispose. Rendering stays manual/immediate: a scripted
element with no render() override auto-renders its children by
default, but overriding render() takes full control (an override must
call renderChildren() itself to opt back in) -- this is deliberately
preserved end to end via a render()-shadow trampoline so overriding
render() always keeps working the same way regardless of how a node
is reached.

New scripting layer (src/dusk/script/module/ui): UIElement/Label/
Rectangle JS classes (Label/Rectangle share UIElement's prototype via
manual chaining, not JS `extends`), exposing x/y/worldX/worldY/parent/
add()/remove()/render()/renderChildren()/dispose(). UI.add()/
UI.remove() manage top-level render roots, mutually exclusive with
being someone's child.

Also: Scene gains a lateUpdate() hook (called once per frame after
every other update, for things like camera-follow that need to react
to where everything else ended up); several duskrpg call sites
(cutscene items, entityinteractable, entityplayer) that depended on
the now-archived RPG textbox are stubbed to console output instead of
a dialogue box, pending the new UI reaching that far.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-13 11:13:23 -05:00

124 lines
3.0 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
#include "ui/widget/uiwidgetlabel.h"
#define UI_DROPDOWN_GAP 4.0f
typedef struct {
uiwidgetlabel_t label;
uiwidgetlabel_t value;
const char_t *const *options;
uint8_t optionCount;
uint8_t selectedIndex;
bool_t highlighted;
} uidropdown_t;
/**
* Initializes a dropdown.
*
* @param dropdown The dropdown to initialize.
* @param label Display label.
* @param options Array of option display strings; caller-owned, must
* outlive the dropdown.
* @param optionCount Number of entries in options. Must be > 0.
* @param selectedIndex Initial selected option index, clamped to
* [0, optionCount - 1].
*/
void uiDropdownInit(
uidropdown_t *dropdown,
const char_t *label,
const char_t *const *options,
const uint8_t optionCount,
const uint8_t selectedIndex
);
/**
* Returns the index of the currently selected option.
*
* @param dropdown The dropdown to query.
* @returns The selected option index.
*/
uint8_t uiDropdownGetSelectedIndex(const uidropdown_t *dropdown);
/**
* Returns the currently selected option's display string.
*
* @param dropdown The dropdown to query.
* @returns The selected option string.
*/
const char_t *uiDropdownGetSelectedOption(const uidropdown_t *dropdown);
/**
* Sets the selected option by index.
*
* @param dropdown The dropdown to update.
* @param index The new selected option index. Must be < optionCount.
*/
void uiDropdownSetSelectedIndex(uidropdown_t *dropdown, const uint8_t index);
/**
* Selects the next option, wrapping around to the first option past
* the last.
*
* @param dropdown The dropdown to update.
*/
void uiDropdownStepNext(uidropdown_t *dropdown);
/**
* Selects the previous option, wrapping around to the last option
* before the first.
*
* @param dropdown The dropdown to update.
*/
void uiDropdownStepPrev(uidropdown_t *dropdown);
/**
* Returns whether the dropdown is highlighted.
*
* @param dropdown The dropdown to query.
* @returns True if highlighted.
*/
bool_t uiDropdownIsHighlighted(const uidropdown_t *dropdown);
/**
* Sets the highlighted state of the dropdown.
*
* @param dropdown The dropdown to update.
* @param highlighted The new highlighted state.
*/
void uiDropdownSetHighlighted(
uidropdown_t *dropdown,
const bool_t highlighted
);
/**
* Draws the dropdown at the given screen position: label, then the
* currently selected option surrounded by cycle arrows.
*
* @param dropdown The dropdown to draw.
* @param x Screen x position.
* @param y Screen y position.
* @return Any error that occurs.
*/
errorret_t uiDropdownDraw(
const uidropdown_t *dropdown,
const float_t x,
const float_t y
);
/**
* Rebuilds the dropdown's cached value label ("< option >") from its
* current selectedIndex. Called internally whenever selectedIndex
* changes.
*
* @param dropdown The dropdown to update.
*/
void uiDropdownRebuildValue(uidropdown_t *dropdown);