AronaCore/core/application/window_manager.cpp
2024-02-28 11:20:18 +08:00

91 lines
2.4 KiB
C++

#include "window_manager.h"
#include <ranges>
#include "audio/plugin_host/plugin_host.h"
window_manager::window_manager() {
main_window_ = nullptr;
}
void window_manager::init(singleton_initliazer& initliazer) {
singleton_t<window_manager>::init(initliazer);
start_idle_thread();
}
void window_manager::release() {
singleton_t<window_manager>::release();
if (main_window_)
glfwDestroyWindow(main_window_);
stop_idle_thread();
}
void window_manager::tick() {
glfwPollEvents();
if (should_close()) {
destroy_all_plugin_host_window();
}
}
void window_manager::destroy_all_plugin_host_window() {
for (const auto& window: host_window_map_ | std::views::values) {
glfwDestroyWindow(window);
}
host_window_map_.clear();
}
GLFWwindow* window_manager::create_main_window(const char* title, int width, int height) {
if (!main_window_)
main_window_ = glfwCreateWindow(width, height, title, nullptr, nullptr);
return main_window_;
}
GLFWwindow* window_manager::create_plugin_host_window(plugin_host* host) {
if (!host->has_editor())
return nullptr;
if (host_window_map_.contains(host))
return host_window_map_[host];
auto editor_size = host->get_editor_size();
auto new_window = glfwCreateWindow(editor_size.x, editor_size.y, host->name.c_str(), nullptr, nullptr);
host_window_map_[host] = new_window;
return new_window;
}
void window_manager::destroy_plugin_host_window(plugin_host* host) {
if (!host_window_map_.contains(host))
return;
glfwDestroyWindow(host_window_map_[host]);
host_window_map_.erase(host);
}
void window_manager::resize_plugin_host_window(plugin_host* host, int width, int height) {
if (!host_window_map_.contains(host))
return;
glfwSetWindowSize(host_window_map_[host], width, height);
}
void window_manager::idle_plugin_host_window() const {
for (const auto& window_map: host_window_map_) {
window_map.first->idle_editor();
}
}
void window_manager::start_idle_thread() {
idle_thread_ = std::thread(&window_manager::idle_thread_func, this);
idle_thread_.detach();
}
void window_manager::stop_idle_thread() {
idle_thread_running_ = false;
if (idle_thread_.joinable())
idle_thread_.join();
}
void window_manager::idle_thread_func() {
idle_thread_running_ = true;
while (idle_thread_running_) {
idle_plugin_host_window();
std::this_thread::yield();
}
}