Skip to content

Commit

Permalink
Fix right mouse button panning at far out zooms, and fix routing debu…
Browse files Browse the repository at this point in the history
…g line transform
  • Loading branch information
rj45 committed May 12, 2024
1 parent e3449d9 commit 54e1b9f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 36 deletions.
25 changes: 6 additions & 19 deletions src/autoroute/autoroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,7 @@ void draw_stroked_line(
Context ctx, HMM_Vec2 start, HMM_Vec2 end, float line_thickness,
HMM_Vec4 color);

static HMM_Vec2 panZoom(RT_Point position, float zoom, HMM_Vec2 pan) {
return HMM_AddV2(HMM_MulV2F(HMM_V2(position.x, position.y), zoom), pan);
}

void autoroute_draw_debug_lines(
AutoRoute *ar, void *ctx, float zoom, HMM_Vec2 pan) {
void autoroute_draw_debug_lines(AutoRoute *ar, void *ctx) {
const RT_Node *nodes;
size_t nodeCount;

Expand All @@ -311,26 +306,18 @@ void autoroute_draw_debug_lines(
if (neighbors[j] < nodeCount) {
RT_Point p2 = nodes[neighbors[j]].position;
draw_stroked_line(
ctx, panZoom(p1, zoom, pan), panZoom(p2, zoom, pan), 1,
ctx, HMM_V2(p1.x, p1.y), HMM_V2(p2.x, p2.y), 1,
HMM_V4(0.5f, 0.5f, 0.7f, 0.5f));
}
}
}

for (uint32_t i = 0; i < circuit_component_len(&ar->view->circuit); i++) {
RT_BoundingBox *box = &ar->boxes[i];
HMM_Vec2 tl = panZoom(
(RT_Point){
.x = box->center.x - box->half_width,
.y = box->center.y - box->half_height,
},
zoom, pan);
HMM_Vec2 br = panZoom(
(RT_Point){
.x = box->center.x + box->half_width,
.y = box->center.y + box->half_height,
},
zoom, pan);
HMM_Vec2 tl =
HMM_V2(box->center.x - box->half_width, box->center.y - box->half_height);
HMM_Vec2 br =
HMM_V2(box->center.x + box->half_width, box->center.y + box->half_height);
draw_stroked_line(
ctx, tl, HMM_V2(br.X, tl.Y), 1, HMM_V4(0.7f, 0.5f, 0.5f, 0.5f));
draw_stroked_line(
Expand Down
3 changes: 1 addition & 2 deletions src/autoroute/autoroute.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ size_t autoroute_wire_vertices(
void autoroute_get_junction_pos(
AutoRoute *ar, JunctionID junctionID, float *x, float *y);

void autoroute_draw_debug_lines(
AutoRoute *ar, void *ctx, float zoom, HMM_Vec2 pan);
void autoroute_draw_debug_lines(AutoRoute *ar, void *ctx);
void autoroute_dump_anchor_boxes(AutoRoute *ar);

#endif // AUTOROUTE_H
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ void event(const sapp_event *event, void *user_data) {
case SAPP_EVENTTYPE_MOUSE_SCROLL: {
app->circuit.input.scroll = HMM_AddV2(
app->circuit.input.scroll, HMM_V2(event->scroll_x, event->scroll_y));

break;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion src/render/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ float draw_get_zoom(DrawContext *draw) { return draw->zoom; }
void draw_begin_frame(DrawContext *draw) {
sgp_push_transform();
sgp_reset_transform();
sgp_translate(draw->pan.X, draw->pan.Y);
sgp_scale(draw->zoom, draw->zoom);
sgp_translate(draw->pan.X, draw->pan.Y);
}

void draw_end_frame(DrawContext *draw) { sgp_pop_transform(); }
Expand Down
21 changes: 8 additions & 13 deletions src/ux/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ static void ux_mouse_down_state_machine(CircuitUX *ux, HMM_Vec2 worldMousePos) {
}
case STATE_PAN: {
HMM_Vec2 delta = HMM_SubV2(worldMousePos, ux->downStart);
ux->downStart = HMM_SubV2(worldMousePos, delta);
draw_add_pan(ux->view.drawCtx, delta);
break;
}
Expand All @@ -290,9 +289,8 @@ static void ux_handle_mouse(CircuitUX *ux) {
ux->view.hovered = NO_ID;
ux->view.hoveredPort = NO_PORT;

HMM_Vec2 worldMousePos = HMM_DivV2F(
HMM_SubV2(ux->input.mousePos, draw_get_pan(ux->view.drawCtx)),
draw_get_zoom(ux->view.drawCtx));
HMM_Vec2 worldMousePos =
draw_screen_to_world(ux->view.drawCtx, ux->input.mousePos);

Box mouseBox = {
.center = worldMousePos,
Expand Down Expand Up @@ -356,25 +354,24 @@ static void ux_zoom(CircuitUX *ux) {

// figure out the correction to the pan so that the zoom is centred on the
// mouse position
HMM_Vec2 correction =
HMM_MulV2F(HMM_SubV2(newMousePos, originalMousePos), newZoom);
HMM_Vec2 correction = HMM_SubV2(newMousePos, originalMousePos);
draw_add_pan(ux->view.drawCtx, correction);
}

void ux_update(CircuitUX *ux) {
float dt = (float)ux->input.frameDuration;
HMM_Vec2 panDelta = HMM_V2(0, 0);
if (bv_is_set(ux->input.keysDown, KEYCODE_W)) {
panDelta.Y += 600.0f * dt * draw_get_zoom(ux->view.drawCtx);
panDelta.Y += 600.0f * dt; // * (1 / draw_get_zoom(ux->view.drawCtx));
}
if (bv_is_set(ux->input.keysDown, KEYCODE_A)) {
panDelta.X += 600.0f * dt * draw_get_zoom(ux->view.drawCtx);
panDelta.X += 600.0f * dt; // * (1 / draw_get_zoom(ux->view.drawCtx));
}
if (bv_is_set(ux->input.keysDown, KEYCODE_S)) {
panDelta.Y -= 600.0f * dt * draw_get_zoom(ux->view.drawCtx);
panDelta.Y -= 600.0f * dt; // * (1 / draw_get_zoom(ux->view.drawCtx));
}
if (bv_is_set(ux->input.keysDown, KEYCODE_D)) {
panDelta.X -= 600.0f * dt * draw_get_zoom(ux->view.drawCtx);
panDelta.X -= 600.0f * dt; // * (1 / draw_get_zoom(ux->view.drawCtx));
}
if (panDelta.X != 0 || panDelta.Y != 0) {
draw_add_pan(ux->view.drawCtx, panDelta);
Expand Down Expand Up @@ -427,8 +424,6 @@ void ux_draw(CircuitUX *ux) {
view_draw(&ux->view);

if (ux->debugLines) {
autoroute_draw_debug_lines(
ux->router, ux->view.drawCtx, draw_get_zoom(ux->view.drawCtx),
draw_get_pan(ux->view.drawCtx));
autoroute_draw_debug_lines(ux->router, ux->view.drawCtx);
}
}

0 comments on commit 54e1b9f

Please sign in to comment.