From e86be28cc04a3278a7053aa8d2aab9342beb3d58 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Wed, 16 Feb 2022 00:26:59 +0100 Subject: [PATCH] move bboxes --- src/main_gui.cpp | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main_gui.cpp b/src/main_gui.cpp index e3c98f0..41edd81 100644 --- a/src/main_gui.cpp +++ b/src/main_gui.cpp @@ -19,12 +19,18 @@ namespace fs = std::filesystem; -void draw(const freling::BoundingBox& box, - const Vector2& offset, - const Color& color) { +Rectangle rect_from_bbox(const freling::BoundingBox& box, + const Vector2& offset) { const Rectangle rect = {offset.x + box.x, offset.y + box.y, static_cast(box.width), static_cast(box.height)}; + return rect; +} + +void draw(const freling::BoundingBox& box, + const Vector2& offset, + const Color& color) { + const Rectangle rect = rect_from_bbox(box, offset); DrawRectangleRec(rect, ColorAlpha(color, 0.3)); DrawRectangleLinesEx(rect, 3, color); } @@ -78,7 +84,8 @@ int main(int argc, const char* argv[]) { bboxes.push_back(freling::BoundingBox({x, y, w, h})); i += 4; } - const std::vector bbox_colors = {RED, GREEN, BLUE}; + const std::vector bbox_colors = {RED, GREEN, BLUE, + YELLOW, ORANGE, PURPLE}; std::vector packed_bboxes; Vector2 win_size = {800, 450}; @@ -102,8 +109,36 @@ int main(int argc, const char* argv[]) { SetWindowSize(win_size.x, win_size.y); const Vector2 b_size = {150, 30}; + Vector2 mouse_pos = {0}; + Vector2 mouse_delta = {0}; + int b_moving_idx = -1; while (!WindowShouldClose()) { + // + // Update + // + mouse_pos = GetMousePosition(); + mouse_delta = GetMouseDelta(); + for (int b = 0, size = bboxes.size(); b < size; ++b) { + const auto& box = bboxes[b]; + const Rectangle rect = rect_from_bbox(box, in_offset); + if (CheckCollisionPointRec(mouse_pos, rect) and + IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + b_moving_idx = b; + } + } + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { + b_moving_idx = -1; + } + if (b_moving_idx != -1) { + auto& box = bboxes[b_moving_idx]; + box.x += mouse_delta.x; + box.y += mouse_delta.y; + } + + // + // Draw + // BeginDrawing(); ClearBackground(RAYWHITE); @@ -150,7 +185,7 @@ int main(int argc, const char* argv[]) { int c = 0; for (const auto& b : bboxes) { - const auto& color = bbox_colors[c % bboxes.size()]; + const auto& color = bbox_colors[c % bbox_colors.size()]; draw(b, in_offset, color); ++c; }; @@ -167,7 +202,7 @@ int main(int argc, const char* argv[]) { int c = 0; for (const auto& b : packed_bboxes) { - const auto& color = bbox_colors[c % bboxes.size()]; + const auto& color = bbox_colors[c % bbox_colors.size()]; draw(b, pack_offset, color); ++c; };