use raygui
This commit is contained in:
parent
8d91216f24
commit
81463d9eb0
9 changed files with 4589 additions and 83 deletions
145
src/main_gui.cpp
145
src/main_gui.cpp
|
@ -5,9 +5,12 @@
|
|||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace raylib {
|
||||
#include <raylib.h>
|
||||
}
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-result"
|
||||
#define RAYGUI_IMPLEMENTATION
|
||||
#include <raygui.h>
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
#include "bounding_box.h"
|
||||
#include "frame.h"
|
||||
|
@ -17,44 +20,40 @@ namespace raylib {
|
|||
namespace fs = std::filesystem;
|
||||
|
||||
struct Button {
|
||||
raylib::Rectangle rect;
|
||||
Rectangle rect;
|
||||
std::string text;
|
||||
bool hover = false;
|
||||
|
||||
bool pressed() {
|
||||
hover =
|
||||
raylib::CheckCollisionPointRec(raylib::GetMousePosition(), rect);
|
||||
return hover and
|
||||
raylib::IsMouseButtonReleased(raylib::MOUSE_BUTTON_LEFT);
|
||||
hover = CheckCollisionPointRec(GetMousePosition(), rect);
|
||||
return hover and IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
|
||||
};
|
||||
|
||||
void draw() const {
|
||||
raylib::DrawRectangleRec(rect, raylib::LIGHTGRAY);
|
||||
raylib::DrawRectangleLines(rect.x, rect.y, rect.width, rect.height,
|
||||
raylib::BLUE);
|
||||
raylib::DrawText(text.c_str(),
|
||||
(rect.x + rect.width / 2 -
|
||||
raylib::MeasureText(text.c_str(), 10) / 2),
|
||||
rect.y + 11, 10, raylib::DARKBLUE);
|
||||
DrawRectangleRec(rect, LIGHTGRAY);
|
||||
DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLUE);
|
||||
DrawText(text.c_str(),
|
||||
(rect.x + rect.width / 2 - MeasureText(text.c_str(), 10) / 2),
|
||||
rect.y + 11, 10, DARKBLUE);
|
||||
};
|
||||
};
|
||||
|
||||
void draw(const BoundingBox& box,
|
||||
const raylib::Vector2& offset,
|
||||
const raylib::Color& color) {
|
||||
const raylib::Rectangle rect = {offset.x + box.left, offset.y + box.top,
|
||||
static_cast<float>(box.right - box.left),
|
||||
static_cast<float>(box.bottom - box.top)};
|
||||
raylib::DrawRectangleRec(rect, raylib::ColorAlpha(color, 0.3));
|
||||
raylib::DrawRectangleLinesEx(rect, 3, color);
|
||||
void draw(const freling::BoundingBox& box,
|
||||
const Vector2& offset,
|
||||
const Color& color) {
|
||||
const Rectangle rect = {offset.x + box.left, offset.y + box.top,
|
||||
static_cast<float>(box.right - box.left),
|
||||
static_cast<float>(box.bottom - box.top)};
|
||||
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
|
||||
DrawRectangleLinesEx(rect, 3, color);
|
||||
}
|
||||
|
||||
raylib::Image image_from_frame(const Frame& frame) {
|
||||
raylib::Image image = {0};
|
||||
Image image_from_frame(const Frame& frame) {
|
||||
Image image = {0};
|
||||
|
||||
const int format = raylib::PIXELFORMAT_UNCOMPRESSED_R8G8B8;
|
||||
const int format = PIXELFORMAT_UNCOMPRESSED_R8G8B8;
|
||||
const unsigned int size =
|
||||
raylib::GetPixelDataSize(frame.width, frame.height, format);
|
||||
GetPixelDataSize(frame.width, frame.height, format);
|
||||
|
||||
image.data = RL_MALLOC(size);
|
||||
memcpy(image.data, frame.data, size);
|
||||
|
@ -87,7 +86,7 @@ int main(int argc, const char* argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::vector<BoundingBox> bboxes;
|
||||
std::vector<freling::BoundingBox> bboxes;
|
||||
bboxes.reserve((argc - 2) / 4);
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
|
@ -95,54 +94,60 @@ int main(int argc, const char* argv[]) {
|
|||
const uint32_t y1 = atoi(argv[i + 1]);
|
||||
const uint32_t x2 = atoi(argv[i + 2]);
|
||||
const uint32_t y2 = atoi(argv[i + 3]);
|
||||
bboxes.push_back(BoundingBox({x1, y1, x2, y2}));
|
||||
bboxes.push_back(freling::BoundingBox({x1, y1, x2, y2}));
|
||||
i += 4;
|
||||
}
|
||||
const std::vector<raylib::Color> bbox_colors = {raylib::RED, raylib::GREEN,
|
||||
raylib::BLUE};
|
||||
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};
|
||||
|
||||
raylib::Vector2 win_size = {800, 450};
|
||||
Vector2 win_size = {800, 450};
|
||||
const int padding = 5;
|
||||
const raylib::Vector2 in_offset = {padding, 40};
|
||||
const Vector2 in_offset = {padding, 40};
|
||||
|
||||
raylib::InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
|
||||
raylib::SetTargetFPS(60);
|
||||
InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
|
||||
SetTargetFPS(60);
|
||||
|
||||
raylib::Image in_img = image_from_frame(*in_frame);
|
||||
raylib::Texture2D in_texture = raylib::LoadTextureFromImage(in_img);
|
||||
Image in_img = image_from_frame(*in_frame);
|
||||
Texture2D in_texture = LoadTextureFromImage(in_img);
|
||||
|
||||
raylib::Image pack_img;
|
||||
raylib::Texture2D pack_texture;
|
||||
Image pack_img;
|
||||
Texture2D pack_texture;
|
||||
bool packed = false;
|
||||
|
||||
win_size.x = std::max(win_size.x, in_offset.x + in_texture.width + padding);
|
||||
win_size.y =
|
||||
std::max(win_size.y, in_offset.y + in_texture.height + padding);
|
||||
|
||||
raylib::SetWindowSize(win_size.x, win_size.y);
|
||||
SetWindowSize(win_size.x, win_size.y);
|
||||
|
||||
const raylib::Vector2 b_size = {150, 30};
|
||||
const Vector2 b_size = {150, 30};
|
||||
Button b_add_box = {{padding, padding, b_size.x, b_size.y}, "Add box"};
|
||||
Button b_pack = {{padding * 2 + b_size.x, padding, b_size.x, b_size.y},
|
||||
"Pack rects"};
|
||||
|
||||
while (!raylib::WindowShouldClose()) {
|
||||
while (!WindowShouldClose()) {
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
//
|
||||
// Update
|
||||
// Buttons
|
||||
//
|
||||
if (b_add_box.pressed()) {
|
||||
bboxes.push_back(BoundingBox({0, 0, 64, 64}));
|
||||
if (GuiButton((Rectangle){padding, padding, b_size.x, b_size.y},
|
||||
"Add Box")) {
|
||||
bboxes.push_back(freling::BoundingBox({0, 0, 64, 64}));
|
||||
}
|
||||
if (b_pack.pressed()) {
|
||||
Frame f_packed = pack(*in_frame, bboxes);
|
||||
|
||||
if (GuiButton((Rectangle){padding * 2 + b_size.x, padding, b_size.x,
|
||||
b_size.y},
|
||||
"Pack Rects")) {
|
||||
std::optional<Frame> f_packed = pack(*in_frame, bboxes);
|
||||
|
||||
if (packed) {
|
||||
raylib::UnloadTexture(pack_texture);
|
||||
raylib::UnloadImage(pack_img);
|
||||
UnloadTexture(pack_texture);
|
||||
UnloadImage(pack_img);
|
||||
}
|
||||
|
||||
pack_img = image_from_frame(f_packed);
|
||||
pack_texture = raylib::LoadTextureFromImage(pack_img);
|
||||
pack_img = image_from_frame(*f_packed);
|
||||
pack_texture = LoadTextureFromImage(pack_img);
|
||||
packed = true;
|
||||
|
||||
win_size.x =
|
||||
|
@ -153,23 +158,15 @@ int main(int argc, const char* argv[]) {
|
|||
in_offset.y + std::max(in_texture.height, pack_texture.height) +
|
||||
padding);
|
||||
|
||||
raylib::SetWindowSize(win_size.x, win_size.y);
|
||||
SetWindowSize(win_size.x, win_size.y);
|
||||
}
|
||||
|
||||
//
|
||||
// Draw
|
||||
//
|
||||
raylib::BeginDrawing();
|
||||
raylib::ClearBackground(raylib::RAYWHITE);
|
||||
|
||||
b_add_box.draw();
|
||||
b_pack.draw();
|
||||
|
||||
// Input image
|
||||
raylib::DrawTexture(in_texture, in_offset.x, in_offset.y,
|
||||
raylib::WHITE);
|
||||
raylib::DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width,
|
||||
in_texture.height, raylib::BLACK);
|
||||
//
|
||||
DrawTexture(in_texture, in_offset.x, in_offset.y, WHITE);
|
||||
DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width,
|
||||
in_texture.height, BLACK);
|
||||
|
||||
int c = 0;
|
||||
for (const auto& b : bboxes) {
|
||||
|
@ -178,27 +175,27 @@ int main(int argc, const char* argv[]) {
|
|||
++c;
|
||||
};
|
||||
|
||||
//
|
||||
// Packed image
|
||||
//
|
||||
if (packed) {
|
||||
const int offset_x = in_offset.x + in_texture.width + padding;
|
||||
raylib::DrawTexture(pack_texture, offset_x, in_offset.y,
|
||||
raylib::WHITE);
|
||||
raylib::DrawRectangleLines(offset_x, in_offset.y,
|
||||
pack_texture.width, pack_texture.height,
|
||||
raylib::BLACK);
|
||||
DrawTexture(pack_texture, offset_x, in_offset.y, WHITE);
|
||||
DrawRectangleLines(offset_x, in_offset.y, pack_texture.width,
|
||||
pack_texture.height, BLACK);
|
||||
}
|
||||
|
||||
raylib::EndDrawing();
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
raylib::UnloadTexture(in_texture);
|
||||
raylib::UnloadImage(in_img);
|
||||
UnloadTexture(in_texture);
|
||||
UnloadImage(in_img);
|
||||
if (packed) {
|
||||
raylib::UnloadTexture(pack_texture);
|
||||
raylib::UnloadImage(pack_img);
|
||||
UnloadTexture(pack_texture);
|
||||
UnloadImage(pack_img);
|
||||
}
|
||||
raylib::CloseWindow();
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue