diff --git a/CMakeLists.txt b/CMakeLists.txt index c74baf14..5492884c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ project(BetterEdit VERSION 1.0.0) file(GLOB SOURCES src/features/*.cpp src/features/scaling/*.cpp + src/utils/*.cpp src/*.cpp ) diff --git a/src/features/TypeInZLayer.cpp b/src/features/TypeInZLayer.cpp new file mode 100644 index 00000000..9beeba96 --- /dev/null +++ b/src/features/TypeInZLayer.cpp @@ -0,0 +1,26 @@ + +#include +#include + +using namespace geode::prelude; + +class $modify(EditorUI) { + bool init(LevelEditorLayer* lel) { + if (!EditorUI::init(lel)) + return false; + + m_currentLayerLabel = EditableBMLabelProxy::replace( + m_currentLayerLabel, 40.f, "Z", + [this](auto str) { + // try { + // m_editorLayer->m_currentLayer = std::stoi(str); + // } + // catch(...) { + // m_editorLayer->m_currentLayer = -1; + // } + } + ); + + return true; + } +}; diff --git a/src/utils/EditableBMLabelProxy.cpp b/src/utils/EditableBMLabelProxy.cpp new file mode 100644 index 00000000..ab667e24 --- /dev/null +++ b/src/utils/EditableBMLabelProxy.cpp @@ -0,0 +1,61 @@ +#include "EditableBMLabelProxy.hpp" + +EditableBMLabelProxy* EditableBMLabelProxy::create() { + auto ret = new EditableBMLabelProxy(); + if (ret && ret->init()) { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; +} + +EditableBMLabelProxy* EditableBMLabelProxy::replace( + CCLabelBMFont* existing, + float width, std::string const& placeholder, + std::function setValue +) { + auto proxy = EditableBMLabelProxy::create(); + + proxy->m_setValue = setValue; + proxy->m_input = InputNode::create(width, placeholder.c_str()); + proxy->m_input->ignoreAnchorPointForPosition(false); + proxy->m_input->getInput()->setDelegate(proxy); + // 2021 Fod was sadistic for not doing this + proxy->m_input->setContentSize(proxy->m_input->getBG()->getScaledContentSize()); + existing->getParent()->addChild(proxy->m_input); + + proxy->setString(existing->getString()); + proxy->setScale(existing->getScale()); + proxy->setZOrder(existing->getZOrder()); + proxy->setPosition(existing->getPosition()); + proxy->setAnchorPoint(existing->getAnchorPoint()); + proxy->setID(existing->getID()); + proxy->setTag(existing->getTag()); + + existing->getParent()->addChild(proxy); + existing->removeFromParent(); + + return proxy; +} + +void EditableBMLabelProxy::updateLabel() {} + +void EditableBMLabelProxy::setString(const char* str) { + this->setString(str, true); +} + +void EditableBMLabelProxy::setString(const char* str, bool needUpdateLabel) { + CCLabelBMFont::setString(str, needUpdateLabel); + if (m_input && !m_ignoreLabelUpdate) { + m_input->setString(str); + } +} + +void EditableBMLabelProxy::textChanged(CCTextInputNode*) { + if (m_setValue && !m_ignoreLabelUpdate) { + m_ignoreLabelUpdate = true; + m_setValue(m_input->getString()); + m_ignoreLabelUpdate = false; + } +} diff --git a/src/utils/EditableBMLabelProxy.hpp b/src/utils/EditableBMLabelProxy.hpp new file mode 100644 index 00000000..7caf878c --- /dev/null +++ b/src/utils/EditableBMLabelProxy.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +using namespace geode::prelude; + +class EditableBMLabelProxy : public CCLabelBMFont, TextInputDelegate { +protected: + Ref m_input = nullptr; + std::function m_setValue = nullptr; + bool m_ignoreLabelUpdate = false; + + static EditableBMLabelProxy* create(); + +public: + static EditableBMLabelProxy* replace( + CCLabelBMFont* existing, + float width, std::string const& placeholder, + std::function setValue + ); + +#define PROXY_FWD_1(fun, param_ty) \ + void fun(param_ty x) override { \ + CCLabelBMFont::fun(x); \ + if (m_input) m_input->fun(x); \ + } + + // PROXY_FWD_1(setContentSize, CCSize const&); + PROXY_FWD_1(setAnchorPoint, CCPoint const&); + PROXY_FWD_1(setPosition, CCPoint const&); + PROXY_FWD_1(setRotation, float); + PROXY_FWD_1(setRotationX, float); + PROXY_FWD_1(setRotationY, float); + PROXY_FWD_1(setScale, float); + PROXY_FWD_1(setScaleX, float); + PROXY_FWD_1(setScaleY, float); + PROXY_FWD_1(setVisible, bool); + PROXY_FWD_1(setSkewX, float); + PROXY_FWD_1(setSkewY, float); + PROXY_FWD_1(setZOrder, int); + + void updateLabel() override; + void setString(const char* str) override; + void setString(const char* str, bool needUpdateLabel) override; + + void textChanged(CCTextInputNode*) override; +};