diff --git a/browedit/HotkeyActions.cpp b/browedit/HotkeyActions.cpp index 8e872888..42d465d5 100644 --- a/browedit/HotkeyActions.cpp +++ b/browedit/HotkeyActions.cpp @@ -11,7 +11,6 @@ #include #include -extern bool previewWall; //move this extern bool dropperEnabled; //move this @@ -144,7 +143,7 @@ void BrowEdit::registerActions() HotkeyRegistry::registerAction(HotkeyAction::WallEdit_AddWall, [this]() { activeMapView->map->wallAddSelected(this); }, hasActiveMapViewWallMode); HotkeyRegistry::registerAction(HotkeyAction::WallEdit_RemoveWall, [this]() { activeMapView->map->wallRemoveSelected(this); }, hasActiveMapViewWallMode); HotkeyRegistry::registerAction(HotkeyAction::WallEdit_ReApply, [this]() { activeMapView->map->wallReApplySelected(this); }, hasActiveMapViewWallMode); - HotkeyRegistry::registerAction(HotkeyAction::WallEdit_Preview, [this]() { previewWall = !previewWall; }, hasActiveMapViewWallMode); + HotkeyRegistry::registerAction(HotkeyAction::WallEdit_Preview, [this]() { activeMapView->wallPreview = !activeMapView->wallPreview; }, hasActiveMapViewWallMode); HotkeyRegistry::registerAction(HotkeyAction::WallEdit_OffsetLower, [this]() { activeMapView->wallOffset = glm::max(1, activeMapView->wallOffset - 1); }, hasActiveMapViewWallMode); HotkeyRegistry::registerAction(HotkeyAction::WallEdit_OffsetRaise, [this]() { activeMapView->wallOffset++; }, hasActiveMapViewWallMode); HotkeyRegistry::registerAction(HotkeyAction::WallEdit_SizeLower, [this]() { activeMapView->wallWidth = glm::max(1, activeMapView->wallWidth - 1); }, hasActiveMapViewWallMode); diff --git a/browedit/Map.cpp b/browedit/Map.cpp index 9accebf0..0cddf81e 100644 --- a/browedit/Map.cpp +++ b/browedit/Map.cpp @@ -972,7 +972,6 @@ void WallCalculation::prepare(BrowEdit* browEdit) uvStart = uv1 + browEdit->activeMapView->textureEditUv1.x * (uv2 - uv1) + browEdit->activeMapView->textureEditUv1.y * (uv3 - uv1); } -extern std::vector selectedWalls; //ewwww void Map::wallAddSelected(BrowEdit* browEdit) { auto gnd = rootNode->getComponent(); @@ -984,7 +983,7 @@ void Map::wallAddSelected(BrowEdit* browEdit) auto ga = new GroupAction(); int id = (int)gnd->tiles.size(); - for (const auto& w : selectedWalls) + for (const auto& w : wallSelection) { auto cube = gnd->cubes[w.x][w.y]; @@ -1023,7 +1022,7 @@ void Map::wallReApplySelected(BrowEdit* browEdit) auto ga = new GroupAction(); int id = (int)gnd->tiles.size(); - for (const auto& w : selectedWalls) + for (const auto& w : wallSelection) { auto cube = gnd->cubes[w.x][w.y]; @@ -1057,7 +1056,7 @@ void Map::wallRemoveSelected(BrowEdit* browEdit) auto gnd = rootNode->getComponent(); auto gndRenderer = rootNode->getComponent(); auto ga = new GroupAction(); - for (const auto& w : selectedWalls) + for (const auto& w : wallSelection) { auto cube = gnd->cubes[w.x][w.y]; if (w.z == 1 && cube->tileSide != -1) @@ -1076,7 +1075,7 @@ void Map::wallFlipSelectedTextureHorizontal(BrowEdit* browEdit) auto gndRenderer = rootNode->getComponent(); auto ga = new GroupAction(); - for (const auto& w : selectedWalls) + for (const auto& w : wallSelection) { auto cube = gnd->cubes[w.x][w.y]; Gnd::Tile* tile = nullptr; @@ -1101,7 +1100,7 @@ void Map::wallFlipSelectedTextureVertical(BrowEdit* browEdit) auto gndRenderer = rootNode->getComponent(); auto ga = new GroupAction(); - for (const auto& w : selectedWalls) + for (const auto& w : wallSelection) { auto cube = gnd->cubes[w.x][w.y]; Gnd::Tile* tile = nullptr; diff --git a/browedit/Map.h b/browedit/Map.h index ac3dc01b..c87caf70 100644 --- a/browedit/Map.h +++ b/browedit/Map.h @@ -22,13 +22,20 @@ class Map std::vector tileSelection; std::vector gatSelection; std::vector waterSelection; + std::vector wallSelection; + + struct { + glm::ivec2 selectionStart; + int selectionSide; + bool selecting = false; + bool panning = false; + } wallSelect; bool changed = false; bool mapHasNoGnd = false; // This variable is meant to prevent the console from being filled with the "map has no gnd" error std::vector getSelectionAroundTiles(); - Node* findAndBuildNode(const std::string &path, Node* root = nullptr); Map(const std::string& name, BrowEdit* browEdit); diff --git a/browedit/MapView.Heightmode.cpp b/browedit/MapView.Heightmode.cpp index 63888530..a78da68a 100644 --- a/browedit/MapView.Heightmode.cpp +++ b/browedit/MapView.Heightmode.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -712,6 +713,39 @@ void MapView::postRenderHeightMode(BrowEdit* browEdit) if (browEdit->windowData.heightEdit.edgeMode == 3 && gnd->tiles.size() > 0) // add walls { + auto ga = new GroupAction(); + int id = (int)gnd->tiles.size(); + + auto addTile = [&](const glm::ivec2& w, int index) + { + auto cube = gnd->cubes[w.x][w.y]; + auto t = new Gnd::Tile(); + + t->color = glm::ivec4(255, 255, 255, 255); + t->textureIndex = 0; + t->lightmapIndex = 0; + + t->v1 = glm::vec2(0.00, 1); + t->v2 = glm::vec2(0.25, 1); + t->v3 = glm::vec2(0.00, 0); + t->v4 = glm::vec2(0.25, 0); + + switch (index) { + case 0: + ga->addAction(new CubeTileChangeAction(w, cube, id, cube->tileFront, cube->tileSide)); + break; + case 1: + ga->addAction(new CubeTileChangeAction(w, cube, cube->tileUp, id, cube->tileSide)); + break; + case 2: + ga->addAction(new CubeTileChangeAction(w, cube, cube->tileUp, cube->tileFront, id)); + break; + } + + cube->tileIds[index] = id++; + ga->addAction(new TileNewAction(t)); + }; + for (auto t : map->tileSelection) { //h1 bottomleft @@ -719,29 +753,32 @@ void MapView::postRenderHeightMode(BrowEdit* browEdit) //h3 topleft //h4 topright if (t.x > 0 && !isTileSelected(t.x - 1, t.y) && ( - gnd->cubes[t.x][t.y]->h1 != gnd->cubes[t.x - 1][t.y]->h2 || + gnd->cubes[t.x][t.y]->h1 != gnd->cubes[t.x - 1][t.y]->h2 || gnd->cubes[t.x][t.y]->h3 != gnd->cubes[t.x - 1][t.y]->h4) && gnd->cubes[t.x - 1][t.y]->tileSide == -1) - gnd->cubes[t.x - 1][t.y]->tileSide = 0; - + addTile(glm::ivec2(t.x - 1, t.y), 2); + if (t.x < gnd->width-1 && !isTileSelected(t.x + 1, t.y) && ( gnd->cubes[t.x][t.y]->h2 != gnd->cubes[t.x + 1][t.y]->h1 || gnd->cubes[t.x][t.y]->h4 != gnd->cubes[t.x + 1][t.y]->h3) && gnd->cubes[t.x][t.y]->tileSide == -1) - gnd->cubes[t.x][t.y]->tileSide = 0; + addTile(t, 2); if (t.y > 0 && !isTileSelected(t.x, t.y - 1) && ( gnd->cubes[t.x][t.y]->h1 != gnd->cubes[t.x][t.y - 1]->h3 || gnd->cubes[t.x][t.y]->h2 != gnd->cubes[t.x][t.y - 1]->h4) && gnd->cubes[t.x][t.y - 1]->tileFront == -1) - gnd->cubes[t.x][t.y - 1]->tileFront = 0; + addTile(glm::ivec2(t.x, t.y - 1), 1); if (t.y < gnd->height-1 && !isTileSelected(t.x, t.y + 1) && ( gnd->cubes[t.x][t.y]->h3 != gnd->cubes[t.x][t.y + 1]->h1 || gnd->cubes[t.x][t.y]->h4 != gnd->cubes[t.x][t.y + 1]->h2) && gnd->cubes[t.x][t.y]->tileFront == -1) - gnd->cubes[t.x][t.y]->tileFront = 0; + addTile(t, 1); + } + if (!ga->isEmpty()) { + map->doAction(ga, browEdit); } } diff --git a/browedit/MapView.Texturemode.cpp b/browedit/MapView.Texturemode.cpp index a8dd8178..6ee9c5f9 100644 --- a/browedit/MapView.Texturemode.cpp +++ b/browedit/MapView.Texturemode.cpp @@ -466,6 +466,11 @@ void MapView::postRenderTextureMode(BrowEdit* browEdit) { auto cube = gnd->cubes[tile.x][tile.y]; + if (textureBrushWidth == 0) + textureBrushWidth = 1; + if (textureBrushHeight == 0) + textureBrushHeight = 1; + glm::vec2 v1 = uvStart + xInc * (float)((tile.x + browEdit->textureFillOffset.x)%textureBrushWidth) + yInc * (float)((tile.y + browEdit->textureFillOffset.y)%textureBrushHeight); glm::vec2 v2 = v1 + xInc; glm::vec2 v3 = v1 + yInc; diff --git a/browedit/MapView.Wallmode.cpp b/browedit/MapView.Wallmode.cpp index 05708fe9..b7677b34 100644 --- a/browedit/MapView.Wallmode.cpp +++ b/browedit/MapView.Wallmode.cpp @@ -14,14 +14,6 @@ #include #include - -glm::ivec2 selectionStart; -int selectionSide; -std::vector selectedWalls; -bool previewWall = false; //move this -bool selecting = false; -bool panning = false; - void MapView::postRenderWallMode(BrowEdit* browEdit) { bool dropper = wallBottomDropper || wallTopDropper; @@ -52,7 +44,7 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) fbo->bind(); if (!ImGui::IsMouseClicked(ImGuiMouseButton_Left) && ImGui::IsMouseDown(ImGuiMouseButton_Left)) - side = selectionSide; + side = map->wallSelect.selectionSide; if (side == 0) { @@ -146,12 +138,12 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) } else { - if (!selecting && !panning) + if (!map->wallSelect.selecting && !map->wallSelect.panning) { if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { bool collision = false; - for (const auto& wall : selectedWalls) + for (const auto& wall : map->wallSelection) { glm::ivec2 tile = glm::ivec2(wall); if (gnd->inMap(tile) && ( @@ -193,14 +185,14 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) if (!collision) { - selectionStart = tileHovered; - selectionSide = side; - selecting = true; + map->wallSelect.selectionStart = tileHovered; + map->wallSelect.selectionSide = side; + map->wallSelect.selecting = true; } else { originalTiles.clear(); - for (const auto& w : selectedWalls) + for (const auto& w : map->wallSelection) { auto cube = gnd->cubes[w.x][w.y]; Gnd::Tile* tile = nullptr; @@ -212,16 +204,16 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) continue; originalTiles.push_back(Gnd::Tile(*tile)); } - panning = true; + map->wallSelect.panning = true; std::cout << "Panning!" << std::endl; } } } - if (selecting) + if (map->wallSelect.selecting) { if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) { - glm::ivec2 tile = selectionStart; + glm::ivec2 tile = map->wallSelect.selectionStart; for (int i = 0; i < 100; i++) { if (gnd->inMap(tile) && ( @@ -230,14 +222,14 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) { auto cube = gnd->cubes[tile.x][tile.y]; glm::vec3 v1, v2, v3, v4; - if (selectionSide == 1) + if (map->wallSelect.selectionSide == 1) { v1 = glm::vec3(10 * tile.x + 10, -cube->h2, 10 * gnd->height - 10 * tile.y + 10); v2 = glm::vec3(10 * tile.x + 10, -cube->h4, 10 * gnd->height - 10 * tile.y); v3 = glm::vec3(10 * tile.x + 10, -gnd->cubes[tile.x + 1][tile.y]->h1, 10 * gnd->height - 10 * tile.y + 10); v4 = glm::vec3(10 * tile.x + 10, -gnd->cubes[tile.x + 1][tile.y]->h3, 10 * gnd->height - 10 * tile.y); } - if (selectionSide == 2) + if (map->wallSelect.selectionSide == 2) { v1 = glm::vec3(10 * tile.x, -cube->h3, 10 * gnd->height - 10 * tile.y); v2 = glm::vec3(10 * tile.x + 10, -cube->h4, 10 * gnd->height - 10 * tile.y); @@ -265,47 +257,47 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) glEnable(GL_DEPTH_TEST); } - if (selectionSide == 1 && tile.y == tileHovered.y) + if (map->wallSelect.selectionSide == 1 && tile.y == tileHovered.y) break; - if (selectionSide == 2 && tile.x == tileHovered.x) + if (map->wallSelect.selectionSide == 2 && tile.x == tileHovered.x) break; - if (selectionSide == 1) - tile += glm::ivec2(0, glm::sign(tileHovered.y - selectionStart.y)); + if (map->wallSelect.selectionSide == 1) + tile += glm::ivec2(0, glm::sign(tileHovered.y - map->wallSelect.selectionStart.y)); else - tile += glm::ivec2(glm::sign(tileHovered.x - selectionStart.x), 0); - if (tileHovered == selectionStart) + tile += glm::ivec2(glm::sign(tileHovered.x - map->wallSelect.selectionStart.x), 0); + if (tileHovered == map->wallSelect.selectionStart) break; } } if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) { auto ga = new GroupAction(); - glm::ivec2 tile = selectionStart; + glm::ivec2 tile = map->wallSelect.selectionStart; for (int i = 0; i < 100; i++) { - if (gnd->inMap(tile) && std::find(selectedWalls.begin(), selectedWalls.end(), glm::ivec3(tile, selectionSide)) == selectedWalls.end()) - ga->addAction(new SelectWallAction(map, glm::ivec3(tile, selectionSide), ga->isEmpty() ? ImGui::GetIO().KeyShift : true, false)); - if (selectionSide == 1 && tile.y == tileHovered.y) + if (gnd->inMap(tile) && std::find(map->wallSelection.begin(), map->wallSelection.end(), glm::ivec3(tile, map->wallSelect.selectionSide)) == map->wallSelection.end()) + ga->addAction(new SelectWallAction(map, glm::ivec3(tile, map->wallSelect.selectionSide), ga->isEmpty() ? ImGui::GetIO().KeyShift : true, false)); + if (map->wallSelect.selectionSide == 1 && tile.y == tileHovered.y) break; - if (selectionSide == 2 && tile.x == tileHovered.x) + if (map->wallSelect.selectionSide == 2 && tile.x == tileHovered.x) break; - if (selectionSide == 1) - tile += glm::ivec2(0, glm::sign(tileHovered.y - selectionStart.y)); + if (map->wallSelect.selectionSide == 1) + tile += glm::ivec2(0, glm::sign(tileHovered.y - map->wallSelect.selectionStart.y)); else - tile += glm::ivec2(glm::sign(tileHovered.x - selectionStart.x), 0); - if (tileHovered == selectionStart) + tile += glm::ivec2(glm::sign(tileHovered.x - map->wallSelect.selectionStart.x), 0); + if (tileHovered == map->wallSelect.selectionStart) break; } if (!ga->isEmpty()) map->doAction(ga, browEdit); else delete ga; - selecting = false; + map->wallSelect.selecting = false; } } - if (panning) + if (map->wallSelect.panning) { - for (const auto& w : selectedWalls) + for (const auto& w : map->wallSelection) { auto cube = gnd->cubes[w.x][w.y]; Gnd::Tile* tile = nullptr; @@ -350,10 +342,10 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) } if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) { - panning = false; + map->wallSelect.panning = false; auto ga = new GroupAction(); int i = 0; - for (const auto& w : selectedWalls) + for (const auto& w : map->wallSelection) { auto cube = gnd->cubes[w.x][w.y]; Gnd::Tile* tile = nullptr; @@ -372,7 +364,7 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) } } } - if (selectedWalls.size() > 0 && browEdit->activeMapView) + if (map->wallSelection.size() > 0 && browEdit->activeMapView) { std::vector verts; std::vector topverts; @@ -381,7 +373,7 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) WallCalculation calculation; calculation.prepare(browEdit); - for (const auto& wall : selectedWalls) + for (const auto& wall : map->wallSelection) { glm::ivec2 tile = glm::ivec2(wall); @@ -407,7 +399,7 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) v3 = glm::vec3(10 * tile.x, -gnd->cubes[tile.x][tile.y + 1]->h1, 10 * gnd->height - 10 * tile.y); normal = glm::vec3(0, 0, 1); } - if (previewWall) + if (wallPreview) { calculation.calcUV(wall, gnd); verts.push_back(VertexP3T2N3(v1, calculation.g_uv1, normal)); @@ -475,7 +467,7 @@ void MapView::postRenderWallMode(BrowEdit* browEdit) glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VertexP3T2N3), verts[0].data + 0); glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(VertexP3T2N3), verts[0].data + 3); glVertexAttribPointer(2, 3, GL_FLOAT, false, sizeof(VertexP3T2N3), verts[0].data + 5); - if (previewWall && browEdit->activeMapView->textureSelected >= 0 && browEdit->activeMapView->textureSelected < gndRenderer->textures.size()) + if (wallPreview && browEdit->activeMapView->textureSelected >= 0 && browEdit->activeMapView->textureSelected < gndRenderer->textures.size()) { simpleShader->setUniform(SimpleShader::Uniforms::textureFac, 1.0f); simpleShader->setUniform(SimpleShader::Uniforms::color, glm::vec4(browEdit->config.wallEditHighlightColor, 1)); diff --git a/browedit/MapView.h b/browedit/MapView.h index 5575c128..279ced66 100644 --- a/browedit/MapView.h +++ b/browedit/MapView.h @@ -99,7 +99,6 @@ class MapView glm::vec2 cameraTargetRot; glm::vec3 cameraTargetPos; - bool snapToGrid = false; bool gridLocal = true; float gridSizeTranslate = 5; @@ -161,7 +160,7 @@ class MapView bool wallAutoStraight = true; bool wallTopDropper = false; bool wallBottomDropper = false; - + bool wallPreview = false; //height edit mode bool mouseDown = false; @@ -178,7 +177,6 @@ class MapView std::vector selectLasso; std::vector objectSelectLasso; - MouseState mouseState; MouseState prevMouseState; diff --git a/browedit/actions/GndTextureActions.cpp b/browedit/actions/GndTextureActions.cpp index 32b34e8d..06ae2fb9 100644 --- a/browedit/actions/GndTextureActions.cpp +++ b/browedit/actions/GndTextureActions.cpp @@ -48,7 +48,78 @@ void GndTextureAddAction::undo(Map* map, BrowEdit* browEdit) std::string GndTextureAddAction::str() { - return "Added Texture"; + return "Added texture " + util::iso_8859_1_to_utf8(fileName); +} + +GndTextureDelAction::GndTextureDelAction(int index) : index(index) +{ +} + +void GndTextureDelAction::perform(Map* map, BrowEdit* browEdit) +{ + auto gnd = map->rootNode->getComponent(); + auto gndRenderer = map->rootNode->getComponent(); + + if (index < 0 || index >= gnd->textures.size()) + return; + + fileName = gnd->textures[index]->file; + + util::ResourceManager::unload(gndRenderer->textures[index]); + gndRenderer->textures.erase(gndRenderer->textures.begin() + index); + + delete gnd->textures[index]; + gnd->textures.erase(gnd->textures.begin() + index); + tiles.clear(); + + for (int i = 0; i < gnd->tiles.size(); i++) { + auto& tile = gnd->tiles[i]; + + if (tile->textureIndex == index) { + tiles.push_back(i); + tile->textureIndex = -1; + } + else if (tile->textureIndex > index) + tile->textureIndex--; + } + + for (auto& mv : browEdit->mapViews) + { + if (mv.map == map && mv.textureSelected >= gnd->textures.size()) + mv.textureSelected = (int)gnd->textures.size() - 1; + } + + gndRenderer->setChunksDirty(); +} + +void GndTextureDelAction::undo(Map* map, BrowEdit* browEdit) +{ + auto gnd = map->rootNode->getComponent(); + auto gndRenderer = map->rootNode->getComponent(); + + if (index < 0 || index > gnd->textures.size()) + return; + + gnd->textures.insert(gnd->textures.begin() + index, new Gnd::Texture(fileName, fileName)); + gndRenderer->textures.insert(gndRenderer->textures.begin() + index, util::ResourceManager::load("data\\texture\\" + fileName)); + + for (int i = 0; i < gnd->tiles.size(); i++) { + auto& tile = gnd->tiles[i]; + + if (tile->textureIndex >= index) + tile->textureIndex++; + } + + for (auto idx : tiles) { + gnd->tiles[idx]->textureIndex = index; + } + + gndRenderer->setChunksDirty(); +} + +std::string GndTextureDelAction::str() +{ + return "Deleted texture " + util::iso_8859_1_to_utf8(fileName); } GndTextureChangeAction::GndTextureChangeAction(int index, const std::string& fileName) : index(index), newTexture(fileName) diff --git a/browedit/actions/GndTextureActions.h b/browedit/actions/GndTextureActions.h index a4707120..c0e8e961 100644 --- a/browedit/actions/GndTextureActions.h +++ b/browedit/actions/GndTextureActions.h @@ -2,6 +2,7 @@ #include "Action.h" #include +#include class GndTextureAddAction : public Action { @@ -15,7 +16,14 @@ class GndTextureAddAction : public Action class GndTextureDelAction : public Action { + int index; + std::string fileName; + std::vector tiles; public: + GndTextureDelAction(int index); + virtual void perform(Map* map, BrowEdit* browEdit) override; + virtual void undo(Map* map, BrowEdit* browEdit) override; + virtual std::string str() override; }; diff --git a/browedit/actions/SelectWallAction.cpp b/browedit/actions/SelectWallAction.cpp index d9131f78..f8601db8 100644 --- a/browedit/actions/SelectWallAction.cpp +++ b/browedit/actions/SelectWallAction.cpp @@ -4,33 +4,33 @@ #include #include -extern std::vector selectedWalls; //eww - - SelectWallAction::SelectWallAction(Map* map, const glm::ivec3 &selection, bool keepSelection, bool deSelect) { + if (map == nullptr) + return; + name = "Select walls"; - oldSelection = selectedWalls; + oldSelection = map->wallSelection; if (keepSelection) newSelection = oldSelection; else - selectedWalls.clear(); + map->wallSelection.clear(); if (!deSelect) newSelection.push_back(selection); else newSelection.erase(std::remove_if(newSelection.begin(), newSelection.end(), [selection](const glm::ivec3 n) { return n == selection; })); - selectedWalls.push_back(selection); + map->wallSelection.push_back(selection); } void SelectWallAction::perform(Map* map, BrowEdit* browEdit) { - selectedWalls = newSelection; + map->wallSelection = newSelection; } void SelectWallAction::undo(Map* map, BrowEdit* browEdit) { - selectedWalls = oldSelection; + map->wallSelection = oldSelection; } std::string SelectWallAction::str() diff --git a/browedit/components/Gnd.cpp b/browedit/components/Gnd.cpp index 0b677ed9..7b2ce879 100644 --- a/browedit/components/Gnd.cpp +++ b/browedit/components/Gnd.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1221,6 +1222,51 @@ void Gnd::cleanTiles() std::cout<< "Tiles cleanup, ending with " << tiles.size() << " tiles" << std::endl; } +void Gnd::removeUnusedTextures(BrowEdit* browEdit) +{ + int oldSize = (int)textures.size(); + + std::set used; + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + auto cube = cubes[x][y]; + + for (int i = 0; i < 3; i++) { + if (cube->tileIds[i] == -1) + continue; + + auto tile = tiles[cube->tileIds[i]]; + + if (tile->textureIndex == -1) + continue; + + used.insert(tile->textureIndex); + } + } + } + + std::vector toDelete; + for (int i = (int)textures.size() - 1; i >= 0; i--) { + if (used.count(i)) continue; + + toDelete.push_back(i); + } + + if (toDelete.size() > 0) { + auto ga = new GroupAction(); + + for (auto index : toDelete) { + std::cout << "Removed " << textures[index]->file << " (id: " << index << ")" << std::endl; + ga->addAction(new GndTextureDelAction(index)); + } + + browEdit->activeMapView->map->doAction(ga, browEdit); + } + + std::cout << "Removed " << (oldSize - (int)textures.size()) << " texture(s)" << std::endl; +} + void Gnd::recalculateNormals() { for (int x = 0; x < width; x++) diff --git a/browedit/components/Gnd.h b/browedit/components/Gnd.h index 64c5293d..34f51711 100644 --- a/browedit/components/Gnd.h +++ b/browedit/components/Gnd.h @@ -35,6 +35,7 @@ class Gnd : public Component, public ImguiProps void makeLightmapsDiffRes(int rx, int ry); void removeZeroHeightWalls(); void cleanTiles(); + void removeUnusedTextures(BrowEdit* browEdit); void recalculateNormals(); void flattenTiles(Map* map, BrowEdit* browEdit, const std::vector& tiles); diff --git a/browedit/windows/MenuBar.cpp b/browedit/windows/MenuBar.cpp index 5508a0b7..9641aaeb 100644 --- a/browedit/windows/MenuBar.cpp +++ b/browedit/windows/MenuBar.cpp @@ -229,6 +229,8 @@ void BrowEdit::menuBar() if (ImGui::MenuItem("Make tiles unique")) activeMapView->map->rootNode->getComponent()->makeTilesUnique(); hotkeyMenuItem("Remove zero height walls", HotkeyAction::Global_ClearZeroHeightWalls); + if (ImGui::MenuItem("Remove unused ground textures")) + activeMapView->map->rootNode->getComponent()->removeUnusedTextures(this); if (ImGui::MenuItem("Clean Tiles")) activeMapView->map->rootNode->getComponent()->cleanTiles(); if (ImGui::MenuItem("Edit lighting/water/fog")) { diff --git a/browedit/windows/WallEditWindow.cpp b/browedit/windows/WallEditWindow.cpp index 8035fa88..98d9f586 100644 --- a/browedit/windows/WallEditWindow.cpp +++ b/browedit/windows/WallEditWindow.cpp @@ -13,9 +13,6 @@ #include #include -extern std::vector selectedWalls; //TODO: I want less extern -extern bool previewWall; - void BrowEdit::showWallWindow() { if (!activeMapView) @@ -429,13 +426,13 @@ void BrowEdit::showWallWindow() ImGui::Checkbox("Auto Straight", &activeMapView->wallAutoStraight); hotkeyButton("Apply Texture", HotkeyAction::WallEdit_ReApply); - previewWall = ImGui::IsItemHovered(); + activeMapView->wallPreview = ImGui::IsItemHovered(); - if (selectedWalls.size() > 0) + if (activeMapView->map->wallSelection.size() > 0) { if(ImGui::CollapsingHeader("Selection Texture Editing", ImGuiTreeNodeFlags_DefaultOpen)) - for (const auto& w: selectedWalls) + for (const auto& w: activeMapView->map->wallSelection) { if (gnd->cubes[w.x][w.y]->tileIds[w.z == 1 ? 2 : 1] > -1) {