summaryrefslogtreecommitdiff
path: root/ui.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-06-01 12:19:16 +1000
committerquou <quou@disroot.org>2024-06-01 12:20:17 +1000
commitea7cd94f7aeb177618db3907a6c86b7252e018f0 (patch)
treee972f9cf590ef756c2e41f3eac5b03e16db08300 /ui.c
Initial commit.
Diffstat (limited to 'ui.c')
-rw-r--r--ui.c152
1 files changed, 152 insertions, 0 deletions
diff --git a/ui.c b/ui.c
new file mode 100644
index 0000000..a7d1070
--- /dev/null
+++ b/ui.c
@@ -0,0 +1,152 @@
+#include "config.h"
+#include "render.h"
+#include "plat.h"
+#include "rcache.h"
+#include "ui.h"
+
+Rectangle rectcut_left(Rectangle* l, int a) {
+ Rectangle r;
+ r.x = l->x;
+ r.y = l->y;
+ r.w = a;
+ r.h = l->h;
+ l->x += a;
+ l->w -= a;
+ return r;
+}
+
+Rectangle rectcut_right(Rectangle* l, int a) {
+ Rectangle r;
+ r.x = l->x + l->w - a;
+ r.y = l->y;
+ r.w = a;
+ r.h = l->h;
+ l->w -= a;
+ return r;
+}
+
+Rectangle rectcut_top(Rectangle* l, int a) {
+ Rectangle r;
+ r.x = l->x;
+ r.y = l->y;
+ r.w = l->w;
+ r.h = a;
+ l->y += a;
+ l->h -= a;
+ return r;
+}
+
+Rectangle rectcut_bottom(Rectangle* l, int a) {
+ Rectangle r;
+ r.x = l->x;
+ r.y = l->y + l->h - a;
+ r.w = l->w;
+ r.h = a;
+ l->h -= a;
+ return r;
+}
+
+Rectangle shrink_rect(const Rectangle* l, int a) {
+ Rectangle r;
+ int a2 = a * 2;
+ r.x = l->x + a;
+ r.y = l->y + a;
+ r.w = l->w - a2;
+ r.h = l->h - a2;
+ return r;
+}
+
+Rectangle centre_rect(
+ const Rectangle* l,
+ const Rectangle* t
+) {
+ Rectangle r;
+ r.x = l->x + l->w / 2 - t->w / 2;
+ r.y = l->y + l->h / 2 - t->h / 2;
+ r.w = t->w;
+ r.h = t->h;
+ return r;
+}
+
+void init_ui(UI* u, Font* f) {
+ u->font = f;
+}
+
+void ui_begin(UI* u, Rectangle* l) {
+ (void)u;
+ l->x = 0;
+ l->y = 0;
+ l->w = get_render_w();
+ l->h = get_render_h();
+}
+
+void ui_end(UI* u) {
+ (void)u;
+}
+
+void ui_label(
+ UI* u,
+ const Rectangle* l,
+ const char* text
+) {
+ rc_add_cmd_rfont_text_col(
+ u->font,
+ l->x,
+ l->y,
+ text,
+ theme_text_colour
+ );
+}
+
+int ui_button(
+ UI* u,
+ const Rectangle* r,
+ const char* label
+) {
+ Rectangle o, t;
+ Colour c;
+ int h;
+ o = shrink_rect(r, theme_outline_width);
+ h = point_rect_overlap(
+ &o,
+ get_mouse_x(),
+ get_mouse_y()
+ );
+ if (h) {
+ if (mbtn_pressed(mbtn_left))
+ c = theme_active_colour;
+ else
+ c = theme_hovered_colour;
+ } else
+ c = theme_background_colour;
+ rc_add_cmd_rect(r, theme_outline_colour);
+ rc_add_cmd_rect(&o, c);
+ t = text_rect(u->font, label);
+ o = centre_rect(&o, &t);
+ rc_add_cmd_rfont_text_col(
+ u->font,
+ o.x,
+ o.y,
+ label,
+ theme_text_colour
+ );
+ return h && mbtn_just_released(mbtn_left);
+}
+
+void ui_container(
+ UI* u,
+ const Rectangle* l,
+ Rectangle* r,
+ const Rectangle* c
+) {
+ Rectangle o, cl;
+ (void)u;
+ rc_add_cmd_rect(l, theme_outline_colour);
+ o = shrink_rect(l, theme_outline_width);
+ rc_add_cmd_rect(&o, theme_background_colour);
+ cl = o;
+ cl.w += cl.x;
+ cl.h += cl.y;
+ rc_add_cmd_clip(&cl);
+ *r = shrink_rect(&o, theme_padding);
+}