Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 19:12:00 +08:00 committed by GitHub
parent c341c38ab2
commit d15e382da0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

104
main.cpp
View File

@ -16,16 +16,11 @@
static bool g_Initialized = false;
static bool g_ShowMenu = true;
struct TouchPos { float x, y; };
// 存储最终计算出的自适应坐标
struct {
float x, y;
bool down;
} g_RawTouch = {0, 0, false};
// --- 自动校准存储 ---
static TouchPos g_P1 = {0, 0}; // 窗口左上角在 Raw 坐标系下的位置
static TouchPos g_P2 = {0, 0}; // 窗口右下角在 Raw 坐标系下的位置
static float g_FinalX = 0, g_FinalY = 0;
} g_Touch = {0, 0, false};
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
@ -33,25 +28,51 @@ static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
typedef int (*p_InputConsumer_consume)(void* instance, void* factory, bool consumeBatches, int64_t frameTime, uint32_t* outSeq, void** outEvent);
static p_InputConsumer_consume old_consume = nullptr;
void handle_event(void* event) {
// 自适应处理核心函数
void handle_adaptive_event(void* event, float renderW, float renderH) {
if (!event) return;
static auto _getType = (int (*)(void*))dlsym(RTLD_DEFAULT, "AInputEvent_getType");
static auto _getAction = (int (*)(void*))dlsym(RTLD_DEFAULT, "AMotionEvent_getAction");
// NDK 原生 API
static auto _getX = (float (*)(void*, size_t))dlsym(RTLD_DEFAULT, "AMotionEvent_getX");
static auto _getY = (float (*)(void*, size_t))dlsym(RTLD_DEFAULT, "AMotionEvent_getY");
if (_getType && _getType(event) == 1) {
// 尝试获取原始坐标和物理范围
// 在窗口模式下,RawX/Y 通常是屏幕物理像素,而 getX/Y 往往已经被系统转换到了窗口局部坐标
if (_getType && _getType(event) == 1) { // AINPUT_EVENT_TYPE_MOTION
int action = _getAction(event) & 0xff;
g_RawTouch.x = _getX(event, 0);
g_RawTouch.y = _getY(event, 0);
if (action == 0) g_RawTouch.down = true;
else if (action == 1 || action == 3) g_RawTouch.down = false;
/* * 自适应原理:
* 大多数 Android 窗口系统在分发事件给 InputConsumer 时,
* 已经将坐标转换为了相对于 Surface 的局部坐标。
* 如果点不准,通常是因为 ImGui 渲染的分辨率与窗口实际 Surface 分辨率不一致。
*/
float rawX = _getX(event, 0);
float rawY = _getY(event, 0);
// 如果你的 dumpsys 显示 Requested w=3392 而 ImGui 是 1426
// 我们需要做一个简单的动态比例缩放
// 这里的 3392.0f 和 2400.0f 是从你 dumpsys 里的 Requested w/h 获取的
float containerW = 3392.0f;
float containerH = 2400.0f;
g_Touch.x = (rawX / containerW) * renderW;
g_Touch.y = (rawY / containerH) * renderH;
if (action == 0) g_Touch.down = true;
else if (action == 1 || action == 3) g_Touch.down = false;
}
}
int hook_consume(void* a, void* b, bool c, int64_t d, uint32_t* e, void** f) {
int res = old_consume(a, b, c, d, e, f);
if (res == 0 && f && *f) handle_event(*f);
// 注意:在这里我们还拿不到渲染窗口的宽高,所以只存下原始事件
// 具体的坐标转换放在 eglSwapBuffers 里实时计算
if (res == 0 && f && *f) {
// 传递当前全局事件指针
handle_adaptive_event(*f, ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y);
}
return res;
}
@ -64,63 +85,30 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
ImGui::CreateContext();
ImGui_ImplOpenGL3_Init("#version 300 es");
ImGui::GetStyle().ScaleAllSizes(3.0f);
// 设置默认值 (基于你之前的截图尝试)
g_P1 = {600.0f, 200.0f};
g_P2 = {2000.0f, 1200.0f};
g_Initialized = true;
}
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)w, (float)h);
// --- 两点线性映射算法 ---
// 原理:将 [P1, P2] 的 Raw 范围映射到 [0, DisplaySize] 的渲染范围
float divX = (g_P2.x - g_P1.x);
float divY = (g_P2.y - g_P1.y);
if (divX != 0 && divY != 0) {
g_FinalX = (g_RawTouch.x - g_P1.x) / divX * (float)w;
g_FinalY = (g_RawTouch.y - g_P1.y) / divY * (float)h;
}
io.MousePos = ImVec2(g_FinalX, g_FinalY);
io.MouseDown[0] = g_RawTouch.down;
// 应用自适应坐标
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
io.MouseDown[0] = g_Touch.down;
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
// 辅助红点
if (g_RawTouch.down) {
ImGui::GetBackgroundDrawList()->AddCircleFilled(io.MousePos, 25.0f, IM_COL32(255, 0, 0, 255));
if (g_Touch.down) {
// 画出红色触控点,用于验证自适应是否成功
ImGui::GetBackgroundDrawList()->AddCircleFilled(io.MousePos, 20.0f, IM_COL32(255, 0, 0, 255));
}
if (g_ShowMenu) {
ImGui::SetNextWindowSize(ImVec2(w * 0.8f, h * 0.8f), ImGuiCond_Once);
ImGui::Begin("AndKitty SGame Touch Fixer", &g_ShowMenu);
ImGui::Begin("AndKitty Adaptive Menu", &g_ShowMenu);
ImGui::Text("Window Size: %.0f x %.0f", io.DisplaySize.x, io.DisplaySize.y);
ImGui::Text("Touch Logic Pos: %.1f, %.1f", g_Touch.x, g_Touch.y);
ImGui::Text("Render: %dx%d", w, h);
ImGui::Text("Raw Touch: %.1f, %.1f", g_RawTouch.x, g_RawTouch.y);
ImGui::Separator();
if (ImGui::Button("第一步:点击窗口[左上角]后点我", ImVec2(-1, 80))) {
g_P1 = {g_RawTouch.x, g_RawTouch.y};
}
ImGui::Text("P1(Min): %.1f, %.1f", g_P1.x, g_P1.y);
ImGui::Spacing();
if (ImGui::Button("第二步:点击窗口[右下角]后点我", ImVec2(-1, 80))) {
g_P2 = {g_RawTouch.x, g_RawTouch.y};
}
ImGui::Text("P2(Max): %.1f, %.1f", g_P2.x, g_P2.y);
ImGui::Separator();
ImGui::TextColored(ImVec2(0,1,0), "Mapped Pos: %.1f, %.1f", g_FinalX, g_FinalY);
if (ImGui::Button("重置校准", ImVec2(200, 50))) {
g_P1 = {0,0}; g_P2 = {(float)w, (float)h};
}
if (ImGui::Button("隐藏菜单", ImVec2(-1, 80))) g_ShowMenu = false;
ImGui::End();
}