u/var: Add u_var_curve

Let's you draw any arbitrary curve from a getter function.
This commit is contained in:
Mateo de Mayo 2022-07-09 11:42:16 -03:00 committed by Moses Turner
parent bf9e6fa370
commit 0324fbae6c
2 changed files with 48 additions and 1 deletions

View file

@ -121,6 +121,25 @@ struct u_var_histogram_f32
float height; //!< Widget height or 0 for auto
};
struct u_var_curve_point
{
// Using doubles like ImPlotPoint
double x;
double y;
};
typedef struct u_var_curve_point (*u_var_curve_getter)(void *data, int i);
struct u_var_curve
{
u_var_curve_getter getter; //!< Getter of 2D points for the curve
void *data; //!< User data for `getter`
int count; //!< Number of points to draw; param i < count
const char *label; //!< Curve name
const char *xlabel; //!< Label of the X axis
const char *ylabel; //!< Label of the Y axis
};
/*!
* What kind of variable is this tracking.
*/
@ -161,6 +180,7 @@ enum u_var_kind
U_VAR_KIND_COMBO,
U_VAR_KIND_HISTOGRAM_F32,
U_VAR_KIND_DRAGGABLE_U16,
U_VAR_KIND_CURVE,
};
#define U_VAR_NAME_STRING_SIZE 256
@ -269,7 +289,8 @@ u_var_force_on(void);
ADD_FUNC(combo, struct u_var_combo, COMBO) \
ADD_FUNC(draggable_f32, struct u_var_draggable_f32, DRAGGABLE_F32) \
ADD_FUNC(draggable_u16, struct u_var_draggable_u16, DRAGGABLE_U16) \
ADD_FUNC(histogram_f32, struct u_var_histogram_f32, HISTOGRAM_F32)
ADD_FUNC(histogram_f32, struct u_var_histogram_f32, HISTOGRAM_F32) \
ADD_FUNC(curve, struct u_var_curve, CURVE)
#define ADD_FUNC(SUFFIX, TYPE, ENUM) void u_var_add_##SUFFIX(void *, TYPE *, const char *);

View file

@ -245,6 +245,31 @@ on_histogram_f32_var(const char *name, void *ptr)
igPlotHistogramFloatPtr(name, h->values, h->count, 0, NULL, FLT_MAX, FLT_MAX, zero, sizeof(float));
}
static ImPlotPoint
curve_var_implot_getter(void *ptr, int i)
{
struct u_var_curve *c = (struct u_var_curve *)ptr;
struct u_var_curve_point point = c->getter(c->data, i);
ImPlotPoint implot_point = {point.x, point.y};
return implot_point;
}
static void
on_curve_var(const char *name, void *ptr)
{
struct u_var_curve *c = (struct u_var_curve *)ptr;
ImVec2 size = {igGetWindowContentRegionWidth(), 256};
bool shown = ImPlot_BeginPlot(name, c->xlabel, c->ylabel, size, 0, 0, 0, 0, 0);
if (!shown) {
return;
}
ImPlot_PlotLineG(c->label, curve_var_implot_getter, c, c->count, 0);
ImPlot_EndPlot();
}
static void
on_draggable_f32_var(const char *name, void *ptr)
{
@ -394,6 +419,7 @@ on_elem(struct u_var_info *info, void *priv)
case U_VAR_KIND_COMBO: on_combo_var(name, ptr); break;
case U_VAR_KIND_DRAGGABLE_U16: on_draggable_u16_var(name, ptr); break;
case U_VAR_KIND_HISTOGRAM_F32: on_histogram_f32_var(name, ptr); break;
case U_VAR_KIND_CURVE: on_curve_var(name, ptr); break;
default: igLabelText(name, "Unknown tag '%i'", kind); break;
}
}