]> git.lizzy.rs Git - nothing.git/commitdiff
(#1142) Full resize snapping
authorrexim <reximkut@gmail.com>
Sat, 23 Nov 2019 18:57:00 +0000 (01:57 +0700)
committerrexim <reximkut@gmail.com>
Sat, 23 Nov 2019 18:57:00 +0000 (01:57 +0700)
src/game/level/level_editor/rect_layer.c

index 6bedb90fadb82c053fbd972c02d4bb3a789d1925..98920633fd30a0d51a74cf03d8f5fb1dab000f5b 100644 (file)
@@ -673,23 +673,69 @@ static int rect_layer_event_resize(RectLayer *layer,
         } break;
 
         case 8: {               // RIGHT
+            float y = rects[layer->selection].y;
+            float x = position.x;
+            float h = rects[layer->selection].h;
+            for (size_t i = 0; i < dynarray_count(layer->rects); ++i) {
+                if (i == (size_t) layer->selection) continue;
+
+                const Rect b = rects[i];
+                if (segment_overlap(vec(y, y + h), vec(b.y, b.y + b.h))) {
+                    temp_snap(&x, b.x, b.w, SNAPPING_THRESHOLD);
+                }
+            }
+
             layer->inter_rect = rect_from_points(
                 rect_position(rects[layer->selection]),
-                vec(position.x,
+                vec(x,
                     rects[layer->selection].y + rects[layer->selection].h));
         } break;
 
         case 9: {               // TOP,RIGHT
+            float x = position.x;
+            float y = position.y;
+            float w = rects[layer->selection].w;
+            float h = rects[layer->selection].h;
+            for (size_t i = 0; i < dynarray_count(layer->rects); ++i) {
+                if (i == (size_t) layer->selection) continue;
+
+                const Rect b = rects[i];
+                if (segment_overlap(vec(y, y + h), vec(b.y, b.y + b.h))) {
+                    temp_snap(&x, b.x, b.w, SNAPPING_THRESHOLD);
+                }
+
+                if (segment_overlap(vec(x, x + w), vec(b.x, b.x + b.w))) {
+                    temp_snap(&y, b.y, b.h, SNAPPING_THRESHOLD);
+                }
+            }
+
             layer->inter_rect = rect_from_points(
-                vec(rects[layer->selection].x, position.y),
-                vec(position.x,
+                vec(rects[layer->selection].x, y),
+                vec(x,
                     rects[layer->selection].y + rects[layer->selection].h));
         } break;
 
         case 12: {              // BOTTOM,RIGHT
+            float x = position.x;
+            float y = position.y;
+            float w = rects[layer->selection].w;
+            float h = rects[layer->selection].h;
+            for (size_t i = 0; i < dynarray_count(layer->rects); ++i) {
+                if (i == (size_t) layer->selection) continue;
+
+                const Rect b = rects[i];
+                if (segment_overlap(vec(y, y + h), vec(b.y, b.y + b.h))) {
+                    temp_snap(&x, b.x, b.w, SNAPPING_THRESHOLD);
+                }
+
+                if (segment_overlap(vec(x, x + w), vec(b.x, b.x + b.w))) {
+                    temp_snap(&y, b.y, b.h, SNAPPING_THRESHOLD);
+                }
+            }
+
             layer->inter_rect = rect_from_points(
                 rect_position(rects[layer->selection]),
-                position);
+                vec(x, y));
         } break;
         }
     } break;