diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index b3185f371..9f33892b9 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -1,479 +1,481 @@ -#include "Engine.hpp" - -#ifndef GLEW_STATIC -#define GLEW_STATIC -#endif - -#include "AssetsManagement.hpp" -#include "audio/audio.hpp" -#include "coders/commons.hpp" -#include "coders/GLSLExtension.hpp" -#include "coders/toml.hpp" -#include "coders/vector_fonts.hpp" -#include "content/ContentControl.hpp" -#include "core_defs.hpp" -#include "debug/Logger.hpp" -#include "devtools/AppScriptsControl.hpp" -#include "devtools/DebuggingServer.hpp" -#include "devtools/Editor.hpp" -#include "devtools/Project.hpp" -#include "devtools/stdin_cmd_reader.hpp" -#include "EnginePaths.hpp" -#include "frontend/locale.hpp" -#include "frontend/menu.hpp" -#include "frontend/screens/Screen.hpp" -#include "graphics/core/DrawContext.hpp" -#include "graphics/core/Shader.hpp" -#include "graphics/ui/elements/Menu.hpp" -#include "graphics/ui/GUI.hpp" -#include "io/io.hpp" -#include "io/settings_io.hpp" -#include "logic/CommandsInterpreter.hpp" -#include "logic/EngineController.hpp" -#include "logic/scripting/scripting_hud.hpp" -#include "logic/scripting/scripting.hpp" -#include "Mainloop.hpp" -#include "network/Network.hpp" -#include "ServerMainloop.hpp" -#include "util/platform.hpp" -#include "util/stringutil.hpp" -#include "window/input.hpp" -#include "window/Window.hpp" -#include "WindowControl.hpp" -#include "world/Level.hpp" - -#include -#include -#include -#include - -static debug::Logger logger("engine"); - -Engine::Engine() = default; -Engine::~Engine() = default; - -static std::unique_ptr instance = nullptr; - -Engine& Engine::getInstance() { - if (!instance) { - instance = std::make_unique(); - } - return *instance; -} - -void Engine::onContentLoad() { - editor->loadTools(); - langs::setup(langs::get_current(), paths->resPaths.collectRoots()); - - if (isHeadless()) { - return; - } - for (auto& pack : content->getAllContentPacks()) { - auto configFolder = pack.folder / "config"; - auto bindsFile = configFolder / "bindings.toml"; - logger.info() << "loading bindings: " << bindsFile.string(); - if (io::is_regular_file(bindsFile)) { - input->getBindings().read( - toml::parse( - bindsFile.string(), io::read_string(bindsFile) - ), - BindType::BIND - ); - } - } - loadAssets(); -} - -void Engine::initializeClient() { - assets = std::make_unique(*this); - windowControl = std::make_unique(*this); - auto [window, input] = windowControl->initialize(); - - this->window = std::move(window); - this->input = std::move(input); - - loadControls(); - - gui = std::make_unique(*this); - if (ENGINE_DEBUG_BUILD) { - menus::create_version_label(*gui); - } - keepAlive(settings.display.windowMode.observe( - [this](int value) { - WindowMode mode = static_cast(value); - if (mode != this->window->getMode()) { - this->window->setMode(mode); - } - }, - true - )); - keepAlive(settings.debug.doTraceShaders.observe( - [](bool value) { - Shader::preprocessor->setTraceOutput(value); - }, - true - )); - - keepAlive(this->input->addKeyCallback(Keycode::ESCAPE, [this]() { - auto& menu = *gui->getMenu(); - if (menu.hasOpenPage() && menu.back()) { - return true; - } - return false; - })); -} - -void Engine::initialize(CoreParameters coreParameters) { - params = std::move(coreParameters); - settingsHandler = std::make_unique(settings); - - logger.info() << "engine version: " << ENGINE_VERSION_STRING; - if (params.headless) { - logger.info() << "engine runs in headless mode"; - } - if (params.projectFolder.empty()) { - params.projectFolder = params.resFolder; - } - paths = std::make_unique(params); - loadProject(); - paths->setupProject(*project); - - editor = std::make_unique(*this); - cmd = std::make_unique(); - - if (project->permissions.has(Permissions::NETWORK) || - !params.debugServerString.empty()) { - network = network::Network::create(settings.network); - } - - if (!params.debugServerString.empty()) { - try { - debuggingServer = std::make_unique( - *this, params.debugServerString - ); - } catch (const std::runtime_error& err) { - throw initialize_error( - "debugging server error: " + std::string(err.what()) - ); - } - } - loadSettings(); - - controller = std::make_unique(*this); - if (!params.headless) { - initializeClient(); - } - audio::initialize( - !params.headless, - project->permissions.has(Permissions::RECORD_AUDIO), - settings.audio - ); - - vector_fonts::initialize(); - - if (settings.ui.language.get() == "auto") { - settings.ui.language.set( - langs::locale_by_envlocale(platform::detect_locale()) - ); - } - content = std::make_unique( - *project, *paths, input.get(), [this]() { onContentLoad(); } - ); - scripting::initialize(*this); - - if (!isHeadless()) { - gui->getMenu()->setPageLoader(scripting::create_page_loader()); - } - keepAlive(settings.ui.language.observe([this](auto lang) { - langs::setup(lang, paths->resPaths.collectRoots()); - }, true)); - - keepAlive(settings.audio.inputDevice.observe([](auto name) { - audio::set_input_device(name == "auto" ? "" : name); - })); - - appScripts = std::make_unique(params); - - if (params.stdinCommands) { - cmd::start_stdin_cmd_reader(*this); - } -} - -void Engine::loadSettings() { - io::path settings_file = EnginePaths::SETTINGS_FILE; - if (io::is_regular_file(settings_file)) { - logger.info() << "loading settings"; - std::string text = io::read_string(settings_file); - try { - toml::parse(*settingsHandler, settings_file.string(), text); - } catch (const parsing_error& err) { - logger.error() << err.errorLog(); - throw; - } - } -} - -void Engine::loadControls() { - io::path controls_file = EnginePaths::CONTROLS_FILE; - if (io::is_regular_file(controls_file)) { - logger.info() << "loading controls"; - std::string text = io::read_string(controls_file); - input->getBindings().read( - toml::parse(controls_file.string(), text), BindType::BIND - ); - } -} - -void Engine::updateHotkeys() { - if (input->jpressed(Keycode::F2)) { - windowControl->saveScreenshot(); - } - if (input->pressed(Keycode::LEFT_CONTROL) && input->pressed(Keycode::F3) && - input->jpressed(Keycode::U)) { - gui->toggleDebug(); - } - if (input->jpressed(Keycode::F11)) { - windowControl->toggleFullscreen(); - } -} - -void Engine::run() { - if (params.headless) { - ServerMainloop(*this).run(); - } else { - Mainloop(*this).run(); - } -} - -void Engine::postUpdate() { - if (network) { - network->update(); - } - postRunnables.run(); - scripting::process_post_runnables(); - - if (debuggingServer) { - debuggingServer->update(); - } -} - -void Engine::detachDebugger() { - debuggingServer.reset(); -} - -void Engine::applicationTick() { - appScripts->tick(); -} - -void Engine::updateFrontend() { - double delta = time.getDelta(); - assets->update(); - updateHotkeys(); - audio::update(delta); - gui->act(delta, window->getSize()); - screen->update(delta); - gui->postAct(); -} - -void Engine::nextFrame(bool waitForRefresh) { - windowControl->nextFrame(waitForRefresh); -} - -void Engine::startPauseLoop() { - assert (network != nullptr); - - bool initialCursorLocked = false; - if (!isHeadless()) { - initialCursorLocked = input->isCursorLocked(); - if (initialCursorLocked) { - input->toggleCursor(); - } - } - while (!isQuitSignal() && debuggingServer) { - network->update(); - if (debuggingServer->update()) { - break; - } - if (isHeadless()) { - platform::sleep(1.0 / params.tps * 1000); - } else { - nextFrame(false); - } - } - if (initialCursorLocked) { - input->toggleCursor(); - } -} - -void Engine::renderFrame() { - if (input->isCursorLocked() != (gui->getActiveFrame() == nullptr)) { - input->toggleCursor(); - } - screen->draw(time.getDelta()); - - DrawContext ctx(nullptr, *window, nullptr); - gui->draw(ctx, *assets->getStorage()); -} - -void Engine::saveSettings() { - logger.info() << "saving settings"; - io::write_string(EnginePaths::SETTINGS_FILE, toml::stringify(*settingsHandler)); - if (!params.headless) { - logger.info() << "saving bindings"; - if (input) { - io::write_string( - EnginePaths::CONTROLS_FILE, input->getBindings().write() - ); - } - } -} - -void Engine::close() { - saveSettings(); - logger.info() << "shutting down"; - if (screen) { - screen->onEngineShutdown(); - screen.reset(); - } - content.reset(); - assets.reset(); - cmd.reset(); - if (gui) { - gui.reset(); - logger.info() << "gui finished"; - } - if (!isHeadless()) { - vector_fonts::finalize(); - } - audio::close(); - debuggingServer.reset(); - network.reset(); - clearKeepedObjects(); - project.reset(); - appScripts.reset(); - scripting::close(); - logger.info() << "scripting finished"; - if (!params.headless) { - window.reset(); - logger.info() << "window closed"; - } - logger.info() << "engine finished"; -} - -void Engine::terminate() { - instance->close(); - instance.reset(); -} - -EngineController* Engine::getController() { - return controller.get(); -} - -void Engine::setLevelConsumer(OnWorldOpen levelConsumer) { - this->levelConsumer = std::move(levelConsumer); -} - -void Engine::loadAssets() { - assets->loadAssets(content->get()); -} - -void Engine::loadProject() { - io::path projectFile = "project:project.toml"; - project = std::make_unique(); - project->path = "project:"; - project->deserialize(io::read_object(projectFile)); - logger.info() << "loaded project " << util::quote(project->name); -} - -void Engine::setScreen(std::shared_ptr screen) { - if (this->screen) { - appScripts->onScreenChange(this->screen->getName(), false); - } - // reset audio channels (stop all sources) - audio::reset_channel(audio::get_channel_index("regular")); - audio::reset_channel(audio::get_channel_index("ambient")); - this->screen = std::move(screen); - if (this->screen) { - this->screen->onOpen(); - } - if (this->screen) { - appScripts->onScreenChange(this->screen->getName(), true); - window->setShouldRefresh(); - } -} - -void Engine::onWorldOpen(std::unique_ptr level, int64_t localPlayer) { - logger.info() << "world open"; - levelConsumer(std::move(level), localPlayer); -} - -void Engine::onWorldClosed() { - logger.info() << "world closed"; - levelConsumer(nullptr, -1); -} - -void Engine::quit() { - logger.info() << "quitSignal set to true"; - quitSignal = true; - if (!isHeadless()) { - window->setShouldClose(true); - } -} - -bool Engine::isQuitSignal() const { - return quitSignal; -} - -EngineSettings& Engine::getSettings() { - return settings; -} - -Assets* Engine::getAssets() { - return assets ? assets->getStorage() : nullptr; -} - -Assets& Engine::requireAssets() { - if (isHeadless()) { - throw std::runtime_error("assets are not available in headless mode"); - } - return *assets->getStorage(); -} - -AssetsLoader& Engine::acquireBackgroundLoader() { - if (isHeadless()) { - throw std::runtime_error("assets are not available in headless mode"); - } - return assets->acquireBackgroundLoader(); -} - -EnginePaths& Engine::getPaths() { - return *paths; -} - -ResPaths& Engine::getResPaths() { - return paths->resPaths; -} - -std::shared_ptr Engine::getScreen() { - return screen; -} - -SettingsHandler& Engine::getSettingsHandler() { - return *settingsHandler; -} - -Time& Engine::getTime() { - return time; -} - -const CoreParameters& Engine::getCoreParameters() const { - return params; -} - -bool Engine::isHeadless() const { - return params.headless; -} - -ContentControl& Engine::getContentControl() { - return *content; -} +#include "Engine.hpp" +#include "WindowBar.hpp" + +#ifndef GLEW_STATIC +#define GLEW_STATIC +#endif + +#include "AssetsManagement.hpp" +#include "audio/audio.hpp" +#include "coders/commons.hpp" +#include "coders/GLSLExtension.hpp" +#include "coders/toml.hpp" +#include "coders/vector_fonts.hpp" +#include "content/ContentControl.hpp" +#include "core_defs.hpp" +#include "debug/Logger.hpp" +#include "devtools/AppScriptsControl.hpp" +#include "devtools/DebuggingServer.hpp" +#include "devtools/Editor.hpp" +#include "devtools/Project.hpp" +#include "devtools/stdin_cmd_reader.hpp" +#include "EnginePaths.hpp" +#include "frontend/locale.hpp" +#include "frontend/menu.hpp" +#include "frontend/screens/Screen.hpp" +#include "graphics/core/DrawContext.hpp" +#include "graphics/core/Shader.hpp" +#include "graphics/ui/elements/Menu.hpp" +#include "graphics/ui/GUI.hpp" +#include "io/io.hpp" +#include "io/settings_io.hpp" +#include "logic/CommandsInterpreter.hpp" +#include "logic/EngineController.hpp" +#include "logic/scripting/scripting_hud.hpp" +#include "logic/scripting/scripting.hpp" +#include "Mainloop.hpp" +#include "network/Network.hpp" +#include "ServerMainloop.hpp" +#include "util/platform.hpp" +#include "util/stringutil.hpp" +#include "window/input.hpp" +#include "window/Window.hpp" +#include "WindowControl.hpp" +#include "world/Level.hpp" + +#include +#include +#include +#include + +static debug::Logger logger("engine"); + +Engine::Engine() = default; +Engine::~Engine() = default; + +static std::unique_ptr instance = nullptr; + +Engine& Engine::getInstance() { + if (!instance) { + instance = std::make_unique(); + } + return *instance; +} + +void Engine::onContentLoad() { + editor->loadTools(); + langs::setup(langs::get_current(), paths->resPaths.collectRoots()); + + if (isHeadless()) { + return; + } + for (auto& pack : content->getAllContentPacks()) { + auto configFolder = pack.folder / "config"; + auto bindsFile = configFolder / "bindings.toml"; + logger.info() << "loading bindings: " << bindsFile.string(); + if (io::is_regular_file(bindsFile)) { + input->getBindings().read( + toml::parse( + bindsFile.string(), io::read_string(bindsFile) + ), + BindType::BIND + ); + } + } + loadAssets(); +} + +void Engine::initializeClient() { + assets = std::make_unique(*this); + windowControl = std::make_unique(*this); + auto [window, input] = windowControl->initialize(); + + this->window = std::move(window); + this->input = std::move(input); + + loadControls(); + + gui = std::make_unique(*this); + if (ENGINE_DEBUG_BUILD) { + menus::create_version_label(*gui); + } + keepAlive(settings.display.windowMode.observe( + [this](int value) { + WindowMode mode = static_cast(value); + if (mode != this->window->getMode()) { + this->window->setMode(mode); + } + }, + true + )); + keepAlive(settings.debug.doTraceShaders.observe( + [](bool value) { + Shader::preprocessor->setTraceOutput(value); + }, + true + )); + + keepAlive(this->input->addKeyCallback(Keycode::ESCAPE, [this]() { + auto& menu = *gui->getMenu(); + if (menu.hasOpenPage() && menu.back()) { + return true; + } + return false; + })); +} + +void Engine::initialize(CoreParameters coreParameters) { + params = std::move(coreParameters); + settingsHandler = std::make_unique(settings); + + logger.info() << "engine version: " << ENGINE_VERSION_STRING; + if (params.headless) { + logger.info() << "engine runs in headless mode"; + } + if (params.projectFolder.empty()) { + params.projectFolder = params.resFolder; + } + paths = std::make_unique(params); + loadProject(); + paths->setupProject(*project); + + editor = std::make_unique(*this); + cmd = std::make_unique(); + + if (project->permissions.has(Permissions::NETWORK) || + !params.debugServerString.empty()) { + network = network::Network::create(settings.network); + } + + if (!params.debugServerString.empty()) { + try { + debuggingServer = std::make_unique( + *this, params.debugServerString + ); + } catch (const std::runtime_error& err) { + throw initialize_error( + "debugging server error: " + std::string(err.what()) + ); + } + } + loadSettings(); + + controller = std::make_unique(*this); + if (!params.headless) { + initializeClient(); + } + audio::initialize( + !params.headless, + project->permissions.has(Permissions::RECORD_AUDIO), + settings.audio + ); + + vector_fonts::initialize(); + + if (settings.ui.language.get() == "auto") { + settings.ui.language.set( + langs::locale_by_envlocale(platform::detect_locale()) + ); + } + content = std::make_unique( + *project, *paths, input.get(), [this]() { onContentLoad(); } + ); + scripting::initialize(*this); + + if (!isHeadless()) { + gui->getMenu()->setPageLoader(scripting::create_page_loader()); + } + keepAlive(settings.ui.language.observe([this](auto lang) { + langs::setup(lang, paths->resPaths.collectRoots()); + }, true)); + + keepAlive(settings.audio.inputDevice.observe([](auto name) { + audio::set_input_device(name == "auto" ? "" : name); + })); + + appScripts = std::make_unique(params); + + if (params.stdinCommands) { + cmd::start_stdin_cmd_reader(*this); + } +} + +void Engine::loadSettings() { + io::path settings_file = EnginePaths::SETTINGS_FILE; + if (io::is_regular_file(settings_file)) { + logger.info() << "loading settings"; + std::string text = io::read_string(settings_file); + try { + toml::parse(*settingsHandler, settings_file.string(), text); + } catch (const parsing_error& err) { + logger.error() << err.errorLog(); + throw; + } + } +} + +void Engine::loadControls() { + io::path controls_file = EnginePaths::CONTROLS_FILE; + if (io::is_regular_file(controls_file)) { + logger.info() << "loading controls"; + std::string text = io::read_string(controls_file); + input->getBindings().read( + toml::parse(controls_file.string(), text), BindType::BIND + ); + } +} + +void Engine::updateHotkeys() { + if (input->jpressed(Keycode::F2)) { + windowControl->saveScreenshot(); + } + if (input->pressed(Keycode::LEFT_CONTROL) && input->pressed(Keycode::F3) && + input->jpressed(Keycode::U)) { + gui->toggleDebug(); + } + if (input->jpressed(Keycode::F11)) { + windowControl->toggleFullscreen(); + } +} + +void Engine::run() { + if (params.headless) { + ServerMainloop(*this).run(); + } else { + Mainloop(*this).run(); + } +} + +void Engine::postUpdate() { + if (network) { + network->update(); + } + postRunnables.run(); + scripting::process_post_runnables(); + + if (debuggingServer) { + debuggingServer->update(); + } +} + +void Engine::detachDebugger() { + debuggingServer.reset(); +} + +void Engine::applicationTick() { + appScripts->tick(); +} + +void Engine::updateFrontend() { + double delta = time.getDelta(); + assets->update(); + updateHotkeys(); + audio::update(delta); + gui->act(delta, window->getSize()); + screen->update(delta); + gui->postAct(); +} + +void Engine::nextFrame(bool waitForRefresh) { + windowControl->nextFrame(waitForRefresh); +} + +void Engine::startPauseLoop() { + assert (network != nullptr); + + bool initialCursorLocked = false; + if (!isHeadless()) { + initialCursorLocked = input->isCursorLocked(); + if (initialCursorLocked) { + input->toggleCursor(); + } + } + while (!isQuitSignal() && debuggingServer) { + network->update(); + if (debuggingServer->update()) { + break; + } + if (isHeadless()) { + platform::sleep(1.0 / params.tps * 1000); + } else { + nextFrame(false); + } + } + if (initialCursorLocked) { + input->toggleCursor(); + } +} + +void Engine::renderFrame() { + if (input->isCursorLocked() != (gui->getActiveFrame() == nullptr)) { + input->toggleCursor(); + } + screen->draw(time.getDelta()); + + DrawContext ctx(nullptr, *window, nullptr); + gui->draw(ctx, *assets->getStorage()); + draw_window_bar(*this); +} + +void Engine::saveSettings() { + logger.info() << "saving settings"; + io::write_string(EnginePaths::SETTINGS_FILE, toml::stringify(*settingsHandler)); + if (!params.headless) { + logger.info() << "saving bindings"; + if (input) { + io::write_string( + EnginePaths::CONTROLS_FILE, input->getBindings().write() + ); + } + } +} + +void Engine::close() { + saveSettings(); + logger.info() << "shutting down"; + if (screen) { + screen->onEngineShutdown(); + screen.reset(); + } + content.reset(); + assets.reset(); + cmd.reset(); + if (gui) { + gui.reset(); + logger.info() << "gui finished"; + } + if (!isHeadless()) { + vector_fonts::finalize(); + } + audio::close(); + debuggingServer.reset(); + network.reset(); + clearKeepedObjects(); + project.reset(); + appScripts.reset(); + scripting::close(); + logger.info() << "scripting finished"; + if (!params.headless) { + window.reset(); + logger.info() << "window closed"; + } + logger.info() << "engine finished"; +} + +void Engine::terminate() { + instance->close(); + instance.reset(); +} + +EngineController* Engine::getController() { + return controller.get(); +} + +void Engine::setLevelConsumer(OnWorldOpen levelConsumer) { + this->levelConsumer = std::move(levelConsumer); +} + +void Engine::loadAssets() { + assets->loadAssets(content->get()); +} + +void Engine::loadProject() { + io::path projectFile = "project:project.toml"; + project = std::make_unique(); + project->path = "project:"; + project->deserialize(io::read_object(projectFile)); + logger.info() << "loaded project " << util::quote(project->name); +} + +void Engine::setScreen(std::shared_ptr screen) { + if (this->screen) { + appScripts->onScreenChange(this->screen->getName(), false); + } + // reset audio channels (stop all sources) + audio::reset_channel(audio::get_channel_index("regular")); + audio::reset_channel(audio::get_channel_index("ambient")); + this->screen = std::move(screen); + if (this->screen) { + this->screen->onOpen(); + } + if (this->screen) { + appScripts->onScreenChange(this->screen->getName(), true); + window->setShouldRefresh(); + } +} + +void Engine::onWorldOpen(std::unique_ptr level, int64_t localPlayer) { + logger.info() << "world open"; + levelConsumer(std::move(level), localPlayer); +} + +void Engine::onWorldClosed() { + logger.info() << "world closed"; + levelConsumer(nullptr, -1); +} + +void Engine::quit() { + logger.info() << "quitSignal set to true"; + quitSignal = true; + if (!isHeadless()) { + window->setShouldClose(true); + } +} + +bool Engine::isQuitSignal() const { + return quitSignal; +} + +EngineSettings& Engine::getSettings() { + return settings; +} + +Assets* Engine::getAssets() { + return assets ? assets->getStorage() : nullptr; +} + +Assets& Engine::requireAssets() { + if (isHeadless()) { + throw std::runtime_error("assets are not available in headless mode"); + } + return *assets->getStorage(); +} + +AssetsLoader& Engine::acquireBackgroundLoader() { + if (isHeadless()) { + throw std::runtime_error("assets are not available in headless mode"); + } + return assets->acquireBackgroundLoader(); +} + +EnginePaths& Engine::getPaths() { + return *paths; +} + +ResPaths& Engine::getResPaths() { + return paths->resPaths; +} + +std::shared_ptr Engine::getScreen() { + return screen; +} + +SettingsHandler& Engine::getSettingsHandler() { + return *settingsHandler; +} + +Time& Engine::getTime() { + return time; +} + +const CoreParameters& Engine::getCoreParameters() const { + return params; +} + +bool Engine::isHeadless() const { + return params.headless; +} + +ContentControl& Engine::getContentControl() { + return *content; +} diff --git a/src/engine/WindowBar.cpp b/src/engine/WindowBar.cpp new file mode 100644 index 000000000..661b30d4c --- /dev/null +++ b/src/engine/WindowBar.cpp @@ -0,0 +1,88 @@ +#include "WindowBar.hpp" + +#include "Engine.hpp" +#include "graphics/core/Batch2D.hpp" +#include "graphics/core/Font.hpp" +#include "graphics/ui/GUI.hpp" +#include "window/Window.hpp" +#include "assets/Assets.hpp" + +#include +#include + +namespace { + std::wstring to_wide(const std::string& text) { + std::wstring_convert> converter; + return converter.from_bytes(text); + } + + constexpr const char* BAR_FONT = "normal"; +} + +void draw_window_bar(Engine& engine) { + auto& window = engine.getWindow(); + const int height = window.getDecorationHeight(); + if (height <= 0) { + return; + } + auto* batch = engine.getGUI().getBatch2D(); + auto font = engine.requireAssets().getShared(BAR_FONT); + if (batch == nullptr || font == nullptr) { + return; + } + const auto size = window.getSize(); + const float barHeight = static_cast(height); + + batch->begin(); + batch->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.82f)); + batch->rect(0.0f, 0.0f, size.x, barHeight); + batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.10f)); + batch->rect(0.0f, barHeight - 1.0f, size.x, 1.0f); + + const std::wstring title = to_wide(window.getTitle()); + const int textX = static_cast(barHeight * 0.4f); + const int textY = (height - font->getLineHeight()) / 2; + batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f)); + font->draw(*batch, title, textX, textY, nullptr, 0); + batch->untexture(); + + const float button = barHeight; + const int hovered = window.getDecorationHoveredButton(); + for (int i = 0; i < 3; i++) { + const float x = size.x - button * (i + 1); + const float cx = x + button * 0.5f; + const float cy = barHeight * 0.5f; + if (hovered == i) { + batch->setColor( + i == 0 ? glm::vec4(0.62f, 0.13f, 0.09f, 0.9f) + : glm::vec4(1.0f, 1.0f, 1.0f, 0.16f) + ); + batch->rect(x, 0.0f, button, barHeight - 1.0f); + } + const float ink = hovered == i ? 1.0f : 0.8f; + const float thickness = 2.0f; + batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, ink)); + if (i == 0) { + for (int s = 0; s < 8; s++) { + batch->rect(cx - 5.0f + s, cy - 5.0f + s, thickness, thickness); + batch->rect(cx + 3.0f - s, cy - 5.0f + s, thickness, thickness); + } + } else if (i == 1 && !window.isMaximized()) { + batch->rect(cx - 6.0f, cy - 6.0f, 12.0f, thickness); + batch->rect(cx - 6.0f, cy + 4.0f, 12.0f, thickness); + batch->rect(cx - 6.0f, cy - 6.0f, thickness, 12.0f); + batch->rect(cx + 4.0f, cy - 6.0f, thickness, 12.0f); + } else if (i == 1) { + batch->rect(cx - 7.0f, cy + 1.0f, 11.0f, thickness); + batch->rect(cx - 7.0f, cy + 1.0f, thickness, 6.0f); + batch->rect(cx + 2.0f, cy + 1.0f, thickness, 6.0f); + batch->rect(cx - 7.0f, cy + 5.0f, 11.0f, thickness); + batch->rect(cx - 2.0f, cy - 5.0f, 9.0f, thickness); + batch->rect(cx - 2.0f, cy - 5.0f, thickness, 6.0f); + batch->rect(cx + 5.0f, cy - 5.0f, thickness, 6.0f); + } else { + batch->rect(cx - 6.0f, cy - 1.0f, 12.0f, thickness); + } + } + batch->flush(); +} diff --git a/src/engine/WindowBar.hpp b/src/engine/WindowBar.hpp new file mode 100644 index 000000000..be3df317b --- /dev/null +++ b/src/engine/WindowBar.hpp @@ -0,0 +1,5 @@ +#pragma once + +class Engine; + +void draw_window_bar(Engine& engine); diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index 997b67bea..3cf8406fb 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -1,189 +1,193 @@ -#pragma once - -#include "delegates.hpp" - -#include -#include -#include -#include -#include -#include -#include - -class DrawContext; -class Assets; -class Camera; -class Batch2D; -struct CursorState; -class Engine; -class Input; -class Window; -struct FontStylesScheme; - -namespace devtools { - class Editor; -} - -class UiDocument; - -/* - Some info about padding and margin. - Padding is element inner space, margin is outer - glm::vec4 usage: - x - left - y - top - z - right - w - bottom - - Outer element - +======================================================================+ - | . . . . | - | .padding.y . . . | - | padding.x . . . . padding.z | - |- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - | - | . . . . | - | . .margin.y . . | - | .margin.x . . margin.z. | - |- - - - - - + - - - - - +====================+- - - - - + - - - - - - | - | . | Inner element | . | - |- - - - - - + - - - - - +====================+- - - - - + - - - - - - | - | . . . . | - | . .margin.w . . | - | . . . . | - |- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - | - | . . . . | - | .padding.w . . . | - | . . . . | - +======================================================================+ -*/ - -namespace gui { - class UINode; - class Container; - class Menu; - class Frame; - - /// @brief The main UI controller - class GUI { - Engine& engine; - Input& input; - std::unique_ptr batch2D; - std::shared_ptr container; - std::shared_ptr hover; - std::shared_ptr pressed; - std::shared_ptr focus; - const UINode* focusedOnStart = nullptr; - std::shared_ptr tooltip; - std::shared_ptr rootDocument; - std::unique_ptr syntaxColorScheme; - std::unordered_map> storage; - std::shared_ptr activeFrame; - - std::unique_ptr uicamera; - std::shared_ptr menu; - std::queue postRunnables; - std::vector> mouseOver; - std::unordered_map> frames; - vec2supplier cursorLocator; - - float tooltipTimer = 0.0f; - float doubleClickTimer = 0.0f; - float doubleClickDelay = 0.5f; - bool doubleClicked = false; - bool debug = false; - - void actMouse(Frame& frame, float delta, const CursorState& cursor); - void actFocused(); - void updateTooltip(float delta); - void resetTooltip(); - public: - static inline std::string CORE_MAIN = "core:main"; - static constexpr int CONTEXT_MENU_ZINDEX = 999; - - GUI(Engine& engine); - ~GUI(); - - /// @brief Get the main menu (Menu) node - std::shared_ptr getMenu(); - - /// @brief Get current focused node - /// @return focused node or nullptr - std::shared_ptr getFocused() const; - - /// @brief Check if all user input is caught by some element like TextBox - bool isFocusCaught() const; - - /// @brief Main input handling and logic update method - /// @param delta delta time - /// @param viewport window size - void act(float delta, const glm::uvec2& viewport); - - /// @brief Draw all visible elements on main container - /// @param pctx parent graphics context - /// @param assets active assets storage - void draw(const DrawContext& pctx, Assets& assets); - - void postAct(); - - /// @brief Add element to the main container - /// @param node UI element - void add(std::shared_ptr node); - - void addFrame(std::shared_ptr frame); - - void setActiveFrame( - const std::string& id, vec2supplier cursorLocator = nullptr - ); - - std::shared_ptr getActiveFrame() const; - - std::shared_ptr getFrame(const std::string& id); - - /// @brief Remove node from the main container - void remove(UINode* node) noexcept; - - void remove(const std::shared_ptr& node) noexcept { - return remove(node.get()); - } - - /// @brief Store node in the GUI nodes dictionary - /// (does not add node to the main container) - /// @param name node key - /// @param node target node - void store(const std::string& name, std::shared_ptr node); - - /// @brief Get node from the GUI nodes dictionary - /// @param name node key - /// @return stored node or nullptr - std::shared_ptr get(const std::string& name) noexcept; - - /// @brief Remove node from the GUI nodes dictionary - /// @param name node key - void remove(const std::string& name) noexcept; - - /// @brief Set node as focused - /// @param node new focused node or nullptr to remove focus - void setFocus(std::shared_ptr node); - - /// @brief Get the main container - /// @deprecated - std::shared_ptr getContainer() const; - - void setSyntaxColorScheme(std::unique_ptr scheme); - FontStylesScheme* getSyntaxColorScheme() const; - - void onAssetsLoad(Assets* assets); - - void postRunnable(const runnable& callback); - - void setDoubleClickDelay(float delay); - float getDoubleClickDelay() const; - - void toggleDebug(); - const Input& getInput() const; - Input& getInput(); - Window& getWindow(); - devtools::Editor& getEditor(); - Engine& getEngine(); - }; -} +#pragma once + +#include "delegates.hpp" + +#include +#include +#include +#include +#include +#include +#include + +class DrawContext; +class Assets; +class Camera; +class Batch2D; +struct CursorState; +class Engine; +class Input; +class Window; +struct FontStylesScheme; + +namespace devtools { + class Editor; +} + +class UiDocument; + +/* + Some info about padding and margin. + Padding is element inner space, margin is outer + glm::vec4 usage: + x - left + y - top + z - right + w - bottom + + Outer element + +======================================================================+ + | . . . . | + | .padding.y . . . | + | padding.x . . . . padding.z | + |- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - | + | . . . . | + | . .margin.y . . | + | .margin.x . . margin.z. | + |- - - - - - + - - - - - +====================+- - - - - + - - - - - - | + | . | Inner element | . | + |- - - - - - + - - - - - +====================+- - - - - + - - - - - - | + | . . . . | + | . .margin.w . . | + | . . . . | + |- - - - - - + - - - - - + - - - - - - - - - -+- - - - - + - - - - - - | + | . . . . | + | .padding.w . . . | + | . . . . | + +======================================================================+ +*/ + +namespace gui { + class UINode; + class Container; + class Menu; + class Frame; + + /// @brief The main UI controller + class GUI { + Engine& engine; + Input& input; + std::unique_ptr batch2D; + std::shared_ptr container; + std::shared_ptr hover; + std::shared_ptr pressed; + std::shared_ptr focus; + const UINode* focusedOnStart = nullptr; + std::shared_ptr tooltip; + std::shared_ptr rootDocument; + std::unique_ptr syntaxColorScheme; + std::unordered_map> storage; + std::shared_ptr activeFrame; + + std::unique_ptr uicamera; + std::shared_ptr menu; + std::queue postRunnables; + std::vector> mouseOver; + std::unordered_map> frames; + vec2supplier cursorLocator; + + float tooltipTimer = 0.0f; + float doubleClickTimer = 0.0f; + float doubleClickDelay = 0.5f; + bool doubleClicked = false; + bool debug = false; + + void actMouse(Frame& frame, float delta, const CursorState& cursor); + void actFocused(); + void updateTooltip(float delta); + void resetTooltip(); + public: + static inline std::string CORE_MAIN = "core:main"; + static constexpr int CONTEXT_MENU_ZINDEX = 999; + + GUI(Engine& engine); + ~GUI(); + + /// @brief Get the main menu (Menu) node + std::shared_ptr getMenu(); + + /// @brief Get current focused node + /// @return focused node or nullptr + std::shared_ptr getFocused() const; + + /// @brief Check if all user input is caught by some element like TextBox + bool isFocusCaught() const; + + /// @brief Main input handling and logic update method + /// @param delta delta time + /// @param viewport window size + void act(float delta, const glm::uvec2& viewport); + + /// @brief Draw all visible elements on main container + /// @param pctx parent graphics context + /// @param assets active assets storage + void draw(const DrawContext& pctx, Assets& assets); + + Batch2D* getBatch2D() { + return batch2D.get(); + } + + void postAct(); + + /// @brief Add element to the main container + /// @param node UI element + void add(std::shared_ptr node); + + void addFrame(std::shared_ptr frame); + + void setActiveFrame( + const std::string& id, vec2supplier cursorLocator = nullptr + ); + + std::shared_ptr getActiveFrame() const; + + std::shared_ptr getFrame(const std::string& id); + + /// @brief Remove node from the main container + void remove(UINode* node) noexcept; + + void remove(const std::shared_ptr& node) noexcept { + return remove(node.get()); + } + + /// @brief Store node in the GUI nodes dictionary + /// (does not add node to the main container) + /// @param name node key + /// @param node target node + void store(const std::string& name, std::shared_ptr node); + + /// @brief Get node from the GUI nodes dictionary + /// @param name node key + /// @return stored node or nullptr + std::shared_ptr get(const std::string& name) noexcept; + + /// @brief Remove node from the GUI nodes dictionary + /// @param name node key + void remove(const std::string& name) noexcept; + + /// @brief Set node as focused + /// @param node new focused node or nullptr to remove focus + void setFocus(std::shared_ptr node); + + /// @brief Get the main container + /// @deprecated + std::shared_ptr getContainer() const; + + void setSyntaxColorScheme(std::unique_ptr scheme); + FontStylesScheme* getSyntaxColorScheme() const; + + void onAssetsLoad(Assets* assets); + + void postRunnable(const runnable& callback); + + void setDoubleClickDelay(float delay); + float getDoubleClickDelay() const; + + void toggleDebug(); + const Input& getInput() const; + Input& getInput(); + Window& getWindow(); + devtools::Editor& getEditor(); + Engine& getEngine(); + }; +} diff --git a/src/window/Window.hpp b/src/window/Window.hpp index c34024cdf..a41c53535 100644 --- a/src/window/Window.hpp +++ b/src/window/Window.hpp @@ -44,6 +44,16 @@ public: virtual void setTitle(const std::string& title) = 0; + virtual const std::string& getTitle() const = 0; + + virtual int getDecorationHeight() const { + return 0; + } + + virtual int getDecorationHoveredButton() const { + return -1; + } + virtual bool isIconSupported() const = 0; virtual void setIcon(const ImageData* image) = 0; diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index 9fe04b82c..59f637fb1 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -360,7 +360,14 @@ public: glfwFocusWindow(window); } + std::string title; + + const std::string& getTitle() const override { + return title; + } + void setTitle(const std::string& title) override { + this->title = title; glfwSetWindowTitle(window, title.c_str()); } diff --git a/src/window/wayland/WaylandBar.cpp b/src/window/wayland/WaylandBar.cpp index e362baa60..ab2f221de 100644 --- a/src/window/wayland/WaylandBar.cpp +++ b/src/window/wayland/WaylandBar.cpp @@ -5,262 +5,16 @@ static constexpr int BAR_HEIGHT = 30; static constexpr int BAR_BUTTON = 30; static constexpr int BAR_EDGE = 5; -static constexpr int BAR_ICON_SIZE = 16; -static constexpr int BAR_FONT_W = 8; -static constexpr int BAR_FONT_H = 8; -static constexpr int BAR_GLYPHS = 159; - -static const uint8_t BAR_FONT[BAR_GLYPHS][BAR_FONT_H] { - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x60, 0x60, 0x60, 0x60, 0x00, 0x60, 0x60}, - {0x00, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x14, 0x14, 0x7e, 0x28, 0xfe, 0x28, 0x68}, - {0x00, 0x10, 0x78, 0x70, 0x78, 0x7c, 0x1c, 0x78}, - {0x00, 0x62, 0xf4, 0xfc, 0x6b, 0x1f, 0x17, 0x23}, - {0x00, 0x38, 0x60, 0x30, 0x7a, 0xde, 0x6c, 0x7e}, - {0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x20, 0x60, 0x60, 0x40, 0x40, 0x60, 0x60, 0x20}, - {0x40, 0x60, 0x20, 0x20, 0x20, 0x20, 0x60, 0x40}, - {0x00, 0x20, 0xf0, 0x70, 0xf0, 0x20, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x10, 0x10}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x40}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60}, - {0x00, 0x20, 0x20, 0x60, 0x40, 0x40, 0xc0, 0x80}, - {0x00, 0x38, 0x6c, 0xcc, 0xcc, 0xcc, 0x6c, 0x38}, - {0x00, 0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x7c}, - {0x00, 0x78, 0x18, 0x18, 0x18, 0x30, 0x60, 0x7c}, - {0x00, 0x78, 0x18, 0x18, 0x38, 0x1c, 0x1c, 0xf8}, - {0x00, 0x18, 0x38, 0x78, 0x58, 0xd8, 0xfc, 0x18}, - {0x00, 0x78, 0x40, 0x40, 0x78, 0x0c, 0x0c, 0x78}, - {0x00, 0x38, 0x60, 0x40, 0xf8, 0x6c, 0x6c, 0x38}, - {0x00, 0xfc, 0x18, 0x18, 0x18, 0x30, 0x30, 0x60}, - {0x00, 0x78, 0x6c, 0x6c, 0x78, 0x6c, 0x6c, 0x78}, - {0x00, 0x78, 0xc8, 0xcc, 0x7c, 0x0c, 0x18, 0x70}, - {0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x60, 0x60}, - {0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x60, 0x60}, - {0x00, 0x00, 0x00, 0x06, 0x3c, 0x60, 0x3c, 0x06}, - {0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00}, - {0x00, 0x00, 0x00, 0x60, 0x3c, 0x0e, 0x3c, 0x60}, - {0x00, 0x70, 0x18, 0x30, 0x60, 0x00, 0x60, 0x60}, - {0x00, 0x1e, 0x63, 0x5d, 0x55, 0x57, 0x5e, 0x62}, - {0x00, 0x38, 0x38, 0x78, 0x6c, 0x6c, 0xfe, 0xc6}, - {0x00, 0x78, 0x6c, 0x6c, 0x7c, 0x6c, 0x6c, 0x7c}, - {0x00, 0x3c, 0x64, 0xc0, 0xc0, 0xc0, 0x64, 0x3c}, - {0x00, 0x7c, 0x6e, 0x66, 0x66, 0x66, 0x6e, 0x7c}, - {0x00, 0x7c, 0x60, 0x60, 0x78, 0x60, 0x60, 0x7c}, - {0x00, 0x7c, 0x60, 0x60, 0x78, 0x60, 0x60, 0x60}, - {0x00, 0x3c, 0x62, 0xc0, 0xce, 0xc6, 0x66, 0x3e}, - {0x00, 0x66, 0x66, 0x66, 0x7e, 0x66, 0x66, 0x66}, - {0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60}, - {0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60}, - {0x00, 0x6e, 0x7c, 0x78, 0x70, 0x78, 0x6c, 0x66}, - {0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7c}, - {0x00, 0x63, 0x77, 0x77, 0x7f, 0x7f, 0x6b, 0x63}, - {0x00, 0x66, 0x76, 0x76, 0x7e, 0x7e, 0x6e, 0x6e}, - {0x00, 0x3c, 0x66, 0xc6, 0xc6, 0xc6, 0x66, 0x3c}, - {0x00, 0x7c, 0x6c, 0x6c, 0x7c, 0x60, 0x60, 0x60}, - {0x00, 0x3c, 0x66, 0xc6, 0xc6, 0xc6, 0x66, 0x3c}, - {0x00, 0x78, 0x6c, 0x6c, 0x78, 0x6c, 0x6c, 0x66}, - {0x00, 0x78, 0x68, 0x60, 0x78, 0x0c, 0x4c, 0x78}, - {0x00, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, - {0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x6c, 0x3c}, - {0x00, 0xc6, 0xc6, 0x6c, 0x6c, 0x78, 0x38, 0x38}, - {0x00, 0xcc, 0xcc, 0x7d, 0x7f, 0x77, 0x73, 0x33}, - {0x00, 0xcc, 0x6c, 0x38, 0x38, 0x38, 0x6c, 0xc6}, - {0x00, 0xcc, 0x6c, 0x78, 0x38, 0x30, 0x30, 0x30}, - {0x00, 0xfc, 0x1c, 0x18, 0x30, 0x70, 0x60, 0xfc}, - {0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x70}, - {0x00, 0x80, 0xc0, 0x40, 0x40, 0x60, 0x20, 0x20}, - {0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0xe0}, - {0x00, 0x38, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00}, - {0x00, 0x00, 0x00, 0x78, 0x08, 0x78, 0xd8, 0x78}, - {0x40, 0x40, 0x40, 0x78, 0x6c, 0x4c, 0x6c, 0x78}, - {0x00, 0x00, 0x00, 0x78, 0x60, 0xc0, 0x60, 0x78}, - {0x0c, 0x0c, 0x0c, 0x7c, 0xcc, 0xcc, 0xcc, 0x7c}, - {0x00, 0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x7c}, - {0x70, 0x60, 0x60, 0xf0, 0x60, 0x60, 0x60, 0x60}, - {0x00, 0x00, 0x00, 0x7c, 0xcc, 0xcc, 0xcc, 0x7c}, - {0x40, 0x40, 0x40, 0x78, 0x6c, 0x4c, 0x4c, 0x4c}, - {0x40, 0x40, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40}, - {0x40, 0x40, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40}, - {0x40, 0x40, 0x40, 0x4c, 0x78, 0x70, 0x78, 0x4c}, - {0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40}, - {0x00, 0x00, 0x00, 0x7f, 0x6d, 0x4d, 0x4d, 0x4d}, - {0x00, 0x00, 0x00, 0x78, 0x6c, 0x4c, 0x4c, 0x4c}, - {0x00, 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78}, - {0x00, 0x00, 0x00, 0x78, 0x6c, 0x4c, 0x6c, 0x78}, - {0x00, 0x00, 0x00, 0x7c, 0xcc, 0xcc, 0xcc, 0x7c}, - {0x00, 0x00, 0x00, 0x78, 0x60, 0x40, 0x40, 0x40}, - {0x00, 0x00, 0x00, 0x78, 0xe0, 0x78, 0x18, 0xf8}, - {0x00, 0x00, 0x60, 0xf0, 0x60, 0x60, 0x60, 0x70}, - {0x00, 0x00, 0x00, 0x4c, 0x4c, 0x4c, 0x6c, 0x7c}, - {0x00, 0x00, 0x00, 0xcc, 0x48, 0x78, 0x70, 0x30}, - {0x00, 0x00, 0x00, 0xdb, 0x5b, 0x7e, 0x7e, 0x76}, - {0x00, 0x00, 0x00, 0xd8, 0x78, 0x30, 0x78, 0xc8}, - {0x00, 0x00, 0x00, 0xcc, 0x48, 0x78, 0x78, 0x30}, - {0x00, 0x00, 0x00, 0xf8, 0x38, 0x30, 0x60, 0xf8}, - {0x18, 0x30, 0x30, 0x70, 0x30, 0x30, 0x30, 0x18}, - {0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40}, - {0x70, 0x30, 0x30, 0x18, 0x30, 0x30, 0x30, 0x70}, - {0x00, 0x00, 0x00, 0x00, 0x72, 0x1c, 0x00, 0x00}, - {0x00, 0x38, 0x38, 0x78, 0x6c, 0x6c, 0xfe, 0xc6}, - {0x00, 0x7c, 0x60, 0x60, 0x7c, 0x6c, 0x6c, 0x7c}, - {0x00, 0x78, 0x6c, 0x6c, 0x7c, 0x6c, 0x6c, 0x7c}, - {0x00, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60}, - {0x00, 0x3e, 0x36, 0x36, 0x36, 0x26, 0x66, 0xff}, - {0x00, 0x7c, 0x60, 0x60, 0x78, 0x60, 0x60, 0x7c}, - {0x00, 0x64, 0x35, 0x3f, 0x3f, 0x3f, 0x64, 0xc4}, - {0x00, 0x78, 0x0c, 0x0c, 0x38, 0x0c, 0x0c, 0xf8}, - {0x00, 0x6e, 0x6e, 0x7e, 0x7e, 0x76, 0x76, 0x66}, - {0x00, 0x6e, 0x6e, 0x7e, 0x7e, 0x76, 0x76, 0x66}, - {0x00, 0x66, 0x6c, 0x78, 0x78, 0x7c, 0x6c, 0x66}, - {0x00, 0x3e, 0x36, 0x36, 0x36, 0x36, 0x66, 0xc6}, - {0x00, 0x63, 0x77, 0x77, 0x7f, 0x7f, 0x6b, 0x63}, - {0x00, 0x66, 0x66, 0x66, 0x7e, 0x66, 0x66, 0x66}, - {0x00, 0x3c, 0x66, 0xc6, 0xc6, 0xc6, 0x66, 0x3c}, - {0x00, 0x7e, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}, - {0x00, 0x7c, 0x6c, 0x6c, 0x7c, 0x60, 0x60, 0x60}, - {0x00, 0x3c, 0x64, 0xc0, 0xc0, 0xc0, 0x64, 0x3c}, - {0x00, 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}, - {0x00, 0xc6, 0x6c, 0x7c, 0x38, 0x38, 0x30, 0x70}, - {0x00, 0x18, 0x7e, 0x7b, 0xd9, 0x7b, 0x7e, 0x18}, - {0x00, 0xcc, 0x6c, 0x38, 0x38, 0x38, 0x6c, 0xc6}, - {0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7f}, - {0x00, 0x66, 0x66, 0x66, 0x7e, 0x06, 0x06, 0x06}, - {0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7f}, - {0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7f}, - {0x00, 0x70, 0xf0, 0x30, 0x3e, 0x3b, 0x3b, 0x3e}, - {0x00, 0x61, 0x61, 0x61, 0x7d, 0x6d, 0x6d, 0x7d}, - {0x00, 0x60, 0x60, 0x60, 0x7c, 0x6c, 0x6c, 0x7c}, - {0x00, 0x70, 0xf8, 0x0c, 0x7c, 0x0c, 0x1c, 0x78}, - {0x00, 0x67, 0x6c, 0x78, 0x78, 0x78, 0x6c, 0x67}, - {0x00, 0x7c, 0x6c, 0x6c, 0x7c, 0x3c, 0x6c, 0x6c}, - {0x00, 0x00, 0x00, 0x78, 0x08, 0x78, 0xd8, 0x78}, - {0x38, 0x40, 0xc0, 0xf8, 0xec, 0xcc, 0x6c, 0x78}, - {0x00, 0x00, 0x00, 0x78, 0x58, 0x78, 0x48, 0x78}, - {0x00, 0x00, 0x00, 0x78, 0x40, 0x40, 0x40, 0x40}, - {0x00, 0x00, 0x00, 0x3c, 0x2c, 0x6c, 0x6c, 0xfe}, - {0x00, 0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x7c}, - {0x00, 0x00, 0x00, 0x6b, 0x3e, 0x3e, 0x7f, 0xc9}, - {0x00, 0x00, 0x00, 0x70, 0x18, 0x70, 0x18, 0xf0}, - {0x00, 0x00, 0x00, 0x5c, 0x7c, 0x7c, 0x6c, 0x6c}, - {0x00, 0x30, 0x00, 0x5c, 0x7c, 0x7c, 0x6c, 0x6c}, - {0x00, 0x00, 0x00, 0x58, 0x70, 0x70, 0x78, 0x4c}, - {0x00, 0x00, 0x00, 0x7c, 0x6c, 0x6c, 0x6c, 0xcc}, - {0x00, 0x00, 0x00, 0x6e, 0x6e, 0x7e, 0x7e, 0x46}, - {0x00, 0x00, 0x00, 0x4c, 0x4c, 0x7c, 0x4c, 0x4c}, - {0x00, 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78}, - {0x00, 0x00, 0x00, 0x7c, 0x4c, 0x4c, 0x4c, 0x4c}, - {0x00, 0x00, 0x00, 0x78, 0x6c, 0x4c, 0x6c, 0x78}, - {0x00, 0x00, 0x00, 0x78, 0x60, 0xc0, 0x60, 0x78}, - {0x00, 0x00, 0x00, 0xf8, 0x30, 0x30, 0x30, 0x30}, - {0x00, 0x00, 0x00, 0xcc, 0x48, 0x78, 0x78, 0x30}, - {0x08, 0x08, 0x08, 0x7f, 0x7b, 0xc9, 0x7b, 0x7f}, - {0x00, 0x00, 0x00, 0xd8, 0x78, 0x30, 0x78, 0xc8}, - {0x00, 0x00, 0x00, 0x4c, 0x4c, 0x4c, 0x4c, 0x7c}, - {0x00, 0x00, 0x00, 0xd8, 0xd8, 0x78, 0x18, 0x18}, - {0x00, 0x00, 0x00, 0x4d, 0x4d, 0x4d, 0x4d, 0x7f}, - {0x00, 0x00, 0x00, 0x4d, 0x4d, 0x4d, 0x4d, 0x7f}, - {0x00, 0x00, 0x00, 0xf0, 0x30, 0x3c, 0x34, 0x3c}, - {0x00, 0x00, 0x00, 0x43, 0x43, 0x7b, 0x4f, 0x7b}, - {0x00, 0x00, 0x00, 0x40, 0x40, 0x78, 0x48, 0x78}, - {0x00, 0x00, 0x00, 0x70, 0x18, 0x78, 0x18, 0x70}, - {0x00, 0x00, 0x00, 0x4e, 0x5b, 0x79, 0x5b, 0x4e}, - {0x00, 0x00, 0x00, 0x78, 0x58, 0x78, 0x78, 0xd8}, -}; - -static int bar_glyph_index(uint32_t codepoint) { - if (codepoint >= 32 && codepoint < 127) { - return static_cast(codepoint) - 32; - } - if (codepoint >= 0x410 && codepoint <= 0x44F) { - return 95 + static_cast(codepoint - 0x410); - } - if (codepoint == 0x401) { - return 95 + 6; - } - if (codepoint == 0x451) { - return 95 + 37; - } - return -1; +const std::string& WaylandWindow::getTitle() const { + return title; } -static std::vector decode_utf8(const std::string& text) { - std::vector codepoints; - for (size_t i = 0; i < text.size();) { - const uint8_t byte = static_cast(text[i]); - uint32_t codepoint = byte; - size_t length = 1; - if ((byte & 0xE0) == 0xC0) { - codepoint = byte & 0x1F; - length = 2; - } else if ((byte & 0xF0) == 0xE0) { - codepoint = byte & 0x0F; - length = 3; - } else if ((byte & 0xF8) == 0xF0) { - codepoint = byte & 0x07; - length = 4; - } - if (i + length > text.size()) { - break; - } - for (size_t k = 1; k < length; k++) { - codepoint = - (codepoint << 6) | (static_cast(text[i + k]) & 0x3F); - } - codepoints.push_back(codepoint); - i += length; - } - return codepoints; +int WaylandWindow::getDecorationHeight() const { + return barVisible() ? BAR_HEIGHT : 0; } - -static void push_quad( - std::vector& target, float x, float y, float w, float h, float u0, - float v0, float u1, float v1, const glm::vec4& color -) { - const float x1 = x + w; - const float y1 = y + h; - const float vertices[] { - x, y, u0, v0, color.r, color.g, color.b, color.a, - x1, y, u1, v0, color.r, color.g, color.b, color.a, - x, y1, u0, v1, color.r, color.g, color.b, color.a, - x1, y, u1, v0, color.r, color.g, color.b, color.a, - x1, y1, u1, v1, color.r, color.g, color.b, color.a, - x, y1, u0, v1, color.r, color.g, color.b, color.a - }; - target.insert(target.end(), vertices, vertices + 48); -} - -bool WaylandWindow::isIconSupported() const { - return true; -} - -void WaylandWindow::setIcon(const ImageData* image) { - if (image == nullptr || image->getData() == nullptr) { - return; - } - iconWidth = static_cast(image->getWidth()); - iconHeight = static_cast(image->getHeight()); - if (iconWidth <= 0 || iconHeight <= 0) { - return; - } - const size_t pixels = static_cast(iconWidth) * iconHeight; - const bool alpha = image->getFormat() == ImageFormat::RGBA8888; - iconPixels.resize(pixels * 4); - for (size_t i = 0; i < pixels; i++) { - iconPixels[i * 4] = image->getData()[i * (alpha ? 4 : 3)]; - iconPixels[i * 4 + 1] = image->getData()[i * (alpha ? 4 : 3) + 1]; - iconPixels[i * 4 + 2] = image->getData()[i * (alpha ? 4 : 3) + 2]; - iconPixels[i * 4 + 3] = alpha ? image->getData()[i * 4 + 3] : 255; - } - if (barIconTexture != 0) { - glDeleteTextures(1, &barIconTexture); - barIconTexture = 0; - } +int WaylandWindow::getDecorationHoveredButton() const { + return hoveredButton; } void WaylandWindow::enableOwnDecorations() { @@ -275,234 +29,6 @@ bool WaylandWindow::barVisible() const { return barEnabled && !fullscreen && Window::mode != WindowMode::BORDERLESS; } -void WaylandWindow::initBarRenderer() { - if (barProgram != 0) { - return; - } - static const char* vertexSource = - "#version 330 core\n" - "layout(location = 0) in vec2 aPos;\n" - "layout(location = 1) in vec2 aUv;\n" - "layout(location = 2) in vec4 aColor;\n" - "uniform vec2 uViewport;\n" - "out vec2 vUv;\n" - "out vec4 vColor;\n" - "void main() {\n" - " vec2 ndc = vec2(aPos.x / uViewport.x * 2.0 - 1.0," - " 1.0 - aPos.y / uViewport.y * 2.0);\n" - " gl_Position = vec4(ndc, 0.0, 1.0);\n" - " vUv = aUv;\n" - " vColor = aColor;\n" - "}\n"; - static const char* fragmentSource = - "#version 330 core\n" - "in vec2 vUv;\n" - "in vec4 vColor;\n" - "out vec4 outColor;\n" - "uniform sampler2D uTexture;\n" - "uniform int uMode;\n" - "void main() {\n" - " if (uMode == 1) {\n" - " outColor = vec4(vColor.rgb," - " vColor.a * texture(uTexture, vUv).r);\n" - " } else if (uMode == 2) {\n" - " outColor = texture(uTexture, vUv) * vColor;\n" - " } else {\n" - " outColor = vColor;\n" - " }\n" - "}\n"; - barProgram = compile_program(vertexSource, fragmentSource); - barViewportLoc = glGetUniformLocation(barProgram, "uViewport"); - barTextureLoc = glGetUniformLocation(barProgram, "uTexture"); - barModeLoc = glGetUniformLocation(barProgram, "uMode"); - glGenVertexArrays(1, &barVao); - glGenBuffers(1, &barVbo); - glBindVertexArray(barVao); - glBindBuffer(GL_ARRAY_BUFFER, barVbo); - glEnableVertexAttribArray(0); - glVertexAttribPointer( - 0, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), nullptr - ); - glEnableVertexAttribArray(1); - glVertexAttribPointer( - 1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), - reinterpret_cast(2 * sizeof(float)) - ); - glEnableVertexAttribArray(2); - glVertexAttribPointer( - 2, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), - reinterpret_cast(4 * sizeof(float)) - ); - glBindVertexArray(0); - glGenTextures(1, &barFontTexture); - glBindTexture(GL_TEXTURE_2D, barFontTexture); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - std::vector pixels(BAR_GLYPHS * BAR_FONT_W * BAR_FONT_H, 0); - for (int glyph = 0; glyph < BAR_GLYPHS; glyph++) { - for (int y = 0; y < BAR_FONT_H; y++) { - for (int x = 0; x < BAR_FONT_W; x++) { - if (BAR_FONT[glyph][y] & (1 << (7 - x))) { - pixels[y * BAR_GLYPHS * BAR_FONT_W + glyph * BAR_FONT_W + x] = - 255; - } - } - } - } - glTexImage2D( - GL_TEXTURE_2D, 0, GL_R8, BAR_GLYPHS * BAR_FONT_W, BAR_FONT_H, 0, - GL_RED, GL_UNSIGNED_BYTE, pixels.data() - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, 0); -} - -void WaylandWindow::initBarIconTexture() { - if (iconPixels.empty() || barIconTexture != 0) { - return; - } - glGenTextures(1, &barIconTexture); - glBindTexture(GL_TEXTURE_2D, barIconTexture); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D( - GL_TEXTURE_2D, 0, GL_RGBA8, iconWidth, iconHeight, 0, GL_RGBA, - GL_UNSIGNED_BYTE, iconPixels.data() - ); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, 0); -} - -void WaylandWindow::barDrawText( - float x, float y, const std::vector& codepoints, int maxChars, - const glm::vec4& color -) { - const float step = 1.0f / static_cast(BAR_GLYPHS * BAR_FONT_W); - float cursor = x; - for (int i = 0; i < static_cast(codepoints.size()) && i < maxChars; i++) { - const int index = bar_glyph_index(codepoints[i]); - if (index >= 0) { - push_quad( - barText, cursor, y, BAR_FONT_W, BAR_FONT_H, - index * BAR_FONT_W * step, 0.0f, - (index * BAR_FONT_W + BAR_FONT_W) * step, 1.0f, color - ); - } - cursor += BAR_FONT_W; - } -} - -void WaylandWindow::barDrawBatch(const std::vector& data, int mode, GLuint texture) { - if (data.empty()) { - return; - } - glUniform1i(barModeLoc, mode); - glBindTexture(GL_TEXTURE_2D, texture); - glBufferData( - GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(), - GL_STREAM_DRAW - ); - glDrawArrays(GL_TRIANGLES, 0, static_cast(data.size() / 8)); -} - -void WaylandWindow::barFlush() { - glUseProgram(barProgram); - glUniform2f( - barViewportLoc, static_cast(size.x), static_cast(size.y) - ); - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - glDisable(GL_SCISSOR_TEST); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glBindVertexArray(barVao); - glBindBuffer(GL_ARRAY_BUFFER, barVbo); - glActiveTexture(GL_TEXTURE0); - glUniform1i(barTextureLoc, 0); - barDrawBatch(barRects, 0, barFontTexture); - barDrawBatch(barIcons, 2, barIconTexture); - barDrawBatch(barText, 1, barFontTexture); - glBindVertexArray(0); - glDisable(GL_BLEND); -} - -void WaylandWindow::drawBar() { - if (!barVisible()) { - return; - } - initBarRenderer(); - initBarIconTexture(); - barRects.clear(); - barText.clear(); - barIcons.clear(); - - const float width = static_cast(size.x); - const float height = static_cast(BAR_HEIGHT); - const glm::vec4 background(0.16f, 0.16f, 0.17f, 1.0f); - const glm::vec4 border(0.09f, 0.09f, 0.10f, 1.0f); - const glm::vec4 ink(0.87f, 0.87f, 0.88f, 1.0f); - const glm::vec4 hover(0.27f, 0.27f, 0.29f, 1.0f); - const glm::vec4 closeHover(0.72f, 0.16f, 0.11f, 1.0f); - - push_quad(barRects, 0, 0, width, height, 0, 0, 0, 0, background); - push_quad(barRects, 0, height - 1, width, 1, 0, 0, 0, 0, border); - - float textX = 10.0f; - if (barIconTexture != 0 && iconWidth > 0) { - push_quad( - barIcons, 7, (height - BAR_ICON_SIZE) * 0.5f, BAR_ICON_SIZE, - BAR_ICON_SIZE, 0, 1, 1, 0, glm::vec4(1.0f) - ); - textX = 7 + BAR_ICON_SIZE + 8; - } - const float buttonsX = width - BAR_BUTTON * 3; - const int maxChars = - static_cast((buttonsX - 8 - textX) / BAR_FONT_W); - if (maxChars > 0) { - barDrawText( - textX, (height - BAR_FONT_H) * 0.5f, decode_utf8(title), - maxChars, ink - ); - } - - for (int i = 0; i < 3; i++) { - const float bx = width - BAR_BUTTON * (i + 1); - if (hoveredButton == i) { - push_quad( - barRects, bx, 0, BAR_BUTTON, height - 1, 0, 0, 0, 0, - i == 0 ? closeHover : hover - ); - } - const float cx = bx + BAR_BUTTON * 0.5f; - const float cy = height * 0.5f; - if (i == 0) { - for (int s = 0; s < 7; s++) { - push_quad(barRects, cx - 4 + s, cy - 4 + s, 1.6f, 1.6f, 0, 0, 0, 0, ink); - push_quad(barRects, cx + 3 - s, cy - 4 + s, 1.6f, 1.6f, 0, 0, 0, 0, ink); - } - } else if (i == 1 && !maximized) { - push_quad(barRects, cx - 4.5f, cy - 4.5f, 9, 1.6f, 0, 0, 0, 0, ink); - push_quad(barRects, cx - 4.5f, cy + 2.9f, 9, 1.6f, 0, 0, 0, 0, ink); - push_quad(barRects, cx - 4.5f, cy - 4.5f, 1.6f, 9, 0, 0, 0, 0, ink); - push_quad(barRects, cx + 2.9f, cy - 4.5f, 1.6f, 9, 0, 0, 0, 0, ink); - } else if (i == 1) { - push_quad(barRects, cx - 3.5f, cy - 5.5f, 8, 1.4f, 0, 0, 0, 0, ink); - push_quad(barRects, cx + 3.1f, cy - 5.5f, 1.4f, 5.5f, 0, 0, 0, 0, ink); - push_quad(barRects, cx - 5.5f, cy - 1.2f, 8, 1.4f, 0, 0, 0, 0, ink); - push_quad(barRects, cx - 5.5f, cy - 1.2f, 1.4f, 6.2f, 0, 0, 0, 0, ink); - push_quad(barRects, cx + 1.1f, cy - 1.2f, 1.4f, 6.2f, 0, 0, 0, 0, ink); - push_quad(barRects, cx - 5.5f, cy + 3.6f, 8, 1.4f, 0, 0, 0, 0, ink); - } else { - push_quad(barRects, cx - 5, cy - 0.8f, 10, 1.6f, 0, 0, 0, 0, ink); - } - } - barFlush(); -} - int WaylandWindow::barButtonAt(double x, double y) const { if (!barVisible() || y < 0.0 || y >= BAR_HEIGHT) { return -1; diff --git a/src/window/wayland/WaylandCommon.cpp b/src/window/wayland/WaylandCommon.cpp index 5da49c6e0..5ea51bbae 100644 --- a/src/window/wayland/WaylandCommon.cpp +++ b/src/window/wayland/WaylandCommon.cpp @@ -6,6 +6,78 @@ debug::Logger waylandLogger("window"); +GlStateGuard::GlStateGuard() { + glGetIntegerv(GL_CURRENT_PROGRAM, &values[0]); + glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &values[1]); + glGetIntegerv(GL_ACTIVE_TEXTURE, &values[2]); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &values[3]); + glGetIntegerv(GL_UNPACK_ALIGNMENT, &values[4]); + glGetIntegerv(GL_BLEND_SRC_RGB, &values[5]); + glGetIntegerv(GL_BLEND_DST_RGB, &values[6]); + glGetIntegerv(GL_BLEND_SRC_ALPHA, &values[7]); + glGetIntegerv(GL_BLEND_DST_ALPHA, &values[8]); + glGetIntegerv(GL_BLEND_EQUATION_RGB, &values[9]); + glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &values[10]); + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &values[11]); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &values[12]); + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &values[13]); + glGetIntegerv(GL_DEPTH_WRITEMASK, &values[14]); + glGetIntegerv(GL_VIEWPORT, viewport); + glGetIntegerv(GL_COLOR_WRITEMASK, colorMask); + for (int i = 0; i < 3; i++) { + attribFlags[i] = glIsEnabled(GL_VERTEX_ATTRIB_ARRAY_ENABLED + i); + glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &attribs[i][0]); + glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &attribs[i][1]); + glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &attribs[i][2]); + glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &attribs[i][3]); + glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &attribs[i][5]); + void* pointer = nullptr; + glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer); + attribs[i][6] = static_cast(reinterpret_cast(pointer)); + } + flags[0] = glIsEnabled(GL_BLEND); + flags[1] = glIsEnabled(GL_DEPTH_TEST); + flags[2] = glIsEnabled(GL_CULL_FACE); + flags[3] = glIsEnabled(GL_SCISSOR_TEST); +} + +GlStateGuard::~GlStateGuard() { + glUseProgram(values[0]); + glBindVertexArray(values[1]); + glActiveTexture(values[2]); + glBindTexture(GL_TEXTURE_2D, values[3]); + glPixelStorei(GL_UNPACK_ALIGNMENT, values[4]); + glBlendFuncSeparate(values[5], values[6], values[7], values[8]); + glBlendEquationSeparate(values[9], values[10]); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, values[12]); + glBindFramebuffer(GL_READ_FRAMEBUFFER, values[13]); + glDepthMask(values[14]); + glColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]); + glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); + const GLenum cap[] {GL_BLEND, GL_DEPTH_TEST, GL_CULL_FACE, GL_SCISSOR_TEST}; + for (int i = 0; i < 4; i++) { + if (flags[i]) { + glEnable(cap[i]); + } else { + glDisable(cap[i]); + } + } + for (int i = 0; i < 3; i++) { + glBindBuffer(GL_ARRAY_BUFFER, static_cast(attribs[i][5])); + glVertexAttribPointer( + i, attribs[i][0], static_cast(attribs[i][1]), + attribs[i][2], attribs[i][3], + reinterpret_cast(static_cast(attribs[i][6])) + ); + if (attribFlags[i]) { + glEnableVertexAttribArray(i); + } else { + glDisableVertexAttribArray(i); + } + } + glBindBuffer(GL_ARRAY_BUFFER, static_cast(values[11])); +} + GLuint compile_program(const char* vertexSource, const char* fragmentSource) { GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexSource, nullptr); diff --git a/src/window/wayland/WaylandCommon.hpp b/src/window/wayland/WaylandCommon.hpp index 6bbb5df1e..3407a0ef1 100644 --- a/src/window/wayland/WaylandCommon.hpp +++ b/src/window/wayland/WaylandCommon.hpp @@ -37,6 +37,17 @@ const wl_data_device_listener& data_device_listener(); const xdg_surface_listener& surface_listener(); const xdg_toplevel_listener& toplevel_listener(); +struct GlStateGuard { + GLint values[16] {}; + GLint attribs[3][7] {}; + GLint viewport[4] {}; + GLint colorMask[4] {}; + GLboolean flags[6] {}; + GLboolean attribFlags[3] {}; + GlStateGuard(); + ~GlStateGuard(); +}; + GLuint compile_program(const char* vertexSource, const char* fragmentSource); extern debug::Logger waylandLogger; diff --git a/src/window/wayland/WaylandWindow.cpp b/src/window/wayland/WaylandWindow.cpp index 51c8a86b5..f24b0a5f8 100644 --- a/src/window/wayland/WaylandWindow.cpp +++ b/src/window/wayland/WaylandWindow.cpp @@ -43,21 +43,6 @@ WaylandWindow::~WaylandWindow() { if (stretchVao) { glDeleteVertexArrays(1, &stretchVao); } - if (barFontTexture) { - glDeleteTextures(1, &barFontTexture); - } - if (barIconTexture) { - glDeleteTextures(1, &barIconTexture); - } - if (barProgram) { - glDeleteProgram(barProgram); - } - if (barVbo) { - glDeleteBuffers(1, &barVbo); - } - if (barVao) { - glDeleteVertexArrays(1, &barVao); - } if (state.dataDevice) { wl_data_device_destroy(state.dataDevice); } @@ -109,7 +94,6 @@ void WaylandWindow::swapBuffers() { } else { cacheContent(); } - drawBar(); requestFrame(); eglSwapBuffers(eglDisplay, eglSurface); resetScissor(); @@ -363,12 +347,16 @@ void WaylandWindow::resetScissor() { std::unique_ptr WaylandWindow::takeScreenshot() { glBindFramebuffer(GL_FRAMEBUFFER, 0); + GLint readBuffer = 0; + glGetIntegerv(GL_READ_BUFFER, &readBuffer); + glReadBuffer(GL_FRONT); auto data = std::make_unique(size.x * size.y * 3); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, size.x, size.y, GL_RGB, GL_UNSIGNED_BYTE, data.get()); return std::make_unique( ImageFormat::RGB888, size.x, size.y, data.release() ); + glReadBuffer(static_cast(readBuffer)); } void WaylandWindow::setFramerate(int framerate) { @@ -445,6 +433,7 @@ void WaylandWindow::resizeCache(int width, int height) { } void WaylandWindow::cacheContent() { + GlStateGuard state; if (stretchProgram == 0) { return; } @@ -460,6 +449,7 @@ void WaylandWindow::cacheContent() { } void WaylandWindow::drawStretched() { + GlStateGuard state; glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, size.x, size.y); glDisable(GL_DEPTH_TEST); diff --git a/src/window/wayland/WaylandWindow.hpp b/src/window/wayland/WaylandWindow.hpp index 76c426399..1df0e861d 100644 --- a/src/window/wayland/WaylandWindow.hpp +++ b/src/window/wayland/WaylandWindow.hpp @@ -37,24 +37,10 @@ static inline bool resizeLogging = getenv("VOXEL_LOG_RESIZE") != nullptr; bool barEnabled = false; bool fullscreen = false; std::string title = "VoxelCore"; -std::vector iconPixels; -int iconWidth = 0; -int iconHeight = 0; int hoveredButton = -1; double lastBarPress = 0.0; double pointerX = 0.0; double pointerY = 0.0; -GLuint barProgram = 0; -GLuint barVao = 0; -GLuint barVbo = 0; -GLuint barFontTexture = 0; -GLuint barIconTexture = 0; -GLint barViewportLoc = -1; -GLint barTextureLoc = -1; -GLint barModeLoc = -1; -std::vector barRects; -std::vector barText; -std::vector barIcons; bool frameOnCallback = false; double frameInterval = 0.0; double nextFrameTime = 0.0; @@ -111,10 +97,18 @@ glm::vec4 scissorArea {}; WindowMode getMode() const override; void focus() override; void setTitle(const std::string& title) override; + const std::string& getTitle() const override; + int getDecorationHeight() const override; + int getDecorationHoveredButton() const override; + + bool isIconSupported() const override { + return false; + } + + void setIcon(const ImageData*) override { + } void setFullscreen(bool enabled); void setMaximized(bool enabled); - bool isIconSupported() const override; - void setIcon(const ImageData* image) override; void resize(int width, int height); void onConfigure(int32_t width, int32_t height, wl_array* states); void applyConfigure(); @@ -129,13 +123,7 @@ glm::vec4 scissorArea {}; void drawStretched(); void enableOwnDecorations(); bool barVisible() const; - void initBarRenderer(); - void initBarIconTexture(); - void barDrawText( float x, float y, const std::vector& codepoints, int maxChars, const glm::vec4& color ); - void barDrawBatch(const std::vector& data, int mode, GLuint texture); - void barFlush(); - void drawBar(); - int barButtonAt(double x, double y) const; + int barButtonAt(double x, double y) const; uint32_t barEdgeAt(double x, double y) const; static CursorShape edgeCursor(uint32_t edges); bool handleBarMotion(double x, double y);