move bboxes

main
Fabien Freling 2022-02-16 00:26:59 +01:00
parent e31683d2fe
commit e86be28cc0
1 changed files with 41 additions and 6 deletions

View File

@ -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<float>(box.width),
static_cast<float>(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<Color> bbox_colors = {RED, GREEN, BLUE};
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE,
YELLOW, ORANGE, PURPLE};
std::vector<freling::BoundingBox> 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;
};