Label formatting
This commit is contained in:
+12
-7
@@ -24,12 +24,15 @@ uiman_init ()
|
||||
Widget_label_printf (game_name, "%s %s", GAME_NAME, GAME_VERSION);
|
||||
|
||||
Widget *frame_time = Widget_label_new (NULL, TEXT_ALIGN_LEFT);
|
||||
Widget_set_on_tick (frame_time, uicb_label_show_float, &delta_time);
|
||||
Widget_set_on_tick (frame_time, uicb_label_show_float,
|
||||
CallbackFormatData_new_float ("Frame: %.2f s", &delta_time));
|
||||
|
||||
Widget *entities_rendered = Widget_label_new (NULL, TEXT_ALIGN_LEFT);
|
||||
Widget *chunks_rendered = Widget_label_new (NULL, TEXT_ALIGN_LEFT);
|
||||
Widget_set_on_tick (entities_rendered, uicb_label_show_int, &n_frame_ents);
|
||||
Widget_set_on_tick (chunks_rendered, uicb_label_show_int, &n_frame_chunks);
|
||||
Widget_set_on_tick (entities_rendered, uicb_label_show_int,
|
||||
CallbackFormatData_new_int ("Entities: %d", &n_frame_ents));
|
||||
Widget_set_on_tick (chunks_rendered, uicb_label_show_int,
|
||||
CallbackFormatData_new_int ("Chunks: %d", &n_frame_chunks));
|
||||
|
||||
Widget *container2 = Widget_container_new (true);
|
||||
Widget_add_child (container2, entities_rendered);
|
||||
@@ -39,11 +42,14 @@ uiman_init ()
|
||||
Widget *pos_y = Widget_label_new (NULL, TEXT_ALIGN_RIGHT);
|
||||
Widget *pos_z = Widget_label_new (NULL, TEXT_ALIGN_RIGHT);
|
||||
Widget_label_set_text_color (pos_x, 1.0, 0.0, 0.0, 1.0);
|
||||
Widget_set_on_tick (pos_x, uicb_label_show_float, &camera_position[0]);
|
||||
Widget_set_on_tick (pos_x, uicb_label_show_float,
|
||||
CallbackFormatData_new_float ("X: %.2f", &camera_position[0]));
|
||||
Widget_label_set_text_color (pos_y, 0.0, 1.0, 0.0, 1.0);
|
||||
Widget_set_on_tick (pos_y, uicb_label_show_float, &camera_position[1]);
|
||||
Widget_set_on_tick (pos_y, uicb_label_show_float,
|
||||
CallbackFormatData_new_float ("Y: %.2f", &camera_position[1]));
|
||||
Widget_label_set_text_color (pos_z, 0.0, 1.0, 1.0, 1.0);
|
||||
Widget_set_on_tick (pos_z, uicb_label_show_float, &camera_position[2]);
|
||||
Widget_set_on_tick (pos_z, uicb_label_show_float,
|
||||
CallbackFormatData_new_float ("Z: %.2f", &camera_position[2]));
|
||||
|
||||
Widget *container3 = Widget_container_new (true);
|
||||
Widget_add_child (container3, pos_x);
|
||||
@@ -64,7 +70,6 @@ uiman_init ()
|
||||
Widget_add_child (container4, wireframe_button);
|
||||
Widget_add_child (container4, flight_button);
|
||||
|
||||
|
||||
Widget_add_child (container, game_name);
|
||||
Widget_add_child (container, frame_time);
|
||||
Widget_add_child (container, container2);
|
||||
|
||||
+22
-5
@@ -3,7 +3,6 @@
|
||||
/* TODO: Layout container children scaling, so some children can be
|
||||
smaller, some bigger, etc. Perhaps android-esque LinearLayout
|
||||
weight? */
|
||||
/* TODO: Label callback with format. */
|
||||
/* TODO: Widget borders. */
|
||||
|
||||
static SimpleMesh quad;
|
||||
@@ -335,18 +334,36 @@ Widget_label_set_text_color (Widget *widget, float r, float g, float b, float a)
|
||||
glm_vec4_copy ((vec4) { r, g, b, a }, widget->label.text_color);
|
||||
}
|
||||
|
||||
CallbackFormatData *
|
||||
CallbackFormatData_new_int (const char *format, int *value)
|
||||
{
|
||||
CallbackFormatData *data = g_new (CallbackFormatData, 1);
|
||||
data->format = format;
|
||||
data->int_value = value;
|
||||
return data;
|
||||
}
|
||||
|
||||
CallbackFormatData *
|
||||
CallbackFormatData_new_float (const char *format, float *value)
|
||||
{
|
||||
CallbackFormatData *data = g_new (CallbackFormatData, 1);
|
||||
data->format = format;
|
||||
data->float_value = value;
|
||||
return data;
|
||||
}
|
||||
|
||||
void
|
||||
uicb_label_show_int (Widget *widget, void *userdata)
|
||||
{
|
||||
int *value = userdata;
|
||||
Widget_label_printf (widget, "%d", *value);
|
||||
CallbackFormatData *value = userdata;
|
||||
Widget_label_printf (widget, value->format, *value->int_value);
|
||||
}
|
||||
|
||||
void
|
||||
uicb_label_show_float (Widget *widget, void *userdata)
|
||||
{
|
||||
float *value = userdata;
|
||||
Widget_label_printf (widget, "%3.2f", *value);
|
||||
CallbackFormatData *value = userdata;
|
||||
Widget_label_printf (widget, value->format, *value->float_value);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -70,6 +70,14 @@ typedef struct Widget
|
||||
void *tick_data;
|
||||
} Widget;
|
||||
|
||||
typedef struct CallbackFormatData {
|
||||
const char *format;
|
||||
union {
|
||||
int *int_value;
|
||||
float *float_value;
|
||||
};
|
||||
} CallbackFormatData;
|
||||
|
||||
static inline float
|
||||
wdg_real_width (mat3 model)
|
||||
{
|
||||
@@ -107,6 +115,9 @@ void Widget_label_set_text (Widget *widget, const char *text);
|
||||
void Widget_label_printf (Widget *widget, const char *format, ...);
|
||||
void Widget_label_set_text_color (Widget *widget, float r, float g, float b, float a);
|
||||
|
||||
CallbackFormatData *CallbackFormatData_new_int (const char *format, int *value);
|
||||
CallbackFormatData *CallbackFormatData_new_float (const char *format, float *value);
|
||||
|
||||
void uicb_label_show_int (Widget *widget, void *userdata);
|
||||
void uicb_label_show_float (Widget *widget, void *userdata);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user