From cb47676e93faa66d562f77cc92109093e4b9bc30 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:19:10 +0300 Subject: [PATCH 01/18] reorganize startpos switcher --- CMakeLists.txt | 1 + src/features/StartPosSwitcher.cpp | 412 ------------------ .../StartPosSwitcher/PlaytestHerePopup.cpp | 69 +++ .../StartPosSwitcher/PlaytestHerePopup.hpp | 57 +++ .../StartPosSwitcher/StartPosButtonBar.cpp | 85 ++++ .../StartPosSwitcher/StartPosButtonBar.hpp | 29 ++ .../StartPosSwitcher/StartPosKind.hpp | 16 + .../StartPosSwitcher/StartPosSwitcher.cpp | 211 +++++++++ 8 files changed, 468 insertions(+), 412 deletions(-) delete mode 100644 src/features/StartPosSwitcher.cpp create mode 100644 src/features/StartPosSwitcher/PlaytestHerePopup.cpp create mode 100644 src/features/StartPosSwitcher/PlaytestHerePopup.hpp create mode 100644 src/features/StartPosSwitcher/StartPosButtonBar.cpp create mode 100644 src/features/StartPosSwitcher/StartPosButtonBar.hpp create mode 100644 src/features/StartPosSwitcher/StartPosKind.hpp create mode 100644 src/features/StartPosSwitcher/StartPosSwitcher.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b11c80a..e2808f04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ file(GLOB SOURCES src/features/MoreTabs/*.cpp src/features/VisibilityToggle/*.cpp src/features/BetterScaling/*.cpp + src/features/StartPosSwitcher/*.cpp src/features/*.cpp src/other/*.cpp src/*.cpp diff --git a/src/features/StartPosSwitcher.cpp b/src/features/StartPosSwitcher.cpp deleted file mode 100644 index 0bc3cf7c..00000000 --- a/src/features/StartPosSwitcher.cpp +++ /dev/null @@ -1,412 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#undef min -#undef max - -using namespace geode::prelude; -using namespace editor_api; - -struct FromLevelStart {}; -struct DefaultBehaviour {}; -using FromPoint = CCPoint; -using FromObj = StartPosObject*; -using StartPosKind = std::variant< - FromLevelStart, - DefaultBehaviour, - FromPoint, - FromObj ->; - -class $modify(StartPosLevel, GJGameLevel) { - StartPosKind m_startPos; - - StartPosLevel() : m_startPos(DefaultBehaviour()) {} - - bool match(CCPoint const& pos) { - if (std::holds_alternative(m_fields->m_startPos)) { - return std::get(m_fields->m_startPos) == pos; - } - return false; - } -}; - -class PlaytestHerePopup : public CCNode { -protected: - Ref m_startPos; - LevelEditorLayer* m_editor; - static Ref s_popup; - - bool init(LevelEditorLayer* lel, StartPosObject* startPos) { - if (!CCNode::init()) - return false; - - m_editor = lel; - m_startPos = startPos; - - this->setZOrder(0xB00B5); - - auto bg = CCScale9Sprite::create("square02_001.png", { 0, 0, 80, 80 }); - bg->setScale(.5f); - bg->setOpacity(80); - bg->setContentSize({ 120, 60 }); - this->addChild(bg); - - auto menu = CCMenu::create(); - menu->setPosition(0, 0); - - auto playTestBtnSpr = CCSprite::createWithSpriteFrameName("GJ_playEditorBtn_001.png"); - playTestBtnSpr->setScale(.615f); - - auto playTestBtn = CCMenuItemSpriteExtra::create( - playTestBtnSpr, this, menu_selector(PlaytestHerePopup::onPlaytest) - ); - playTestBtn->setPosition(-15.f, .0f); - menu->addChild(playTestBtn); - - auto playInGameBtnSpr = CCSprite::createWithSpriteFrameName("GJ_playBtn2_001.png"); - playInGameBtnSpr->setScale(.3f); - - auto playInGameBtn = CCMenuItemSpriteExtra::create( - playInGameBtnSpr, this, menu_selector(PlaytestHerePopup::onPlayInGame) - ); - playInGameBtn->setPosition(15.f, .0f); - menu->addChild(playInGameBtn); - - this->addChild(menu); - - CCDirector::sharedDirector()->getTouchDispatcher()->incrementForcePrio(2); - menu->registerWithTouchDispatcher(); - - s_popup = this; - - return true; - } - - static PlaytestHerePopup* create( - LevelEditorLayer* lel, StartPosObject* startPos - ) { - auto ret = new PlaytestHerePopup; - if (ret && ret->init(lel, startPos)) { - ret->autorelease(); - return ret; - } - CC_SAFE_DELETE(ret); - return nullptr; - } - - // has to be defined outside because it used StartPosSwitchLayer - void onPlaytest(CCObject*); - void onPlayInGame(CCObject*) { - this->select(); - m_editor->m_editorUI->onPause(nullptr); - EditorPauseLayer::get()->setVisible(false); - EditorPauseLayer::get()->onSaveAndPlay(nullptr); - } - -public: - void select() { - static_cast( - m_editor->m_level - )->m_fields->m_startPos = m_startPos->getPosition(); - } - - static void move() { - if (s_popup) { - s_popup->setPosition( - s_popup->m_startPos->getPositionX(), - s_popup->m_startPos->getPositionY() + 35.f - ); - } - } - - static void show(LevelEditorLayer* lel, StartPosObject* startPos) { - PlaytestHerePopup::hide(); - PlaytestHerePopup::create(lel, startPos); - PlaytestHerePopup::move(); - lel->m_objectLayer->addChild(s_popup); - } - - static void hide() { - if (s_popup) { - s_popup->removeFromParent(); - s_popup = nullptr; - } - } -}; -Ref PlaytestHerePopup::s_popup = nullptr; - -class $modify(PlayLayer) { - void addObject(GameObject* obj) { - auto oldStartPos = m_startPos; - PlayLayer::addObject(obj); - if (oldStartPos) { - m_startPos = oldStartPos; - m_playerStartPosition = oldStartPos->getPosition(); - } - if (obj->m_objectID == 31) { - if (static_cast(m_level)->match(obj->getPosition())) { - m_startPos = static_cast(obj); - m_playerStartPosition = obj->getPosition(); - } - if (std::holds_alternative(static_cast( - m_level - )->m_fields->m_startPos)) { - m_startPos = nullptr; - m_playerStartPosition = CCPoint { 0, 105.f }; - } - } - } -}; - -class $modify(StartPosSwitchLayer, LevelEditorLayer) { - StartPosKind m_selectedStartPos; - - StartPosSwitchLayer() : m_selectedStartPos(DefaultBehaviour()) { - PlaytestHerePopup::hide(); - } - - void addSpecial(GameObject* obj) { - LevelEditorLayer::addSpecial(obj); - if (obj->m_objectID == 31) { - if (static_cast(m_level)->match(obj->getPosition())) { - m_fields->m_selectedStartPos = static_cast(obj); - } - } - } - - // void destructor() { - // PlaytestHerePopup::hide(); - // LevelEditorLayer::~LevelEditorLayer(); - // } - - void handleAction(bool idk, CCArray* idk2) { - LevelEditorLayer::handleAction(idk, idk2); - PlaytestHerePopup::move(); - } - - void setupLevelStart(LevelSettingsObject* obj) { - // selected start position - if (std::holds_alternative(m_fields->m_selectedStartPos)) { - auto obj = std::get(m_fields->m_selectedStartPos); - this->setStartPosObject(obj); - auto startPos = obj->getPosition(); - m_player1->setStartPos(startPos); - m_player1->resetObject(); - m_player2->setStartPos(startPos); - m_player2->resetObject(); - LevelEditorLayer::setupLevelStart(obj->m_levelSettings); - } - // from level start - else if (std::holds_alternative(m_fields->m_selectedStartPos)) { - this->setStartPosObject(nullptr); - m_player1->setStartPos({ 0.f, 105.f }); - m_player1->resetObject(); - m_player2->setStartPos({ 0.f, 105.f }); - m_player2->resetObject(); - LevelEditorLayer::setupLevelStart(m_levelSettings); - } - // default behaviour - else { - LevelEditorLayer::setupLevelStart(obj); - } - } -}; - -$onEditorExit { - PlaytestHerePopup::hide(); -} - -void PlaytestHerePopup::onPlaytest(CCObject*) { - this->select(); - static_cast( - m_editor - )->m_fields->m_selectedStartPos = m_startPos; - m_editor->setStartPosObject(m_startPos); - EditorUI::get()->onPlaytest(EditorUI::get()->m_playtestBtn); -} - -class $modify(EditorPauseLayer) { - bool init(LevelEditorLayer* lel) { - if (!EditorPauseLayer::init(lel)) - return false; - - PlaytestHerePopup::hide(); - - return true; - } -}; - -class StartPosButtonBar : public CCMenu { -protected: - LevelEditorLayer* m_editor; - - bool init(LevelEditorLayer* lel) { - if (!CCMenu::init()) - return false; - - m_editor = lel; - - auto winSize = CCDirector::get()->getWinSize(); - - this->ignoreAnchorPointForPosition(false); - this->setPosition(winSize.width / 2, 45.f); - this->setContentSize({ 200.f, 60.f }); - - auto goToStartSpr = ButtonSprite::create( - CCSprite::createWithSpriteFrameName("start-from-start.png"_spr), - 0x32, true, 0x32, "GJ_button_01.png", .7f - ); - auto goToStartBtn = CCMenuItemSpriteExtra::create( - goToStartSpr, this, menu_selector(StartPosButtonBar::onGoToStart) - ); - goToStartBtn->setPosition( - m_obContentSize.width / 2 - 25.f, - m_obContentSize.height / 2 - ); - this->addChild(goToStartBtn); - - auto goToLastSpr = ButtonSprite::create( - CCSprite::createWithSpriteFrameName("last-start-pos.png"_spr), - 0x32, true, 0x32, "GJ_button_01.png", .75f - ); - auto goToLastBtn = CCMenuItemSpriteExtra::create( - goToLastSpr, this, menu_selector(StartPosButtonBar::onGoToLast) - ); - goToLastBtn->setPosition( - m_obContentSize.width / 2 + 25.f, - m_obContentSize.height / 2 - ); - this->addChild(goToLastBtn); - - auto infoSpr = CCSprite::createWithSpriteFrameName("GJ_infoIcon_001.png"); - auto infoBtn = CCMenuItemSpriteExtra::create( - infoSpr, this, menu_selector(StartPosButtonBar::onInfo) - ); - infoBtn->setPosition(m_obContentSize / 2 + ccp(60.f, 0.f)); - this->addChild(infoBtn); - - return true; - } - - void onInfo(CCObject*) { - FLAlertLayer::create( - "Start Pos Info", - "From Level Start: Start playing from the beginning of the " - "level, ignoring all start positions\n" - "From Last Start Pos: Start playing from the last start " - "position in the level", - "OK" - )->show(); - } - - void onGoToStart(CCObject*) { - static_cast( - m_editor - )->m_fields->m_selectedStartPos = FromLevelStart(); - static_cast( - m_editor->m_level - )->m_fields->m_startPos = FromLevelStart(); - } - - void onGoToLast(CCObject*) { - StartPosObject* last = nullptr; - for(auto* obj : cocos::CCArrayExt(m_editor->m_objects)) { - if (obj->m_objectID == 31) { - if (!last || last->getPositionX() < obj->getPositionX()) { - last = obj; - } - } - } - if (last) { - static_cast( - m_editor - )->m_fields->m_selectedStartPos = last; - static_cast( - m_editor->m_level - )->m_fields->m_startPos = last->getPosition(); - } else { - static_cast( - m_editor - )->m_fields->m_selectedStartPos = DefaultBehaviour(); - static_cast( - m_editor->m_level - )->m_fields->m_startPos = DefaultBehaviour(); - } - } - -public: - static StartPosButtonBar* create(LevelEditorLayer* lel) { - auto ret = new StartPosButtonBar; - if (ret && ret->init(lel)) { - ret->autorelease(); - return ret; - } - CC_SAFE_DELETE(ret); - return nullptr; - } -}; - -class $modify(MyEditorUI, EditorUI) { - bool init(LevelEditorLayer* lel) { - if (!EditorUI::init(lel)) - return false; - - MoreTabs::get(this)->addEditTab( - "start-pos-border.png"_spr, - StartPosButtonBar::create(m_editorLayer) - ); - - return true; - } - - void deleteObject(GameObject* obj, bool filter) { - EditorUI::deleteObject(obj, filter); - PlaytestHerePopup::hide(); - } - - void selectObjects(CCArray* objects, bool ignoreFilters) { - EditorUI::selectObjects(objects, ignoreFilters); - PlaytestHerePopup::hide(); - } - - void selectObject(GameObject* obj, bool filter) { - EditorUI::selectObject(obj, filter); - MoreTabs::get(this)->switchEditTab(1); - } - - void moveObject(GameObject* obj, CCPoint pos) { - EditorUI::moveObject(obj, pos); - PlaytestHerePopup::move(); - } - - void moveObjectCall(EditCommand cmd) { - EditorUI::moveObjectCall(cmd); - PlaytestHerePopup::move(); - } - - void deselectAll() { - EditorUI::deselectAll(); - PlaytestHerePopup::hide(); - } -}; - -class $modify(GameObject) { - void selectObject(ccColor3B color) { - GameObject::selectObject(color); - PlaytestHerePopup::hide(); - if (m_objectID == 31) { - PlaytestHerePopup::show( - LevelEditorLayer::get(), - static_cast(as(this)) - ); - } - } -}; diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp new file mode 100644 index 00000000..55b750d7 --- /dev/null +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp @@ -0,0 +1,69 @@ +#include "PlaytestHerePopup.hpp" + +struct StartPosLevel; + +bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { + if (!CCNode::init()) + return false; + + m_callback = callback; + m_editor = lel; + m_startPos = startPos; + + this->setZOrder(0xB00B5); + + auto bg = CCScale9Sprite::create("square02_001.png", { 0, 0, 80, 80 }); + bg->setScale(.5f); + bg->setOpacity(80); + bg->setContentSize({ 120, 60 }); + this->addChild(bg); + + auto menu = CCMenu::create(); + menu->setPosition(0, 0); + + auto playTestBtnSpr = CCSprite::createWithSpriteFrameName("GJ_playEditorBtn_001.png"); + playTestBtnSpr->setScale(.615f); + + auto playTestBtn = CCMenuItemSpriteExtra::create( + playTestBtnSpr, this, menu_selector(PlaytestHerePopup::onPlaytest) + ); + playTestBtn->setPosition(-15.f, .0f); + menu->addChild(playTestBtn); + + auto playInGameBtnSpr = CCSprite::createWithSpriteFrameName("GJ_playBtn2_001.png"); + playInGameBtnSpr->setScale(.3f); + + auto playInGameBtn = CCMenuItemSpriteExtra::create( + playInGameBtnSpr, this, menu_selector(PlaytestHerePopup::onPlayInGame) + ); + playInGameBtn->setPosition(15.f, .0f); + menu->addChild(playInGameBtn); + + this->addChild(menu); + + CCDirector::sharedDirector()->getTouchDispatcher()->incrementForcePrio(2); + menu->registerWithTouchDispatcher(); + + s_popup = this; + + return true; +} + +void PlaytestHerePopup::onPlaytest(CCObject*) { + this->select(); + m_callback(m_startPos); + m_editor->setStartPosObject(m_startPos); + EditorUI::get()->onPlaytest(EditorUI::get()->m_playtestBtn); +} + +void PlaytestHerePopup::onPlayInGame(CCObject*) { + this->select(); + log::info("startposition: {}, {}", m_startPos->getPositionX(), m_startPos->getPositionY()); + m_editor->m_editorUI->onPause(nullptr); + EditorPauseLayer::get()->setVisible(false); + EditorPauseLayer::get()->onSaveAndPlay(nullptr); +} + +void PlaytestHerePopup::select() { + m_callback(m_startPos->getPosition()); +} \ No newline at end of file diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.hpp b/src/features/StartPosSwitcher/PlaytestHerePopup.hpp new file mode 100644 index 00000000..6b218cc3 --- /dev/null +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include + +#include "StartPosKind.hpp" + +using namespace geode::prelude; + +class PlaytestHerePopup : public CCNode { +protected: + Ref m_startPos; + LevelEditorLayer* m_editor; + inline static PlaytestHerePopup* s_popup = nullptr; + std::function m_callback; + + bool init(LevelEditorLayer*, StartPosObject*, std::function); + + static PlaytestHerePopup* create( + LevelEditorLayer* lel, StartPosObject* startPos, std::function callback + ) { + auto ret = new PlaytestHerePopup; + if (ret && ret->init(lel, startPos, callback)) { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; + } + + void onPlaytest(CCObject*); + void onPlayInGame(CCObject*); +public: + void select(); + + static void move() { + if (s_popup) { + s_popup->setPosition( + s_popup->m_startPos->getPositionX(), + s_popup->m_startPos->getPositionY() + 35.f + ); + } + } + + static void show(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { + PlaytestHerePopup::hide(); + PlaytestHerePopup::create(lel, startPos, callback); + PlaytestHerePopup::move(); + lel->m_objectLayer->addChild(s_popup); + } + + static void hide() { + if (s_popup) { + s_popup->removeFromParent(); + s_popup = nullptr; + } + } +}; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp new file mode 100644 index 00000000..ab4cedda --- /dev/null +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -0,0 +1,85 @@ +#include "StartPosButtonBar.hpp" + +bool StartPosButtonBar::init(LevelEditorLayer* lel, std::function changeCallback) { + if (!CCMenu::init()) + return false; + + m_callback = changeCallback; + m_editor = lel; + + auto winSize = CCDirector::get()->getWinSize(); + + this->ignoreAnchorPointForPosition(false); + this->setPosition(winSize.width / 2, 45.f); + this->setContentSize({ 200.f, 60.f }); + + auto goToStartSpr = ButtonSprite::create( + CCSprite::createWithSpriteFrameName("start-from-start.png"_spr), + 0x32, true, 0x32, "GJ_button_01.png", .7f + ); + auto goToStartBtn = CCMenuItemSpriteExtra::create( + goToStartSpr, this, menu_selector(StartPosButtonBar::onGoToStart) + ); + goToStartBtn->setPosition( + m_obContentSize.width / 2 - 25.f, + m_obContentSize.height / 2 + ); + this->addChild(goToStartBtn); + + auto goToLastSpr = ButtonSprite::create( + CCSprite::createWithSpriteFrameName("last-start-pos.png"_spr), + 0x32, true, 0x32, "GJ_button_01.png", .75f + ); + auto goToLastBtn = CCMenuItemSpriteExtra::create( + goToLastSpr, this, menu_selector(StartPosButtonBar::onGoToLast) + ); + goToLastBtn->setPosition( + m_obContentSize.width / 2 + 25.f, + m_obContentSize.height / 2 + ); + this->addChild(goToLastBtn); + + auto infoSpr = CCSprite::createWithSpriteFrameName("GJ_infoIcon_001.png"); + auto infoBtn = CCMenuItemSpriteExtra::create( + infoSpr, this, menu_selector(StartPosButtonBar::onInfo) + ); + infoBtn->setPosition(m_obContentSize / 2 + ccp(60.f, 0.f)); + this->addChild(infoBtn); + + return true; +} + +void StartPosButtonBar::onInfo(CCObject*) { + FLAlertLayer::create( + "Start Pos Info", + "From Level Start: Start playing from the beginning of the " + "level, ignoring all start positions\n" + "From Last Start Pos: Start playing from the last start " + "position in the level", + "OK" + )->show(); +} + +void StartPosButtonBar::onGoToStart(CCObject*) { + if (m_callback) { + m_callback(FromLevelStart()); + } +} + +void StartPosButtonBar::onGoToLast(CCObject*) { + StartPosObject* last = nullptr; + for(auto* obj : cocos::CCArrayExt(m_editor->m_objects)) { + if (obj->m_objectID == 31) { + if (!last || last->getPositionX() < obj->getPositionX()) { + last = obj; + } + } + } + if (last) { + log::info("calling last"); + m_callback(last); + } else { + log::info("default"); + m_callback(DefaultBehaviour()); + } +} \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.hpp b/src/features/StartPosSwitcher/StartPosButtonBar.hpp new file mode 100644 index 00000000..e04f476e --- /dev/null +++ b/src/features/StartPosSwitcher/StartPosButtonBar.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "StartPosKind.hpp" + +using namespace geode::prelude; + +class StartPosButtonBar : public CCMenu { +protected: + LevelEditorLayer* m_editor; + std::function m_callback; + + bool init(LevelEditorLayer*, std::function); + void onInfo(CCObject*); + void onGoToStart(CCObject*); + void onGoToLast(CCObject*); + +public: + static StartPosButtonBar* create(LevelEditorLayer* lel, std::function callback) { + auto ret = new StartPosButtonBar; + if (ret && ret->init(lel, callback)) { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; + } +}; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosKind.hpp b/src/features/StartPosSwitcher/StartPosKind.hpp new file mode 100644 index 00000000..937f3506 --- /dev/null +++ b/src/features/StartPosSwitcher/StartPosKind.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +using namespace geode::prelude; + +struct FromLevelStart {}; +struct DefaultBehaviour {}; +using FromPoint = CCPoint; +using FromObj = StartPosObject*; +using StartPosKind = std::variant< + FromLevelStart, + DefaultBehaviour, + FromPoint, + FromObj +>; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp new file mode 100644 index 00000000..5c3232a6 --- /dev/null +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#undef min +#undef max + +#include "PlaytestHerePopup.hpp" +#include "StartPosKind.hpp" +#include "StartPosButtonBar.hpp" + +using namespace geode::prelude; +using namespace editor_api; + +StartPosKind g_startPos = DefaultBehaviour(); + +bool match(CCPoint const& pos) { + log::info("match called"); + if (std::holds_alternative(g_startPos)) { + if (std::get(g_startPos) == pos) { + log::info("matched!"); + } + return std::get(g_startPos) == pos; + } + return false; +} + +void updateActiveStartPos(StartPosKind startPos) { + g_startPos = startPos; +} + +class $modify(PlayLayer) { + void addObject(GameObject* obj) { + if (std::holds_alternative(g_startPos)) { + PlayLayer::addObject(obj); + return; + } + StartPosObject* old = nullptr; + if (m_startPos) { + old = m_startPos; + } + PlayLayer::addObject(obj); + if (old) { + m_startPos = old; + m_playerStartPosition = old->getPosition(); + } + if (obj->m_objectID == 31) { + log::info("pos: {}, {}", obj->getPositionX(), obj->getPositionY()); + log::info("active startpos: {}, {}", m_startPos->getPositionX(), m_startPos->getPositionY()); + if (std::holds_alternative(g_startPos)) { + auto startpos = std::get(g_startPos); + log::info("global startpos: {}, {}", startpos->getPositionX(), startpos->getPositionY()); + } + if (std::holds_alternative(g_startPos)) { + auto coords = std::get(g_startPos); + log::info("global startpos: {}, {}", coords.x, coords.y); + } + if (match(obj->getPosition())) { + m_startPos = static_cast(obj); + m_playerStartPosition = obj->getPosition(); + } + if (std::holds_alternative(g_startPos)) { + m_startPos = nullptr; + m_playerStartPosition = CCPoint { 0, 105.f }; + } + } + } +}; + +class $modify(StartPosSwitchLayer, LevelEditorLayer) { + bool init(GJGameLevel* level) { + if (!LevelEditorLayer::init(level)) { + return false; + } + + g_startPos = DefaultBehaviour(); + return true; + } + + void addSpecial(GameObject* obj) { + log::info("add special"); + LevelEditorLayer::addSpecial(obj); + if (obj->m_objectID == 31) { + if (match(obj->getPosition())) { + g_startPos = static_cast(obj); + } + } + } + + // void destructor() { + // PlaytestHerePopup::hide(); + // LevelEditorLayer::~LevelEditorLayer(); + // } + + void handleAction(bool idk, CCArray* idk2) { + LevelEditorLayer::handleAction(idk, idk2); + PlaytestHerePopup::move(); + } + + void setupLevelStart(LevelSettingsObject* obj) { + // selected start position + if (std::holds_alternative(g_startPos)) { + auto obj = std::get(g_startPos); + this->setStartPosObject(obj); + auto startPos = obj->getPosition(); + m_player1->setStartPos(startPos); + m_player1->resetObject(); + m_player2->setStartPos(startPos); + m_player2->resetObject(); + LevelEditorLayer::setupLevelStart(obj->m_levelSettings); + } + // from level start + else if (std::holds_alternative(g_startPos)) { + this->setStartPosObject(nullptr); + m_player1->setStartPos({ 0.f, 105.f }); + m_player1->resetObject(); + m_player2->setStartPos({ 0.f, 105.f }); + m_player2->resetObject(); + LevelEditorLayer::setupLevelStart(m_levelSettings); + } + // default behaviour + else { + LevelEditorLayer::setupLevelStart(obj); + } + } +}; + +$onEditorExit { + PlaytestHerePopup::hide(); +} + + +class $modify(EditorPauseLayer) { + bool init(LevelEditorLayer* lel) { + if (!EditorPauseLayer::init(lel)) + return false; + + PlaytestHerePopup::hide(); + + return true; + } +}; + + +class $modify(MyEditorUI, EditorUI) { + bool init(LevelEditorLayer* lel) { + if (!EditorUI::init(lel)) + return false; + + MoreTabs::get(this)->addEditTab( + "start-pos-border.png"_spr, + StartPosButtonBar::create(m_editorLayer, [](StartPosKind startPos) { + g_startPos = startPos; + }) + ); + + return true; + } + + void deleteObject(GameObject* obj, bool filter) { + EditorUI::deleteObject(obj, filter); + PlaytestHerePopup::hide(); + } + + void selectObjects(CCArray* objects, bool ignoreFilters) { + EditorUI::selectObjects(objects, ignoreFilters); + PlaytestHerePopup::hide(); + } + + void selectObject(GameObject* obj, bool filter) { + EditorUI::selectObject(obj, filter); + MoreTabs::get(this)->switchEditTab(1); + } + + void moveObject(GameObject* obj, CCPoint pos) { + EditorUI::moveObject(obj, pos); + PlaytestHerePopup::move(); + } + + void moveObjectCall(EditCommand cmd) { + EditorUI::moveObjectCall(cmd); + PlaytestHerePopup::move(); + } + + void deselectAll() { + EditorUI::deselectAll(); + PlaytestHerePopup::hide(); + } +}; + +class $modify(GameObject) { + void selectObject(ccColor3B color) { + GameObject::selectObject(color); + PlaytestHerePopup::hide(); + if (m_objectID == 31) { + PlaytestHerePopup::show( + LevelEditorLayer::get(), + static_cast(as(this)), + [](StartPosKind startPos) { + g_startPos = startPos; + } + ); + } + } +}; From a93591275b9c4cf203683a153a992f52af3ab95a Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:19:57 +0300 Subject: [PATCH 02/18] oops --- src/features/StartPosSwitcher/PlaytestHerePopup.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp index 55b750d7..625f37ac 100644 --- a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp @@ -1,7 +1,5 @@ #include "PlaytestHerePopup.hpp" -struct StartPosLevel; - bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { if (!CCNode::init()) return false; From d58807b36e2e3c2f7d65638c68bf8b98692ef0cf Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:57:34 +0300 Subject: [PATCH 03/18] fix ldm display on 0 objects --- src/features/LDMObjectCount.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/features/LDMObjectCount.cpp b/src/features/LDMObjectCount.cpp index 468d887e..e0bc6041 100644 --- a/src/features/LDMObjectCount.cpp +++ b/src/features/LDMObjectCount.cpp @@ -15,11 +15,20 @@ class $modify(LDMCountEditorPauseLayer, EditorPauseLayer) { auto menu = static_cast(this->getChildByID("info-menu")); auto objectCountLabel = static_cast(menu->getChildByID("object-count-label")); + auto objectCount = this->countValidObjects(); + std::string objectCountText = objectCountLabel->getString(); std::stringstream ss; ss << std::string(objectCountLabel->getString()); - int ldmCount = this->countLDMObjects(); - float percentage = static_cast(ldmCount) / this->countValidObjects() * 100; + int ldmCount; + float percentage; + if (countValidObjects == 0) { + ldmCount = 0; + percentage = 0.f; + } else { + ldmCount = this->countLDMObjects(); + percentage = static_cast(ldmCount) / objectCount * 100; + } ss << " | " << ldmCount << " LDM (" << std::fixed << std::setprecision(2) << percentage << "%)"; objectCountLabel->setString(ss.str().c_str()); From 30d58de79129ee178edba65e93b59494c51be603 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sun, 8 Oct 2023 22:23:26 +0300 Subject: [PATCH 04/18] ldm object count now compiles --- src/features/LDMObjectCount.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/LDMObjectCount.cpp b/src/features/LDMObjectCount.cpp index e0bc6041..098170f7 100644 --- a/src/features/LDMObjectCount.cpp +++ b/src/features/LDMObjectCount.cpp @@ -22,7 +22,7 @@ class $modify(LDMCountEditorPauseLayer, EditorPauseLayer) { ss << std::string(objectCountLabel->getString()); int ldmCount; float percentage; - if (countValidObjects == 0) { + if (objectCount == 0) { ldmCount = 0; percentage = 0.f; } else { From 4bee95d531734c0bf3b00b3bf8464d741dbae900 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Sun, 8 Oct 2023 22:23:38 +0300 Subject: [PATCH 05/18] startpos switcher switcher start positions better --- .../StartPosSwitcher/StartPosSwitcher.cpp | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 5c3232a6..c41ed1fb 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -36,38 +36,31 @@ void updateActiveStartPos(StartPosKind startPos) { } class $modify(PlayLayer) { + StartPosObject* activeStartPos = nullptr; + bool fromLevelStart = false; void addObject(GameObject* obj) { if (std::holds_alternative(g_startPos)) { PlayLayer::addObject(obj); return; - } - StartPosObject* old = nullptr; - if (m_startPos) { - old = m_startPos; } PlayLayer::addObject(obj); - if (old) { - m_startPos = old; - m_playerStartPosition = old->getPosition(); - } if (obj->m_objectID == 31) { - log::info("pos: {}, {}", obj->getPositionX(), obj->getPositionY()); - log::info("active startpos: {}, {}", m_startPos->getPositionX(), m_startPos->getPositionY()); - if (std::holds_alternative(g_startPos)) { - auto startpos = std::get(g_startPos); - log::info("global startpos: {}, {}", startpos->getPositionX(), startpos->getPositionY()); - } - if (std::holds_alternative(g_startPos)) { - auto coords = std::get(g_startPos); - log::info("global startpos: {}, {}", coords.x, coords.y); - } if (match(obj->getPosition())) { - m_startPos = static_cast(obj); - m_playerStartPosition = obj->getPosition(); + m_fields->activeStartPos = static_cast(obj); } if (std::holds_alternative(g_startPos)) { + m_fields->fromLevelStart = true; + } + + if (m_fields->fromLevelStart) { m_startPos = nullptr; - m_playerStartPosition = CCPoint { 0, 105.f }; + m_playerStartPosition = CCPointZero; + return; + } + + if (m_fields->activeStartPos) { + m_startPos = m_fields->activeStartPos; + m_playerStartPosition = m_fields->activeStartPos->getPosition(); } } } @@ -84,7 +77,6 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { } void addSpecial(GameObject* obj) { - log::info("add special"); LevelEditorLayer::addSpecial(obj); if (obj->m_objectID == 31) { if (match(obj->getPosition())) { From 1306528e04757ead76f26e75b9809c72442b9dd0 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:06:17 +0300 Subject: [PATCH 06/18] add counter + made active startpos update on moves --- .../StartPosSwitcher/PlaytestHerePopup.cpp | 4 +- .../StartPosSwitcher/StartPosButtonBar.cpp | 62 +++++-- .../StartPosSwitcher/StartPosButtonBar.hpp | 3 +- .../StartPosSwitcher/StartPosKind.hpp | 4 +- .../StartPosSwitcher/StartPosSwitcher.cpp | 156 ++++++++++++------ 5 files changed, 155 insertions(+), 74 deletions(-) diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp index 625f37ac..3194d8ce 100644 --- a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp @@ -49,17 +49,15 @@ bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, st void PlaytestHerePopup::onPlaytest(CCObject*) { this->select(); - m_callback(m_startPos); m_editor->setStartPosObject(m_startPos); EditorUI::get()->onPlaytest(EditorUI::get()->m_playtestBtn); } void PlaytestHerePopup::onPlayInGame(CCObject*) { this->select(); - log::info("startposition: {}, {}", m_startPos->getPositionX(), m_startPos->getPositionY()); m_editor->m_editorUI->onPause(nullptr); - EditorPauseLayer::get()->setVisible(false); EditorPauseLayer::get()->onSaveAndPlay(nullptr); + EditorPauseLayer::get()->setVisible(false); } void PlaytestHerePopup::select() { diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp index ab4cedda..2e32cc1b 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.cpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -22,10 +22,17 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel, std::functionsetPosition( m_obContentSize.width / 2 - 25.f, - m_obContentSize.height / 2 + m_obContentSize.height / 2 + 10.f ); this->addChild(goToStartBtn); + auto counterLabel = CCLabelBMFont::create("", "bigFont.fnt"); + counterLabel->setScale(0.6f); + counterLabel->setPosition({ m_obContentSize.width / 2, m_obContentSize.height / 2 - 25.f }); + m_counterLabel = counterLabel; + this->addChild(counterLabel); + this->setStartPosCounters(DefaultBehaviour()); + auto goToLastSpr = ButtonSprite::create( CCSprite::createWithSpriteFrameName("last-start-pos.png"_spr), 0x32, true, 0x32, "GJ_button_01.png", .75f @@ -35,15 +42,16 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel, std::functionsetPosition( m_obContentSize.width / 2 + 25.f, - m_obContentSize.height / 2 + m_obContentSize.height / 2 + 10.f ); this->addChild(goToLastBtn); auto infoSpr = CCSprite::createWithSpriteFrameName("GJ_infoIcon_001.png"); + infoSpr->setScale(0.7f); auto infoBtn = CCMenuItemSpriteExtra::create( infoSpr, this, menu_selector(StartPosButtonBar::onInfo) ); - infoBtn->setPosition(m_obContentSize / 2 + ccp(60.f, 0.f)); + infoBtn->setPosition(m_obContentSize / 2 + ccp(70.f, 10.f)); this->addChild(infoBtn); return true; @@ -61,25 +69,45 @@ void StartPosButtonBar::onInfo(CCObject*) { } void StartPosButtonBar::onGoToStart(CCObject*) { - if (m_callback) { - m_callback(FromLevelStart()); - } + m_callback(FromLevelStart()); + this->setStartPosCounters(FromLevelStart()); } void StartPosButtonBar::onGoToLast(CCObject*) { - StartPosObject* last = nullptr; - for(auto* obj : cocos::CCArrayExt(m_editor->m_objects)) { - if (obj->m_objectID == 31) { - if (!last || last->getPositionX() < obj->getPositionX()) { - last = obj; + m_callback(DefaultBehaviour()); + this->setStartPosCounters(DefaultBehaviour()); +} + +void StartPosButtonBar::setStartPosCounters(StartPosKind startPos) { + int totalStartPos = 0; + int activeIndex = 0; + if (!m_editor || !m_editor->m_objects) { + return; + } + + for (auto object : CCArrayExt(m_editor->m_objects)) { + if (object->m_objectID != 31) { + continue; + } + + totalStartPos++; + if (std::holds_alternative(startPos)) { + auto pos = std::get(startPos); + if (object->getPosition() == pos) { + activeIndex = totalStartPos; } } } - if (last) { - log::info("calling last"); - m_callback(last); - } else { - log::info("default"); - m_callback(DefaultBehaviour()); + + if (std::holds_alternative(startPos)) { + activeIndex = totalStartPos; + } + + if (std::holds_alternative(startPos)) { + activeIndex = 0; } + + std::string str = std::to_string(activeIndex) + " / " + std::to_string(totalStartPos); + + m_counterLabel->setString(str.c_str()); } \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.hpp b/src/features/StartPosSwitcher/StartPosButtonBar.hpp index e04f476e..92ba23bc 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.hpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.hpp @@ -10,13 +10,14 @@ class StartPosButtonBar : public CCMenu { protected: LevelEditorLayer* m_editor; std::function m_callback; + CCLabelBMFont* m_counterLabel = nullptr; bool init(LevelEditorLayer*, std::function); void onInfo(CCObject*); void onGoToStart(CCObject*); void onGoToLast(CCObject*); - public: + void setStartPosCounters(StartPosKind); static StartPosButtonBar* create(LevelEditorLayer* lel, std::function callback) { auto ret = new StartPosButtonBar; if (ret && ret->init(lel, callback)) { diff --git a/src/features/StartPosSwitcher/StartPosKind.hpp b/src/features/StartPosSwitcher/StartPosKind.hpp index 937f3506..7f87b119 100644 --- a/src/features/StartPosSwitcher/StartPosKind.hpp +++ b/src/features/StartPosSwitcher/StartPosKind.hpp @@ -7,10 +7,8 @@ using namespace geode::prelude; struct FromLevelStart {}; struct DefaultBehaviour {}; using FromPoint = CCPoint; -using FromObj = StartPosObject*; using StartPosKind = std::variant< FromLevelStart, DefaultBehaviour, - FromPoint, - FromObj + FromPoint >; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index c41ed1fb..7e5144db 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -21,18 +21,48 @@ using namespace editor_api; StartPosKind g_startPos = DefaultBehaviour(); bool match(CCPoint const& pos) { - log::info("match called"); if (std::holds_alternative(g_startPos)) { - if (std::get(g_startPos) == pos) { - log::info("matched!"); - } return std::get(g_startPos) == pos; } return false; } -void updateActiveStartPos(StartPosKind startPos) { - g_startPos = startPos; +/** + * Fetches the startpos that matches the given position. If objects is passed, it searches the objects, if not it tries to look in LevelEditorLayer and PlayLayer. + * If none is found, returns nullptr. +*/ +StartPosObject* getStartPosByPosition(CCPoint const& pos, CCArray* objects) { + if (!objects) { + auto levelEdit = LevelEditorLayer::get(); + if (levelEdit) { + objects = levelEdit->m_objects; + } + + auto playLayer = PlayLayer::get(); + if (!objects && playLayer) { + objects = playLayer->m_objects; + } + + if (!objects) { + return nullptr; + } + } + + for (auto object : CCArrayExt(objects)) { + auto editorUI = EditorUI::get(); + if (object->m_objectID == 31) { + if (object->getPosition() == pos) { + return static_cast(object); + } + // attention please... THE HACKIEST FIX!! + if (editorUI && object->getPosition() == editorUI->getGridSnappedPos(pos)) { + g_startPos = editorUI->getGridSnappedPos(pos); + return static_cast(object); + } + } + } + + return nullptr; } class $modify(PlayLayer) { @@ -67,59 +97,40 @@ class $modify(PlayLayer) { }; class $modify(StartPosSwitchLayer, LevelEditorLayer) { - bool init(GJGameLevel* level) { - if (!LevelEditorLayer::init(level)) { - return false; - } - - g_startPos = DefaultBehaviour(); - return true; - } - - void addSpecial(GameObject* obj) { - LevelEditorLayer::addSpecial(obj); - if (obj->m_objectID == 31) { - if (match(obj->getPosition())) { - g_startPos = static_cast(obj); - } - } - } - - // void destructor() { - // PlaytestHerePopup::hide(); - // LevelEditorLayer::~LevelEditorLayer(); - // } - void handleAction(bool idk, CCArray* idk2) { LevelEditorLayer::handleAction(idk, idk2); PlaytestHerePopup::move(); } void setupLevelStart(LevelSettingsObject* obj) { - // selected start position - if (std::holds_alternative(g_startPos)) { - auto obj = std::get(g_startPos); - this->setStartPosObject(obj); - auto startPos = obj->getPosition(); - m_player1->setStartPos(startPos); + if (std::holds_alternative(g_startPos)) { + auto pos = std::get(g_startPos); + auto startPos = getStartPosByPosition(pos, m_objects); + if (!startPos) { + Notification::create("Couldn't setup startpos switcher.", CCSprite::createWithSpriteFrameName("edit_delBtnSmall_001.png"))->show(); + LevelEditorLayer::setupLevelStart(obj); + return; + } + this->setStartPosObject(startPos); + m_player1->setStartPos(startPos->getPosition()); m_player1->resetObject(); - m_player2->setStartPos(startPos); + m_player2->setStartPos(startPos->getPosition()); m_player2->resetObject(); - LevelEditorLayer::setupLevelStart(obj->m_levelSettings); + + LevelEditorLayer::setupLevelStart(startPos->m_levelSettings); + return; } - // from level start - else if (std::holds_alternative(g_startPos)) { + if (std::holds_alternative(g_startPos)) { this->setStartPosObject(nullptr); - m_player1->setStartPos({ 0.f, 105.f }); + m_player1->setStartPos(CCPointZero); m_player1->resetObject(); - m_player2->setStartPos({ 0.f, 105.f }); + m_player2->setStartPos(CCPointZero); m_player2->resetObject(); LevelEditorLayer::setupLevelStart(m_levelSettings); + return; } // default behaviour - else { - LevelEditorLayer::setupLevelStart(obj); - } + LevelEditorLayer::setupLevelStart(obj); } }; @@ -127,7 +138,6 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { PlaytestHerePopup::hide(); } - class $modify(EditorPauseLayer) { bool init(LevelEditorLayer* lel) { if (!EditorPauseLayer::init(lel)) @@ -137,27 +147,54 @@ class $modify(EditorPauseLayer) { return true; } -}; + void onSaveAndPlay(CCObject* sender) { + if (std::holds_alternative(g_startPos)) { + auto startpos = getStartPosByPosition(std::get(g_startPos), m_editorLayer->m_objects); + } + EditorPauseLayer::onSaveAndPlay(sender); + } +}; class $modify(MyEditorUI, EditorUI) { + StartPosButtonBar* buttonBar = nullptr; bool init(LevelEditorLayer* lel) { if (!EditorUI::init(lel)) return false; + + auto buttonBar = StartPosButtonBar::create(m_editorLayer, [](StartPosKind startPos) { + g_startPos = startPos; + }); + buttonBar->setID("start-pos-button-bar"_spr); + m_fields->buttonBar = buttonBar; MoreTabs::get(this)->addEditTab( "start-pos-border.png"_spr, - StartPosButtonBar::create(m_editorLayer, [](StartPosKind startPos) { - g_startPos = startPos; - }) + buttonBar ); + if (std::holds_alternative(g_startPos)) { + return true; + } + buttonBar->setStartPosCounters(g_startPos); return true; } + void onCreateObject(int id) { + EditorUI::onCreateObject(id); + log::info("I guess this is id: {}", id); + } + void deleteObject(GameObject* obj, bool filter) { EditorUI::deleteObject(obj, filter); PlaytestHerePopup::hide(); + if (obj->m_objectID == 31 && std::holds_alternative(g_startPos)) { + auto pos = std::get(g_startPos); + if (obj->getPosition() == pos) { + g_startPos = DefaultBehaviour(); + } + } + m_fields->buttonBar->setStartPosCounters(g_startPos); } void selectObjects(CCArray* objects, bool ignoreFilters) { @@ -171,8 +208,23 @@ class $modify(MyEditorUI, EditorUI) { } void moveObject(GameObject* obj, CCPoint pos) { + bool movedActiveStartPos = false; + if (!m_editorLayer->m_editorInitialising) { + if (obj->m_objectID == 31 && std::holds_alternative(g_startPos)) { + auto position = std::get(g_startPos); + if (position == obj->getPosition()) { + movedActiveStartPos = true; + log::info("active startPos: {}, {}", position.x, position.y); + } + } + } EditorUI::moveObject(obj, pos); PlaytestHerePopup::move(); + if (movedActiveStartPos) { + g_startPos = obj->getPosition(); + m_fields->buttonBar->setStartPosCounters(g_startPos); + log::info("startPos: {}, {}", obj->getPositionX(), obj->getPositionY()); + } } void moveObjectCall(EditCommand cmd) { @@ -194,8 +246,12 @@ class $modify(GameObject) { PlaytestHerePopup::show( LevelEditorLayer::get(), static_cast(as(this)), - [](StartPosKind startPos) { + [this](StartPosKind startPos) { g_startPos = startPos; + auto buttonBar = static_cast(LevelEditorLayer::get()->m_editorUI->getChildByID("start-pos-button-bar"_spr)); + if (buttonBar) { + buttonBar->setStartPosCounters(startPos); + } } ); } From 1ff07b37cdfdd61028e74fb86039574df75f6b7f Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:22:33 +0300 Subject: [PATCH 07/18] made smart startpos not set gravity for ball and spider --- mod.json | 2 +- src/features/SmartStartPos.cpp | 37 +++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/mod.json b/mod.json index 358c5568..cc50ec37 100644 --- a/mod.json +++ b/mod.json @@ -67,7 +67,7 @@ "type": "bool", "default": true, "name": "Smart StartPos", - "description": "When enabled, all StartPoses in the editor will be automatically set for you" + "description": "When enabled, all StartPoses in the editor will be automatically set for you. Does not set gravity for ball and spider gamemodes." }, "playtest-lag-fix": { "type": "bool", diff --git a/src/features/SmartStartPos.cpp b/src/features/SmartStartPos.cpp index bfbccec3..8343158b 100644 --- a/src/features/SmartStartPos.cpp +++ b/src/features/SmartStartPos.cpp @@ -18,22 +18,7 @@ class $modify(LevelEditorLayer) { } void setupStartPos(StartPosObject* startPos) { - for (size_t i = 0; i < gravityPortals.size(); i++) - { - if (gravityPortals[i]->getPositionX() - 10 > startPos->getPositionX()) - break; - if (gravityPortals[i]->getPositionX() - 10 < startPos->getPositionX()) - startPos->m_levelSettings->m_isFlipped = gravityPortals[i]->m_objectID == 11; - } - - startPos->m_levelSettings->m_startDual = LevelEditorLayer::get()->m_levelSettings->m_startDual; - for (size_t i = 0; i < dualPortals.size(); i++) - { - if (dualPortals[i]->getPositionX() - 10 > startPos->getPositionX()) - break; - if (dualPortals[i]->getPositionX() - 10 < startPos->getPositionX()) - startPos->m_levelSettings->m_startDual = dualPortals[i]->m_objectID == 286; - } + bool isBallOrSpider = false; startPos->m_levelSettings->m_startMode = LevelEditorLayer::get()->m_levelSettings->m_startMode; for (size_t i = 0; i < gamemodePortals.size(); i++) { @@ -51,6 +36,7 @@ class $modify(LevelEditorLayer) { break; case 47: startPos->m_levelSettings->m_startMode = (int)IconType::Ball; + isBallOrSpider = true; break; case 111: startPos->m_levelSettings->m_startMode = (int)IconType::Ufo; @@ -63,10 +49,29 @@ class $modify(LevelEditorLayer) { break; case 1331: startPos->m_levelSettings->m_startMode = (int)IconType::Spider; + isBallOrSpider = true; break; } } } + if (!isBallOrSpider) { + for (size_t i = 0; i < gravityPortals.size(); i++) + { + if (gravityPortals[i]->getPositionX() - 10 > startPos->getPositionX()) + break; + if (gravityPortals[i]->getPositionX() - 10 < startPos->getPositionX()) + startPos->m_levelSettings->m_isFlipped = gravityPortals[i]->m_objectID == 11; + } + } + + startPos->m_levelSettings->m_startDual = LevelEditorLayer::get()->m_levelSettings->m_startDual; + for (size_t i = 0; i < dualPortals.size(); i++) + { + if (dualPortals[i]->getPositionX() - 10 > startPos->getPositionX()) + break; + if (dualPortals[i]->getPositionX() - 10 < startPos->getPositionX()) + startPos->m_levelSettings->m_startDual = dualPortals[i]->m_objectID == 286; + } startPos->m_levelSettings->m_startMini = LevelEditorLayer::get()->m_levelSettings->m_startMini; for (size_t i = 0; i < miniPortals.size(); i++) From ee1570d8f6e9a67a73a16cca157be8e186bf09df Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:22:47 +0300 Subject: [PATCH 08/18] reset startpos switcher on level exit --- src/features/StartPosSwitcher/StartPosSwitcher.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 7e5144db..a56210ac 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -154,6 +154,16 @@ class $modify(EditorPauseLayer) { } EditorPauseLayer::onSaveAndPlay(sender); } + + void onExitEditor(CCObject* sender) { + EditorPauseLayer::onExitEditor(sender); + g_startPos = DefaultBehaviour(); + } + + void onSaveAndExit(CCObject* sender) { + EditorPauseLayer::onSaveAndExit(sender); + g_startPos = DefaultBehaviour(); + } }; class $modify(MyEditorUI, EditorUI) { From d1f1433a448e6443abf184e9cda3701e3cdd314c Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:28:06 +0300 Subject: [PATCH 09/18] reset startpos on level exit --- src/features/StartPosSwitcher/StartPosSwitcher.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index a56210ac..214a8e73 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -138,6 +139,14 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { PlaytestHerePopup::hide(); } +// In case $onEditorExit runs on save and play +class $modify(GameManager) { + void returnToLastScene(GJGameLevel* level) { + GameManager::returnToLastScene(level); + g_startPos = DefaultBehaviour(); + } +}; + class $modify(EditorPauseLayer) { bool init(LevelEditorLayer* lel) { if (!EditorPauseLayer::init(lel)) From f3f94bf0536e7d901f9a187ac37105fefa0fe7e3 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:28:32 +0300 Subject: [PATCH 10/18] remove logs --- src/features/StartPosSwitcher/StartPosSwitcher.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 214a8e73..fc9a49a1 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -199,11 +199,6 @@ class $modify(MyEditorUI, EditorUI) { return true; } - void onCreateObject(int id) { - EditorUI::onCreateObject(id); - log::info("I guess this is id: {}", id); - } - void deleteObject(GameObject* obj, bool filter) { EditorUI::deleteObject(obj, filter); PlaytestHerePopup::hide(); @@ -233,7 +228,6 @@ class $modify(MyEditorUI, EditorUI) { auto position = std::get(g_startPos); if (position == obj->getPosition()) { movedActiveStartPos = true; - log::info("active startPos: {}, {}", position.x, position.y); } } } @@ -242,7 +236,6 @@ class $modify(MyEditorUI, EditorUI) { if (movedActiveStartPos) { g_startPos = obj->getPosition(); m_fields->buttonBar->setStartPosCounters(g_startPos); - log::info("startPos: {}, {}", obj->getPositionX(), obj->getPositionY()); } } From 9c10ff8c78371f1b5f40393c74321b333f2c666f Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:45:11 +0300 Subject: [PATCH 11/18] checkpoint in case I screw it up --- .../StartPosSwitcher/StartPosManager.cpp | 143 +++++++++++++++ .../StartPosSwitcher/StartPosManager.hpp | 26 +++ .../StartPosSwitcher/StartPosButtonBar.cpp | 169 ++++++++++++++++-- .../StartPosSwitcher/StartPosButtonBar.hpp | 18 +- .../StartPosSwitcher/StartPosSwitcher.cpp | 23 +++ 5 files changed, 363 insertions(+), 16 deletions(-) create mode 100644 src/Features/StartPosSwitcher/StartPosManager.cpp create mode 100644 src/Features/StartPosSwitcher/StartPosManager.hpp diff --git a/src/Features/StartPosSwitcher/StartPosManager.cpp b/src/Features/StartPosSwitcher/StartPosManager.cpp new file mode 100644 index 00000000..57de3574 --- /dev/null +++ b/src/Features/StartPosSwitcher/StartPosManager.cpp @@ -0,0 +1,143 @@ +#include "StartPosManager.hpp" + +StartPosManager* StartPosManager::get() { + if (!s_instance) { + auto instance = new StartPosManager; + if (instance) { + s_instance = instance; + return instance; + } + + CC_SAFE_DELETE(instance); + return instance; + } + return s_instance; +} + +void StartPosManager::setStartPositions(CCArray* objects) { + m_positions.clear(); + m_positions.push_back(CCPointZero); + for (auto object : CCArrayExt(objects)) { + if (object->m_objectID == 31) { + auto skip = false; + for (auto position : m_positions) { + if (position == object->getPosition()) { + skip = true; + break; + } + } + if (skip) { + continue; + } + m_positions.push_back(object->getPosition()); + } + } + + this->sort(); + + if (m_positions.size() == 1) { + m_active = CCPointZero; + return; + } + + m_active = m_positions.at(m_positions.size() - 1); +} + +void StartPosManager::sort() { + std::sort(m_positions.begin(), m_positions.end(), [](CCPoint first, CCPoint second) { + return first.x < second.x; + }); +} + +void StartPosManager::setActive(CCPoint const& pos) { + bool set = false; + for (auto startPos : m_positions) { + if (startPos == pos) { + m_active = pos; + set = true; + break; + } + } + + if (!set) { + m_active = m_positions.at(m_positions.size() - 1); + } +} + +void StartPosManager::addStartPos(CCPoint const& position) { + for (auto startPosition : m_positions) { + if (startPosition == position) { + return; + } + } + + this->sort(); +} + +void StartPosManager::moveStartPos(CCPoint const& before, CCPoint const& after) { + if (!LevelEditorLayer::get() || LevelEditorLayer::get()->m_editorInitialising) { + return; + } + + for (auto& position : m_positions) { + if (position == before) { + position = after; + break; + } + } + + this->sort(); +} + +StartPosObject* StartPosManager::getStartPosFromPoint(CCPoint const& point) { + if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising) { + for (auto object : CCArrayExt(LevelEditorLayer::get()->m_objects)) { + if ( + object->m_objectID == 31 && + object->getPosition() == point || + object->getPosition() == LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point) + ) { + return static_cast(object); + } + } + + return nullptr; + } + + if (PlayLayer::get()) { + for (auto object : CCArrayExt(PlayLayer::get()->m_objects)) { + if ( + object->m_objectID == 31 && + object->getPosition() == point + ) { + return static_cast(object); + } + } + } + return nullptr; +} + +void StartPosManager::next() { + for (auto pos : m_positions) { + if (pos.x > m_active.x) { + m_active = pos; + return; + } + } +} + +void StartPosManager::previous() { + auto found = CCPointZero; + for (auto pos : m_positions) { + if (pos.x < m_active.x) { + found = pos; + } + } + + m_active = found; +} + +void StartPosManager::clear() { + m_active = CCPointZero; + m_positions.clear(); +} \ No newline at end of file diff --git a/src/Features/StartPosSwitcher/StartPosManager.hpp b/src/Features/StartPosSwitcher/StartPosManager.hpp new file mode 100644 index 00000000..17306ed0 --- /dev/null +++ b/src/Features/StartPosSwitcher/StartPosManager.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +using namespace geode::prelude; + +class StartPosManager : public CCObject { +protected: + std::vector m_positions; + CCPoint m_active; + + inline static StartPosManager* s_instance; + void sort(); +public: + static StartPosManager* get(); + void setStartPositions(CCArray* objects); + void setActive(CCPoint const& pos); + void addStartPos(CCPoint const& pos); + void moveStartPos(CCPoint const& before, CCPoint const& after); + StartPosObject* getStartPosFromPoint(CCPoint const& pos); + void next(); + void previous(); + void clear(); +}; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp index 2e32cc1b..5b972455 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.cpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -22,13 +22,14 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel, std::functionsetPosition( m_obContentSize.width / 2 - 25.f, - m_obContentSize.height / 2 + 10.f + m_obContentSize.height / 2 + 20.f ); this->addChild(goToStartBtn); auto counterLabel = CCLabelBMFont::create("", "bigFont.fnt"); counterLabel->setScale(0.6f); counterLabel->setPosition({ m_obContentSize.width / 2, m_obContentSize.height / 2 - 25.f }); + counterLabel->limitLabelWidth(80.0f, 0.6f, 0.4f); m_counterLabel = counterLabel; this->addChild(counterLabel); this->setStartPosCounters(DefaultBehaviour()); @@ -42,18 +43,35 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel, std::functionsetPosition( m_obContentSize.width / 2 + 25.f, - m_obContentSize.height / 2 + 10.f + m_obContentSize.height / 2 + 20.f ); this->addChild(goToLastBtn); auto infoSpr = CCSprite::createWithSpriteFrameName("GJ_infoIcon_001.png"); - infoSpr->setScale(0.7f); + infoSpr->setScale(0.8f); auto infoBtn = CCMenuItemSpriteExtra::create( infoSpr, this, menu_selector(StartPosButtonBar::onInfo) ); - infoBtn->setPosition(m_obContentSize / 2 + ccp(70.f, 10.f)); + infoBtn->setPosition(m_obContentSize / 2 + ccp(65.f, 20.f)); this->addChild(infoBtn); + auto arrowLeft = CCSprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); + arrowLeft->setScale(0.7f); + auto leftbutton = CCMenuItemSpriteExtra::create( + arrowLeft, this, menu_selector(StartPosButtonBar::onPrevious) + ); + leftbutton->setPosition(m_obContentSize / 2 + ccp(-45.0f, -25.0f)); + this->addChild(leftbutton); + + auto arrowRight = CCSprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); + arrowRight->setScale(0.7f); + arrowRight->setFlipX(true); + auto rightButton = CCMenuItemSpriteExtra::create( + arrowRight, this, menu_selector(StartPosButtonBar::onNext) + ); + rightButton->setPosition(m_obContentSize / 2 + ccp(45.0f, -25.0f)); + this->addChild(rightButton); + return true; } @@ -79,35 +97,158 @@ void StartPosButtonBar::onGoToLast(CCObject*) { } void StartPosButtonBar::setStartPosCounters(StartPosKind startPos) { - int totalStartPos = 0; - int activeIndex = 0; + m_totalStartPositions = 0; + m_activeIndex = 0; if (!m_editor || !m_editor->m_objects) { return; } + CCArrayExt startPositions; + for (auto object : CCArrayExt(m_editor->m_objects)) { if (object->m_objectID != 31) { continue; } - totalStartPos++; - if (std::holds_alternative(startPos)) { - auto pos = std::get(startPos); - if (object->getPosition() == pos) { - activeIndex = totalStartPos; + startPositions.push_back(static_cast(object)); + m_totalStartPositions++; + } + + if (std::holds_alternative(startPos) && m_activeIndex == 0) { + auto pos = std::get(startPos); + std::sort(startPositions.begin(), startPositions.end(), [](StartPosObject* x, StartPosObject* y){ + return x->getPositionX() < y->getPositionX(); + }); + int i = 0; + for (auto object : startPositions) { + i++; + if (object->getPosition() == pos || object->getPosition() == m_editor->m_editorUI->getGridSnappedPos(pos)) { + if (object->getPosition() == pos) { + m_activeIndex = i; + m_active = startPos; + } else if (!m_editor->m_editorInitialising && object->getPosition() == m_editor->m_editorUI->getGridSnappedPos(pos)) { + m_activeIndex = i; + m_active = startPos; + } } } } if (std::holds_alternative(startPos)) { - activeIndex = totalStartPos; + m_activeIndex = m_totalStartPositions; + m_active = startPos; } if (std::holds_alternative(startPos)) { - activeIndex = 0; + m_activeIndex = 0; + m_active = startPos; } - std::string str = std::to_string(activeIndex) + " / " + std::to_string(totalStartPos); + std::string str = std::to_string(m_activeIndex) + " / " + std::to_string(m_totalStartPositions); m_counterLabel->setString(str.c_str()); +} + +void StartPosButtonBar::onNext(CCObject*) { + if (m_activeIndex == m_totalStartPositions) { + return; + } + if (std::holds_alternative(m_active)) { + return; + } + + log::info("active: {}", m_activeIndex); + auto found = this->search(m_active, FindHigher); + m_callback(found); + this->setStartPosCounters(found); +} + +void StartPosButtonBar::onPrevious(CCObject*) { + if (m_activeIndex == 0) { + return; + } + if (std::holds_alternative(m_active)) { + return; + } + if (m_activeIndex == 1) { + m_active = FromLevelStart(); + m_callback(m_active); + this->setStartPosCounters(m_active); + } + + auto found = this->search(m_active, FindSmaller); + m_callback(found); + this->setStartPosCounters(found); +} + +StartPosKind StartPosButtonBar::search(StartPosKind current, StartPositionSearchType searchType) { + std::vector positions; + + for (auto object : CCArrayExt(m_editor->m_objects)) { + if (object->m_objectID == 31) { + switch (searchType) { + case StartPositionSearchType::FindHigher: { + if (std::holds_alternative(current)) { + return current; + } + if (std::holds_alternative(current)) { + positions.push_back(object->getPosition()); + continue; + } + if (object->getPositionX() > std::get(current).x) { + positions.push_back(object->getPosition()); + } + } break; + case StartPositionSearchType::FindSmaller: { + if (std::holds_alternative(current)) { + return current; + } + if (std::holds_alternative(current)) { + positions.push_back(object->getPosition()); + continue; + } + if (object->getPositionX() < std::get(current).x) { + positions.push_back(object->getPosition()); + } + } break; + } + } + } + + for (auto pos : positions) { + log::info("position: {}, {}", pos.x, pos.y); + } + + if (searchType == StartPositionSearchType::FindSmaller) { + positions.push_back(CCPointZero); + } + + if (positions.size() == 0) { + return current; + } + + switch (searchType) { + case StartPositionSearchType::FindHigher: { + std::sort(positions.begin(), positions.end(), [](CCPoint first, CCPoint second) { + return first.x < second.x; + }); + + return positions.at(0); + } break; + case StartPositionSearchType::FindSmaller: { + std::sort(positions.begin(), positions.end(), [](CCPoint first, CCPoint second) { + return first.x > second.x; + }); + + if (std::holds_alternative(current) && positions.size() > 1) { + return positions.at(1); + } + + if (positions.at(0) == CCPointZero) { + return FromLevelStart(); + } + return positions.at(0); + } break; + } + return current; } \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.hpp b/src/features/StartPosSwitcher/StartPosButtonBar.hpp index 92ba23bc..49a95715 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.hpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.hpp @@ -1,23 +1,37 @@ #pragma once #include +#include +#include #include "StartPosKind.hpp" using namespace geode::prelude; +enum StartPositionSearchType { + FindHigher, + FindSmaller +}; + class StartPosButtonBar : public CCMenu { protected: LevelEditorLayer* m_editor; std::function m_callback; CCLabelBMFont* m_counterLabel = nullptr; + StartPosKind m_active = DefaultBehaviour(); + + int m_totalStartPositions = 0; + int m_activeIndex = 0; bool init(LevelEditorLayer*, std::function); + StartPosKind search(StartPosKind current, StartPositionSearchType searchType); +public: + void setStartPosCounters(StartPosKind); void onInfo(CCObject*); void onGoToStart(CCObject*); void onGoToLast(CCObject*); -public: - void setStartPosCounters(StartPosKind); + void onNext(CCObject*); + void onPrevious(CCObject*); static StartPosButtonBar* create(LevelEditorLayer* lel, std::function callback) { auto ret = new StartPosButtonBar; if (ret && ret->init(lel, callback)) { diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index fc9a49a1..9c0c8993 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -70,6 +70,7 @@ class $modify(PlayLayer) { StartPosObject* activeStartPos = nullptr; bool fromLevelStart = false; void addObject(GameObject* obj) { + PlayLayer::addObject(obj); if (std::holds_alternative(g_startPos)) { PlayLayer::addObject(obj); return; @@ -98,6 +99,16 @@ class $modify(PlayLayer) { }; class $modify(StartPosSwitchLayer, LevelEditorLayer) { + void addSpecial(GameObject* obj) { + LevelEditorLayer::addSpecial(obj); + if (!m_editorInitialising) { + auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); + if (bar) { + bar->setStartPosCounters(g_startPos); + } + } + } + void handleAction(bool idk, CCArray* idk2) { LevelEditorLayer::handleAction(idk, idk2); PlaytestHerePopup::move(); @@ -109,6 +120,12 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { auto startPos = getStartPosByPosition(pos, m_objects); if (!startPos) { Notification::create("Couldn't setup startpos switcher.", CCSprite::createWithSpriteFrameName("edit_delBtnSmall_001.png"))->show(); + g_startPos = DefaultBehaviour(); + auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); + if (bar) { + bar->setStartPosCounters(g_startPos); + } + LevelEditorLayer::setupLevelStart(obj); return; } @@ -199,6 +216,12 @@ class $modify(MyEditorUI, EditorUI) { return true; } + void onDeleteStartPos(CCObject* sender) { + EditorUI::onDeleteStartPos(sender); + g_startPos = DefaultBehaviour(); + m_fields->buttonBar->setStartPosCounters(g_startPos); + } + void deleteObject(GameObject* obj, bool filter) { EditorUI::deleteObject(obj, filter); PlaytestHerePopup::hide(); From ced8afffcb0240467dbf0fc0d411fea8e30ff93c Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:59:19 +0300 Subject: [PATCH 12/18] startpos switcher gets rewritten (part 51) --- .../StartPosSwitcher/StartPosManager.cpp | 87 +++++- .../StartPosSwitcher/StartPosManager.hpp | 13 +- .../StartPosSwitcher/PlaytestHerePopup.cpp | 8 +- .../StartPosSwitcher/PlaytestHerePopup.hpp | 10 +- .../StartPosSwitcher/StartPosButtonBar.cpp | 158 ++--------- .../StartPosSwitcher/StartPosButtonBar.hpp | 18 +- .../StartPosSwitcher/StartPosKind.hpp | 14 - .../StartPosSwitcher/StartPosSwitcher.cpp | 248 +++++++++--------- 8 files changed, 248 insertions(+), 308 deletions(-) delete mode 100644 src/features/StartPosSwitcher/StartPosKind.hpp diff --git a/src/Features/StartPosSwitcher/StartPosManager.cpp b/src/Features/StartPosSwitcher/StartPosManager.cpp index 57de3574..90181cda 100644 --- a/src/Features/StartPosSwitcher/StartPosManager.cpp +++ b/src/Features/StartPosSwitcher/StartPosManager.cpp @@ -17,6 +17,9 @@ StartPosManager* StartPosManager::get() { void StartPosManager::setStartPositions(CCArray* objects) { m_positions.clear(); m_positions.push_back(CCPointZero); + if (!objects) { + return; + } for (auto object : CCArrayExt(objects)) { if (object->m_objectID == 31) { auto skip = false; @@ -30,6 +33,12 @@ void StartPosManager::setStartPositions(CCArray* objects) { continue; } m_positions.push_back(object->getPosition()); + auto editor = LevelEditorLayer::get(); + if (m_active != CCPointZero && editor && !editor->m_editorInitialising) { + if (object->getPosition() == editor->m_editorUI->getGridSnappedPos(m_active)) { + m_active = object->getPosition(); + } + } } } @@ -39,8 +48,12 @@ void StartPosManager::setStartPositions(CCArray* objects) { m_active = CCPointZero; return; } + if (m_initialized) { + return; + } m_active = m_positions.at(m_positions.size() - 1); + m_initialized = true; } void StartPosManager::sort() { @@ -51,8 +64,11 @@ void StartPosManager::sort() { void StartPosManager::setActive(CCPoint const& pos) { bool set = false; + log::info("setting: {}, {} as active...", pos.x, pos.y); for (auto startPos : m_positions) { + log::info("comparing to: {}, {}", startPos.x, startPos.y); if (startPos == pos) { + log::info("set!"); m_active = pos; set = true; break; @@ -60,6 +76,7 @@ void StartPosManager::setActive(CCPoint const& pos) { } if (!set) { + log::info("defaulting..."); m_active = m_positions.at(m_positions.size() - 1); } } @@ -70,34 +87,55 @@ void StartPosManager::addStartPos(CCPoint const& position) { return; } } - + m_positions.push_back(position); this->sort(); + for (auto pos : m_positions) { + log::info("pos: {}, {}", pos.x, pos.y); + } + log::info("active: {}, {}", m_active.x, m_active.y); } -void StartPosManager::moveStartPos(CCPoint const& before, CCPoint const& after) { +void StartPosManager::replaceStartPos(CCPoint const& before, CCPoint const& after) { if (!LevelEditorLayer::get() || LevelEditorLayer::get()->m_editorInitialising) { return; } + // log::info("before"); + // for (auto pos : m_positions) { + // log::info("pos: {}, {}", pos.x, pos.y); + // } + // log::info("active: {}, {}", m_active.x, m_active.y); + for (auto& position : m_positions) { if (position == before) { position = after; break; } } + if (m_active == before) { + m_active = after; + } this->sort(); + // log::info("after"); + // for (auto pos : m_positions) { + // log::info("pos: {}, {}", pos.x, pos.y); + // } + // log::info("active: {}, {}", m_active.x, m_active.y); } StartPosObject* StartPosManager::getStartPosFromPoint(CCPoint const& point) { - if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising) { + if (LevelEditorLayer::get()) { for (auto object : CCArrayExt(LevelEditorLayer::get()->m_objects)) { - if ( - object->m_objectID == 31 && - object->getPosition() == point || - object->getPosition() == LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point) - ) { - return static_cast(object); + if (object->m_objectID == 31) { + if (object->getPosition() == point) { + return static_cast(object); + } + + if (!LevelEditorLayer::get()->m_editorInitialising && object->getPosition() == LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point)) { + this->replaceStartPos(point, LevelEditorLayer::get()->m_editorUI->getGridSnappedPos(point)); + return static_cast(object); + } } } @@ -140,4 +178,35 @@ void StartPosManager::previous() { void StartPosManager::clear() { m_active = CCPointZero; m_positions.clear(); + m_initialized = false; +} + +bool StartPosManager::isDefault() { + if (m_positions.size() == 0) { + return true; + } + return m_active == m_positions.at(m_positions.size() - 1); +} + +bool StartPosManager::isLevelStart() { + if (m_positions.size() == 0) { + return false; + } + return m_active == m_positions.at(0); +} + +CCPoint StartPosManager::getActive() { + return m_active; +} + +std::vector StartPosManager::getPositions() { + return m_positions; +} + +void StartPosManager::setDefault() { + m_active = m_positions.at(m_positions.size() - 1); +} + +void StartPosManager::setFirst() { + m_active = m_positions.at(0); } \ No newline at end of file diff --git a/src/Features/StartPosSwitcher/StartPosManager.hpp b/src/Features/StartPosSwitcher/StartPosManager.hpp index 17306ed0..62418c37 100644 --- a/src/Features/StartPosSwitcher/StartPosManager.hpp +++ b/src/Features/StartPosSwitcher/StartPosManager.hpp @@ -8,8 +8,9 @@ using namespace geode::prelude; class StartPosManager : public CCObject { protected: - std::vector m_positions; - CCPoint m_active; + std::vector m_positions = {}; + CCPoint m_active = CCPointZero; + bool m_initialized = false; inline static StartPosManager* s_instance; void sort(); @@ -18,9 +19,15 @@ class StartPosManager : public CCObject { void setStartPositions(CCArray* objects); void setActive(CCPoint const& pos); void addStartPos(CCPoint const& pos); - void moveStartPos(CCPoint const& before, CCPoint const& after); + void replaceStartPos(CCPoint const& before, CCPoint const& after); + bool isDefault(); + bool isLevelStart(); StartPosObject* getStartPosFromPoint(CCPoint const& pos); + CCPoint getActive(); + std::vector getPositions(); void next(); void previous(); void clear(); + void setDefault(); + void setFirst(); }; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp index 3194d8ce..fb8e9b00 100644 --- a/src/features/StartPosSwitcher/PlaytestHerePopup.cpp +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.cpp @@ -1,12 +1,12 @@ #include "PlaytestHerePopup.hpp" -bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { +bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { if (!CCNode::init()) return false; - m_callback = callback; m_editor = lel; m_startPos = startPos; + m_callback = callback; this->setZOrder(0xB00B5); @@ -49,17 +49,19 @@ bool PlaytestHerePopup::init(LevelEditorLayer* lel, StartPosObject* startPos, st void PlaytestHerePopup::onPlaytest(CCObject*) { this->select(); + m_callback(); m_editor->setStartPosObject(m_startPos); EditorUI::get()->onPlaytest(EditorUI::get()->m_playtestBtn); } void PlaytestHerePopup::onPlayInGame(CCObject*) { this->select(); + m_callback(); m_editor->m_editorUI->onPause(nullptr); EditorPauseLayer::get()->onSaveAndPlay(nullptr); EditorPauseLayer::get()->setVisible(false); } void PlaytestHerePopup::select() { - m_callback(m_startPos->getPosition()); + StartPosManager::get()->setActive(m_startPos->getPosition()); } \ No newline at end of file diff --git a/src/features/StartPosSwitcher/PlaytestHerePopup.hpp b/src/features/StartPosSwitcher/PlaytestHerePopup.hpp index 6b218cc3..e782a230 100644 --- a/src/features/StartPosSwitcher/PlaytestHerePopup.hpp +++ b/src/features/StartPosSwitcher/PlaytestHerePopup.hpp @@ -2,7 +2,7 @@ #include -#include "StartPosKind.hpp" +#include "StartPosManager.hpp" using namespace geode::prelude; @@ -11,12 +11,12 @@ class PlaytestHerePopup : public CCNode { Ref m_startPos; LevelEditorLayer* m_editor; inline static PlaytestHerePopup* s_popup = nullptr; - std::function m_callback; + std::function m_callback; - bool init(LevelEditorLayer*, StartPosObject*, std::function); + bool init(LevelEditorLayer*, StartPosObject*, std::function); static PlaytestHerePopup* create( - LevelEditorLayer* lel, StartPosObject* startPos, std::function callback + LevelEditorLayer* lel, StartPosObject* startPos, std::function callback ) { auto ret = new PlaytestHerePopup; if (ret && ret->init(lel, startPos, callback)) { @@ -41,7 +41,7 @@ class PlaytestHerePopup : public CCNode { } } - static void show(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { + static void show(LevelEditorLayer* lel, StartPosObject* startPos, std::function callback) { PlaytestHerePopup::hide(); PlaytestHerePopup::create(lel, startPos, callback); PlaytestHerePopup::move(); diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp index 5b972455..16ed84be 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.cpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -1,10 +1,9 @@ #include "StartPosButtonBar.hpp" -bool StartPosButtonBar::init(LevelEditorLayer* lel, std::function changeCallback) { +bool StartPosButtonBar::init(LevelEditorLayer* lel) { if (!CCMenu::init()) return false; - m_callback = changeCallback; m_editor = lel; auto winSize = CCDirector::get()->getWinSize(); @@ -30,9 +29,10 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel, std::functionsetScale(0.6f); counterLabel->setPosition({ m_obContentSize.width / 2, m_obContentSize.height / 2 - 25.f }); counterLabel->limitLabelWidth(80.0f, 0.6f, 0.4f); + counterLabel->setString("0 / 0"); m_counterLabel = counterLabel; this->addChild(counterLabel); - this->setStartPosCounters(DefaultBehaviour()); + this->setStartPosCounters(); auto goToLastSpr = ButtonSprite::create( CCSprite::createWithSpriteFrameName("last-start-pos.png"_spr), @@ -87,63 +87,47 @@ void StartPosButtonBar::onInfo(CCObject*) { } void StartPosButtonBar::onGoToStart(CCObject*) { - m_callback(FromLevelStart()); - this->setStartPosCounters(FromLevelStart()); + StartPosManager::get()->setFirst(); + this->setStartPosCounters(); } void StartPosButtonBar::onGoToLast(CCObject*) { - m_callback(DefaultBehaviour()); - this->setStartPosCounters(DefaultBehaviour()); + StartPosManager::get()->setDefault(); + this->setStartPosCounters(); } -void StartPosButtonBar::setStartPosCounters(StartPosKind startPos) { +void StartPosButtonBar::setStartPosCounters() { m_totalStartPositions = 0; m_activeIndex = 0; if (!m_editor || !m_editor->m_objects) { return; } - CCArrayExt startPositions; + m_totalStartPositions = StartPosManager::get()->getPositions().size() - 1; + auto active = StartPosManager::get()->getActive(); - for (auto object : CCArrayExt(m_editor->m_objects)) { - if (object->m_objectID != 31) { - continue; - } + bool found = false; - startPositions.push_back(static_cast(object)); - m_totalStartPositions++; + if (StartPosManager::get()->isDefault()) { + m_activeIndex = m_totalStartPositions; + found = true; + } + + if (StartPosManager::get()->isLevelStart() && !found) { + m_activeIndex = 0; + found = true; } - if (std::holds_alternative(startPos) && m_activeIndex == 0) { - auto pos = std::get(startPos); - std::sort(startPositions.begin(), startPositions.end(), [](StartPosObject* x, StartPosObject* y){ - return x->getPositionX() < y->getPositionX(); - }); + if (!found) { int i = 0; - for (auto object : startPositions) { - i++; - if (object->getPosition() == pos || object->getPosition() == m_editor->m_editorUI->getGridSnappedPos(pos)) { - if (object->getPosition() == pos) { - m_activeIndex = i; - m_active = startPos; - } else if (!m_editor->m_editorInitialising && object->getPosition() == m_editor->m_editorUI->getGridSnappedPos(pos)) { - m_activeIndex = i; - m_active = startPos; - } + for (auto pos : StartPosManager::get()->getPositions()) { + if (pos == active) { + m_activeIndex = i; } + i++; } } - if (std::holds_alternative(startPos)) { - m_activeIndex = m_totalStartPositions; - m_active = startPos; - } - - if (std::holds_alternative(startPos)) { - m_activeIndex = 0; - m_active = startPos; - } - std::string str = std::to_string(m_activeIndex) + " / " + std::to_string(m_totalStartPositions); m_counterLabel->setString(str.c_str()); @@ -153,102 +137,16 @@ void StartPosButtonBar::onNext(CCObject*) { if (m_activeIndex == m_totalStartPositions) { return; } - if (std::holds_alternative(m_active)) { - return; - } - log::info("active: {}", m_activeIndex); - auto found = this->search(m_active, FindHigher); - m_callback(found); - this->setStartPosCounters(found); + StartPosManager::get()->next(); + this->setStartPosCounters(); } void StartPosButtonBar::onPrevious(CCObject*) { if (m_activeIndex == 0) { return; } - if (std::holds_alternative(m_active)) { - return; - } - if (m_activeIndex == 1) { - m_active = FromLevelStart(); - m_callback(m_active); - this->setStartPosCounters(m_active); - } - - auto found = this->search(m_active, FindSmaller); - m_callback(found); - this->setStartPosCounters(found); -} - -StartPosKind StartPosButtonBar::search(StartPosKind current, StartPositionSearchType searchType) { - std::vector positions; - - for (auto object : CCArrayExt(m_editor->m_objects)) { - if (object->m_objectID == 31) { - switch (searchType) { - case StartPositionSearchType::FindHigher: { - if (std::holds_alternative(current)) { - return current; - } - if (std::holds_alternative(current)) { - positions.push_back(object->getPosition()); - continue; - } - if (object->getPositionX() > std::get(current).x) { - positions.push_back(object->getPosition()); - } - } break; - case StartPositionSearchType::FindSmaller: { - if (std::holds_alternative(current)) { - return current; - } - if (std::holds_alternative(current)) { - positions.push_back(object->getPosition()); - continue; - } - if (object->getPositionX() < std::get(current).x) { - positions.push_back(object->getPosition()); - } - } break; - } - } - } - - for (auto pos : positions) { - log::info("position: {}, {}", pos.x, pos.y); - } - if (searchType == StartPositionSearchType::FindSmaller) { - positions.push_back(CCPointZero); - } - - if (positions.size() == 0) { - return current; - } - - switch (searchType) { - case StartPositionSearchType::FindHigher: { - std::sort(positions.begin(), positions.end(), [](CCPoint first, CCPoint second) { - return first.x < second.x; - }); - - return positions.at(0); - } break; - case StartPositionSearchType::FindSmaller: { - std::sort(positions.begin(), positions.end(), [](CCPoint first, CCPoint second) { - return first.x > second.x; - }); - - if (std::holds_alternative(current) && positions.size() > 1) { - return positions.at(1); - } - - if (positions.at(0) == CCPointZero) { - return FromLevelStart(); - } - return positions.at(0); - } break; - } - return current; + StartPosManager::get()->previous(); + this->setStartPosCounters(); } \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.hpp b/src/features/StartPosSwitcher/StartPosButtonBar.hpp index 49a95715..8ad2eae0 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.hpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.hpp @@ -4,37 +4,29 @@ #include #include -#include "StartPosKind.hpp" +#include "StartPosManager.hpp" using namespace geode::prelude; -enum StartPositionSearchType { - FindHigher, - FindSmaller -}; - class StartPosButtonBar : public CCMenu { protected: LevelEditorLayer* m_editor; - std::function m_callback; CCLabelBMFont* m_counterLabel = nullptr; - StartPosKind m_active = DefaultBehaviour(); int m_totalStartPositions = 0; int m_activeIndex = 0; - bool init(LevelEditorLayer*, std::function); - StartPosKind search(StartPosKind current, StartPositionSearchType searchType); + bool init(LevelEditorLayer*); public: - void setStartPosCounters(StartPosKind); + void setStartPosCounters(); void onInfo(CCObject*); void onGoToStart(CCObject*); void onGoToLast(CCObject*); void onNext(CCObject*); void onPrevious(CCObject*); - static StartPosButtonBar* create(LevelEditorLayer* lel, std::function callback) { + static StartPosButtonBar* create(LevelEditorLayer* lel) { auto ret = new StartPosButtonBar; - if (ret && ret->init(lel, callback)) { + if (ret && ret->init(lel)) { ret->autorelease(); return ret; } diff --git a/src/features/StartPosSwitcher/StartPosKind.hpp b/src/features/StartPosSwitcher/StartPosKind.hpp deleted file mode 100644 index 7f87b119..00000000 --- a/src/features/StartPosSwitcher/StartPosKind.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -using namespace geode::prelude; - -struct FromLevelStart {}; -struct DefaultBehaviour {}; -using FromPoint = CCPoint; -using StartPosKind = std::variant< - FromLevelStart, - DefaultBehaviour, - FromPoint ->; \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 9c0c8993..6f7f8031 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -13,98 +13,66 @@ #undef max #include "PlaytestHerePopup.hpp" -#include "StartPosKind.hpp" #include "StartPosButtonBar.hpp" +#include "StartPosManager.hpp" using namespace geode::prelude; using namespace editor_api; -StartPosKind g_startPos = DefaultBehaviour(); - -bool match(CCPoint const& pos) { - if (std::holds_alternative(g_startPos)) { - return std::get(g_startPos) == pos; - } - return false; -} - -/** - * Fetches the startpos that matches the given position. If objects is passed, it searches the objects, if not it tries to look in LevelEditorLayer and PlayLayer. - * If none is found, returns nullptr. -*/ -StartPosObject* getStartPosByPosition(CCPoint const& pos, CCArray* objects) { - if (!objects) { - auto levelEdit = LevelEditorLayer::get(); - if (levelEdit) { - objects = levelEdit->m_objects; - } - - auto playLayer = PlayLayer::get(); - if (!objects && playLayer) { - objects = playLayer->m_objects; - } - - if (!objects) { - return nullptr; - } - } - - for (auto object : CCArrayExt(objects)) { - auto editorUI = EditorUI::get(); - if (object->m_objectID == 31) { - if (object->getPosition() == pos) { - return static_cast(object); - } - // attention please... THE HACKIEST FIX!! - if (editorUI && object->getPosition() == editorUI->getGridSnappedPos(pos)) { - g_startPos = editorUI->getGridSnappedPos(pos); - return static_cast(object); - } - } - } - - return nullptr; -} +struct RecalculateStartPosData { + CCPoint original; + CCPoint newPoint; +}; class $modify(PlayLayer) { StartPosObject* activeStartPos = nullptr; bool fromLevelStart = false; void addObject(GameObject* obj) { PlayLayer::addObject(obj); - if (std::holds_alternative(g_startPos)) { - PlayLayer::addObject(obj); + if (obj->m_objectID != 31) { + return; + } + if (StartPosManager::get()->isDefault()) { + return; + } + if (StartPosManager::get()->isLevelStart()) { + m_startPos = nullptr; + m_playerStartPosition = CCPointZero; return; } - PlayLayer::addObject(obj); - if (obj->m_objectID == 31) { - if (match(obj->getPosition())) { - m_fields->activeStartPos = static_cast(obj); - } - if (std::holds_alternative(g_startPos)) { - m_fields->fromLevelStart = true; - } - if (m_fields->fromLevelStart) { - m_startPos = nullptr; - m_playerStartPosition = CCPointZero; - return; - } + auto active = StartPosManager::get()->getActive(); + if (obj->getPosition() == StartPosManager::get()->getActive() && !m_fields->activeStartPos) { + m_fields->activeStartPos = static_cast(obj); + } - if (m_fields->activeStartPos) { - m_startPos = m_fields->activeStartPos; - m_playerStartPosition = m_fields->activeStartPos->getPosition(); - } + if (m_fields->activeStartPos) { + m_startPos = m_fields->activeStartPos; + m_playerStartPosition = m_fields->activeStartPos->getPosition(); } } }; class $modify(StartPosSwitchLayer, LevelEditorLayer) { + bool init(GJGameLevel* level) { + if (!LevelEditorLayer::init(level)) { + return false; + } + StartPosManager::get()->setStartPositions(m_objects); + auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); + if (bar) { + bar->setStartPosCounters(); + } + + return true; + } void addSpecial(GameObject* obj) { LevelEditorLayer::addSpecial(obj); - if (!m_editorInitialising) { + if (!m_editorInitialising && obj->m_objectID == 31) { + StartPosManager::get()->addStartPos(obj->getPosition()); auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); if (bar) { - bar->setStartPosCounters(g_startPos); + bar->setStartPosCounters(); } } } @@ -115,30 +83,8 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { } void setupLevelStart(LevelSettingsObject* obj) { - if (std::holds_alternative(g_startPos)) { - auto pos = std::get(g_startPos); - auto startPos = getStartPosByPosition(pos, m_objects); - if (!startPos) { - Notification::create("Couldn't setup startpos switcher.", CCSprite::createWithSpriteFrameName("edit_delBtnSmall_001.png"))->show(); - g_startPos = DefaultBehaviour(); - auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); - if (bar) { - bar->setStartPosCounters(g_startPos); - } - - LevelEditorLayer::setupLevelStart(obj); - return; - } - this->setStartPosObject(startPos); - m_player1->setStartPos(startPos->getPosition()); - m_player1->resetObject(); - m_player2->setStartPos(startPos->getPosition()); - m_player2->resetObject(); - - LevelEditorLayer::setupLevelStart(startPos->m_levelSettings); - return; - } - if (std::holds_alternative(g_startPos)) { + StartPosManager::get()->setStartPositions(m_objects); + if (StartPosManager::get()->isLevelStart()) { this->setStartPosObject(nullptr); m_player1->setStartPos(CCPointZero); m_player1->resetObject(); @@ -147,8 +93,25 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { LevelEditorLayer::setupLevelStart(m_levelSettings); return; } - // default behaviour - LevelEditorLayer::setupLevelStart(obj); + auto active = StartPosManager::get()->getActive(); + auto startPos = StartPosManager::get()->getStartPosFromPoint(active); + if (!startPos) { + Notification::create("Couldn't setup Start Position switcher.", CCSprite::createWithSpriteFrameName("edit_delBtnSmall_001.png"))->show(); + StartPosManager::get()->setDefault(); + auto bar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); + if (bar) { + bar->setStartPosCounters(); + } + LevelEditorLayer::setupLevelStart(obj); + return; + } + + this->setStartPosObject(startPos); + m_player1->setStartPos(startPos->getPosition()); + m_player1->resetObject(); + m_player2->setStartPos(startPos->getPosition()); + m_player2->resetObject(); + LevelEditorLayer::setupLevelStart(startPos->m_levelSettings); } }; @@ -160,7 +123,7 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { class $modify(GameManager) { void returnToLastScene(GJGameLevel* level) { GameManager::returnToLastScene(level); - g_startPos = DefaultBehaviour(); + StartPosManager::get()->clear(); } }; @@ -175,32 +138,31 @@ class $modify(EditorPauseLayer) { } void onSaveAndPlay(CCObject* sender) { - if (std::holds_alternative(g_startPos)) { - auto startpos = getStartPosByPosition(std::get(g_startPos), m_editorLayer->m_objects); - } + StartPosManager::get()->setStartPositions(m_editorLayer->m_objects); EditorPauseLayer::onSaveAndPlay(sender); } void onExitEditor(CCObject* sender) { EditorPauseLayer::onExitEditor(sender); - g_startPos = DefaultBehaviour(); + StartPosManager::get()->clear(); } void onSaveAndExit(CCObject* sender) { EditorPauseLayer::onSaveAndExit(sender); - g_startPos = DefaultBehaviour(); + StartPosManager::get()->clear(); } }; class $modify(MyEditorUI, EditorUI) { StartPosButtonBar* buttonBar = nullptr; + + bool holding = false; + std::unordered_map startPosOriginals; bool init(LevelEditorLayer* lel) { - if (!EditorUI::init(lel)) + if (!EditorUI::init(lel)) { return false; - - auto buttonBar = StartPosButtonBar::create(m_editorLayer, [](StartPosKind startPos) { - g_startPos = startPos; - }); + } + auto buttonBar = StartPosButtonBar::create(m_editorLayer); buttonBar->setID("start-pos-button-bar"_spr); m_fields->buttonBar = buttonBar; @@ -208,30 +170,27 @@ class $modify(MyEditorUI, EditorUI) { "start-pos-border.png"_spr, buttonBar ); - - if (std::holds_alternative(g_startPos)) { - return true; - } - buttonBar->setStartPosCounters(g_startPos); return true; } void onDeleteStartPos(CCObject* sender) { EditorUI::onDeleteStartPos(sender); - g_startPos = DefaultBehaviour(); - m_fields->buttonBar->setStartPosCounters(g_startPos); + StartPosManager::get()->clear(); + StartPosManager::get()->setStartPositions(nullptr); + m_fields->buttonBar->setStartPosCounters(); } void deleteObject(GameObject* obj, bool filter) { EditorUI::deleteObject(obj, filter); PlaytestHerePopup::hide(); - if (obj->m_objectID == 31 && std::holds_alternative(g_startPos)) { - auto pos = std::get(g_startPos); - if (obj->getPosition() == pos) { - g_startPos = DefaultBehaviour(); + if (obj->m_objectID == 31) { + auto manager = StartPosManager::get(); + if (obj->getPosition() == manager->getActive()) { + manager->clear(); } + manager->setStartPositions(m_editorLayer->m_objects); + m_fields->buttonBar->setStartPosCounters(); } - m_fields->buttonBar->setStartPosCounters(g_startPos); } void selectObjects(CCArray* objects, bool ignoreFilters) { @@ -245,21 +204,49 @@ class $modify(MyEditorUI, EditorUI) { } void moveObject(GameObject* obj, CCPoint pos) { - bool movedActiveStartPos = false; - if (!m_editorLayer->m_editorInitialising) { - if (obj->m_objectID == 31 && std::holds_alternative(g_startPos)) { - auto position = std::get(g_startPos); - if (position == obj->getPosition()) { - movedActiveStartPos = true; - } - } + if (m_editorLayer->m_editorInitialising) { + EditorUI::moveObject(obj, pos); + PlaytestHerePopup::move(); + return; + } + bool movedStartPos = false; + CCPoint original; + if (obj->m_objectID == 31) { + movedStartPos = true; + original = obj->getPosition(); } EditorUI::moveObject(obj, pos); PlaytestHerePopup::move(); - if (movedActiveStartPos) { - g_startPos = obj->getPosition(); - m_fields->buttonBar->setStartPosCounters(g_startPos); + if (movedStartPos) { + // if holding, add to map and recalculate once when touch ends + if (m_fields->holding && !m_fields->startPosOriginals.contains(obj)) { + m_fields->startPosOriginals[obj] = original; + return; + } + if (m_fields->holding) { + return; + } + StartPosManager::get()->replaceStartPos(original, obj->getPosition()); + m_fields->buttonBar->setStartPosCounters(); + } + } + + // doing moving with freemove a lil better + bool ccTouchBegan(CCTouch* touch, CCEvent* event) { + auto ret = EditorUI::ccTouchBegan(touch, event); + m_fields->holding = true; + return ret; + } + + // if we are free moving any startpos, update the manager and clear the map + void ccTouchEnded(CCTouch* touch, CCEvent* event) { + EditorUI::ccTouchEnded(touch, event); + for (const auto& kv : m_fields->startPosOriginals) { + StartPosManager::get()->replaceStartPos(kv.second, kv.first->getPosition()); } + m_fields->buttonBar->setStartPosCounters(); + m_fields->startPosOriginals.clear(); + m_fields->holding = false; } void moveObjectCall(EditCommand cmd) { @@ -281,11 +268,10 @@ class $modify(GameObject) { PlaytestHerePopup::show( LevelEditorLayer::get(), static_cast(as(this)), - [this](StartPosKind startPos) { - g_startPos = startPos; + [this]() { auto buttonBar = static_cast(LevelEditorLayer::get()->m_editorUI->getChildByID("start-pos-button-bar"_spr)); if (buttonBar) { - buttonBar->setStartPosCounters(startPos); + buttonBar->setStartPosCounters(); } } ); From 3e810a4967a036d1644b37b4391551fd210a8c10 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:06:57 +0300 Subject: [PATCH 13/18] fix switching to edit tab on freemove touchEnded --- src/features/StartPosSwitcher/StartPosSwitcher.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 6f7f8031..8e62c7f8 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -235,6 +235,11 @@ class $modify(MyEditorUI, EditorUI) { bool ccTouchBegan(CCTouch* touch, CCEvent* event) { auto ret = EditorUI::ccTouchBegan(touch, event); m_fields->holding = true; + + // bad fix for bad bug :) + if (m_freeMoveEnabled) { + MoreTabs::get(this, false)->switchEditTab(1); + } return ret; } From 4aef14787f432a3f3e3a91c69e423dbd6cb372f6 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:40:50 +0300 Subject: [PATCH 14/18] optimize smart startpos --- src/features/SmartStartPos.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/features/SmartStartPos.cpp b/src/features/SmartStartPos.cpp index 8343158b..080297b9 100644 --- a/src/features/SmartStartPos.cpp +++ b/src/features/SmartStartPos.cpp @@ -220,7 +220,8 @@ class $modify(LevelEditorLayer) { speedChanges.push_back(g); break; } - - SSPsetup(); + if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising) { + this->setupStartPos(static_cast(g)); + } } }; \ No newline at end of file From fa9656f7730c056e564854e7cdbbc5313ddc3e2d Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:41:29 +0300 Subject: [PATCH 15/18] delete fix + label fix + byebye logs --- .../StartPosSwitcher/StartPosManager.cpp | 19 ------------ .../StartPosSwitcher/StartPosButtonBar.cpp | 3 +- .../StartPosSwitcher/StartPosSwitcher.cpp | 31 +++++++++++-------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/Features/StartPosSwitcher/StartPosManager.cpp b/src/Features/StartPosSwitcher/StartPosManager.cpp index 90181cda..975a664b 100644 --- a/src/Features/StartPosSwitcher/StartPosManager.cpp +++ b/src/Features/StartPosSwitcher/StartPosManager.cpp @@ -64,11 +64,8 @@ void StartPosManager::sort() { void StartPosManager::setActive(CCPoint const& pos) { bool set = false; - log::info("setting: {}, {} as active...", pos.x, pos.y); for (auto startPos : m_positions) { - log::info("comparing to: {}, {}", startPos.x, startPos.y); if (startPos == pos) { - log::info("set!"); m_active = pos; set = true; break; @@ -76,7 +73,6 @@ void StartPosManager::setActive(CCPoint const& pos) { } if (!set) { - log::info("defaulting..."); m_active = m_positions.at(m_positions.size() - 1); } } @@ -89,10 +85,6 @@ void StartPosManager::addStartPos(CCPoint const& position) { } m_positions.push_back(position); this->sort(); - for (auto pos : m_positions) { - log::info("pos: {}, {}", pos.x, pos.y); - } - log::info("active: {}, {}", m_active.x, m_active.y); } void StartPosManager::replaceStartPos(CCPoint const& before, CCPoint const& after) { @@ -100,12 +92,6 @@ void StartPosManager::replaceStartPos(CCPoint const& before, CCPoint const& afte return; } - // log::info("before"); - // for (auto pos : m_positions) { - // log::info("pos: {}, {}", pos.x, pos.y); - // } - // log::info("active: {}, {}", m_active.x, m_active.y); - for (auto& position : m_positions) { if (position == before) { position = after; @@ -117,11 +103,6 @@ void StartPosManager::replaceStartPos(CCPoint const& before, CCPoint const& afte } this->sort(); - // log::info("after"); - // for (auto pos : m_positions) { - // log::info("pos: {}, {}", pos.x, pos.y); - // } - // log::info("active: {}, {}", m_active.x, m_active.y); } StartPosObject* StartPosManager::getStartPosFromPoint(CCPoint const& point) { diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp index 16ed84be..4ba1e313 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.cpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -28,7 +28,7 @@ bool StartPosButtonBar::init(LevelEditorLayer* lel) { auto counterLabel = CCLabelBMFont::create("", "bigFont.fnt"); counterLabel->setScale(0.6f); counterLabel->setPosition({ m_obContentSize.width / 2, m_obContentSize.height / 2 - 25.f }); - counterLabel->limitLabelWidth(80.0f, 0.6f, 0.4f); + counterLabel->limitLabelWidth(60.0f, 0.6f, 0.1f); counterLabel->setString("0 / 0"); m_counterLabel = counterLabel; this->addChild(counterLabel); @@ -131,6 +131,7 @@ void StartPosButtonBar::setStartPosCounters() { std::string str = std::to_string(m_activeIndex) + " / " + std::to_string(m_totalStartPositions); m_counterLabel->setString(str.c_str()); + m_counterLabel->limitLabelWidth(60.0f, 0.6f, 0.1f); } void StartPosButtonBar::onNext(CCObject*) { diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index 8e62c7f8..f167e846 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -77,6 +77,24 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { } } + void removeSpecial(GameObject* obj) { + LevelEditorLayer::removeSpecial(obj); + + if (obj->m_objectID != 31) { + return; + } + PlaytestHerePopup::hide(); + if (obj->m_objectID == 31) { + auto manager = StartPosManager::get(); + if (obj->getPosition() == manager->getActive()) { + manager->clear(); + } + manager->setStartPositions(m_objects); + auto buttonBar = static_cast(m_editorUI->getChildByID("start-pos-button-bar"_spr)); + buttonBar->setStartPosCounters(); + } + } + void handleAction(bool idk, CCArray* idk2) { LevelEditorLayer::handleAction(idk, idk2); PlaytestHerePopup::move(); @@ -180,19 +198,6 @@ class $modify(MyEditorUI, EditorUI) { m_fields->buttonBar->setStartPosCounters(); } - void deleteObject(GameObject* obj, bool filter) { - EditorUI::deleteObject(obj, filter); - PlaytestHerePopup::hide(); - if (obj->m_objectID == 31) { - auto manager = StartPosManager::get(); - if (obj->getPosition() == manager->getActive()) { - manager->clear(); - } - manager->setStartPositions(m_editorLayer->m_objects); - m_fields->buttonBar->setStartPosCounters(); - } - } - void selectObjects(CCArray* objects, bool ignoreFilters) { EditorUI::selectObjects(objects, ignoreFilters); PlaytestHerePopup::hide(); From 682068d07e6cc21e95244fd23c1cc069fe49edc1 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:13:19 +0300 Subject: [PATCH 16/18] oops --- src/features/SmartStartPos.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/SmartStartPos.cpp b/src/features/SmartStartPos.cpp index 080297b9..9d80d6e5 100644 --- a/src/features/SmartStartPos.cpp +++ b/src/features/SmartStartPos.cpp @@ -220,7 +220,7 @@ class $modify(LevelEditorLayer) { speedChanges.push_back(g); break; } - if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising) { + if (LevelEditorLayer::get() && !LevelEditorLayer::get()->m_editorInitialising && g->m_objectID == 31) { this->setupStartPos(static_cast(g)); } } From 4d92e141ef1b4be05e66a35b570be0cec47e73ed Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:14:21 +0300 Subject: [PATCH 17/18] fix some playtest stuff and unwanted ccTouchEnded --- .../StartPosSwitcher/StartPosSwitcher.cpp | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/features/StartPosSwitcher/StartPosSwitcher.cpp b/src/features/StartPosSwitcher/StartPosSwitcher.cpp index f167e846..09c17139 100644 --- a/src/features/StartPosSwitcher/StartPosSwitcher.cpp +++ b/src/features/StartPosSwitcher/StartPosSwitcher.cpp @@ -19,23 +19,21 @@ using namespace geode::prelude; using namespace editor_api; -struct RecalculateStartPosData { - CCPoint original; - CCPoint newPoint; -}; +bool g_enteringLevel = false; class $modify(PlayLayer) { StartPosObject* activeStartPos = nullptr; bool fromLevelStart = false; void addObject(GameObject* obj) { + g_enteringLevel = false; PlayLayer::addObject(obj); if (obj->m_objectID != 31) { return; } - if (StartPosManager::get()->isDefault()) { + if (StartPosManager::get()->isDefault() && !m_fields->activeStartPos) { return; } - if (StartPosManager::get()->isLevelStart()) { + if (StartPosManager::get()->isLevelStart() && !m_fields->activeStartPos) { m_startPos = nullptr; m_playerStartPosition = CCPointZero; return; @@ -79,10 +77,12 @@ class $modify(StartPosSwitchLayer, LevelEditorLayer) { void removeSpecial(GameObject* obj) { LevelEditorLayer::removeSpecial(obj); - if (obj->m_objectID != 31) { return; } + if (g_enteringLevel) { + return; + } PlaytestHerePopup::hide(); if (obj->m_objectID == 31) { auto manager = StartPosManager::get(); @@ -157,6 +157,7 @@ class $modify(EditorPauseLayer) { void onSaveAndPlay(CCObject* sender) { StartPosManager::get()->setStartPositions(m_editorLayer->m_objects); + g_enteringLevel = true; EditorPauseLayer::onSaveAndPlay(sender); } @@ -251,12 +252,14 @@ class $modify(MyEditorUI, EditorUI) { // if we are free moving any startpos, update the manager and clear the map void ccTouchEnded(CCTouch* touch, CCEvent* event) { EditorUI::ccTouchEnded(touch, event); - for (const auto& kv : m_fields->startPosOriginals) { - StartPosManager::get()->replaceStartPos(kv.second, kv.first->getPosition()); + if (m_fields->holding && !m_fields->startPosOriginals.empty()) { + for (const auto& kv : m_fields->startPosOriginals) { + StartPosManager::get()->replaceStartPos(kv.second, kv.first->getPosition()); + } + m_fields->buttonBar->setStartPosCounters(); + m_fields->startPosOriginals.clear(); + m_fields->holding = false; } - m_fields->buttonBar->setStartPosCounters(); - m_fields->startPosOriginals.clear(); - m_fields->holding = false; } void moveObjectCall(EditCommand cmd) { From c1ad19cbf3e5cbaba807b7f51d8aa635e01c9741 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Tue, 10 Oct 2023 21:04:34 +0300 Subject: [PATCH 18/18] add camera movement when switching startpositions --- api/include/MoreTabs.hpp | 6 ++++++ api/src/MoreTabs.cpp | 6 ------ src/features/StartPosSwitcher/StartPosButtonBar.cpp | 5 +++++ src/features/StartPosSwitcher/StartPosButtonBar.hpp | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/api/include/MoreTabs.hpp b/api/include/MoreTabs.hpp index a14aff1a..6c436665 100644 --- a/api/include/MoreTabs.hpp +++ b/api/include/MoreTabs.hpp @@ -4,6 +4,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include using namespace geode::prelude; diff --git a/api/src/MoreTabs.cpp b/api/src/MoreTabs.cpp index 36d5155b..2ab5aae6 100644 --- a/api/src/MoreTabs.cpp +++ b/api/src/MoreTabs.cpp @@ -1,10 +1,4 @@ #include -#include -#include -#include -#include -#include -#include using namespace keybinds; using namespace editor_api; diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.cpp b/src/features/StartPosSwitcher/StartPosButtonBar.cpp index 4ba1e313..3fbdda0b 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.cpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.cpp @@ -141,6 +141,7 @@ void StartPosButtonBar::onNext(CCObject*) { StartPosManager::get()->next(); this->setStartPosCounters(); + editor_api::moveGameLayerTo(m_editor->m_editorUI, StartPosManager::get()->getStartPosFromPoint(StartPosManager::get()->getActive())); } void StartPosButtonBar::onPrevious(CCObject*) { @@ -150,4 +151,8 @@ void StartPosButtonBar::onPrevious(CCObject*) { StartPosManager::get()->previous(); this->setStartPosCounters(); + if (StartPosManager::get()->getActive() == CCPointZero) { + return; + } + editor_api::moveGameLayerTo(m_editor->m_editorUI, StartPosManager::get()->getStartPosFromPoint(StartPosManager::get()->getActive())); } \ No newline at end of file diff --git a/src/features/StartPosSwitcher/StartPosButtonBar.hpp b/src/features/StartPosSwitcher/StartPosButtonBar.hpp index 8ad2eae0..c0b32596 100644 --- a/src/features/StartPosSwitcher/StartPosButtonBar.hpp +++ b/src/features/StartPosSwitcher/StartPosButtonBar.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "StartPosManager.hpp"