fix naive packer

This commit is contained in:
Fabien Freling 2022-02-16 13:25:22 +01:00
parent 1d851158fc
commit 7d2b7b9ba7
3 changed files with 36 additions and 16 deletions

View file

@ -119,8 +119,10 @@ int main(int argc, const char* argv[]) {
const Vector2 b_size = {150, 30};
Vector2 mouse_pos = {0};
Vector2 mouse_delta = {0};
int b_moving_idx = -1;
int b_resize_idx = -1;
constexpr int idx_unset = -1;
int b_moving_idx = idx_unset;
int b_resize_idx = idx_unset;
int b_delete_idx = idx_unset;
while (!WindowShouldClose()) {
//
@ -131,8 +133,10 @@ int main(int argc, const char* argv[]) {
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)) {
if (not CheckCollisionPointRec(mouse_pos, rect)) {
continue;
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
const Rectangle handle_area = {
rect.x + rect.width - resize_handle,
rect.y + rect.height - resize_handle, resize_handle,
@ -143,23 +147,30 @@ int main(int argc, const char* argv[]) {
b_moving_idx = b;
}
}
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
b_delete_idx = b;
}
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
b_moving_idx = -1;
b_resize_idx = -1;
b_moving_idx = idx_unset;
b_resize_idx = idx_unset;
}
if (b_moving_idx != -1) {
if (b_moving_idx != idx_unset) {
auto& box = bboxes[b_moving_idx];
box.x += mouse_delta.x;
box.y += mouse_delta.y;
}
if (b_resize_idx != -1) {
if (b_resize_idx != idx_unset) {
auto& box = bboxes[b_resize_idx];
box.width = std::max(static_cast<int>(box.width + mouse_delta.x),
resize_handle);
box.height = std::max(static_cast<int>(box.height + mouse_delta.y),
resize_handle);
}
if (b_delete_idx != idx_unset) {
bboxes.erase(bboxes.begin() + b_delete_idx);
b_delete_idx = idx_unset;
}
//
// Draw