inputnode has been very fun

This commit is contained in:
altalk23 2024-01-27 21:09:57 +03:00
parent b4f4ddd664
commit bb1fcbe678
4 changed files with 57 additions and 43 deletions

View file

@ -1 +1 @@
2.0.0-beta.10
2.0.0-beta.11

View file

@ -5,15 +5,6 @@
#include <cocos2d.h>
namespace geode {
class GEODE_DLL TextInputNodeFix : public CCTextInputNode {
public:
static TextInputNodeFix* create(
float width, float height, char const* placeholder, char const* fallbackFont, int size, char const* fontFile
);
bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) override;
};
class GEODE_DLL InputNode : public cocos2d::CCMenuItem {
protected:
cocos2d::extension::CCScale9Sprite* m_bgSprite;

View file

@ -28,6 +28,13 @@ namespace geode::addresser {
template <class Function, class Class>
Class rthunkAdjust(Function func, Class self);
template <class Function>
intptr_t getVirtualOffset(Function func);
template <class Function>
intptr_t getThunkOffset(Function func);
template <class Class>
concept HasZeroConstructor = requires {
new Class(ZeroConstructor);
@ -137,8 +144,24 @@ namespace geode::addresser {
template <class Function, class Class>
friend Class rthunkAdjust(Function func, Class self);
template <class Function>
friend intptr_t getVirtualOffset(Function func);
template <class Function>
friend intptr_t getThunkOffset(Function func);
};
template <class Function>
inline intptr_t getVirtualOffset(Function func) {
return Addresser::indexOf(func);
}
template <class Function>
inline intptr_t getThunkOffset(Function func) {
return Addresser::thunkOf(func);
}
/**
* Gets the real address of a virtual function
*/

View file

@ -3,45 +3,38 @@
using namespace geode::prelude;
TextInputNodeFix* TextInputNodeFix::create(
float width, float height, char const* placeholder, char const* fallbackFont, int size, char const* fontFile
) {
auto pRet = new TextInputNodeFix();
if (pRet && pRet->init(width, height, placeholder, fallbackFont, size, fontFile)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
// rob only uses `CCTextInputNode`s in mostly-flat hierarchies, which still
// happen to work with the weird vanilla code. this fix makes it work even in
// deep hierarchies, because the vanilla code uses `getParent` and manually
// calculates the child location in the world space based on that rather than
// using `convertToNodeSpace`.
static constexpr int INPUT_TAG = 0x80082;
bool TextInputNodeFix::ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) {
auto const touchPos = touch->getLocation();
auto const size = this->getContentSize();
auto const pos = this->convertToNodeSpace(touchPos) + m_textField->getAnchorPoint() * size;
#include <Geode/modify/CCTextInputNode.hpp>
if (pos.x < 0 || pos.x > size.width || pos.y < 0 || pos.y > size.height) {
this->onClickTrackNode(false);
return false;
struct TextInputNodeFix : Mofify<TextInputNodeFix, CCTextInputNode> {
bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) {
if (this->getTag() != INPUT_TAG) return CCTextInputNode::ccTouchBegan(touch, event);
auto const touchPos = touch->getLocation();
auto const size = this->getContentSize();
auto const pos = this->convertToNodeSpace(touchPos) + m_textField->getAnchorPoint() * size;
if (pos.x < 0 || pos.x > size.width || pos.y < 0 || pos.y > size.height) {
this->onClickTrackNode(false);
return false;
}
if (m_delegate && !m_delegate->allowTextInput(this)) {
this->onClickTrackNode(false);
return false;
}
this->onClickTrackNode(true);
// TODO: this also relies on the broken position calculation
// this->updateCursorPosition(pos, {{0, 0}, size});
return true;
}
if (m_delegate && !m_delegate->allowTextInput(this)) {
this->onClickTrackNode(false);
return false;
}
this->onClickTrackNode(true);
// TODO: this also relies on the broken position calculation
// this->updateCursorPosition(pos, {{0, 0}, size});
return true;
}
std::string InputNode::getString() {
@ -85,16 +78,23 @@ bool InputNode::init(
m_bgSprite->setPosition(width / 2, height / 2);
this->addChild(m_bgSprite);
m_input = TextInputNodeFix::create(width - 10.0f, height, placeholder, "Thonburi", 24, font);
m_input = CCTextInputNode::create(width - 10.0f, height, placeholder, "Thonburi", 24, font);
m_input->setLabelPlaceholderColor({ 150, 150, 150 });
m_input->setLabelPlaceholderScale(.75f);
m_input->setMaxLabelScale(.85f);
m_input->setMaxLabelLength(maxCharCount);
m_input->setPosition(width / 2, height / 2);
m_input->setTag(INPUT_TAG);
if (filter.length()) {
m_input->setAllowedChars(filter);
}
this->addChild(m_input);
auto thunkFunc = static_cast<bool(CCTouchDelegate::*)(cocos2d::CCTouch*, cocos2d::CCEvent*)>(
&CCTextInputNode::ccTouchBegan
);
auto thunkTable = *reinterpret_cast<void***>(m_input + addresser::getThunkOffset(thunkFunc));
thunkTable[addresser::getThunkIndex(thunkFunc) / sizeof(void*)] = &TextInputNodeFix2::ccTouchBegan;
this->setContentSize({ width, height });
this->setAnchorPoint({ .5f, .5f });