mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-08 07:25:54 +01:00
Update main.cpp
This commit is contained in:
parent
2f93934cc8
commit
285017e10f
81
main.cpp
81
main.cpp
@ -16,7 +16,7 @@
|
||||
|
||||
static bool g_Initialized = false;
|
||||
|
||||
// --- Zero-Config Core Structure ---
|
||||
// --- 零配置全自动核心结构 ---
|
||||
struct {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
@ -25,20 +25,21 @@ struct {
|
||||
float renderW = 0.0f;
|
||||
float renderH = 0.0f;
|
||||
|
||||
// Physical screen resolution
|
||||
float physW = 3392.0f;
|
||||
float physH = 2400.0f;
|
||||
|
||||
bool isSizeAutoDetected = false;
|
||||
} g_Touch;
|
||||
|
||||
// --- 右下角无极缩放状态 ---
|
||||
static float g_DynamicScale = 1.0f;
|
||||
static float g_BaseWindowWidth = 0.0f;
|
||||
|
||||
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;
|
||||
@ -48,8 +49,6 @@ void AutoFetchDeviceResolution() {
|
||||
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;
|
||||
@ -58,7 +57,6 @@ void AutoFetchDeviceResolution() {
|
||||
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;
|
||||
@ -66,7 +64,6 @@ void handle_android_event(AInputEvent* event) {
|
||||
float rawX = AMotionEvent_getX(event, 0);
|
||||
float rawY = AMotionEvent_getY(event, 0);
|
||||
|
||||
// Auto Scaling Logic
|
||||
float autoScaleX = g_Touch.renderW / g_Touch.physW;
|
||||
float autoScaleY = g_Touch.renderH / g_Touch.physH;
|
||||
|
||||
@ -99,19 +96,9 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
|
||||
g_Touch.renderW = (float)w;
|
||||
g_Touch.renderH = (float)h;
|
||||
|
||||
// --- 纯净原生初始化 ---
|
||||
if (!g_Initialized) {
|
||||
ImGui::CreateContext();
|
||||
ImGui_ImplOpenGL3_Init("#version 300 es");
|
||||
|
||||
// 仅在初始化时进行一次移动端适配放大,不再干涉后续的原生运行逻辑
|
||||
float initScale = (float)h / 1000.0f;
|
||||
if (initScale < 1.0f) initScale = 1.0f;
|
||||
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
style.ScaleAllSizes(3.5f * initScale);
|
||||
ImGui::GetIO().FontGlobalScale = 3.5f * initScale;
|
||||
|
||||
g_Initialized = true;
|
||||
}
|
||||
|
||||
@ -120,22 +107,54 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
|
||||
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
|
||||
io.MouseDown[0] = g_Touch.down;
|
||||
|
||||
// --- 核心:将窗口宽度转换为全局 UI 缩放倍率 ---
|
||||
float deviceScale = (float)h / 1000.0f;
|
||||
if (deviceScale < 1.0f) deviceScale = 1.0f;
|
||||
float finalScale = deviceScale * g_DynamicScale; // 基础设备倍率 × 玩家拖拽倍率
|
||||
|
||||
// 备份默认样式以供每一帧缩放克隆
|
||||
static ImGuiStyle default_style;
|
||||
static bool style_backed_up = false;
|
||||
if (!style_backed_up) {
|
||||
default_style = ImGui::GetStyle();
|
||||
style_backed_up = true;
|
||||
}
|
||||
|
||||
// 每一帧应用等比缩放
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
style = default_style;
|
||||
style.ScaleAllSizes(3.0f * finalScale);
|
||||
style.WindowRounding = 12.0f;
|
||||
style.WindowMinSize = ImVec2(100.0f, 100.0f); // 保证缩放后依然能缩小回来
|
||||
io.FontGlobalScale = 3.0f * finalScale;
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// 绘制点击反馈点
|
||||
if (g_Touch.down) {
|
||||
ImGui::GetForegroundDrawList()->AddCircleFilled(io.MousePos, 20.0f, IM_COL32(0, 255, 0, 200));
|
||||
}
|
||||
|
||||
// --- 菜单 UI ---
|
||||
// 初始化基准窗口宽度 (屏幕宽度的 40%)
|
||||
if (g_BaseWindowWidth == 0.0f) {
|
||||
g_BaseWindowWidth = w * 0.4f;
|
||||
}
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(30, 30), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(w * 0.6f, h * 0.5f), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(g_BaseWindowWidth, h * 0.5f), ImGuiCond_FirstUseEver);
|
||||
|
||||
// 原汁原味的 ImGui 窗口
|
||||
// 原生窗口(允许折叠,允许拖拽拉伸)
|
||||
if (ImGui::Begin("Zero-Config Engine")) {
|
||||
|
||||
ImGui::TextColored(ImVec4(0, 1, 1, 1), "[*] Pure Native ImGui");
|
||||
// 【关键黑科技】:实时读取当前窗口的物理宽度,计算出缩放比例!
|
||||
float currentWidth = ImGui::GetWindowWidth();
|
||||
g_DynamicScale = currentWidth / g_BaseWindowWidth;
|
||||
|
||||
// 限制缩放极值,防止拉得太大卡死或者太小看不见
|
||||
if (g_DynamicScale < 0.4f) g_DynamicScale = 0.4f;
|
||||
if (g_DynamicScale > 3.0f) g_DynamicScale = 3.0f;
|
||||
|
||||
ImGui::TextColored(ImVec4(0, 1, 1, 1), "[*] Magic Resize Active");
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::Text("Render Surface: %.0f x %.0f", g_Touch.renderW, g_Touch.renderH);
|
||||
@ -145,21 +164,21 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
|
||||
} else {
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "Physical Screen: %.0f x %.0f (Fallback)", g_Touch.physW, g_Touch.physH);
|
||||
}
|
||||
|
||||
ImGui::Text("Current Scale: X=%.4f, Y=%.4f",
|
||||
g_Touch.renderW / g_Touch.physW,
|
||||
g_Touch.renderH / g_Touch.physH);
|
||||
|
||||
ImGui::Separator();
|
||||
ImGui::Text("- Drag bottom-right corner to resize.");
|
||||
ImGui::Text("- Double-click title bar to collapse.");
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "-> Drag bottom-right corner to Zoom UI!");
|
||||
ImGui::TextColored(ImVec4(1, 1, 0, 1), "-> Double-click title bar to collapse.");
|
||||
ImGui::Separator();
|
||||
|
||||
static bool esp_enabled = true;
|
||||
ImGui::Checkbox("Enable ESP (Demo)", &esp_enabled);
|
||||
|
||||
ImGui::End();
|
||||
|
||||
static float test_slider = 50.0f;
|
||||
ImGui::SliderFloat("FOV Size", &test_slider, 0.0f, 100.0f);
|
||||
}
|
||||
|
||||
// 将 End 放在 if 外面,彻底修复折叠报错问题
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Render();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user