Skip to content

Commit

Permalink
add pinch to zoom on android
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Feb 7, 2024
1 parent b3ae720 commit 0b66091
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/features/FixMouseControls.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

#include <Geode/Bindings.hpp>
#include <Geode/modify/EditorUI.hpp>
#include <Geode/Loader.hpp>
#include <Geode/loader/Mod.hpp>
#include <numbers>

#undef min
#undef max

using namespace geode::prelude;

#ifdef GEODE_IS_DESKTOP

class $modify(EditorUI) {
$override
virtual void scrollWheel(float y, float x) {
Expand Down Expand Up @@ -84,3 +85,5 @@ class $modify(EditorUI) {
m_swipeStart = m_swipeStart + rel;
}
};

#endif
90 changes: 90 additions & 0 deletions src/features/PinchToZoom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#include <Geode/modify/EditorUI.hpp>
#include <Geode/loader/Mod.hpp>
#include <numbers>

#undef min
#undef max

using namespace geode::prelude;

#ifdef GEODE_IS_MOBILE

// thank you camila icreate 🙏

class $modify(EditorUI) {
std::unordered_set<Ref<CCTouch>> touches;

$override
bool ccTouchBegan(CCTouch* touch, CCEvent* event) override {
if (m_fields->touches.size() == 1 || EditorUI::ccTouchBegan(touch, event)) {
m_fields->touches.insert(touch);
return true;
}
return false;
}

$override
void ccTouchMoved(CCTouch* touch, CCEvent* event) override {
if (m_fields->touches.size() == 2) {
// thanks https://math.stackexchange.com/questions/4408515/calculate-coordinates-after-pinch-to-zoom-gesture!!

auto winSize = CCDirector::get()->getWinSize();
auto objLayer = m_editorLayer->m_objectLayer;

auto it = m_fields->touches.begin();
auto a = *it;
auto b = *++it;

auto aPrev = a->getPreviousLocationInView();
if (aPrev == CCPointZero) {
aPrev = a->getLocationInView();
}
auto bPrev = b->getPreviousLocationInView();
if (bPrev == CCPointZero) {
bPrev = b->getLocationInView();
}

auto prevAspectDiv = fabsf(aPrev.x - bPrev.x);
if (fpclassify(prevAspectDiv) == FP_ZERO) {
return;
}
auto prevAspect = fabsf(aPrev.y - bPrev.y) / prevAspectDiv;

auto aNext = a->getLocationInView();
auto bNext = b->getLocationInView();

auto bMapped = ccp(bNext.x, fabsf(prevAspect * fabsf(aNext.x - bNext.x) - aNext.y));

auto zoomDiv = fabsf(aPrev.y - bPrev.y);
if (fpclassify(zoomDiv) == FP_ZERO) {
return;
}
auto zoom = fabsf(aNext.y - bMapped.y) / zoomDiv;

auto objPos = objLayer->getPosition();
this->updateZoom(objLayer->getScale() * zoom);
objLayer->setPosition(ccp(
aNext.x + zoom * (objPos.x - aPrev.x),
(winSize.height - aNext.y) + zoom * (objPos.y - (winSize.height - aPrev.y))
));
}
else {
EditorUI::ccTouchMoved(touch, event);
}
}

$override
void ccTouchEnded(CCTouch* touch, CCEvent* event) override {
EditorUI::ccTouchEnded(touch, event);
m_fields->touches.erase(touch);
}

$override
void ccTouchCancelled(CCTouch* touch, CCEvent* event) override {
EditorUI::ccTouchCancelled(touch, event);
m_fields->touches.erase(touch);
}
};

#endif

0 comments on commit 0b66091

Please sign in to comment.