Skip to content

Commit

Permalink
use keyDownExec and keyUpExec to override mouse positioning on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettDev committed Dec 1, 2024
1 parent 9804607 commit e8cff7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"win": "2.2074",
"mac": "2.2074"
},
"version": "v4.2.0-beta.10",
"version": "v4.2.0-beta.11",
"id": "spaghettdev.betterinputs",
"name": "BetterInputs",
"developer": "SpaghettDev",
Expand Down
57 changes: 29 additions & 28 deletions src/macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ inline bool keyDown(PlatformKey key, NSEvent* event)


static KeyEventType keyDownExecOIMP;
void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
void keyDownExec(EAGLView* self, SEL sel, NSEvent* event)
{
if (!g_selectedInput)
return keyDownExecOIMP(self, sel, event);

Expand Down Expand Up @@ -150,42 +151,39 @@ void keyDownExec(EAGLView* self, SEL sel, NSEvent* event) {
}

static KeyEventType keyUpExecOIMP;
void keyUpExec(EAGLView* self, SEL sel, NSEvent* event) {
if (!g_selectedInput)
return keyUpExecOIMP(self, sel, event);
void keyUpExec(EAGLView* self, SEL sel, NSEvent* event)
{
if (g_selectedInput)
return;

keyUpExecOIMP(self, sel, event);
}


// TODO: move to hooking mouseDownExec/mouseUpExec
// handles mouse clicks
struct BetterTouchDispatcher : geode::Modify<BetterTouchDispatcher, cocos2d::CCTouchDispatcher>

static KeyEventType mouseDownExecOIMP;
void mouseDownExec(EAGLView* self, SEL sel, NSEvent* event)
{
// https://github.com/ninXout/Crystal-Client/blob/7df5a8336ccb852bc984e55dd29ca27bb1741443/src/ImGui/ImGui.cpp#L96
void touches(cocos2d::CCSet* touches, cocos2d::CCEvent* event, unsigned int type)
{
if (!g_selectedInput)
return cocos2d::CCTouchDispatcher::touches(touches, event, type);
if (!g_selectedInput)
return mouseDOwnExecOIMP(self, self, event);

auto* touch = static_cast<cocos2d::CCTouch*>(touches->anyObject());
const auto touchPos = touch->getLocation();
cocos2d::CCTouch touch{};
touch.setTouchInfo(0, touchPos.x, [[[event window] contentView] frame].size.height - touchPos.y);

if (type == TouchMessageType::Began)
{
cocos2d::CCSize winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
g_selectedInput->useUpdateBlinkPos(true);

// the touch event's origin is bottom left
cocos2d::CCTouch touch{};
touch.setTouchInfo(0, touchPos.x, winSize.height - touchPos.y);
// 🥰
g_selectedInput->ccTouchBegan(&touch, nullptr);
}

g_selectedInput->useUpdateBlinkPos(true);
static KeyEventType mouseUpExecOIMP;
void mouseUpExec(EAGLView* self, SEL sel, NSEvent* event)
{
if (!g_selectedInput)
return mouseUpExecOIMP(self, sel, event);

// 🥰
g_selectedInput->ccTouchBegan(&touch, nullptr);
}
else
g_selectedInput->useUpdateBlinkPos(false);
}
};
g_selectedInput->useUpdateBlinkPos(false);
}


// https://github.com/qimiko/click-on-steps/blob/d8a87e93b5407e5f2113a9715363a5255724c901/src/macos.mm#L101
Expand All @@ -195,4 +193,7 @@ void touches(cocos2d::CCSet* touches, cocos2d::CCEvent* event, unsigned int type

HOOK_OBJC_METHOD(eaglView, KeyEventType, keyDownExec, keyDownExec:);
HOOK_OBJC_METHOD(eaglView, KeyEventType, keyUpExec, keyUpExec:);

HOOK_OBJC_METHOD(eaglView, KeyEventType, mouseDownExec, mouseDownExec:);
HOOK_OBJC_METHOD(eaglView, KeyEventType, mouseUpExec, mouseUpExec:);
}
4 changes: 2 additions & 2 deletions src/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ struct BetterCCEGLView : Modify<BetterCCEGLView, CCEGLView>
{
CCEGLView::onGLFWMouseCallBack(window, button, action, mods);

if (!g_selectedInput || button != GLFW_MOUSE_BUTTON_1 || action == 2) return;
if (!g_selectedInput || button != GLFW_MOUSE_BUTTON_1 || action == GLFW_REPEAT) return;

if (action == 1)
if (action == GLFW_PRESS)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint mousePos = BI::cocos::getMousePosition();
Expand Down

0 comments on commit e8cff7d

Please sign in to comment.