cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit 433500bc3be3fd9a234aea976b465203a43d9a33
parent cf1dc4c2d23be0ffabbe11e54124331f25fb255b
Author: hhvn <dev@hhvn.uk>
Date:   Mon, 12 Sep 2022 19:07:56 +0100

All your shape are belong to Geom

Diffstat:
Msrc/main.h | 15+++++++--------
Msrc/struct.h | 46++++++++++++++++++++--------------------------
Msrc/ui.c | 55+++++++++++++++----------------------------------------
Msrc/ui/bodies.c | 26+++++++++++++-------------
Msrc/ui/main.c | 28+++++++++++++---------------
Msrc/ui/struct.h | 14+++++++-------
6 files changed, 75 insertions(+), 109 deletions(-)

diff --git a/src/main.h b/src/main.h @@ -44,13 +44,13 @@ float strnum(char *str); #define PAD 10 #define TPX 2 #define TPY 1 -#define EXPLODE_RECT(r) r.x, r.y, r.w, r.h -#define EXPLODE_CIRCLE(c) c.centre, c.r -#define RLIFY_RECT(r) ((Rectangle){ EXPLODE_RECT(r) }) -#define GEOMIFY_RECT(r) ((Geom){UI_RECT, .rect = r}) -#define GEOMIFY_CIRCLE(c) ((Geom){UI_CIRCLE, .circle = c}) -#define GEOM_RECT(x, y, w, h) ((Geom){UI_RECT, .rect = {x, y, w, h}}) -#define GEOM_CIRCLE(centre, r) ((Geom){UI_CIRCLE, .circle = {centre, r}) +#define RECT(nx, ny, nw, nh) (Geom){UI_RECT, .x = nx, .y = ny, .w = nw, .h = nh} +#define CIRCLE(x, y, nr) (Geom){UI_CIRCLE, .centre={x, y}, .r = nr} +#define CIRCLEV(v, nr) (Geom){UI_CIRCLE, .centre = v, .r = nr} +#define RLIFY_RECT(g) (Rectangle){g.x, g.y, g.w, g.h} +#define EXPLODE_RECT(g) g.x, g.y, g.w, g.h +#define EXPLODE_CIRCLE(g) g.centre.x, g.centre.y, g.r +#define EXPLODE_CIRCLEV(g) g.centre, g.r extern Tabs view_tabs; extern void (*view_handlers[UI_VIEW_LAST])(int); extern void (*view_drawers[UI_VIEW_LAST])(void); @@ -68,7 +68,6 @@ int ui_textsize(char *text); float ui_get_scroll(void); int ui_checkbox_size(Checkbox *checkbox); int ui_collides(Geom geom, Vector2 point); -int ui_collides_rect(Rect rect, Vector2 point); int ui_onscreen(Vector2 point); int ui_onscreen_ring(Vector2 centre, float r); int ui_onscreen_circle(Vector2 centre, float r); diff --git a/src/struct.h b/src/struct.h @@ -83,28 +83,6 @@ typedef struct { } Save; /* ui.c */ -/* I know raylib has Rectangle, but I don't want to type width and height */ -typedef struct { - int x, y; - int w, h; -} Rect; - -/* pane.c */ -#define PANESCROLL {NULL, 1, 0, 0} -#define PANENOSCROLL {NULL, 0, 0, 0} -typedef struct { - Rect *geom; - int scroll; - int max; - int off; -} Pane; - -/* ui.c again */ -typedef struct { - Vector2 centre; - int r; -} Circle; - enum Geometries { UI_RECT, UI_CIRCLE, @@ -113,16 +91,32 @@ enum Geometries { typedef struct { enum Geometries type; union { - Rect rect; - Circle circle; + struct { + int x, y; + int w, h; + }; + struct { + Vector2 centre; + int r; + }; }; } Geom; +/* pane.c */ +#define PANESCROLL {NULL, 1, 0, 0} +#define PANENOSCROLL {NULL, 0, 0, 0} +typedef struct { + Geom *geom; + int scroll; + int max; + int off; +} Pane; + typedef struct { float w, h; float diag; Vector2 centre; - Rect rect; /* for passing to functions */ + Geom rect; /* for passing to functions */ } Screen; #define TABS_MAX 16 @@ -166,7 +160,7 @@ typedef struct { char *placeholder; char *val[DROPDOWN_MAX]; /* internal */ - Rect rect; + Geom rect; Pane pane; } Dropdown; diff --git a/src/ui.c b/src/ui.c @@ -153,24 +153,18 @@ ui_collides(Geom geom, Vector2 point) { switch (geom.type) { case UI_CIRCLE: return CheckCollisionPointCircle(point, - EXPLODE_CIRCLE(geom.circle)); + EXPLODE_CIRCLEV(geom)); case UI_RECT: default: /* -Wreturn-type bitches without default */ - return CheckCollisionPointRec(point, RLIFY_RECT(geom.rect)); + return CheckCollisionPointRec(point, RLIFY_RECT(geom)); } } int -ui_collides_rect(Rect rect, Vector2 point) { - Geom geom = {UI_RECT, .rect = rect}; - return ui_collides(geom, point); -} - -int ui_onscreen(Vector2 point) { if (!pane_visible(point.y, point.y)) return 0; point.y = pane_y(point.y); - return ui_collides(GEOMIFY_RECT(screen.rect), point); + return ui_collides(screen.rect, point); } int @@ -200,18 +194,7 @@ ui_clickable_register(Geom geom, enum UiElements type, void *elem) { for (i = 0; i < CLICKABLE_MAX; i++) { if (!clickable[i].elem) { - clickable[i].geom.type = geom.type; - switch (geom.type) { - case UI_RECT: - clickable[i].geom.rect = geom.rect; - clickable[i].geom.rect.y = pane_y(geom.rect.y); - break; - case UI_CIRCLE: - clickable[i].geom.circle = geom.circle; - clickable[i].geom.circle.centre.y = - pane_y(geom.circle.centre.y); - break; - } + clickable[i].geom = geom; clickable[i].type = type; clickable[i].elem = elem; return; @@ -227,29 +210,25 @@ ui_clickable_handle(Vector2 mouse, MouseButton button, Clickable *clickable) { Checkbox *checkbox; Dropdown *drop; Input *input; - Rect *rect; - /* Circle *circle; */ + Geom *geom = &clickable->geom; int ftabw, fw, fn, tabw, x; int i; - rect = &clickable->geom.rect; - /* circle = &clickable->geom.circle; */ - switch (clickable->type) { case UI_TAB: if (button != MOUSE_BUTTON_LEFT) return; tabs = clickable->elem; - for (fw = rect->w, fn = i = 0; i < tabs->n; i++) { + for (fw = geom->w, fn = i = 0; i < tabs->n; i++) { if (!tabs->tabs[i].w) fn++; else fw -= tabs->tabs[i].w; } ftabw = fw / fn; - for (i = 0, x = rect->x; i < tabs->n; x += tabw, i++) { + for (i = 0, x = geom->x; i < tabs->n; x += tabw, i++) { if (i == tabs->n - 1) - tabw = rect->x + rect->w - x; + tabw = geom->x + geom->w - x; else if (!tabs->tabs[i].w) tabw = ftabw; else @@ -274,7 +253,7 @@ ui_clickable_handle(Vector2 mouse, MouseButton button, Clickable *clickable) { if (focus.p != input) { ui_update_focus(UI_INPUT, input); } else { - i = (mouse.x - TPX - rect->x + charpx / 2) / charpx; + i = (mouse.x - TPX - geom->x + charpx / 2) / charpx; if (i < input->len) input->cur = i; else if (i > 0) @@ -288,7 +267,7 @@ ui_clickable_handle(Vector2 mouse, MouseButton button, Clickable *clickable) { if (focus.p != drop) { ui_update_focus(UI_DROPDOWN, drop); } else { - i = (mouse.y - rect->y) / FONT_SIZE; + i = (mouse.y - geom->y) / FONT_SIZE; if (i != 0 && i <= drop->n) drop->sel = i - 1; ui_update_focus(0, NULL); @@ -536,7 +515,7 @@ ui_draw_tabs(int x, int y, int w, int h, Tabs *tabs) { if (tabs->sel == 0) ui_draw_rectangle(x, y + 1, 1, h - 1, col_bg); /* undraw left border */ if (tabs->sel == tabs->n - 1) ui_draw_rectangle(x + w - 1, y + 1, 1, h - 1, col_bg); /* undraw right border */ - ui_clickable_register(GEOM_RECT(x, y, w, h), UI_TAB, tabs); + ui_clickable_register(RECT(x, y, w, h), UI_TAB, tabs); } void @@ -563,8 +542,7 @@ ui_draw_checkbox(int x, int y, Checkbox *box) { ui_print(x + w + (w / 2), y + (h / 6), col_fg, "%s", box->label); rw = w + (w / 2) + ui_textsize(box->label); if (box->enabled) - ui_clickable_register(GEOM_RECT(x, y, - rw, h), + ui_clickable_register(RECT(x, y, rw, h), UI_CHECKBOX, box); return rw; } @@ -575,7 +553,6 @@ ui_draw_dropdown(int x, int y, int w, Dropdown *d) { int fh, ph; int focused; int i; - Geom geom = {UI_RECT}; focused = focus.p == d; fh = h + (focused ? h * d->n : 0); @@ -583,7 +560,7 @@ ui_draw_dropdown(int x, int y, int w, Dropdown *d) { ui_draw_border_around(x, y, w, ph, 1); - d->rect = (Rect){x, y, w, fh + TPY}; + d->rect = RECT(x, y, w, fh + TPY); d->pane.geom = &d->rect; pane_begin(&d->pane); @@ -599,8 +576,7 @@ ui_draw_dropdown(int x, int y, int w, Dropdown *d) { } } - geom.rect = (Rect){x, y, w, ph}; - ui_clickable_register(geom, UI_DROPDOWN, d); + ui_clickable_register(d->rect, UI_DROPDOWN, d); pane_end(); } @@ -609,7 +585,6 @@ ui_draw_input(int x, int y, int w, Input *in) { int h = FONT_SIZE; int focused = focus.p == in; int cw; - Geom geom = {UI_RECT, .rect = {x, y, w, h}}; /* Dirty hack: truncate str to length that fits */ cw = w / charpx - 1; @@ -629,7 +604,7 @@ ui_draw_input(int x, int y, int w, Input *in) { if (focused) { ui_draw_rectangle(x + TPX + charpx * in->cur, y + TPY, 1, FONT_SIZE, col_fg); } - ui_clickable_register(geom, UI_INPUT, in); + ui_clickable_register(RECT(x, y, w, h), UI_INPUT, in); } Vector2 diff --git a/src/ui/bodies.c b/src/ui/bodies.c @@ -57,7 +57,7 @@ ui_handle_view_bodies(int nowsel) { if (!nowsel) { if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && - ui_collides_rect(v->bodies, m)) { + ui_collides(v->bodies, m)) { pos = (m.y + v->pane.bodies.off - v->bodies.y - PAD) / FONT_SIZE; for (i = j = 0; i < v->sys->bodies_len && j < pos; i++) @@ -98,18 +98,18 @@ ui_draw_view_bodies(void) { int i; Body *body; - v->stars = (Rect){ PAD, VIEWS_HEIGHT + PAD * 2 + FONT_SIZE, - screen.w - PAD * 2, FONT_SIZE * 6 }; - v->disp = (Rect){ v->stars.x, v->stars.y + v->stars.h + PAD, - v->stars.w, FONT_SIZE + PAD}; - v->bodies = (Rect){ v->disp.x, v->disp.y + v->disp.h + PAD/2, - v->disp.w, screen.h - v->bodies.y - INFOBOX_H - BUTTONS }; - v->loc = (Rect){ v->bodies.x, v->bodies.y + v->bodies.h + PAD, - INFOBOX_W, INFOBOX_H }; - v->mins = (Rect){ v->loc.x + v->loc.w + PAD, v->loc.y, - INFOBOX_W, INFOBOX_H }; - v->hab = (Rect){ v->mins.x + v->mins.w + PAD, v->mins.y, - INFOBOX_W, INFOBOX_H }; + v->stars = RECT(PAD, VIEWS_HEIGHT + PAD * 2 + FONT_SIZE, + screen.w - PAD * 2, FONT_SIZE * 6); + v->disp = RECT(v->stars.x, v->stars.y + v->stars.h + PAD, + v->stars.w, FONT_SIZE + PAD); + v->bodies = RECT(v->disp.x, v->disp.y + v->disp.h + PAD/2, + v->disp.w, screen.h - v->bodies.y - INFOBOX_H - BUTTONS); + v->loc = RECT(v->bodies.x, v->bodies.y + v->bodies.h + PAD, + INFOBOX_W, INFOBOX_H); + v->mins = RECT(v->loc.x + v->loc.w + PAD, v->loc.y, + INFOBOX_W, INFOBOX_H); + v->hab = RECT(v->mins.x + v->mins.w + PAD, v->mins.y, + INFOBOX_W, INFOBOX_H); if (!v->sel) v->bodies.h += INFOBOX_H; diff --git a/src/ui/main.c b/src/ui/main.c @@ -30,12 +30,10 @@ View_main view_main = { .comettail = {1, 1, "Comet tails"}, /* TODO */ .geom = { .type = UI_RECT, - .rect = { - .x = PAD, - .y = VIEWS_HEIGHT + PAD, - .w = 200, - .h = 400, - }, + .x = PAD, + .y = VIEWS_HEIGHT + PAD, + .w = 200, + .h = 400, }, .pane = PANESCROLL, }, @@ -222,7 +220,7 @@ ui_draw_view_main(void) { Vector2 mouse = GetMousePosition(); Vector2 mousekm = pxtokm(mouse); Vector2 ruler; - Rect rect; + Geom geom; Body *body; float dist; size_t i; @@ -271,15 +269,15 @@ ui_draw_view_main(void) { col_info, "%s", strkm(dist)); /* infobox */ - ui_draw_tabbed_window(EXPLODE_RECT(view_main.infobox.geom.rect), + ui_draw_tabbed_window(EXPLODE_RECT(view_main.infobox.geom), &view_main.infobox.tabs); - x = view_main.infobox.geom.rect.x + FONT_SIZE; - y = view_main.infobox.geom.rect.y + WINDOW_TAB_HEIGHT; - rect.x = x - FONT_SIZE; - rect.y = y; - rect.w = view_main.infobox.geom.rect.w - WINDOW_BORDER; - rect.h = view_main.infobox.geom.rect.h - WINDOW_TAB_HEIGHT - WINDOW_BORDER; - view_main.infobox.pane.geom = &rect; + x = view_main.infobox.geom.x + FONT_SIZE; + y = view_main.infobox.geom.y + WINDOW_TAB_HEIGHT; + geom = RECT(x - FONT_SIZE, + y, + view_main.infobox.geom.w - WINDOW_BORDER, + view_main.infobox.geom.h - WINDOW_TAB_HEIGHT - WINDOW_BORDER); + view_main.infobox.pane.geom = &geom; pane_begin(&view_main.infobox.pane); ui_draw_checkbox(x, y += FONT_SIZE*1.5, &view_main.infobox.names.dwarf); diff --git a/src/ui/struct.h b/src/ui/struct.h @@ -47,17 +47,17 @@ typedef struct { Pane stars; Pane bodies; } pane; - Rect stars; - Rect disp; - Rect bodies; - Rect loc; - Rect mins; - Rect hab; + Geom stars; + Geom disp; + Geom bodies; + Geom loc; + Geom mins; + Geom hab; } View_bodies; typedef struct { struct { - Rect geom; + Geom geom; } info; int pan; Vector2 off;