From bafe54b0100f2b094ba4bd01d32614928bc46b88 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 24 Apr 2022 21:09:58 +0900 Subject: [PATCH] [view] Add a cexpr enum for grav_t And use it for hud_scoreboard_gravity. Putting the enum def in view made the most sense as view does own the base type and the enum is likely to be by useful for other settings. --- include/QF/ui/view.h | 2 ++ include/client/hud.h | 1 - libs/client/hud.c | 28 +++------------------------- libs/ui/view.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/include/QF/ui/view.h b/include/QF/ui/view.h index d3d6f17e7..2a1e42bb7 100644 --- a/include/QF/ui/view.h +++ b/include/QF/ui/view.h @@ -66,6 +66,8 @@ typedef enum { grav_northwest, ///< +ve X right, +ve Y down, -X left, -ve Y up } grav_t; +extern struct exprtype_s grav_t_type; + /** The view object. */ typedef struct view_s view_t; diff --git a/include/client/hud.h b/include/client/hud.h index 82fa94365..042a78bbc 100644 --- a/include/client/hud.h +++ b/include/client/hud.h @@ -31,7 +31,6 @@ extern int hud_sb_lines; extern int hud_sbar; -extern char *hud_scoreboard_gravity; extern int hud_swap; extern struct view_s *sbar_view; diff --git a/libs/client/hud.c b/libs/client/hud.c index 5011b3a6e..c6ac8659e 100644 --- a/libs/client/hud.c +++ b/libs/client/hud.c @@ -52,7 +52,7 @@ static cvar_t hud_sbar_cvar = { .flags = CVAR_ARCHIVE, .value = { .type = &cexpr_int, .value = &hud_sbar }, }; -char *hud_scoreboard_gravity; +grav_t hud_scoreboard_gravity; static cvar_t hud_scoreboard_gravity_cvar = { .name = "hud_scoreboard_gravity", .description = @@ -60,7 +60,7 @@ static cvar_t hud_scoreboard_gravity_cvar = { "northeast, west, east, southwest, south, southeast", .default_value = "center", .flags = CVAR_ARCHIVE, - .value = { .type = 0/* not used */, .value = &hud_scoreboard_gravity }, + .value = { .type = &grav_t_type, .value = &hud_scoreboard_gravity }, }; int hud_swap; static cvar_t hud_swap_cvar = { @@ -123,29 +123,7 @@ hud_swap_f (void *data, const cvar_t *cvar) static void hud_scoreboard_gravity_f (void *data, const cvar_t *cvar) { - grav_t grav; - - if (strequal (hud_scoreboard_gravity, "center")) - grav = grav_center; - else if (strequal (hud_scoreboard_gravity, "northwest")) - grav = grav_northwest; - else if (strequal (hud_scoreboard_gravity, "north")) - grav = grav_north; - else if (strequal (hud_scoreboard_gravity, "northeast")) - grav = grav_northeast; - else if (strequal (hud_scoreboard_gravity, "west")) - grav = grav_west; - else if (strequal (hud_scoreboard_gravity, "east")) - grav = grav_east; - else if (strequal (hud_scoreboard_gravity, "southwest")) - grav = grav_southwest; - else if (strequal (hud_scoreboard_gravity, "south")) - grav = grav_south; - else if (strequal (hud_scoreboard_gravity, "southeast")) - grav = grav_southeast; - else - grav = grav_center; - view_setgravity (hud_overlay_view, grav); + view_setgravity (hud_overlay_view, hud_scoreboard_gravity); } void diff --git a/libs/ui/view.c b/libs/ui/view.c index 71c4a3b4b..ac0c4cbfc 100644 --- a/libs/ui/view.c +++ b/libs/ui/view.c @@ -39,8 +39,47 @@ #endif #include +#include "QF/cexpr.h" #include "QF/ui/view.h" +static exprenum_t grav_t_enum; +exprtype_t grav_t_type = { + .name = "grav_t", + .size = sizeof (grav_t), + .data = &grav_t_enum, + .get_string = cexpr_enum_get_string, +}; +static grav_t grav_t_values[] = { + grav_center, + grav_north, + grav_northeast, + grav_east, + grav_southeast, + grav_south, + grav_southwest, + grav_west, + grav_northwest, +}; +static exprsym_t grav_t_symbols[] = { + { "center", &grav_t_type, grav_t_values + grav_center }, + { "north", &grav_t_type, grav_t_values + grav_north }, + { "northeast", &grav_t_type, grav_t_values + grav_northeast }, + { "east", &grav_t_type, grav_t_values + grav_east }, + { "southeast", &grav_t_type, grav_t_values + grav_southeast }, + { "south", &grav_t_type, grav_t_values + grav_south }, + { "southwest", &grav_t_type, grav_t_values + grav_southwest }, + { "west", &grav_t_type, grav_t_values + grav_west }, + { "northwest", &grav_t_type, grav_t_values + grav_northwest }, + {} +}; +static exprtab_t grav_t_symtab = { + grav_t_symbols, +}; +static exprenum_t grav_t_enum = { + &grav_t_type, + &grav_t_symtab, +}; + static void setgeometry (view_t *view) {