Compare commits
	
		
			2 Commits
		
	
	
		
			fb1af03308
			...
			718e7eec21
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 718e7eec21 | |||
| c5b263e948 | 
@@ -35,16 +35,17 @@ struct ui_ctx {
 | 
			
		||||
  u64 count;
 | 
			
		||||
  i64 hovered;
 | 
			
		||||
  i64 active;
 | 
			
		||||
  i64 mouse_x;
 | 
			
		||||
  i64 mouse_y;
 | 
			
		||||
  i64 rel_x;
 | 
			
		||||
  i64 rel_y;
 | 
			
		||||
  i32 mouse_x;
 | 
			
		||||
  i32 mouse_y;
 | 
			
		||||
  i32 rel_x;
 | 
			
		||||
  i32 rel_y;
 | 
			
		||||
  bool mouse_down;
 | 
			
		||||
  bool mouse_up;
 | 
			
		||||
  const window_t *wnd;
 | 
			
		||||
  ui_elem_t elements[MAX_UI_ELEMENTS];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void init_ui_ctx(ui_ctx_t *ctx);
 | 
			
		||||
void reset_ui_ctx(ui_ctx_t *ctx);
 | 
			
		||||
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
 | 
			
		||||
                      const SDL_Event *event);
 | 
			
		||||
 
 | 
			
		||||
@@ -42,6 +42,9 @@ i32 run_main_loop(void) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  compositor_t comp = {0};
 | 
			
		||||
 | 
			
		||||
  init_ui_ctx(&(comp.ctx));
 | 
			
		||||
 | 
			
		||||
  comp.rects = (rect_t *)malloc(sizeof(rect_t) * MAX_NODES);
 | 
			
		||||
  comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								src/ui.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								src/ui.c
									
									
									
									
									
								
							@@ -2,7 +2,6 @@
 | 
			
		||||
#include "SDL_events.h"
 | 
			
		||||
#include "aliases/aliases.h"
 | 
			
		||||
#include "window.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
typedef struct ui_elem_colours ui_elem_colours_t;
 | 
			
		||||
struct ui_elem_colours {
 | 
			
		||||
@@ -28,21 +27,22 @@ bool aabb(const ui_elem_t *elem, i32 x, i32 y) {
 | 
			
		||||
         y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void init_ui_ctx(ui_ctx_t *ctx) {
 | 
			
		||||
  *ctx = (ui_ctx_t){0};
 | 
			
		||||
  ctx->hovered = -1;
 | 
			
		||||
  ctx->active = -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reset_ui_ctx(ui_ctx_t *ctx) {
 | 
			
		||||
  ctx->count = 0;
 | 
			
		||||
  ctx->mouse_down = false;
 | 
			
		||||
  ctx->mouse_up = false;
 | 
			
		||||
  ctx->rel_x = 0;
 | 
			
		||||
  ctx->rel_y = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
 | 
			
		||||
                      const SDL_Event *event) {
 | 
			
		||||
  switch (event->type) {
 | 
			
		||||
  case SDL_MOUSEMOTION:
 | 
			
		||||
    ctx->rel_x = event->motion.xrel;
 | 
			
		||||
    ctx->rel_y = event->motion.yrel;
 | 
			
		||||
 | 
			
		||||
    if (wnd->id == event->motion.windowID) {
 | 
			
		||||
      ctx->mouse_x = event->motion.x;
 | 
			
		||||
      ctx->mouse_y = event->motion.y;
 | 
			
		||||
@@ -85,7 +85,7 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
 | 
			
		||||
  fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill);
 | 
			
		||||
  draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border);
 | 
			
		||||
 | 
			
		||||
  if (wnd != ctx->wnd) {
 | 
			
		||||
  if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -115,24 +115,26 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
 | 
			
		||||
  ui_elem_t elem = (ui_elem_t){
 | 
			
		||||
      .id = (ctx->count)++,
 | 
			
		||||
      .rect = rect,
 | 
			
		||||
      .type = UI_ELEM_BUTTON,
 | 
			
		||||
      .type = UI_ELEM_NODE,
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill);
 | 
			
		||||
  draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border);
 | 
			
		||||
  fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill);
 | 
			
		||||
  draw_rect(wnd, rect, colours[UI_ELEM_NODE].border);
 | 
			
		||||
 | 
			
		||||
  if (wnd != ctx->wnd) {
 | 
			
		||||
  if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
 | 
			
		||||
    return rect;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (ctx->mouse_up) {
 | 
			
		||||
    ctx->hovered = ctx->active = -1;
 | 
			
		||||
    ctx->rel_x = ctx->rel_y = 0;
 | 
			
		||||
    return rect;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (ctx->hovered == elem.id && ctx->active == elem.id) {
 | 
			
		||||
    return (rect_t){
 | 
			
		||||
        .topleft.x = rect.topleft.x + ctx->rel_x,
 | 
			
		||||
        .topleft.y = rect.topleft.y + ctx->rel_y,
 | 
			
		||||
        .topleft.x = ctx->mouse_x + ctx->rel_x,
 | 
			
		||||
        .topleft.y = ctx->mouse_y + ctx->rel_y,
 | 
			
		||||
        .w = rect.w,
 | 
			
		||||
        .h = rect.h,
 | 
			
		||||
    };
 | 
			
		||||
@@ -146,6 +148,8 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
 | 
			
		||||
 | 
			
		||||
  if (ctx->mouse_down) {
 | 
			
		||||
    ctx->active = elem.id;
 | 
			
		||||
    ctx->rel_x = rect.topleft.x - ctx->mouse_x;
 | 
			
		||||
    ctx->rel_y = rect.topleft.y - ctx->mouse_y;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return rect;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user