-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindows.cpp
127 lines (109 loc) · 4.22 KB
/
Windows.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <Geode/DefaultInclude.hpp>
#ifdef GEODE_IS_WINDOWS
#include <Geode/cocos/robtop/glfw/glfw3.h>
#include "../include/API.hpp"
#include "Platform.hpp"
using namespace geode::prelude;
using namespace mouse;
struct _GLFWwindow
{
struct _GLFWwindow* next; // 0
// Window settings and state
GLboolean iconified; // 4
GLboolean resizable;
GLboolean decorated;
GLboolean visible;
GLboolean closed; // 8
void* userPointer; // c
GLFWvidmode videoMode; // 10
GLFWmonitor* monitor; // 28
// Window input state
GLboolean stickyKeys; // 2c
GLboolean stickyMouseButtons;
double cursorPosX, cursorPosY; // 30
int cursorMode; // 40
char mouseButton[GLFW_MOUSE_BUTTON_LAST + 1];// 44
char key[GLFW_KEY_LAST + 1]; // 4c
// OpenGL extensions and context attributes
int clientAPI; // 1ac
int glMajor, glMinor, glRevision; // 1b0
GLboolean glForward, glDebug; // 1b4
int glProfile; // 1b8
int glRobustness; // 1bc
PFNGLGETSTRINGIPROC GetStringi; // 1c0
PAD(0x20);
struct {
GLFWwindowposfun pos; // 1ec
GLFWwindowsizefun size; // 1f0
GLFWwindowclosefun close; // 1f4
GLFWwindowrefreshfun refresh; // 1f8
GLFWwindowfocusfun focus; // 1fc
GLFWwindowiconifyfun iconify; // 200
GLFWframebuffersizefun fbsize; // 204
GLFWmousebuttonfun mouseButton;// 208
GLFWcursorposfun cursorPos; // 20c
GLFWcursorenterfun cursorEnter;// 210
GLFWscrollfun scroll; // 214
GLFWkeyfun key; // 218
GLFWcharfun character; // 21c
} callbacks;
// there's other stuff i'm too lazy to include
};
static CCPoint convertMouseCoords(double x, double y) {
auto* director = CCDirector::get();
auto* gl = director->getOpenGLView();
auto winSize = director->getWinSize();
auto frameSize = gl->getFrameSize();
auto mouse = CCPoint { static_cast<float>(x), static_cast<float>(y) } / frameSize;
return ccp(mouse.x, 1.f - mouse.y) * winSize;
}
#include <Geode/modify/CCEGLView.hpp>
static GLFWcursorposfun originalCursorPosFun = nullptr;
void __cdecl glfwPosCallback(GLFWwindow* window, double x, double y) {
originalCursorPosFun(window, x, y);
auto event = MouseMoveEvent(Mouse::get()->getCapturingNode(), convertMouseCoords(x, y));
postMouseEventThroughTouches(
event,
(Mouse::get()->isHeld(MouseButton::Left) ?
CCTOUCHMOVED :
CCTOUCHOTHER)
);
}
void setCursorPosCallback(GLFWwindow* window) {
if (!originalCursorPosFun) {
originalCursorPosFun = reinterpret_cast<_GLFWwindow*>(window)->callbacks.cursorPos;
reinterpret_cast<_GLFWwindow*>(window)->callbacks.cursorPos
= reinterpret_cast<GLFWcursorposfun>(&glfwPosCallback);
}
}
$on_mod(Loaded) {
if (CCEGLView::get()->getWindow()) {
setCursorPosCallback(CCEGLView::get()->getWindow());
}
};
struct $modify(CCEGLViewModify, CCEGLView) {
void setupWindow(CCRect size) {
CCEGLView::setupWindow(size);
setCursorPosCallback(m_pMainWindow);
}
void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int mods) {
if (action) {
Mouse::get()->m_heldButtons.insert(static_cast<MouseButton>(button));
}
else {
Mouse::get()->m_heldButtons.erase(static_cast<MouseButton>(button));
}
auto event = MouseClickEvent(
Mouse::get()->getCapturingNode(),
static_cast<MouseButton>(button), action,
getMousePos()
);
postMouseEventThroughTouches(
event,
(static_cast<MouseButton>(button) == MouseButton::Left ?
(action ? CCTOUCHBEGAN : CCTOUCHENDED) :
CCTOUCHOTHER)
);
}
};
#endif