Skip to content

Commit 888503e

Browse files
authored
Added dynamic editor window size selection (#328)
* default window size selected to fix the highest available resolution in a pre-determined 16:9 resolutions list. * if no available resolution is found (work area < 640, 360), an exception is thrown.
1 parent 7e5f5e4 commit 888503e

5 files changed

Lines changed: 85 additions & 14 deletions

File tree

Sources/Overload/OvEditor/src/OvEditor/Core/Context.cpp

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,58 @@
99
#include <OvRendering/Entities/Light.h>
1010
#include <OvCore/Global/ServiceLocator.h>
1111
#include <OvTools/Utils/SystemCalls.h>
12+
#include <OvDebug/Assertion.h>
1213

1314
#include "OvEditor/Core/Context.h"
1415

1516
using namespace OvCore::Global;
1617
using namespace OvCore::ResourceManagement;
1718

19+
constexpr std::array<std::pair<int, int>, 13> kResolutions
20+
{
21+
std::make_pair(640, 360), // nHD
22+
std::make_pair(854, 480), // FWVGA
23+
std::make_pair(960, 540), // qHD
24+
std::make_pair(1024, 576), // WSVGA
25+
std::make_pair(1280, 720), // HD
26+
std::make_pair(1366, 768), // FWXGA
27+
std::make_pair(1600, 900), // HD+
28+
std::make_pair(1920, 1080), // Full HD
29+
std::make_pair(2560, 1440), // QHD
30+
std::make_pair(3200, 1800), // QHD+
31+
std::make_pair(3840, 2160), // 4K UHD
32+
std::make_pair(5120, 2880), // 5K
33+
std::make_pair(7680, 4320), // 8K UHD
34+
};
35+
36+
std::array<int, 4> FindBestFitWindowSizeAndPosition(std::array<int, 4> p_workAreaSize)
37+
{
38+
// Extract work area dimensions
39+
int workAreaX = p_workAreaSize[0];
40+
int workAreaY = p_workAreaSize[1];
41+
int workAreaWidth = p_workAreaSize[2];
42+
int workAreaHeight = p_workAreaSize[3];
43+
44+
// Iterate over available resolutions
45+
for (auto it = kResolutions.rbegin(); it != kResolutions.rend(); ++it)
46+
{
47+
int width = it->first;
48+
int height = it->second;
49+
50+
// Check if resolution fits within work area
51+
if (width <= workAreaWidth && height <= workAreaHeight)
52+
{
53+
// Center the resolution within the work area
54+
int posX = workAreaX + workAreaWidth / 2 - width / 2;
55+
int posY = workAreaY + workAreaHeight / 2 - height / 2;
56+
57+
return { posX, posY, width, height };
58+
}
59+
}
60+
61+
OVASSERT(false, "No resolution found to fit the work area");
62+
}
63+
1864
OvEditor::Core::Context::Context(const std::string& p_projectPath, const std::string& p_projectName) :
1965
projectPath(p_projectPath),
2066
projectName(p_projectName),
@@ -42,23 +88,21 @@ OvEditor::Core::Context::Context(const std::string& p_projectPath, const std::st
4288
OvWindowing::Settings::DeviceSettings deviceSettings;
4389
deviceSettings.contextMajorVersion = 4;
4490
deviceSettings.contextMinorVersion = 3;
45-
windowSettings.title = "Overload";
46-
windowSettings.width = 1600;
47-
windowSettings.height = 900;
4891

4992
/* Window creation */
5093
device = std::make_unique<OvWindowing::Context::Device>(deviceSettings);
94+
const auto workAreaSize = device->GetWorkAreaSize();
95+
const auto bestFitWindowSizeAndPosition = FindBestFitWindowSizeAndPosition(workAreaSize);
96+
windowSettings.title = "Overload";
97+
windowSettings.x = bestFitWindowSizeAndPosition[0];
98+
windowSettings.y = bestFitWindowSizeAndPosition[1];
99+
windowSettings.width = bestFitWindowSizeAndPosition[2];
100+
windowSettings.height = bestFitWindowSizeAndPosition[3];
51101
window = std::make_unique<OvWindowing::Window>(*device, windowSettings);
52102
std::vector<uint64_t> iconRaw = { 0,0,144115188614240000,7500771567664627712,7860776967494637312,0,0,0,0,7212820467466371072,11247766461832697600,14274185407633888512,12905091124788992000,5626708973701824512,514575842263176960,0,0,6564302121125019648,18381468271671515136,18381468271654737920,18237353083595659264,18165295488836311040,6708138037527189504,0,4186681893338480640,7932834557741046016,17876782538917681152,11319824055216379904,15210934132358518784,18381468271520454400,1085667680982603520,0,18093237891929479168,18309410677600032768,11391881649237530624,7932834561381570304,17300321784231761408,15210934132375296000,8293405106311272448,2961143145139082752,16507969723533236736,17516777143216379904,10671305705855129600,7356091234422036224,16580027318695106560,2240567205413984000,18381468271470188544,10959253511276599296,4330520004484136960,10815138323200743424,11607771853338181632,8364614976649238272,17444719546862998784,2669156352,18381468269893064448,6419342512197474304,11103650170688640000,6492244531366860800,14346241902646925312,13841557270159628032,7428148827772098304,3464698581331941120,18381468268953606144,1645680384,18381468271554008832,7140201027266418688,5987558797656659712,17588834734687262208,7284033640602212096,14273902834169157632,18381468269087692288,6852253225049397248,17732667349600245504,16291515470083266560,10022503688432981760,11968059825861367552,9733991836700645376,14850363587428816640,18381468271168132864,16147400282007410688,656430432014827520,18381468270950094848,15715054717226194944,72057596690306560,11823944635485519872,15859169905251653376,17084149004500473856,8581352906816952064,2527949855582584832,18381468271419856896,8581352907253225472,252776704,1376441223417430016,14994761349590357760,10527190521537370112,0,9806614576878321664,18381468271671515136,17156206598538401792,6059619689256392448,10166619973990488064,18381468271403079424,17444719549178451968,420746240,870625192710242304,4906133035823863552,18381468269289150464,18381468271671515136,18381468271671515136,9950729769032620032,14778305994951169792,269422336,0,0,18381468268785833984,8941923452686178304,18381468270950094848,3440842496,1233456333565402880,0,0,0,11823944636091210240,2383877888,16724143605745719296,2316834816,0,0 };
53103
window->SetIconFromMemory(reinterpret_cast<uint8_t*>(iconRaw.data()), 16, 16);
54104
inputManager = std::make_unique<OvWindowing::Inputs::InputManager>(*window);
55105
window->MakeCurrentContext();
56-
57-
/* Center Window */
58-
auto [monWidth, monHeight] = device->GetMonitorSize();
59-
auto [winWidth, winHeight] = window->GetSize();
60-
window->SetPosition(monWidth / 2 - winWidth / 2, monHeight / 2 - winHeight / 2);
61-
62106
device->SetVsync(true);
63107

64108
/* Graphics context creation */

Sources/Overload/OvWindowing/include/OvWindowing/Context/Device.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include <unordered_map>
10+
#include <array>
1011

1112
#include <OvTools/Eventing/Event.h>
1213

@@ -46,6 +47,11 @@ namespace OvWindowing::Context
4647
*/
4748
std::pair<int16_t, int16_t> GetMonitorSize() const;
4849

50+
/**
51+
* Return the size, in pixels, of the work area of the primary monitor
52+
*/
53+
std::array<int, 4> GetWorkAreaSize() const;
54+
4955
/**
5056
* Return an instance of GLFWcursor corresponding to the given shape
5157
* @param p_cursorShape

Sources/Overload/OvWindowing/include/OvWindowing/Settings/WindowSettings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ namespace OvWindowing::Settings
3939
*/
4040
uint16_t height = 600;
4141

42+
/**
43+
* X position of the window
44+
*/
45+
int16_t x = 0;
46+
47+
/**
48+
* Y position of the window
49+
*/
50+
int16_t y = 0;
51+
4252
/**
4353
* Minimum width of the window.
4454
* Use WindowSettings::DontCare to disable limit

Sources/Overload/OvWindowing/src/OvWindowing/Context/Device.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ std::pair<int16_t, int16_t> OvWindowing::Context::Device::GetMonitorSize() const
5656
return std::pair<int16_t, int16_t>(static_cast<int16_t>(mode->width), static_cast<int16_t>(mode->height));
5757
}
5858

59+
std::array<int, 4> OvWindowing::Context::Device::GetWorkAreaSize() const
60+
{
61+
std::array<int, 4> workArea;
62+
63+
glfwGetMonitorWorkarea(
64+
glfwGetPrimaryMonitor(),
65+
workArea.data(),
66+
workArea.data() + 1,
67+
workArea.data() + 2,
68+
workArea.data() + 3);
69+
70+
return workArea;
71+
}
72+
5973
GLFWcursor * OvWindowing::Context::Device::GetCursorInstance(Cursor::ECursorShape p_cursorShape) const
6074
{
6175
return m_cursors.at(p_cursorShape);

Sources/Overload/OvWindowing/src/OvWindowing/Window.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ OvWindowing::Window::Window(const Context::Device& p_device, const Settings::Win
2020
m_device(p_device),
2121
m_title(p_windowSettings.title),
2222
m_size{ p_windowSettings.width, p_windowSettings.height },
23+
m_position{ p_windowSettings.x, p_windowSettings.y },
2324
m_minimumSize { p_windowSettings.minimumWidth, p_windowSettings.minimumHeight },
2425
m_maximumSize { p_windowSettings.maximumWidth, p_windowSettings.maximumHeight },
2526
m_fullscreen(p_windowSettings.fullscreen),
@@ -328,11 +329,7 @@ void OvWindowing::Window::CreateGlfwWindow(const Settings::WindowSettings& p_win
328329
else
329330
{
330331
UpdateSizeLimit();
331-
332-
auto[x, y] = GetPosition();
333-
m_position.first = x;
334-
m_position.second = y;
335-
332+
SetPosition(m_position.first, m_position.second);
336333
__WINDOWS_MAP[m_glfwWindow] = this;
337334
}
338335
}

0 commit comments

Comments
 (0)