Files
dusk/src/dusk/ui/widget/uislider.h
T

186 lines
4.4 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"
#define UI_SLIDER_TRACK_WIDTH 80.0f
#define UI_SLIDER_TRACK_HEIGHT 4.0f
#define UI_SLIDER_STEP_MARKER_WIDTH 2.0f
#define UI_SLIDER_STEP_MARKER_OVERHANG 1.0f
#define UI_SLIDER_GAP 4.0f
#define UI_SLIDER_VALUE_TEXT_MAX 16
/**
* Number of discrete steps below which an int slider renders individual
* step markers on its track instead of a smooth fill.
*/
#define UI_SLIDER_STEP_MARKERS_MAX 10
typedef enum {
UI_SLIDER_TYPE_FLOAT,
UI_SLIDER_TYPE_INT,
UI_SLIDER_TYPE_COUNT
} uislidertype_t;
typedef union {
float_t f;
int32_t i;
} uislidervalue_t;
typedef struct {
const char_t *label;
uislidertype_t type;
uislidervalue_t value;
uislidervalue_t min;
uislidervalue_t max;
uislidervalue_t step;
bool_t highlighted;
} uislider_t;
/**
* Initializes a floating point slider.
*
* @param slider The slider to initialize.
* @param label Display label.
* @param value Initial value, clamped to [min, max].
* @param min Minimum value.
* @param max Maximum value.
* @param step The amount a single step moves the value by.
*/
void uiSliderInitFloat(
uislider_t *slider,
const char_t *label,
const float_t value,
const float_t min,
const float_t max,
const float_t step
);
/**
* Initializes an integer slider.
*
* @param slider The slider to initialize.
* @param label Display label.
* @param value Initial value, clamped to [min, max].
* @param min Minimum value.
* @param max Maximum value.
* @param step The amount a single step moves the value by.
*/
void uiSliderInitInt(
uislider_t *slider,
const char_t *label,
const int32_t value,
const int32_t min,
const int32_t max,
const int32_t step
);
/**
* Returns the slider's current value as a float. Works for both slider
* types; int values are widened to float.
*
* @param slider The slider to query.
* @returns The current value.
*/
float_t uiSliderGetFloat(const uislider_t *slider);
/**
* Returns the slider's current value as an int. Only valid for
* UI_SLIDER_TYPE_INT sliders.
*
* @param slider The slider to query.
* @returns The current value.
*/
int32_t uiSliderGetInt(const uislider_t *slider);
/**
* Sets the slider's value, clamped to [min, max]. Only valid for
* UI_SLIDER_TYPE_FLOAT sliders.
*
* @param slider The slider to update.
* @param value The new value.
*/
void uiSliderSetFloat(uislider_t *slider, const float_t value);
/**
* Sets the slider's value, clamped to [min, max]. Only valid for
* UI_SLIDER_TYPE_INT sliders.
*
* @param slider The slider to update.
* @param value The new value.
*/
void uiSliderSetInt(uislider_t *slider, const int32_t value);
/**
* Moves the slider's value up by one step, wrapping around to its min
* if the step would exceed its max.
*
* @param slider The slider to update.
*/
void uiSliderStepUp(uislider_t *slider);
/**
* Moves the slider's value down by one step, wrapping around to its
* max if the step would go below its min.
*
* @param slider The slider to update.
*/
void uiSliderStepDown(uislider_t *slider);
/**
* Returns the slider's current value normalized to a 0..1 range based
* on its min/max.
*
* @param slider The slider to query.
* @returns The normalized value.
*/
float_t uiSliderGetRatio(const uislider_t *slider);
/**
* Returns the number of discrete steps between min and max. Always 0
* for float sliders.
*
* @param slider The slider to query.
* @returns The step count.
*/
int32_t uiSliderGetStepCount(const uislider_t *slider);
/**
* Returns whether the slider is highlighted.
*
* @param slider The slider to query.
* @returns True if highlighted.
*/
bool_t uiSliderIsHighlighted(const uislider_t *slider);
/**
* Sets the highlighted state of the slider.
*
* @param slider The slider to update.
* @param highlighted The new highlighted state.
*/
void uiSliderSetHighlighted(uislider_t *slider, const bool_t highlighted);
/**
* Draws the slider at the given screen position: label, then track,
* then the current value as text. Int sliders with fewer than
* UI_SLIDER_STEP_MARKERS_MAX discrete steps render a marker per step
* instead of a plain track.
*
* @param slider The slider to draw.
* @param x Screen x position.
* @param y Screen y position.
* @return Any error that occurs.
*/
errorret_t uiSliderDraw(
const uislider_t *slider,
const float_t x,
const float_t y
);