Fix moving behaviour
This commit is contained in:
		@@ -76,13 +76,6 @@ i32 run_main_loop(void) {
 | 
			
		||||
 | 
			
		||||
  i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
 | 
			
		||||
 | 
			
		||||
  quad qd = (quad){
 | 
			
		||||
      .p0 = (point){140, 40},
 | 
			
		||||
      .p1 = (point){190, 40},
 | 
			
		||||
      .p2 = (point){170, 200},
 | 
			
		||||
      .p3 = (point){210, 200},
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  while (comp.running) {
 | 
			
		||||
    while (SDL_PollEvent(&(comp.event))) {
 | 
			
		||||
      handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
 | 
			
		||||
@@ -159,8 +152,6 @@ i32 run_main_loop(void) {
 | 
			
		||||
                                  comp.nodes[i].colours);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qd = ui_quad(main_window, &(comp.ctx), qd, op_node_colours);
 | 
			
		||||
 | 
			
		||||
    for (u64 i = 0; i < MAX_WINDOWS; ++i) {
 | 
			
		||||
      swap_buffers(&(comp.windows[i]));
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										54
									
								
								src/ui.c
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								src/ui.c
									
									
									
									
									
								
							@@ -3,10 +3,7 @@
 | 
			
		||||
#include "aliases/aliases.h"
 | 
			
		||||
#include "window.h"
 | 
			
		||||
 | 
			
		||||
bool aabb(rect rect, i32 x, i32 y) {
 | 
			
		||||
  return x > rect.topleft.x && x <= rect.topleft.x + rect.w &&
 | 
			
		||||
         y > rect.topleft.y && y <= rect.topleft.y + rect.h;
 | 
			
		||||
}
 | 
			
		||||
INTERNAL bool aabb(rect rec, i32 x, i32 y);
 | 
			
		||||
 | 
			
		||||
void init_ui_ctx(ui_ctx *ctx) {
 | 
			
		||||
  *ctx = (ui_ctx){0};
 | 
			
		||||
@@ -18,6 +15,8 @@ void reset_ui_ctx(ui_ctx *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 *wnd, ui_ctx *ctx, const SDL_Event *event) {
 | 
			
		||||
@@ -26,6 +25,8 @@ void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
 | 
			
		||||
    if (wnd->id == event->motion.windowID) {
 | 
			
		||||
      ctx->mouse_x = event->motion.x;
 | 
			
		||||
      ctx->mouse_y = event->motion.y;
 | 
			
		||||
      ctx->rel_x += event->motion.xrel;
 | 
			
		||||
      ctx->rel_y += event->motion.yrel;
 | 
			
		||||
      ctx->wnd = wnd;
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
@@ -36,19 +37,19 @@ void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
 | 
			
		||||
      ctx->mouse_y = event->button.y;
 | 
			
		||||
      ctx->mouse_down = true;
 | 
			
		||||
      ctx->wnd = wnd;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  case SDL_MOUSEBUTTONUP:
 | 
			
		||||
    if (wnd->id == event->button.windowID) {
 | 
			
		||||
      ctx->mouse_x = event->button.x;
 | 
			
		||||
      ctx->mouse_y = event->button.y;
 | 
			
		||||
      ctx->mouse_up = true;
 | 
			
		||||
      ctx->wnd = wnd;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ui_button(const window *wnd, ui_ctx *ctx, rect rec,
 | 
			
		||||
@@ -106,12 +107,10 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (ctx->hovered == id && ctx->active == id) {
 | 
			
		||||
    return (rect){
 | 
			
		||||
        .topleft.x = ctx->mouse_x + ctx->rel_x,
 | 
			
		||||
        .topleft.y = ctx->mouse_y + ctx->rel_y,
 | 
			
		||||
        .w = rec.w,
 | 
			
		||||
        .h = rec.h,
 | 
			
		||||
    };
 | 
			
		||||
    rec.topleft.x += ctx->rel_x;
 | 
			
		||||
    rec.topleft.y += ctx->rel_y;
 | 
			
		||||
 | 
			
		||||
    return rec;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
 | 
			
		||||
@@ -122,8 +121,6 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
 | 
			
		||||
 | 
			
		||||
  if (ctx->mouse_down) {
 | 
			
		||||
    ctx->active = id;
 | 
			
		||||
    ctx->rel_x = rec.topleft.x - ctx->mouse_x;
 | 
			
		||||
    ctx->rel_y = rec.topleft.y - ctx->mouse_y;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return rec;
 | 
			
		||||
@@ -149,16 +146,16 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (ctx->hovered == id && ctx->active == id) {
 | 
			
		||||
    return (quad){
 | 
			
		||||
        .p0 = (point){ctx->mouse_x - qd.p0.x + ctx->rel_x,
 | 
			
		||||
                      ctx->mouse_y - qd.p0.y + ctx->rel_y},
 | 
			
		||||
        .p1 = (point){ctx->mouse_x - qd.p1.x + ctx->rel_x,
 | 
			
		||||
                      ctx->mouse_y - qd.p1.y + ctx->rel_y},
 | 
			
		||||
        .p2 = (point){ctx->mouse_x - qd.p2.x + ctx->rel_x,
 | 
			
		||||
                      ctx->mouse_y - qd.p2.y + ctx->rel_y},
 | 
			
		||||
        .p3 = (point){ctx->mouse_x - qd.p3.x + ctx->rel_x,
 | 
			
		||||
                      ctx->mouse_y - qd.p3.y + ctx->rel_y},
 | 
			
		||||
    };
 | 
			
		||||
    qd.p0.x += ctx->rel_x;
 | 
			
		||||
    qd.p0.y += ctx->rel_y;
 | 
			
		||||
    qd.p1.x += ctx->rel_x;
 | 
			
		||||
    qd.p1.y += ctx->rel_y;
 | 
			
		||||
    qd.p2.x += ctx->rel_x;
 | 
			
		||||
    qd.p2.y += ctx->rel_y;
 | 
			
		||||
    qd.p3.x += ctx->rel_x;
 | 
			
		||||
    qd.p3.y += ctx->rel_y;
 | 
			
		||||
 | 
			
		||||
    return qd;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  i32 _min_x_1 = qd.p0.x < qd.p1.x ? qd.p0.x : qd.p1.x;
 | 
			
		||||
@@ -184,8 +181,6 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
 | 
			
		||||
      .h = max_y - min_y,
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  draw_rect(wnd, rec, (colour){.abgr = 0xff000000});
 | 
			
		||||
 | 
			
		||||
  if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
 | 
			
		||||
    return qd;
 | 
			
		||||
  }
 | 
			
		||||
@@ -194,9 +189,12 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
 | 
			
		||||
 | 
			
		||||
  if (ctx->mouse_down) {
 | 
			
		||||
    ctx->active = id;
 | 
			
		||||
    ctx->rel_x = qd.p0.x - ctx->mouse_x;
 | 
			
		||||
    ctx->rel_y = qd.p0.y - ctx->mouse_y;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return qd;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
INTERNAL bool aabb(rect rec, i32 x, i32 y) {
 | 
			
		||||
  return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y &&
 | 
			
		||||
         y <= rec.topleft.y + rec.h;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user