mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-06 14:35:51 +01:00
Update main.cpp
This commit is contained in:
parent
d880c8f95e
commit
71a35f3ba6
158
main.cpp
158
main.cpp
@ -5,75 +5,79 @@
|
||||
#include <GLES3/gl3.h>
|
||||
#include <dlfcn.h>
|
||||
#include <android/input.h>
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
#include "dobby.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_android.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
|
||||
#define TAG "MT_Auto_Drive"
|
||||
#define TAG "ZeroConfig_Drive"
|
||||
|
||||
static bool g_Initialized = false;
|
||||
|
||||
// --- 全自动自适应结构 ---
|
||||
// --- Zero-Config Core Structure ---
|
||||
struct {
|
||||
float rawX = 0.0f;
|
||||
float rawY = 0.0f;
|
||||
float mappedX = 0.0f;
|
||||
float mappedY = 0.0f;
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
bool down = false;
|
||||
|
||||
float renderW = 0.0f;
|
||||
float renderH = 0.0f;
|
||||
|
||||
// 设备固定的物理分辨率 (从你之前的 wm size 获取)
|
||||
// 只要设备不换,这两个数字永远不用动
|
||||
float physicalW = 3392.0f;
|
||||
float physicalH = 2400.0f;
|
||||
|
||||
// 内部计算参数 (自动生成,不再需要手动调)
|
||||
float scaleX = 1.0f;
|
||||
float scaleY = 1.0f;
|
||||
float offsetX = 0.0f;
|
||||
float offsetY = 0.0f;
|
||||
// Physical screen resolution
|
||||
float physW = 3392.0f;
|
||||
float physH = 2400.0f;
|
||||
|
||||
bool isSizeAutoDetected = false;
|
||||
} g_Touch;
|
||||
|
||||
// 自动定点校准状态机
|
||||
static bool g_WaitingForTopLeft = false;
|
||||
|
||||
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
|
||||
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;
|
||||
|
||||
// --- Auto Fetch Device Physical Resolution ---
|
||||
void AutoFetchDeviceResolution() {
|
||||
FILE* pipe = popen("wm size", "r");
|
||||
if (!pipe) return;
|
||||
|
||||
char buffer[128];
|
||||
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
||||
int w = 0, h = 0;
|
||||
if (sscanf(buffer, "Physical size: %dx%d", &w, &h) == 2 ||
|
||||
sscanf(buffer, "Override size: %dx%d", &w, &h) == 2) {
|
||||
|
||||
// Landscape force logic
|
||||
g_Touch.physW = (float)std::max(w, h);
|
||||
g_Touch.physH = (float)std::min(w, h);
|
||||
g_Touch.isSizeAutoDetected = true;
|
||||
}
|
||||
}
|
||||
pclose(pipe);
|
||||
}
|
||||
|
||||
// --- Input Event Handler ---
|
||||
void handle_android_event(AInputEvent* event) {
|
||||
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
|
||||
int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
|
||||
|
||||
float currentRawX = AMotionEvent_getX(event, 0);
|
||||
float currentRawY = AMotionEvent_getY(event, 0);
|
||||
float rawX = AMotionEvent_getX(event, 0);
|
||||
float rawY = AMotionEvent_getY(event, 0);
|
||||
|
||||
g_Touch.rawX = currentRawX;
|
||||
g_Touch.rawY = currentRawY;
|
||||
// Auto Scaling Logic
|
||||
float autoScaleX = g_Touch.renderW / g_Touch.physW;
|
||||
float autoScaleY = g_Touch.renderH / g_Touch.physH;
|
||||
|
||||
g_Touch.x = rawX * autoScaleX;
|
||||
g_Touch.y = rawY * autoScaleY;
|
||||
|
||||
if (action == AMOTION_EVENT_ACTION_DOWN) {
|
||||
// --- 一键自动对齐逻辑 ---
|
||||
if (g_WaitingForTopLeft) {
|
||||
// 原理:当手指点击游戏画面真正的左上角时,Mapped坐标应该是 (0,0)
|
||||
// 推出公式:0 = (Raw + Offset) * Scale => Offset = -Raw
|
||||
g_Touch.offsetX = -currentRawX;
|
||||
g_Touch.offsetY = -currentRawY;
|
||||
g_WaitingForTopLeft = false;
|
||||
return; // 吃掉这次点击,防止误触菜单
|
||||
}
|
||||
g_Touch.down = true;
|
||||
} else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) {
|
||||
g_Touch.down = false;
|
||||
}
|
||||
|
||||
// 核心映射:动态比例 + 自动偏移
|
||||
g_Touch.mappedX = (currentRawX + g_Touch.offsetX) * g_Touch.scaleX;
|
||||
g_Touch.mappedY = (currentRawY + g_Touch.offsetY) * g_Touch.scaleY;
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,72 +99,63 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
|
||||
g_Touch.renderW = (float)w;
|
||||
g_Touch.renderH = (float)h;
|
||||
|
||||
// --- 真正的自适应缩放核心 ---
|
||||
// 每一帧自动计算比例,如果你在游戏中途拉动分屏线条,这里会瞬间无缝适应!
|
||||
g_Touch.scaleX = g_Touch.renderW / g_Touch.physicalW;
|
||||
g_Touch.scaleY = g_Touch.renderH / g_Touch.physicalH;
|
||||
|
||||
if (!g_Initialized) {
|
||||
ImGui::CreateContext();
|
||||
ImGui_ImplOpenGL3_Init("#version 300 es");
|
||||
|
||||
float uiScale = (float)h / 1000.0f;
|
||||
if (uiScale < 1.0f) uiScale = 1.0f;
|
||||
ImGui::GetStyle().ScaleAllSizes(3.5f * uiScale);
|
||||
ImGui::GetIO().FontGlobalScale = 3.5f * uiScale;
|
||||
|
||||
g_Initialized = true;
|
||||
}
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.DisplaySize = ImVec2(g_Touch.renderW, g_Touch.renderH);
|
||||
io.MousePos = ImVec2(g_Touch.mappedX, g_Touch.mappedY);
|
||||
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
|
||||
io.MouseDown[0] = g_Touch.down;
|
||||
|
||||
// --- 动态字体缩放 (移出了 Init,每一帧都会自适应) ---
|
||||
// 如果你拉动分屏线,或者横竖屏切换,字体会瞬间跟着变大变小
|
||||
float uiScale = (float)h / 1000.0f;
|
||||
if (uiScale < 1.0f) uiScale = 1.0f;
|
||||
io.FontGlobalScale = 3.5f * uiScale;
|
||||
|
||||
// 修改样式,把控件也变大,防止手指点不到
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
style.ItemSpacing = ImVec2(8.0f * uiScale, 8.0f * uiScale);
|
||||
style.FramePadding = ImVec2(8.0f * uiScale, 8.0f * uiScale);
|
||||
style.WindowRounding = 12.0f;
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// 绘制自动校准状态提示
|
||||
ImDrawList* draw = ImGui::GetForegroundDrawList();
|
||||
if (g_WaitingForTopLeft) {
|
||||
// 全屏半透明黑底 + 红色提示,引导用户点击
|
||||
draw->AddRectFilled(ImVec2(0, 0), ImVec2(w, h), IM_COL32(0, 0, 0, 150));
|
||||
draw->AddText(ImGui::GetFont(), ImGui::GetFontSize() * 1.5f, ImVec2(w * 0.1f, h * 0.4f),
|
||||
IM_COL32(255, 50, 50, 255), "【自动校准中】\n请用手指准确点击游戏画面的【最左上角】一次!", NULL, 0.0f);
|
||||
} else if (g_Touch.down) {
|
||||
// 正常点按时的准星
|
||||
draw->AddLine(ImVec2(0, g_Touch.mappedY), ImVec2(w, g_Touch.mappedY), IM_COL32(0, 255, 0, 150), 2.0f);
|
||||
draw->AddLine(ImVec2(g_Touch.mappedX, 0), ImVec2(g_Touch.mappedX, h), IM_COL32(0, 255, 0, 150), 2.0f);
|
||||
draw->AddCircleFilled(ImVec2(g_Touch.mappedX, g_Touch.mappedY), 20.0f, IM_COL32(255, 0, 0, 255));
|
||||
// 绘制点击反馈点
|
||||
if (g_Touch.down) {
|
||||
ImGui::GetForegroundDrawList()->AddCircleFilled(io.MousePos, 20.0f, IM_COL32(0, 255, 0, 200));
|
||||
}
|
||||
|
||||
// 菜单界面
|
||||
ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(w * 0.8f, h * 0.8f), ImGuiCond_FirstUseEver);
|
||||
|
||||
if (ImGui::Begin("Auto-Adaptive Menu", nullptr, ImGuiWindowFlags_NoCollapse)) {
|
||||
// --- 菜单 UI ---
|
||||
ImGui::SetNextWindowPos(ImVec2(30, 30), ImGuiCond_FirstUseEver);
|
||||
// 增加 AlwaysAutoResize:窗口大小会自动贴合文字,再也不需要去拉右下角的三角缩放了
|
||||
if (ImGui::Begin("Zero-Config Engine", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize)) {
|
||||
|
||||
ImGui::TextColored(ImVec4(0, 1, 1, 1), "★ 全自动适配引擎已启动 ★");
|
||||
ImGui::Text("Render Res (实时): %.0f x %.0f", g_Touch.renderW, g_Touch.renderH);
|
||||
ImGui::Text("Auto Scale (实时): X=%.4f, Y=%.4f", g_Touch.scaleX, g_Touch.scaleY);
|
||||
ImGui::TextColored(ImVec4(0, 1, 1, 1), "[*] Auto-Adaptive Engine Active");
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "如果点不准,只需点击下方按钮,");
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "然后摸一下游戏【最左上角】即可。");
|
||||
ImGui::Text("Render Surface: %.0f x %.0f", g_Touch.renderW, g_Touch.renderH);
|
||||
|
||||
if (ImGui::Button("🎯 一键自动校准偏移", ImVec2(-1, 120))) {
|
||||
g_WaitingForTopLeft = true;
|
||||
if (g_Touch.isSizeAutoDetected) {
|
||||
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Physical Screen: %.0f x %.0f (Auto)", g_Touch.physW, g_Touch.physH);
|
||||
} else {
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Physical Screen: %.0f x %.0f (Fallback)", g_Touch.physW, g_Touch.physH);
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
ImGui::Text("当前 Offset: X=%.1f, Y=%.1f", g_Touch.offsetX, g_Touch.offsetY);
|
||||
ImGui::Text("Current Scale: X=%.4f, Y=%.4f",
|
||||
g_Touch.renderW / g_Touch.physW,
|
||||
g_Touch.renderH / g_Touch.physH);
|
||||
|
||||
// 预留设备修改接口,如果换手机了,修改这里的物理分辨率即可
|
||||
if (ImGui::CollapsingHeader("高级设置 (换设备才需要点开)")) {
|
||||
ImGui::Text("设备物理分辨率 (横屏)");
|
||||
ImGui::DragFloat("Physical W", &g_Touch.physicalW, 1.0f, 1000.0f, 5000.0f);
|
||||
ImGui::DragFloat("Physical H", &g_Touch.physicalH, 1.0f, 1000.0f, 5000.0f);
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::Text("- Fully automatic calibration.");
|
||||
ImGui::Text("- Seamless split-screen support.");
|
||||
|
||||
static bool esp_enabled = true;
|
||||
ImGui::Checkbox("Enable ESP (Demo)", &esp_enabled);
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
@ -177,12 +172,15 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
|
||||
}
|
||||
|
||||
void* init_thread(void*) {
|
||||
AutoFetchDeviceResolution();
|
||||
|
||||
sleep(6);
|
||||
void* sym_egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
|
||||
if (sym_egl) DobbyHook(sym_egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
|
||||
|
||||
void* consume_addr = DobbySymbolResolver("libinput.so", "_ZN7android13InputConsumer7consumeEPNS_26InputEventFactoryInterfaceEblPjPPNS_10InputEventE");
|
||||
if (consume_addr) DobbyHook(consume_addr, (void*)hook_consume, (void**)&old_consume);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user