Skip to content

Commit

Permalink
add type in Z layer input, functionality coming later when i have RE'd
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Feb 8, 2024
1 parent 0017e1f commit def5dba
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
26 changes: 26 additions & 0 deletions src/features/TypeInZLayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#include <Geode/modify/EditorUI.hpp>
#include <utils/EditableBMLabelProxy.hpp>

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;
}
};
61 changes: 61 additions & 0 deletions src/utils/EditableBMLabelProxy.cpp
Original file line number Diff line number Diff line change
@@ -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<void(std::string const&)> 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;
}
}
48 changes: 48 additions & 0 deletions src/utils/EditableBMLabelProxy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <Geode/DefaultInclude.hpp>
#include <Geode/utils/cocos.hpp>

using namespace geode::prelude;

class EditableBMLabelProxy : public CCLabelBMFont, TextInputDelegate {
protected:
Ref<InputNode> m_input = nullptr;
std::function<void(std::string const&)> m_setValue = nullptr;
bool m_ignoreLabelUpdate = false;

static EditableBMLabelProxy* create();

public:
static EditableBMLabelProxy* replace(
CCLabelBMFont* existing,
float width, std::string const& placeholder,
std::function<void(std::string const&)> 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;
};

0 comments on commit def5dba

Please sign in to comment.