Skip to content

Commit b013f75

Browse files
committed
clamp button position, fix crash
1 parent 64e836b commit b013f75

4 files changed

Lines changed: 49 additions & 50 deletions

File tree

src/backend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ class $modify(CCTouchDispatcher) {
373373
}
374374

375375
const auto pos = toVec2(touch->getLocation());
376+
GEODE_MOBILE(io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);)
376377
io.AddMousePosEvent(pos.x, pos.y);
377378
if (io.WantCaptureMouse) {
378379
bool didGDSwallow = false;
@@ -406,6 +407,7 @@ class $modify(CCTouchDispatcher) {
406407
// TODO: dragging out of gd makes it click in imgui
407408
if (!didGDSwallow) {
408409
if (type == CCTOUCHBEGAN || type == CCTOUCHMOVED) {
410+
GEODE_MOBILE(io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);)
409411
io.AddMouseButtonEvent(0, true);
410412
}
411413
else {

src/nodes/DragButton.cpp

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,37 @@
44
using namespace geode::prelude;
55

66
bool DragButton::init(CCNode* node, std::function<void()> onPress) {
7-
if (!CCLayer::init()) return false;
7+
if (!CCLayer::init()) return false;
88
this->setTouchEnabled(true);
9-
this->setAnchorPoint({.5f, .5f});
9+
this->setAnchorPoint({.5f, .5f});
1010
this->ignoreAnchorPointForPosition(false);
11-
this->scheduleUpdate();
12-
if (node) {
13-
this->setContentSize(node->getScaledContentSize());
14-
this->addChildAtPosition(node, Anchor::Center, CCPoint{0, 0});
15-
}
16-
this->m_onPress = onPress;
17-
return true;
11+
this->scheduleUpdate();
12+
if (node) {
13+
this->setContentSize(node->getScaledContentSize());
14+
this->addChildAtPosition(node, Anchor::Center, CCPoint{0, 0});
15+
}
16+
this->m_onPress = onPress;
17+
return true;
1818
}
1919

2020
bool DragButton::ccTouchBegan(CCTouch* touch, CCEvent* event) {
21-
if (!this->m_bVisible) return false;
22-
CCPoint local = convertToNodeSpace(touch->getLocation());
23-
if (this->getContentWidth() > local.x && local.x > 0 && this->getContentHeight() > local.y && local.y > 0) {
21+
if (!this->m_bVisible) return false;
22+
CCPoint local = convertToNodeSpace(touch->getLocation());
23+
if (this->getContentWidth() > local.x && local.x > 0 && this->getContentHeight() > local.y && local.y > 0) {
2424
this->stopActionByTag(123);
2525
auto action = CCEaseSineOut::create(CCScaleTo::create(.3f, .8f));
2626
action->setTag(123);
2727
this->runAction(action);
2828

29-
m_diff = this->getParent()->convertToNodeSpace(touch->getLocation()) - this->getPosition();
30-
return true;
31-
}
32-
return false;
29+
m_diff = this->getParent()->convertToNodeSpace(touch->getLocation()) - this->getPosition();
30+
return true;
31+
}
32+
return false;
3333
}
3434

3535
void DragButton::ccTouchMoved(CCTouch* touch, CCEvent* event) {
3636
auto pos = this->getParent()->convertToNodeSpace(touch->getLocation()) - m_diff;
37-
auto winSize = CCDirector::get()->getWinSize();
38-
pos.x = std::clamp(pos.x, 0.f, winSize.width);
39-
pos.y = std::clamp(pos.y, 0.f, winSize.height);
40-
this->setPosition(pos);
37+
this->setPosition(pos);
4138
}
4239

4340
void DragButton::ccTouchEnded(CCTouch* touch, CCEvent* event) {
@@ -46,45 +43,46 @@ void DragButton::ccTouchEnded(CCTouch* touch, CCEvent* event) {
4643
action->setTag(123);
4744
this->runAction(action);
4845

49-
if ((touch->getLocation() - touch->getStartLocation()).getLength() > 3.f) return;
50-
if (this->m_onPress) this->m_onPress();
46+
if ((touch->getLocation() - touch->getStartLocation()).getLength() > 3.f) return;
47+
if (this->m_onPress) this->m_onPress();
5148
}
5249

5350
void DragButton::ccTouchCancelled(CCTouch* touch, CCEvent* event) {
54-
this->ccTouchEnded(touch, event);
55-
}
56-
57-
void DragButton::setCallback(std::function<void()> onPress) {
58-
this->m_onPress = onPress;
59-
}
60-
61-
std::function<void()> DragButton::getCallback() {
62-
return this->m_onPress;
51+
this->ccTouchEnded(touch, event);
6352
}
6453

6554
void DragButton::registerWithTouchDispatcher() {
6655
CCTouchDispatcher::get()->addTargetedDelegate(this, -512, true);
6756
}
6857

6958
DragButton* DragButton::create(CCNode* node, std::function<void ()> onPress) {
70-
auto ret = new DragButton;
71-
if (ret->init(node, onPress)) {
72-
ret->autorelease();
73-
return ret;
74-
}
75-
delete ret;
76-
return nullptr;
59+
auto ret = new DragButton;
60+
if (ret->init(node, onPress)) {
61+
ret->autorelease();
62+
return ret;
63+
}
64+
delete ret;
65+
return nullptr;
7766
}
7867

7968
void DragButton::update(float dt) {
80-
const Settings& settings = DevTools::get()->getSettings();
81-
if (PlayLayer::get() && !CCScene::get()->getChildByType<PauseLayer>(0) && !settings.buttonInGame) {
82-
setVisible(false);
83-
} else if (auto lel = LevelEditorLayer::get(); lel && ((lel->m_playbackMode == PlaybackMode::Playing && !settings.buttonInGame) || !settings.buttonInEditor)) {
84-
setVisible(false);
85-
} else {
86-
setVisible(true);
87-
}
69+
const Settings& settings = DevTools::get()->getSettings();
70+
if (PlayLayer::get() && !CCScene::get()->getChildByType<PauseLayer>(0) && !settings.buttonInGame) {
71+
setVisible(false);
72+
} else if (auto lel = LevelEditorLayer::get(); lel && ((lel->m_playbackMode == PlaybackMode::Playing && !settings.buttonInGame) || !settings.buttonInEditor)) {
73+
setVisible(false);
74+
} else {
75+
setVisible(true);
76+
}
77+
}
78+
79+
void DragButton::setPosition(cocos2d::CCPoint const& pos_) {
80+
auto winSize = CCDirector::get()->getWinSize();
81+
float pad = 5.f;
82+
auto pos = pos_;
83+
pos.x = std::clamp(pos.x, pad, winSize.width - pad);
84+
pos.y = std::clamp(pos.y, pad, winSize.height - pad);
85+
CCNode::setPosition(pos);
8886
}
8987

9088
DragButton* DragButton::get() {

src/nodes/DragButton.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class DragButton : public cocos2d::CCLayer {
1818

1919
void update(float dt) override;
2020
public:
21-
void setCallback(std::function<void()> onPress);
22-
std::function<void()> getCallback();
23-
2421
static DragButton* create(cocos2d::CCNode* node, std::function<void()> onPress);
2522
static DragButton* get();
23+
24+
void setPosition(cocos2d::CCPoint const& position) override;
2625
};

src/pages/Tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void DevTools::drawTree() {
9292
#endif
9393
m_prevQuery = m_searchQuery;
9494
ImGui::SetNextItemWidth(-1.f);
95-
ImGui::InputTextWithHint("", U8STR(FEATHER_SEARCH " Search for a node..."), &m_searchQuery, ImGuiInputTextFlags_EnterReturnsTrue);
95+
ImGui::InputTextWithHint("##search", U8STR(FEATHER_SEARCH " Search for a node..."), &m_searchQuery, ImGuiInputTextFlags_EnterReturnsTrue);
9696

9797
this->drawTreeBranch(CCDirector::get()->getRunningScene(), 0);
9898
}

0 commit comments

Comments
 (0)