Skip to content

Commit 6430285

Browse files
committed
fix mouse positioning
1 parent a305139 commit 6430285

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/macos.mm

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ inline bool keyDown(PlatformKey key, NSEvent* event)
3939
{
4040
inline cocos2d::CCPoint getMousePosition(NSEvent* event)
4141
{
42-
auto windowFrame = [[event window] frame];
43-
auto viewFrame = [[[event window] contentView] frame];
44-
auto winSize = cocos2d::CCDirector::get()->getWinSize();
45-
auto scaleFactor = cocos2d::CCPoint(winSize) / ccp(viewFrame.size.width, viewFrame.size.height);
46-
auto mouse = [event locationInWindow];
42+
NSWindow* window = [event window];
43+
NSView* contentView = [window contentView];
44+
NSPoint mouseInWindow = [event locationInWindow];
45+
NSPoint mouseInView = [contentView convertPoint:mouseInWindow fromView:nil];
4746

48-
return ccp(mouse.x - windowFrame.origin.x, winSize.height - (mouse.y - windowFrame.origin.y)) * scaleFactor;
47+
NSRect viewFrame = [contentView frame];
48+
cocos2d::CCSize winSize = cocos2d::CCDirector::get()->getWinSize();
49+
cocos2d::CCPoint scaleFactor = cocos2d::CCPoint(winSize.width / viewFrame.size.width, winSize.height / viewFrame.size.height);
50+
51+
CGFloat cocosY = [contentView isFlipped] ? (viewFrame.size.height - mouseInView.y) : mouseInView.y;
52+
53+
return cocos2d::CCPoint(mouseInView.x * scaleFactor.x, cocosY * scaleFactor.y);
4954
}
5055
}
5156
}
@@ -203,17 +208,11 @@ void mouseDownExec(EAGLView* self, SEL sel, NSEvent* event)
203208
if (!g_selectedInput)
204209
return mouseDownExecOIMP(self, sel, event);
205210

206-
NSPoint m = [event locationInWindow];
207-
208-
geode::log::debug("mouse pos is ({}, {})", m.x, m.y);
209-
210211
cocos2d::CCSize winSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
211-
cocos2d::CCPoint mousePos = cocos2d::CCPoint{ static_cast<float>(m.x), static_cast<float>(m.y) };
212+
cocos2d::CCPoint mousePos = BI::cocos::getMousePosition(event);
212213

213-
// NSWindow's mouse origin is the bottom left
214-
// CCTouch's mouse origin is top left (because of course it is)
215214
cocos2d::CCTouch touch{};
216-
touch.setTouchInfo(0, mousePos.x, winSize.height - mousePos.y);
215+
touch.setTouchInfo(0, mousePos.x, /* winSize.height - */ mousePos.y);
217216

218217
g_selectedInput->useUpdateBlinkPos(true);
219218

0 commit comments

Comments
 (0)