forked from projectM-visualizer/frontend-sdl-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectMGUI.cpp
232 lines (182 loc) · 5.85 KB
/
ProjectMGUI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "ProjectMGUI.h"
#include "AnonymousProFont.h"
#include "LiberationSansFont.h"
#include "ProjectMWrapper.h"
#include "SDLRenderingWindow.h"
#include "imgui.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_sdl2.h"
#include <Poco/NotificationCenter.h>
#include <Poco/Util/Application.h>
#include <utility>
const char* ProjectMGUI::name() const
{
return "Preset Selection GUI";
}
void ProjectMGUI::initialize(Poco::Util::Application& app)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
Poco::Path userConfigurationDir = Poco::Path::configHome();
userConfigurationDir.makeDirectory().append("projectM/");
userConfigurationDir.setFileName(app.config().getString("application.baseName") + ".UI.ini");
_uiIniFileName = userConfigurationDir.toString();
io.IniFilename = _uiIniFileName.c_str();
ImGui::StyleColorsDark();
auto& renderingWindow = Poco::Util::Application::instance().getSubsystem<SDLRenderingWindow>();
auto& projectMWrapper = Poco::Util::Application::instance().getSubsystem<ProjectMWrapper>();
_projectMWrapper = &projectMWrapper;
_renderingWindow = renderingWindow.GetRenderingWindow();
_glContext = renderingWindow.GetGlContext();
ImGui_ImplSDL2_InitForOpenGL(_renderingWindow, _glContext);
ImGui_ImplOpenGL3_Init("#version 150"); // changing this from '130' to '150' fixes the UI not displaying on MacOS 15
UpdateFontSize();
// Set a sensible minimum window size to prevent layout assertions
auto& style = ImGui::GetStyle();
style.WindowMinSize = {128, 128};
Poco::NotificationCenter::defaultCenter().addObserver(_displayToastNotificationObserver);
}
void ProjectMGUI::uninitialize()
{
Poco::NotificationCenter::defaultCenter().removeObserver(_displayToastNotificationObserver);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
_projectMWrapper = nullptr;
_renderingWindow = nullptr;
_glContext = nullptr;
}
void ProjectMGUI::UpdateFontSize()
{
ImGuiIO& io = ImGui::GetIO();
auto displayIndex = SDL_GetWindowDisplayIndex(_renderingWindow);
if (displayIndex < 0)
{
poco_debug_f1(_logger, "Could not get display index for application window: %s", std::string(SDL_GetError()));
return;
}
auto newScalingFactor = GetScalingFactor();
// Only interested in changes of .05 or more
if (std::abs(_textScalingFactor - newScalingFactor) < 0.05)
{
return;
}
poco_debug_f3(_logger, "Scaling factor change for display %?d: %hf -> %hf", displayIndex, _textScalingFactor, newScalingFactor);
_textScalingFactor = newScalingFactor;
ImFontConfig config;
config.MergeMode = true;
io.Fonts->Clear();
_uiFont = io.Fonts->AddFontFromMemoryCompressedTTF(&AnonymousPro_compressed_data, AnonymousPro_compressed_size, floor(24.0f * _textScalingFactor));
_toastFont = io.Fonts->AddFontFromMemoryCompressedTTF(&LiberationSans_compressed_data, LiberationSans_compressed_size, floor(40.0f * _textScalingFactor));
io.Fonts->Build();
ImGui_ImplOpenGL3_CreateFontsTexture();
ImGui::GetStyle().ScaleAllSizes(1.0);
}
void ProjectMGUI::ProcessInput(const SDL_Event& event)
{
ImGui_ImplSDL2_ProcessEvent(&event);
}
void ProjectMGUI::Toggle()
{
_visible = !_visible;
}
void ProjectMGUI::Visible(bool visible)
{
_visible = visible;
}
bool ProjectMGUI::Visible() const
{
return _visible;
}
void ProjectMGUI::Draw()
{
// Don't render UI at all if there's no need.
if (!_toast && !_visible)
{
return;
}
ImGui_ImplSDL2_NewFrame();
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
float secondsSinceLastFrame = .0f;
if (_lastFrameTicks == 0)
{
_lastFrameTicks = SDL_GetTicks64();
}
else
{
auto currentFrameTicks = SDL_GetTicks64();
secondsSinceLastFrame = static_cast<float>(currentFrameTicks - _lastFrameTicks) * .001f;
_lastFrameTicks = currentFrameTicks;
}
if (_toast)
{
if (!_toast->Draw(secondsSinceLastFrame))
{
_toast.reset();
}
}
if (_visible)
{
_mainMenu.Draw();
_settingsWindow.Draw();
_aboutWindow.Draw();
_helpWindow.Draw();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
bool ProjectMGUI::WantsKeyboardInput()
{
auto& io = ImGui::GetIO();
return io.WantCaptureKeyboard;
}
bool ProjectMGUI::WantsMouseInput()
{
auto& io = ImGui::GetIO();
return io.WantCaptureMouse;
}
void ProjectMGUI::PushToastFont()
{
ImGui::PushFont(_toastFont);
}
void ProjectMGUI::PushUIFont()
{
ImGui::PushFont(_uiFont);
}
void ProjectMGUI::PopFont()
{
ImGui::PopFont();
}
void ProjectMGUI::ShowSettingsWindow()
{
_settingsWindow.Show();
}
void ProjectMGUI::ShowAboutWindow()
{
_aboutWindow.Show();
}
void ProjectMGUI::ShowHelpWindow()
{
_helpWindow.Show();
}
float ProjectMGUI::GetScalingFactor()
{
int windowWidth;
int windowHeight;
int renderWidth;
int renderHeight;
SDL_GetWindowSize(_renderingWindow, &windowWidth, &windowHeight);
SDL_GL_GetDrawableSize(_renderingWindow, &renderWidth, &renderHeight);
// If the OS has a scaled UI, this will return the inverse factor. E.g. if the display is scaled to 200%,
// the renderWidth (in actual pixels) will be twice as much as the "virtual" unscaled window width.
return ((static_cast<float>(windowWidth) / static_cast<float>(renderWidth)) + (static_cast<float>(windowHeight) / static_cast<float>(renderHeight))) * 0.5f;
}
void ProjectMGUI::DisplayToastNotificationHandler(const Poco::AutoPtr<DisplayToastNotification>& notification)
{
if (Poco::Util::Application::instance().config().getBool("projectM.displayToasts", true))
{
_toast = std::make_unique<ToastMessage>(notification->ToastText(), 3.0f);
}
}