Skip to content

Commit

Permalink
Fix window placement for different monitor scales.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainProton42 committed Aug 7, 2021
1 parent fd1aa93 commit 7f75e68
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/godot_holoplay/HoloPlayVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,11 @@ void HoloPlayVolume::_notification(int what) {
ERR_PRINT("Could not create GLFW window!");
return;
}
glfwSetWindowSize(window, screen_w, screen_h);
glfwSetWindowPos(window, win_x+1, win_y+1);

glfwGetWindowContentScale(window, &scale_x, &scale_y);
glfwSetWindowContentScaleCallback(window, HoloPlayVolume::static_window_content_scale_callback);
glfwSetWindowPos(window, scale_x*win_x+1, scale_y*win_y+1);
glfwSetWindowSize(window, scale_x*screen_w, scale_y*screen_h);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetWindowUserPointer(window, (void *)this);
glfwSetWindowFocusCallback(window, HoloPlayVolume::static_window_focus_callback);
Expand Down Expand Up @@ -476,8 +479,9 @@ void HoloPlayVolume::update_device_properties() {
create_viewports_and_cameras();

if (window) {
glfwSetWindowSize(window, screen_w, screen_h);
glfwSetWindowPos(window, win_x+1, win_y+1);
glfwGetWindowContentScale(window, &scale_x, &scale_y);
glfwSetWindowPos(window, scale_x*win_x+1, scale_y*win_y+1);
glfwSetWindowSize(window, scale_x*screen_w, scale_y*screen_h);
}

// Resize quilt texture.
Expand Down Expand Up @@ -615,7 +619,8 @@ void HoloPlayVolume::render_frame() {

glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind quilt_fbo.

glViewport(0, 0, screen_w, screen_h);

glViewport(0, 0, scale_x*screen_w, scale_y*screen_h);

// Create light field.
glBindTexture(GL_TEXTURE_2D, quilt_tex);
Expand Down Expand Up @@ -659,13 +664,25 @@ void HoloPlayVolume::static_window_focus_callback(GLFWwindow *window, int focuse
hpv->window_focus_callback((bool)focused);
}

void HoloPlayVolume::static_window_content_scale_callback(GLFWwindow* window, float xscale, float yscale) {
HoloPlayVolume *hpv = (HoloPlayVolume *)glfwGetWindowUserPointer(window);
hpv->window_content_scale_callback(xscale, yscale);
}

void HoloPlayVolume::window_focus_callback(bool focused) {
if (focused) {
HWND hwnd = (HWND)OS::get_singleton()->get_native_handle(OS::WINDOW_HANDLE);
SetActiveWindow(hwnd);
}
}

void HoloPlayVolume::window_content_scale_callback(float xscale, float yscale) {
scale_x = xscale;
scale_y = yscale;
glfwSetWindowPos(window, scale_x*win_x+1, scale_y*win_y+1);
glfwSetWindowSize(window, scale_x*screen_w, scale_y*screen_h);
}

void HoloPlayVolume::grab_mouse() {
if (!grabbed_display) { // if no display has grabbed the mouse, change mouse mode
Input *input = Input::get_singleton();
Expand Down
4 changes: 4 additions & 0 deletions src/godot_holoplay/HoloPlayVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class HoloPlayVolume : public Spatial {

HDC hdc;
GLFWwindow* window = nullptr;
float scale_x = 1.0;
float scale_y = 1.0;

std::vector<RID> viewports;
std::vector<RID> cameras;
Expand Down Expand Up @@ -147,7 +149,9 @@ class HoloPlayVolume : public Spatial {
static HoloPlayVolume *grabbed_display;

static void static_window_focus_callback(GLFWwindow* window, int focused);
static void static_window_content_scale_callback(GLFWwindow* window, float xscale, float yscale);
void window_focus_callback(bool focused);
void window_content_scale_callback(float xscale, float yscale);

Vector2 mouse_pos;
Input::MouseMode orig_mouse_mode;
Expand Down

0 comments on commit 7f75e68

Please sign in to comment.