map regions in origin

This commit is contained in:
Fabien Freling 2022-02-16 19:16:17 +01:00
parent 38e9a97a85
commit 4447cd67d1
7 changed files with 82 additions and 12 deletions

View file

@ -16,6 +16,7 @@
#include "a2.h"
#include "bounding_box.h"
#include "frame.h"
#include "mapping.h"
#include "pack.h"
#include "png.h"
@ -31,12 +32,12 @@ Rectangle rect_from_bbox(const freling::BoundingBox& box,
return rect;
}
void draw(const freling::BoundingBox& box,
const Vector2& offset,
const Color& color) {
void draw_resizable(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);
DrawRectangleLinesEx(rect, 2, color);
DrawTriangle(
(Vector2){rect.x + rect.width - resize_handle, rect.y + rect.height},
(Vector2){rect.x + rect.width, rect.y + rect.height},
@ -44,6 +45,13 @@ void draw(const freling::BoundingBox& box,
color);
}
void draw_region(const freling::BoundingBox& box,
const Vector2& offset,
const Color& color) {
const Rectangle rect = rect_from_bbox(box, offset);
DrawRectangleRec(rect, ColorAlpha(color, 0.8));
}
Image image_from_frame(const Frame& frame) {
Image image = {0};
@ -97,6 +105,7 @@ int main(int argc, const char* argv[]) {
YELLOW, ORANGE, PURPLE};
std::vector<freling::BoundingBox> packed_bboxes;
std::vector<freling::BoundingBox> a2_bboxes;
std::vector<freling::BoundingBox> a2_bboxes_origin;
Vector2 win_size = {800, 450};
const int padding = 5;
@ -162,6 +171,7 @@ int main(int argc, const char* argv[]) {
auto& box = bboxes[b_moving_idx];
box.x += mouse_delta.x;
box.y += mouse_delta.y;
packed = false;
}
if (b_resize_idx != idx_unset) {
auto& box = bboxes[b_resize_idx];
@ -169,11 +179,13 @@ int main(int argc, const char* argv[]) {
resize_handle);
box.height = std::max(static_cast<int>(box.height + mouse_delta.y),
resize_handle);
packed = false;
}
if (b_delete_idx != idx_unset) {
bboxes.erase(bboxes.begin() + b_delete_idx);
b_delete_idx = idx_unset;
}
a2_processed = a2_processed and packed;
//
// Draw
@ -192,6 +204,7 @@ int main(int argc, const char* argv[]) {
if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
b_size.y},
"Pack Rects")) {
a2_processed = false;
std::optional<Frame> f_packed =
freling::pack(*in_frame, bboxes, packed_bboxes);
@ -219,6 +232,8 @@ int main(int argc, const char* argv[]) {
b_size.x, b_size.y},
"A2")) {
a2_bboxes = freling::A2(packed_bboxes);
a2_bboxes_origin =
freling::map_to_origin(bboxes, packed_bboxes, a2_bboxes);
a2_processed = true;
}
@ -232,10 +247,19 @@ int main(int argc, const char* argv[]) {
int c = 0;
for (const auto& b : bboxes) {
const auto& color = bbox_colors[c % bbox_colors.size()];
draw(b, in_offset, color);
draw_resizable(b, in_offset, color);
++c;
};
c = 0;
if (a2_processed) {
for (const auto& b : a2_bboxes_origin) {
const auto& color = bbox_colors[c % bbox_colors.size()];
draw_region(b, in_offset, color);
++c;
};
}
//
// Packed image
//
@ -249,7 +273,8 @@ int main(int argc, const char* argv[]) {
int c = 0;
for (const auto& b : packed_bboxes) {
const auto& color = bbox_colors[c % bbox_colors.size()];
draw(b, pack_offset, color);
const Rectangle rect = rect_from_bbox(b, pack_offset);
DrawRectangleLinesEx(rect, 2, color);
++c;
};
@ -257,7 +282,7 @@ int main(int argc, const char* argv[]) {
int c = 0;
for (const auto& b : a2_bboxes) {
const auto& color = bbox_colors[c % bbox_colors.size()];
draw(b, pack_offset, color);
draw_region(b, pack_offset, color);
++c;
};
}