diff --git a/src/EndLevelLayer.cpp b/src/EndLevelLayer.cpp index aeabb61..d787ca2 100644 --- a/src/EndLevelLayer.cpp +++ b/src/EndLevelLayer.cpp @@ -12,11 +12,6 @@ using namespace geode::prelude; class $modify(MyEndLevelLayer, EndLevelLayer) { - static const char* grabRandomQuote(std::vector vector = Manager::getSharedInstance()->quotes) { - std::mt19937 randomSeed(std::random_device{}()); - std::shuffle(vector.begin(), vector.end(), randomSeed); - return vector.front().c_str(); - } CCSprite* getHideButtonSprite() { return typeinfo_cast(getChildByIDRecursive("hide-button")->getChildren()->objectAtIndex(0)); } @@ -183,12 +178,39 @@ class $modify(MyEndLevelLayer, EndLevelLayer) { } } */ + void applyCustomLevelCompleteText(const std::string_view matchPlayLayer) { + if (!getModBool("customLevelCompleteText") || !getModBool("enabled")) return; + const auto lvlCompleteText = typeinfo_cast(getChildByIDRecursive("level-complete-text")); + if (!lvlCompleteText) return; + Manager* manager = managerMacro; + auto origOpacity = lvlCompleteText->getOpacity(); + lvlCompleteText->setOpacity(0); + if (getModString("alsoReplacePlayLayerLCT") == "Separate From EndLevelLayer") manager->generateNewSprites(getModString("customLCTMode")); + if (manager->chosenMode == "Images" && !manager->sharedReplacementSprite.empty()) { + CCSprite* newSprite = CCSprite::create(manager->sharedReplacementSprite.c_str()); + lvlCompleteText->addChild(newSprite); + lvlCompleteText->updateLayout(); + newSprite->setPosition(lvlCompleteText->getContentSize() / 2.f); + newSprite->setID("custom-level-complete-sprite-endlevellayer"_spr); + } else if (manager->chosenMode == "Oxygene One" && !manager->sharedReplacementLabel.empty()) { + CCLabelBMFont* newLabel = CCLabelBMFont::create(manager->sharedReplacementLabel.c_str(), "levelCompleteFont.fnt"_spr); + newLabel->setExtraKerning(5); + newLabel->limitLabelWidth(380.f, 1.0f, 0.25f); + lvlCompleteText->addChild(newLabel); + lvlCompleteText->updateLayout(); + newLabel->setPosition(lvlCompleteText->getContentSize() / 2.f); + newLabel->setID("custom-level-complete-label-endlevellayer"_spr); + } else { + lvlCompleteText->setOpacity(origOpacity); + return log::info("failed. manager->chosenMode: {}", manager->chosenMode); + } + } void applyRandomQuoteAndFont(PlayLayer* playLayer, GJGameLevel* theLevel) { auto endTextLabel = typeinfo_cast(getChildByIDRecursive("end-text")); if (!endTextLabel) return; Manager* manager = managerMacro; - std::string randomString = MyEndLevelLayer::grabRandomQuote(); - if (!manager->customQuotes.empty() && getModBool("customTextsOnly")) randomString = MyEndLevelLayer::grabRandomQuote(manager->customQuotes); + std::string randomString = manager->grabRandomString(); + if (!manager->customQuotes.empty() && getModBool("customTextsOnly")) randomString = manager->grabRandomString(manager->customQuotes); if ("Make sure to screenshot this win!" == randomString) { #ifdef GEODE_IS_MACOS randomString = "Press Command + Shift + 3 to screenshot this win!"; @@ -255,5 +277,6 @@ class $modify(MyEndLevelLayer, EndLevelLayer) { auto playLayer = PlayLayer::get(); if (!playLayer) return; if (getModBool("endTexts")) MyEndLevelLayer::applyRandomQuoteAndFont(playLayer, playLayer->m_level); + if (getModBool("customLevelCompleteText")) MyEndLevelLayer::applyCustomLevelCompleteText(getModString("alsoReplacePlayLayerLCT")); } }; \ No newline at end of file diff --git a/src/Manager.hpp b/src/Manager.hpp index a8b1fb9..eea05a4 100644 --- a/src/Manager.hpp +++ b/src/Manager.hpp @@ -1,4 +1,5 @@ #pragma once +#include // Manager.hpp structure by acaruso // reused with explicit permission and strong encouragement @@ -34,8 +35,9 @@ class Manager { float compactEndscreenFallbackPosition = CCDirector::get()->getWinSize().width * 0.6f; - CCLabelBMFont* sharedReplacementLabel = nullptr; - CCSprite* sharedReplacementSprite = nullptr; + std::string sharedReplacementLabel = ""; + std::string sharedReplacementSprite = ""; + std::string chosenMode = ""; static Manager* getSharedInstance() { if (!instance) { @@ -43,5 +45,37 @@ class Manager { } return instance; } - + static bool isSupportedExtension(const std::string& fileName) { + return !fileName.empty() && (geode::utils::string::endsWith(fileName, ".png") || geode::utils::string::endsWith(fileName, ".jpg")); + } + static std::string grabRandomString(std::vector vector = getSharedInstance()->quotes) { + std::mt19937 randomSeed(std::random_device{}()); + std::shuffle(vector.begin(), vector.end(), randomSeed); + return vector.front(); + } + static void generateNewSprites(std::string customLCTMode) { + Manager* manager = getSharedInstance(); + log::info("customLCTMode: {}", customLCTMode); + if (customLCTMode == "Combined" || customLCTMode == "Images") { + if (!manager->customLevelCompleteSprites.empty()) manager->customLevelCompleteSprites.clear(); + for (const auto& file : std::filesystem::directory_iterator((Mod::get()->getConfigDir() / R"(levelCompleteImages)").string())) { + if (isSupportedExtension(file.path().extension().string())) { + manager->customLevelCompleteSprites.push_back(file.path().string()); + } + } + std::string chosenSprite; // init sprite + if (!manager->customLevelCompleteSprites.empty()) chosenSprite = grabRandomString(manager->customLevelCompleteSprites); + else if (customLCTMode == "Images" && chosenSprite.empty()) return; // return early if no file name was selected even though user selected "Images" mode + manager->sharedReplacementSprite = chosenSprite; + } + if (customLCTMode == "Combined" || customLCTMode == "Oxygene One") { + manager->sharedReplacementLabel = Mod::get()->getSettingValue("customLevelCompleteTextsOnly") ? grabRandomString(manager->customLevelCompleteQuotes) : grabRandomString(manager->levelCompleteQuotes); + } + if (customLCTMode == "Combined") customLCTMode = manager->grabRandomString(manager->knownCLCTModesBesidesCombined); + log::info("customLCTMode: {} (should not be Combined)", customLCTMode); + if (customLCTMode == "Combined") { + return log::info("something went terribly wrong --- check contents of the `manager->knownCLCTModesBesidesCombined` vector"); + } + manager->chosenMode = customLCTMode; + } }; \ No newline at end of file diff --git a/src/PlayLayer.cpp b/src/PlayLayer.cpp index 9dc118e..67f07f1 100644 --- a/src/PlayLayer.cpp +++ b/src/PlayLayer.cpp @@ -1,5 +1,4 @@ #include -#include #include "Manager.hpp" #define getModBool Mod::get()->getSettingValue @@ -9,14 +8,6 @@ using namespace geode::prelude; class $modify(MyPlayLayer, PlayLayer) { - static std::string grabRandomString(std::vector vector = managerMacro->quotes) { - std::mt19937 randomSeed(std::random_device{}()); - std::shuffle(vector.begin(), vector.end(), randomSeed); - return vector.front(); - } - static bool isSupportedExtension(const std::string& fileName) { - return !fileName.empty() && (geode::utils::string::endsWith(fileName, ".png") || geode::utils::string::endsWith(fileName, ".jpg")); - } void levelComplete() { if (!m_level) return PlayLayer::levelComplete(); Manager* manager = managerMacro; @@ -26,12 +17,16 @@ class $modify(MyPlayLayer, PlayLayer) { PlayLayer::levelComplete(); } void onQuit() { - Manager::getSharedInstance()->lastFlukedPercent = 0; + Manager* manager = managerMacro; + manager->lastFlukedPercent = 0; + manager->sharedReplacementSprite = ""; + manager->sharedReplacementLabel = ""; + manager->chosenMode = ""; PlayLayer::onQuit(); } void showCompleteText() { PlayLayer::showCompleteText(); - if (!m_level || !getModBool("enabled") || !getModBool("customLevelCompleteText") || getModString("alsoReplacePlayLayerLCT") == "Disabled") return; + if (!m_level || !getModBool("enabled") || !getModBool("customLevelCompleteText")) return; Manager* manager = managerMacro; CCSprite* toModify = nullptr; for (const auto node : CCArrayExt(this->getChildren())) { @@ -41,41 +36,35 @@ class $modify(MyPlayLayer, PlayLayer) { } } if (!toModify) return; - std::string customLCTMode = getModString("customLCTMode"); - auto originalOpacity = toModify->getOpacity(); + std::string customLCTModePlayLayer = getModString("customLCTMode"); bool lctReplaced = false; + manager->generateNewSprites(customLCTModePlayLayer); + // "omg ery why not just choose a mode first???" + // assigning it to manager = easier "shared replacement" with EndLevelLayer later + if (getModString("alsoReplacePlayLayerLCT") == "Disabled") return; + auto origOpacity = toModify->getOpacity(); toModify->setOpacity(0); - if (customLCTMode == "Combined" || customLCTMode == "Images") { - if (!manager->customLevelCompleteSprites.empty()) manager->customLevelCompleteSprites.clear(); - for (const auto& file : std::filesystem::directory_iterator((Mod::get()->getConfigDir() / R"(levelCompleteImages)").string())) { - if (isSupportedExtension(file.path().extension().string())) { - manager->customLevelCompleteSprites.push_back(file.path().string()); - } - } - std::string chosenSprite; // init sprite - if (!manager->customLevelCompleteSprites.empty()) chosenSprite = grabRandomString(manager->customLevelCompleteSprites); - else if (customLCTMode == "Images" && chosenSprite.empty()) return toModify->setOpacity(originalOpacity); // return early if no file name was selected even though user selected "Images" mode - manager->sharedReplacementSprite = CCSprite::create(chosenSprite.c_str()); - } - if (customLCTMode == "Combined" || customLCTMode == "Oxygene One") { - std::string customQuote = getModBool("customLevelCompleteTextsOnly") ? grabRandomString(manager->customLevelCompleteQuotes) : grabRandomString(manager->levelCompleteQuotes); - CCLabelBMFont* replacementLabel = CCLabelBMFont::create(customQuote.c_str(), "levelCompleteFont.fnt"_spr); - replacementLabel->setExtraKerning(5); - manager->sharedReplacementLabel = replacementLabel; - } - if (customLCTMode == "Combined") customLCTMode = grabRandomString(manager->knownCLCTModesBesidesCombined); - if (customLCTMode == "Oxygene One" && manager->sharedReplacementLabel && !lctReplaced) { - toModify->addChild(manager->sharedReplacementLabel); - manager->sharedReplacementLabel->setPosition(toModify->getContentSize() / 2.f); - manager->sharedReplacementLabel->limitLabelWidth(370.f, 1.0f, 0.25f); - manager->sharedReplacementLabel->setID("custom-level-complete-label-playlayer"_spr); + if (manager->chosenMode == "Images" && !manager->sharedReplacementSprite.empty() && !lctReplaced) { + CCSprite* newSprite = CCSprite::create(manager->sharedReplacementSprite.c_str()); + toModify->addChild(newSprite); + toModify->updateLayout(); + newSprite->setPosition(toModify->getContentSize() / 2.f); + newSprite->setID("custom-level-complete-sprite-playlayer"_spr); lctReplaced = true; - } else if (customLCTMode == "Images" && manager->sharedReplacementSprite && !lctReplaced) { - toModify->addChild(manager->sharedReplacementSprite); - manager->sharedReplacementSprite->setPosition(toModify->getContentSize() / 2.f); - manager->sharedReplacementSprite->setID("custom-level-complete-sprite-playlayer"_spr); + } + if (manager->chosenMode == "Oxygene One" && !manager->sharedReplacementLabel.empty() && !lctReplaced) { + CCLabelBMFont* newLabel = CCLabelBMFont::create(manager->sharedReplacementLabel.c_str(), "levelCompleteFont.fnt"_spr); + newLabel->setExtraKerning(5); + newLabel->limitLabelWidth(380.f, 1.0f, 0.25f); + toModify->addChild(newLabel); + toModify->updateLayout(); + newLabel->setPosition(toModify->getContentSize() / 2.f); + newLabel->setID("custom-level-complete-label-playlayer"_spr); lctReplaced = true; } - if (!lctReplaced) return toModify->setOpacity(originalOpacity); + if (!lctReplaced) { + toModify->setOpacity(origOpacity); + return log::info("failed replacing sprite. customLCTMode: {} | manager->chosenMode: {}", customLCTModePlayLayer, manager->chosenMode); + } } }; \ No newline at end of file