Add background color & fill rects

This commit is contained in:
Edgaru089 2024-04-16 19:34:36 +08:00
parent 9cd789cf53
commit 65b1c29199
2 changed files with 61 additions and 3 deletions

26
map.cpp
View File

@ -15,7 +15,8 @@ static std::list<EntityBase *> entities;
static EntityBase *selected_entity = NULL; static EntityBase *selected_entity = NULL;
static EntityBase *to_delete = NULL; static EntityBase *to_delete = NULL;
static Vec2 offset0(0, 0); static Vec2 offset0(0, 0);
static double cutoff = 1500; static double cutoff = 1500;
static float bgcolor[3] = {0.0f, 0.0f, 0.0f};
void ui_map() { void ui_map() {
@ -32,6 +33,7 @@ void ui_map() {
for (EntityBase *e: entities) for (EntityBase *e: entities)
delete e; delete e;
entities.clear(); entities.clear();
selected_entity = NULL;
std::istringstream is(buf_code); std::istringstream is(buf_code);
std::string line; std::string line;
@ -58,8 +60,15 @@ void ui_map() {
e = new LevelTransition(); e = new LevelTransition();
CMD("CAMERA_FOCUS") CMD("CAMERA_FOCUS")
e = new CameraFocus(); e = new CameraFocus();
CMD("FILL_BOX")
e = new FillBox;
CMD("CUTOFF") CMD("CUTOFF")
cutoff = strtod(strtok(NULL, " "), NULL); cutoff = strtod(strtok(NULL, " "), NULL);
CMD("BACKGROUND") {
bgcolor[0] = strtof(TOKEN, NULL) / 255.0f;
bgcolor[1] = strtof(TOKEN, NULL) / 255.0f;
bgcolor[2] = strtof(TOKEN, NULL) / 255.0f;
}
if (e != NULL) { if (e != NULL) {
e->from_strtok(); e->from_strtok();
@ -106,8 +115,14 @@ void ui_map() {
entities.push_back((new CameraFocus())->setpos(center)); entities.push_back((new CameraFocus())->setpos(center));
selected_entity = entities.back(); selected_entity = entities.back();
} }
ig::SameLine();
if (ig::Button("Fill Box")) {
entities.push_back((new FillBox())->setpos(center));
selected_entity = entities.back();
}
DragDouble("Cutoff depth", &cutoff); DragDouble("Cutoff depth", &cutoff);
ImGui::ColorEdit3("Background", bgcolor, ImGuiColorEditFlags_PickerHueWheel);
ig::SeparatorText("Entities"); ig::SeparatorText("Entities");
if (ig::Selectable("<<< CLEAR >>>", selected_entity == NULL)) if (ig::Selectable("<<< CLEAR >>>", selected_entity == NULL))
@ -126,6 +141,10 @@ void ui_map() {
} }
ig::End(); ig::End();
ImVec2 screen_size = ig::GetIO().DisplaySize;
ImDrawList *drawlist = ig::GetBackgroundDrawList();
drawlist->AddRectFilled(ImVec2(0, 0), screen_size, ImGui::ColorConvertFloat4ToU32(ImVec4(bgcolor[0], bgcolor[1], bgcolor[2], 1.0f)));
if (ig::Begin("Properities") && selected_entity != NULL) { if (ig::Begin("Properities") && selected_entity != NULL) {
ig::PushID(selected_entity); ig::PushID(selected_entity);
selected_entity->imgui(); selected_entity->imgui();
@ -133,12 +152,12 @@ void ui_map() {
} }
ig::End(); ig::End();
ImVec2 screen_size = ig::GetIO().DisplaySize;
ImDrawList *drawlist = ig::GetBackgroundDrawList();
drawlist->AddLine(ImVec2(0, offset.y), ImVec2(screen_size.x, offset.y), IM_COL32_WHITE); drawlist->AddLine(ImVec2(0, offset.y), ImVec2(screen_size.x, offset.y), IM_COL32_WHITE);
drawlist->AddLine(ImVec2(offset.x, 0), ImVec2(offset.x, screen_size.y), IM_COL32_WHITE); drawlist->AddLine(ImVec2(offset.x, 0), ImVec2(offset.x, screen_size.y), IM_COL32_WHITE);
drawlist->AddLine(ImVec2(0, cutoff + offset.y), ImVec2(screen_size.x, cutoff + offset.y), IM_COL32(255, 0, 0, 255)); drawlist->AddLine(ImVec2(0, cutoff + offset.y), ImVec2(screen_size.x, cutoff + offset.y), IM_COL32(255, 0, 0, 255));
// Draw every entity // Draw every entity
for (EntityBase *e: entities)
e->draw_prev(offset, e == selected_entity);
for (EntityBase *e: entities) for (EntityBase *e: entities)
e->draw(offset, e == selected_entity); e->draw(offset, e == selected_entity);
@ -146,6 +165,7 @@ void ui_map() {
static std::string buff; static std::string buff;
buff.clear(); buff.clear();
buff = "CUTOFF " + std::to_string((int)std::round(cutoff)) + "\n"; buff = "CUTOFF " + std::to_string((int)std::round(cutoff)) + "\n";
buff += "BACKGROUND " + std::to_string((int)std::round(bgcolor[0] * 255.0f)) + " " + std::to_string((int)std::round(bgcolor[1] * 255.0f)) + " " + std::to_string((int)std::round(bgcolor[2] * 255.0f)) + "\n";
for (EntityBase *e: entities) { for (EntityBase *e: entities) {
buff += e->to_file(); buff += e->to_file();
} }

View File

@ -19,6 +19,7 @@ public:
// Pointer to the entity base is pushed outside // Pointer to the entity base is pushed outside
virtual void imgui() {} virtual void imgui() {}
virtual void draw_prev(Vec2 offset, bool selected) {}
virtual void draw(Vec2 offset, bool selected) {} virtual void draw(Vec2 offset, bool selected) {}
virtual EntityBase *setpos(Vec2 pos) { return this; } virtual EntityBase *setpos(Vec2 pos) { return this; }
}; };
@ -312,3 +313,40 @@ public:
Box2 box; Box2 box;
}; };
class FillBox: public EntityBase {
public:
std::string type() override { return "fill_box"; }
std::string to_file() override {
snprintf(buf, sizeof(buf), "FILL_BOX %.0f %.0f %.0f %.0lf %.0lf %.0lf %.0lf\n", color[0] * 255, color[1] * 255, color[2] * 255, box.lefttop.x, box.lefttop.y, box.size.x, box.size.y);
return buf;
}
void from_strtok() override {
color[0] = TOKEN_DOUBLE / 255.0;
color[1] = TOKEN_DOUBLE / 255.0;
color[2] = TOKEN_DOUBLE / 255.0;
double a, b, c, d;
a = TOKEN_DOUBLE;
b = TOKEN_DOUBLE;
c = TOKEN_DOUBLE;
d = TOKEN_DOUBLE;
box = Box2(a, b, c, d);
}
void imgui() override {
ig::ColorEdit3("Fill", color, ImGuiColorEditFlags_PickerHueWheel);
DragBox2("", &box);
}
void draw_prev(Vec2 offset, bool selected) override {
ImDrawList *d = ig::GetBackgroundDrawList();
Box2 world = box.offset(offset);
d->AddRectFilled(world.lefttop.im(), (world.lefttop + world.size).im(), ImColor(color[0], color[1], color[2]));
}
EntityBase *setpos(Vec2 pos) override {
box.lefttop = pos;
return this;
}
Box2 box;
float color[3];
};