From 7157c963a94dbabe899650b9d1159a1c29a80d47 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Mon, 22 Apr 2024 07:03:25 +0800 Subject: [PATCH] Add FillPoly entity type --- map.cpp | 7 ++++++ map_types.hpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/map.cpp b/map.cpp index ba5be4b..b39bdb1 100644 --- a/map.cpp +++ b/map.cpp @@ -62,6 +62,8 @@ void ui_map() { e = new CameraFocus(); CMD("FILL") e = new FillBox; + CMD("FILLPOLY") + e = new FillPoly; CMD("CUTOFF") cutoff = strtod(strtok(NULL, " "), NULL); CMD("BACKGROUND") { @@ -120,6 +122,11 @@ void ui_map() { entities.push_back((new FillBox())->setpos(center)); selected_entity = entities.back(); } + ig::SameLine(); + if (ig::Button("Fill Poly")) { + entities.push_back((new FillPoly())->setpos(center)); + selected_entity = entities.back(); + } DragDouble("Cutoff depth", &cutoff); ImGui::ColorEdit3("Background", bgcolor, ImGuiColorEditFlags_PickerHueWheel); diff --git a/map_types.hpp b/map_types.hpp index 1378803..a6975b9 100644 --- a/map_types.hpp +++ b/map_types.hpp @@ -7,6 +7,7 @@ #include #include #include +#include class EntityBase { @@ -350,3 +351,67 @@ public: Box2 box; float color[3]; }; + +class FillPoly: public EntityBase { +public: + std::string type() override { return "fillpoly"; } + std::string to_file() override { + std::string ans; + snprintf(buf, sizeof(buf), "FILLPOLY %.0f %.0f %.0f %d", color[0] * 255, color[1] * 255, color[2] * 255, (int)points.size()); + ans += buf; + + for (const Vec2 &p: points) { + snprintf(buf, sizeof(buf), " %.0lf %.0lf", p.x, p.y); + ans += buf; + } + + ans += "\n"; + return ans; + } + void from_strtok() override { + color[0] = TOKEN_DOUBLE / 255.0; + color[1] = TOKEN_DOUBLE / 255.0; + color[2] = TOKEN_DOUBLE / 255.0; + int n = TOKEN_INT; + points.resize(n); + for (int i = 0; i < n; i++) { + double x = TOKEN_DOUBLE; + double y = TOKEN_DOUBLE; + points[i] = Vec2(x, y); + } + } + + void imgui() override { + ig::ColorEdit3("Fill", color, ImGuiColorEditFlags_PickerHueWheel); + ig::SameLine(); + if (ig::Button("Add Point")) { + if (points.empty()) + points.push_back(pos0); + else + points.push_back(points.back()); + } + for (int i = 0; i < points.size(); i++) { + ig::PushID(i); + DragVec2("", &points[i]); + ig::PopID(); + } + } + void draw_prev(Vec2 offset, bool selected) override { + if (points.size() < 3) + return; + ImDrawList *d = ig::GetBackgroundDrawList(); + static std::vector screen; + screen.clear(); + for (const Vec2 &p: points) + screen.push_back((p + offset).im()); + d->AddConvexPolyFilled(screen.data(), screen.size(), ImColor(color[0], color[1], color[2])); + } + EntityBase *setpos(Vec2 pos) override { + pos0 = pos; + return this; + } + + std::vector points; + float color[3]; + Vec2 pos0; +};