inputnode fix

This commit is contained in:
altalk23 2024-01-26 16:29:14 +03:00
parent 67c7305acc
commit c551d430c3
5 changed files with 59 additions and 68 deletions

View file

@ -5,6 +5,15 @@
#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

@ -1,67 +0,0 @@
// FIXME: This fix ends up breaking some of the vanilla text inputs.
#if 0
using namespace geode::prelude;
// 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`.
struct CCTextInputNodeFix : Modify<CCTextInputNodeFix, CCTextInputNode> {
bool ccTouchBegan(CCTouch* touch, CCEvent* 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)
return false;
if (m_delegate && !m_delegate->allowTextInput(this))
return false;
this->onClickTrackNode(true);
return true;
}
};
#endif
#include <Geode/Geode.hpp>
// This doesn't actually add any IDs since those aren't needed for
// CCTextInputNode where everything is accessible through members.
// This is to fix the effects of the epic mistake of Cocos2d inventing
// ignoreAnchorPointForPosition which causes the content size of
// text input nodes to be way off
#if 0
// this doesnt work either
#include <Geode/modify/CCTextInputNode.hpp>
struct $modify(CCTextInputNode) {
bool init(float width, float height, const char* caption, const char* thonburi, int maxCharCount, const char* font) {
if (!CCTextInputNode::init(width, height, caption, thonburi, maxCharCount, font))
return false;
this->ignoreAnchorPointForPosition(false);
this->fixPosition();
return true;
}
void fixPosition() {
if (!m_bIgnoreAnchorPointForPosition && m_placeholderLabel) {
this->setAnchorPoint(m_placeholderLabel->getAnchorPoint());
m_placeholderLabel->setPosition(m_obContentSize * m_obAnchorPoint);
}
}
void updateLabel(gd::string text) {
CCTextInputNode::updateLabel(text);
this->fixPosition();
}
};
#endif

View file

@ -87,6 +87,12 @@ struct CustomMenuLayer : Modify<CustomMenuLayer, MenuLayer> {
bottomMenu->updateLayout();
auto input = InputNode::create(200.f, "Search");
input->setPosition(winSize.width / 2, winSize.height - 20.0f);
input->setID("search-input");
input->setAnchorPoint({ 0.5f, 0.5f });
this->addChild(input);
if (auto node = this->getChildByID("settings-gamepad-icon")) {
node->setPositionX(
bottomMenu->getChildByID("settings-button")->getPositionX() + winSize.width / 2

View file

@ -572,6 +572,8 @@ void ModListLayer::reloadList(bool keepScroll, std::optional<ModListQuery> const
m_checkForUpdatesBtn->removeFromParent();
m_checkForUpdatesBtn = nullptr;
}
handleTouchPriority(this);
}
void ModListLayer::updateAllStates() {

View file

@ -3,6 +3,47 @@
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`.
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;
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;
}
std::string InputNode::getString() {
return m_input->getString();
}
@ -44,7 +85,7 @@ bool InputNode::init(
m_bgSprite->setPosition(width / 2, height / 2);
this->addChild(m_bgSprite);
m_input = CCTextInputNode::create(width - 10.0f, height, placeholder, font);
m_input = TextInputNodeFix::create(width - 10.0f, height, placeholder, "Thonburi", 24, font);
m_input->setLabelPlaceholderColor({ 150, 150, 150 });
m_input->setLabelPlaceholderScale(.75f);
m_input->setMaxLabelScale(.85f);