ice1000 / jimgui

:sparkling_heart: Pure Java binding for dear-imgui
Apache License 2.0
186 stars 13 forks source link

Window sizing APIs #36

Closed ice1000 closed 3 years ago

ice1000 commented 5 years ago

The APIs are easy adding. I have another private project that did all of these. I'll post them here as some notes, contributions are welcomed.

For Windows, the window resizing is irrelevant to dx9, but win32 API:


auto huge::life_cycle::getWindowSize(PlatformObject object) -> ImVec2 {
    auto &&rect = getWindowBounds(object);
    return { rect.z, rect.w };
}

auto huge::life_cycle::getWindowPos(PlatformObject object) -> ImVec2 {
    auto &&rect = getWindowBounds(object);
    return { rect.x, rect.y };
}

auto huge::life_cycle::getWindowBounds(PlatformObject object) -> ImVec4 {
    auto wc = reinterpret_cast<Ptr<WindowsClassEx>> (object);
    RECT rect{};
    GetWindowRect(wc->hwnd, &rect);
    return {
            static_cast<float>(rect.left),
            static_cast<float>(rect.top),
            static_cast<float>(rect.right - rect.left),
            static_cast<float>(rect.bottom - rect.top),
    };
}

auto huge::life_cycle::setWindowSize(PlatformObject object, ComVec2 size) -> void {
    auto wc = reinterpret_cast<Ptr<WindowsClassEx>> (object);

    SetWindowPos(wc->hwnd, nullptr, 0, 0,
                 static_cast<int>(size.x),
                 static_cast<int>(size.y),
                 SWP_NOMOVE);
}

auto huge::life_cycle::setWindowPos(PlatformObject object, ComVec2 size) -> void {
    auto wc = reinterpret_cast<Ptr<WindowsClassEx>> (object);

    SetWindowPos(wc->hwnd, nullptr,
                 static_cast<int>(size.x),
                 static_cast<int>(size.y), 0, 0, SWP_NOSIZE);
}

auto huge::life_cycle::setWindowBounds(PlatformObject object, ComVec4 bounds) -> void {
    auto wc = reinterpret_cast<Ptr<WindowsClassEx>> (object);

    SetWindowPos(wc->hwnd, nullptr,
                 static_cast<int>(bounds.x),
                 static_cast<int>(bounds.y),
                 static_cast<int>(bounds.z),
                 static_cast<int>(bounds.w),
                 0);
}

*nix-wise it's related to GLFW:


auto huge::life_cycle::getWindowSize(PlatformObject object) -> ImVec2 {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    int w, h;
    glfwGetWindowSize(window, &w, &h);
    return {static_cast<float>(w), static_cast<float>(h)};
}

auto huge::life_cycle::getWindowPos(PlatformObject object) -> ImVec2 {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    int x, y;
    glfwGetWindowPos(window, &x, &y);
    return {static_cast<float>(x), static_cast<float>(y)};
}

auto huge::life_cycle::getWindowBounds(PlatformObject object) -> ImVec4 {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    int x, y, xPlusW, yPlusH;
    glfwGetWindowFrameSize(window, &x, &y, &xPlusW, &yPlusH);
    return {
            static_cast<float>(x), static_cast<float>(y),
            static_cast<float>(xPlusW - x), static_cast<float>(yPlusH - y)
    };
}

auto huge::life_cycle::setWindowSize(PlatformObject object, ComVec2 size) -> void {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    glfwSetWindowSize(window, static_cast<int>(size.x), static_cast<int>(size.y));
}

auto huge::life_cycle::setWindowPos(PlatformObject object, ComVec2 size) -> void {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    glfwSetWindowPos(window, static_cast<int>(size.x), static_cast<int>(size.y));
}

auto huge::life_cycle::setWindowBounds(PlatformObject object, ComVec4 bounds) -> void {
    auto *window = reinterpret_cast<Ptr<GLFWwindow>> (object);
    glfwSetWindowPos(window, static_cast<int>(bounds.x), static_cast<int>(bounds.y));
    glfwSetWindowSize(window, static_cast<int>(bounds.z), static_cast<int>(bounds.w));
}

Not very sure about how the JVM API wrapper of these shits should be.

Originally posted by @ice1000 in https://github.com/ice1000/jimgui/issues/19#issuecomment-505222737

ice1000 commented 5 years ago

I was using some predefined aliases:

using ComVec4 = const ImVec4&;
using ComVec2 = const ImVec2&;

and PlatformObject for jimgui should be jlong

ice1000 commented 3 years ago

This is done. Why was I still opening this issue? Closing.