Skip to content

Commit

Permalink
+ scale window sizes according to content scale changes
Browse files Browse the repository at this point in the history
+ minor refactor (e.g. make WindowController a singleton)
  • Loading branch information
chrxh committed Jul 18, 2023
1 parent 4b212c6 commit 6c86592
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 82 deletions.
2 changes: 1 addition & 1 deletion source/Base/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Const
{
std::string const ProgramVersion = "4.0.0.beta.24";
std::string const ProgramVersion = "4.0.0.beta.25";

std::string const BasePath = "resources/";

Expand Down
8 changes: 8 additions & 0 deletions source/Gui/AlienWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "GlobalSettings.h"
#include "StyleRepository.h"
#include "WindowController.h"

_AlienWindow::_AlienWindow(std::string const& title, std::string const& settingsNode, bool defaultOn)
: _title(title)
Expand All @@ -28,6 +29,13 @@ void _AlienWindow::process()

ImGui::SetNextWindowBgAlpha(Const::WindowAlpha * ImGui::GetStyle().Alpha);
if (ImGui::Begin(_title.c_str(), &_on)) {
if (!_sizeInitialized) {
auto size = ImGui::GetWindowSize();
auto& windowController = WindowController::getInstance();
auto factor = windowController.getContentScaleFactor() / windowController.getLastContentScaleFactor();
ImGui::SetWindowSize({size.x * factor, size.y * factor});
_sizeInitialized = true;
}
processIntern();
}
ImGui::End();
Expand Down
1 change: 1 addition & 0 deletions source/Gui/AlienWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class _AlienWindow
virtual void processBackground() {}
virtual void processActivated() {}

bool _sizeInitialized = false;
bool _on = false;
std::string _title;
std::string _settingsNode;
Expand Down
3 changes: 1 addition & 2 deletions source/Gui/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ using SelectionWindow = std::shared_ptr<_SelectionWindow>;
class _PatternEditorWindow;
using PatternEditorWindow = std::shared_ptr<_PatternEditorWindow>;

class _WindowController;
using WindowController = std::shared_ptr<_WindowController>;
class WindowController;

class _SavePatternDialog;
using SavePatternDialog = std::shared_ptr<_SavePatternDialog>;
Expand Down
28 changes: 14 additions & 14 deletions source/Gui/DisplaySettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace
auto const RightColumnWidth = 185.0f;
}

_DisplaySettingsDialog::_DisplaySettingsDialog(WindowController const& windowController)
: _windowController(windowController)
_DisplaySettingsDialog::_DisplaySettingsDialog()
{
auto primaryMonitor = glfwGetPrimaryMonitor();
_videoModes = glfwGetVideoModes(primaryMonitor, &_videoModesCount);
Expand All @@ -37,14 +36,14 @@ void _DisplaySettingsDialog::process()

ImGui::OpenPopup("Display settings");
if (ImGui::BeginPopupModal("Display settings", NULL, ImGuiWindowFlags_None)) {
auto isFullscreen = !_windowController->isWindowedMode();
auto isFullscreen = !WindowController::getInstance().isWindowedMode();

if(AlienImGui::ToggleButton(AlienImGui::ToggleButtonParameters().name("Full screen"), isFullscreen)) {
if (isFullscreen) {
setFullscreen(_selectionIndex);
} else {
_origSelectionIndex = _selectionIndex;
_windowController->setWindowedMode();
WindowController::getInstance().setWindowedMode();
}
}

Expand All @@ -62,7 +61,7 @@ void _DisplaySettingsDialog::process()
}
ImGui::EndDisabled();

auto fps = _windowController->getFps();
auto fps = WindowController::getInstance().getFps();
if (AlienImGui::SliderInt(
AlienImGui::SliderIntParameters()
.name("Frames per second")
Expand All @@ -72,7 +71,7 @@ void _DisplaySettingsDialog::process()
.max(100)
.tooltip("A high frame rate leads to a greater GPU workload for rendering and thus lowers the simulation speed (time steps per second)."),
&fps)) {
_windowController->setFps(fps);
WindowController::getInstance().setFps(fps);
}

AlienImGui::Separator();
Expand All @@ -87,8 +86,8 @@ void _DisplaySettingsDialog::process()
if (AlienImGui::Button("Cancel")) {
ImGui::CloseCurrentPopup();
_show = false;
_windowController->setMode(_origMode);
_windowController->setFps(_origFps);
WindowController::getInstance().setMode(_origMode);
WindowController::getInstance().setFps(_origFps);
_selectionIndex = _origSelectionIndex;
}

Expand All @@ -101,16 +100,16 @@ void _DisplaySettingsDialog::show()
_show = true;
_selectionIndex = getSelectionIndex();
_origSelectionIndex = _selectionIndex;
_origMode = _windowController->getMode();
_origFps = _windowController->getFps();
_origMode = WindowController::getInstance().getMode();
_origFps = WindowController::getInstance().getFps();
}

void _DisplaySettingsDialog::setFullscreen(int selectionIndex)
{
if (0 == selectionIndex) {
_windowController->setDesktopMode();
WindowController::getInstance().setDesktopMode();
} else {
_windowController->setUserDefinedResolution(_videoModes[selectionIndex - 1]);
WindowController::getInstance().setUserDefinedResolution(_videoModes[selectionIndex - 1]);
}
}

Expand All @@ -126,8 +125,9 @@ namespace
int _DisplaySettingsDialog::getSelectionIndex() const
{
auto result = 0;
if (!_windowController->isWindowedMode() && !_windowController->isDesktopMode()) {
auto userMode = _windowController->getUserDefinedResolution();
auto& windowController = WindowController::getInstance();
if (!windowController.isWindowedMode() && !windowController.isDesktopMode()) {
auto userMode = windowController.getUserDefinedResolution();
for (int i = 0; i < _videoModesCount; ++i) {
if (_videoModes[i] == userMode) {
return i + 1;
Expand Down
4 changes: 1 addition & 3 deletions source/Gui/DisplaySettingsDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class _DisplaySettingsDialog
{
public:
_DisplaySettingsDialog(WindowController const& windowController);
_DisplaySettingsDialog();
~_DisplaySettingsDialog();

void process();
Expand All @@ -17,8 +17,6 @@ class _DisplaySettingsDialog
int getSelectionIndex() const;
std::vector<std::string> createVideoModeStrings() const;

WindowController _windowController;

bool _show = false;
std::string _origMode;
int _origSelectionIndex;
Expand Down
19 changes: 10 additions & 9 deletions source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ _MainWindow::_MainWindow(SimulationController const& simController, SimpleLogger

auto glfwVersion = initGlfw();

_windowController = std::make_shared<_WindowController>();
WindowController::getInstance().init();

auto windowData = _windowController->getWindowData();
auto windowData = WindowController::getInstance().getWindowData();
glfwSetFramebufferSizeCallback(windowData.window, framebuffer_size_callback);
glfwSwapInterval(1); //enable vsync

Expand All @@ -123,7 +123,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, SimpleLogger
throw std::runtime_error("Failed to initialize GLAD");
}

_viewport = std::make_shared<_Viewport>(_windowController);
_viewport = std::make_shared<_Viewport>();
_uiController = std::make_shared<_UiController>();
_autosaveController = std::make_shared<_AutosaveController>(_simController, _viewport);

Expand All @@ -148,7 +148,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, SimpleLogger
_newSimulationDialog = std::make_shared<_NewSimulationDialog>(_simController, _temporalControlWindow, _viewport, _statisticsWindow);
_openSimulationDialog = std::make_shared<_OpenSimulationDialog>(_simController, _temporalControlWindow, _statisticsWindow, _viewport);
_saveSimulationDialog = std::make_shared<_SaveSimulationDialog>(_simController, _viewport);
_displaySettingsDialog = std::make_shared<_DisplaySettingsDialog>(_windowController);
_displaySettingsDialog = std::make_shared<_DisplaySettingsDialog>();
_patternAnalysisDialog = std::make_shared<_PatternAnalysisDialog>(_simController);
_fpsController = std::make_shared<_FpsController>();
_browserWindow = std::make_shared<_BrowserWindow>(_simController, _networkController, _statisticsWindow, _viewport, _temporalControlWindow);
Expand Down Expand Up @@ -226,7 +226,7 @@ void _MainWindow::mainLoop()

void _MainWindow::shutdown()
{
_windowController->shutdown();
WindowController::getInstance().shutdown();
_autosaveController->shutdown();

ImGui_ImplOpenGL3_Shutdown();
Expand Down Expand Up @@ -353,7 +353,7 @@ void _MainWindow::renderSimulation()
}
ImGui::Render();

_fpsController->processForceFps(_windowController->getFps());
_fpsController->processForceFps(WindowController::getInstance().getFps());

ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(_window);
Expand Down Expand Up @@ -673,10 +673,11 @@ void _MainWindow::processMenubar()
_displaySettingsDialog->show();
}
if (ImGui::IsKeyPressed(GLFW_KEY_F7)) {
if (_windowController->isDesktopMode()) {
_windowController->setWindowedMode();
auto& windowController = WindowController::getInstance();
if (windowController.isDesktopMode()) {
windowController.setWindowedMode();
} else {
_windowController->setDesktopMode();
windowController.setDesktopMode();
}
}
if (io.KeyAlt && ImGui::IsKeyPressed(GLFW_KEY_K)) {
Expand Down
1 change: 0 additions & 1 deletion source/Gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class _MainWindow
ImageToPatternDialog _imageToPatternDialog;

ModeController _modeController;
WindowController _windowController;
SimulationController _simController;
StartupController _startupController;
AutosaveController _autosaveController;
Expand Down
40 changes: 21 additions & 19 deletions source/Gui/StyleRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <imgui_freetype.h>
#include <implot.h>

#include "GlobalSettings.h"
#include "WindowController.h"
#include "Base/Resources.h"

#include "Fonts/DroidSans.h"
Expand All @@ -26,55 +28,55 @@ StyleRepository& StyleRepository::getInstance()

void StyleRepository::init()
{
float temp;
glfwGetMonitorContentScale(
glfwGetPrimaryMonitor(), &_contentScaleFactor, &temp); //consider only horizontal content scale
auto scaleFactor = WindowController::getInstance().getContentScaleFactor();

auto& style = ImGui::GetStyle();
style.ScaleAllSizes(scaleFactor);


ImGuiIO& io = ImGui::GetIO();

//default font (small with icons)
io.Fonts->AddFontFromMemoryCompressedTTF(DroidSans_compressed_data, DroidSans_compressed_size, 16.0f * _contentScaleFactor);
io.Fonts->AddFontFromMemoryCompressedTTF(DroidSans_compressed_data, DroidSans_compressed_size, 16.0f * scaleFactor);

ImFontConfig configMerge;
configMerge.MergeMode = true;
configMerge.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_LightHinting;
static const ImWchar rangesIcons[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryCompressedTTF(
FontAwesomeSolid_compressed_data,
FontAwesomeSolid_compressed_size,
16.0f * _contentScaleFactor,
FontAwesomeSolid_compressed_size, 16.0f * scaleFactor,
&configMerge,
rangesIcons);

//small bold font
_smallBoldFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSansBold_compressed_data, DroidSansBold_compressed_size, 16.0f * _contentScaleFactor);
_smallBoldFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSansBold_compressed_data, DroidSansBold_compressed_size, 16.0f * scaleFactor);

//medium bold font
_mediumBoldFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSansBold_compressed_data, DroidSansBold_compressed_size, 24.0f * _contentScaleFactor);
_mediumBoldFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSansBold_compressed_data, DroidSansBold_compressed_size, 24.0f * scaleFactor);

//medium font
_mediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSans_compressed_data, DroidSans_compressed_size, 24.0f * _contentScaleFactor);
_mediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSans_compressed_data, DroidSans_compressed_size, 24.0f * scaleFactor);

//large font
_largeFont = io.Fonts->AddFontFromMemoryCompressedTTF(
DroidSans_compressed_data, DroidSans_compressed_size, 48.0f * _contentScaleFactor);
_largeFont = io.Fonts->AddFontFromMemoryCompressedTTF(DroidSans_compressed_data, DroidSans_compressed_size, 48.0f * scaleFactor);

//icon font
_iconFont = io.Fonts->AddFontFromMemoryCompressedTTF(AlienIconFont_compressed_data, AlienIconFont_compressed_size, 24.0f * _contentScaleFactor);
_iconFont = io.Fonts->AddFontFromMemoryCompressedTTF(AlienIconFont_compressed_data, AlienIconFont_compressed_size, 24.0f * scaleFactor);

static const ImWchar rangesIcons2[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryCompressedTTF(
FontAwesomeSolid_compressed_data, FontAwesomeSolid_compressed_size, 28.0f * _contentScaleFactor, &configMerge, rangesIcons2);
FontAwesomeSolid_compressed_data, FontAwesomeSolid_compressed_size, 28.0f * scaleFactor, &configMerge, rangesIcons2);
io.Fonts->Build();

//monospace medium font
_monospaceMediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(Cousine_Regular_compressed_data, Cousine_Regular_compressed_size, 14.0f * _contentScaleFactor);
_monospaceMediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(Cousine_Regular_compressed_data, Cousine_Regular_compressed_size, 14.0f * scaleFactor);

//monospace large font
_monospaceLargeFont = io.Fonts->AddFontFromMemoryCompressedTTF(Cousine_Regular_compressed_data, Cousine_Regular_compressed_size, 128.0f * _contentScaleFactor);
_monospaceLargeFont = io.Fonts->AddFontFromMemoryCompressedTTF(Cousine_Regular_compressed_data, Cousine_Regular_compressed_size, 128.0f * scaleFactor);

_reefMediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(Reef_compressed_data, Reef_compressed_size, 24.0f * _contentScaleFactor);
_reefLargeFont = io.Fonts->AddFontFromMemoryCompressedTTF(Reef_compressed_data, Reef_compressed_size, 64.0f * _contentScaleFactor);
_reefMediumFont = io.Fonts->AddFontFromMemoryCompressedTTF(Reef_compressed_data, Reef_compressed_size, 24.0f * scaleFactor);
_reefLargeFont = io.Fonts->AddFontFromMemoryCompressedTTF(Reef_compressed_data, Reef_compressed_size, 64.0f * scaleFactor);

ImPlot::GetStyle().AntiAliasedLines = true;
}
Expand Down Expand Up @@ -126,10 +128,10 @@ ImFont* StyleRepository::getReefLargeFont() const

float StyleRepository::scale(float value) const
{
return _contentScaleFactor * value;
return WindowController::getInstance().getContentScaleFactor() * value;
}

float StyleRepository::scaleInverse(float value) const
{
return _contentScaleFactor / value;
return WindowController::getInstance().getContentScaleFactor() / value;
}
1 change: 0 additions & 1 deletion source/Gui/StyleRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class StyleRepository
private:
StyleRepository() = default;

float _contentScaleFactor = 1.0f;
ImFont* _iconFont = nullptr;
ImFont* _smallBoldFont = nullptr;
ImFont* _mediumBoldFont = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/Viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "WindowController.h"

_Viewport::_Viewport(WindowController const& windowController)
_Viewport::_Viewport()
{
_viewSize = windowController->getStartupWindowSize();
_viewSize = WindowController::getInstance().getStartupWindowSize();
}

float _Viewport::getZoomFactor() const
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/Viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class _Viewport
{
public:
_Viewport(WindowController const& windowController);
_Viewport();

float getZoomFactor() const;
void setZoomFactor(float zoomFactor);
Expand Down
Loading

0 comments on commit 6c86592

Please sign in to comment.