From 4e12ad52b6e2ac60fe00b644a66d4f76983cef87 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 9 Feb 2025 17:43:30 +0100 Subject: [PATCH] Added a BackgroundComponent to replace NinePatchComponent for rendering menu and popup backgrounds --- es-core/CMakeLists.txt | 2 + .../src/components/BackgroundComponent.cpp | 44 +++++++++++++++++++ es-core/src/components/BackgroundComponent.h | 35 +++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 es-core/src/components/BackgroundComponent.cpp create mode 100644 es-core/src/components/BackgroundComponent.h diff --git a/es-core/CMakeLists.txt b/es-core/CMakeLists.txt index efaea1bb6..15510c3ac 100644 --- a/es-core/CMakeLists.txt +++ b/es-core/CMakeLists.txt @@ -39,6 +39,7 @@ set(CORE_HEADERS # Secondary GUI components ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimatedImageComponent.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BackgroundComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BadgeComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BusyComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.h @@ -117,6 +118,7 @@ set(CORE_SOURCES # Secondary GUI components ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimatedImageComponent.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BackgroundComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BadgeComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BusyComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.cpp diff --git a/es-core/src/components/BackgroundComponent.cpp b/es-core/src/components/BackgroundComponent.cpp new file mode 100644 index 000000000..16e6b8cff --- /dev/null +++ b/es-core/src/components/BackgroundComponent.cpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: MIT +// +// ES-DE Frontend +// BackgroundComponent.cpp +// +// Displays a background frame with rounded corners. +// Used by menus, popups etc. +// + +#include "components/BackgroundComponent.h" + +BackgroundComponent::BackgroundComponent(const glm::vec2 cornerSize) + : mRenderer {Renderer::getInstance()} + , mCornerSize {cornerSize} + , mFrameColor {mMenuColorFrame} + , mCornerAntialias {true} +{ +} + +void BackgroundComponent::fitTo(glm::vec2 size, glm::vec3 position, glm::vec2 padding) +{ + if (padding != glm::vec2 {0.0f, 0.0f}) { + size += padding; + position.x -= padding.x / 2.0f; + position.y -= padding.y / 2.0f; + } + + setSize(size); + setPosition(position); +} + +void BackgroundComponent::render(const glm::mat4& parentTrans) +{ + if (!isVisible()) + return; + + glm::mat4 trans {parentTrans * getTransform()}; + + mRenderer->setMatrix(trans); + mRenderer->drawRect(0.0f, 0.0f, mSize.x, mSize.y, mFrameColor, mFrameColor, false, mOpacity, + 1.0f, Renderer::BlendFactor::SRC_ALPHA, + Renderer::BlendFactor::ONE_MINUS_SRC_ALPHA, + mCornerSize.x * mRenderer->getScreenResolutionModifier()); +} diff --git a/es-core/src/components/BackgroundComponent.h b/es-core/src/components/BackgroundComponent.h new file mode 100644 index 000000000..1beccd62d --- /dev/null +++ b/es-core/src/components/BackgroundComponent.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT +// +// ES-DE Frontend +// BackgroundComponent.h +// +// Displays a background frame with rounded corners. +// Used by menus, popups etc. +// + +#ifndef ES_CORE_COMPONENTS_BACKGROUND_COMPONENT_H +#define ES_CORE_COMPONENTS_BACKGROUND_COMPONENT_H + +#include "GuiComponent.h" +#include "renderers/Renderer.h" + +class BackgroundComponent : public GuiComponent +{ +public: + BackgroundComponent(const glm::vec2 cornerSize = glm::vec2 {30.0f, 30.0f}); + + void fitTo(glm::vec2 size, + glm::vec3 position = {0.0f, 0.0f, 0.0f}, + glm::vec2 padding = {0.0f, 0.0f}); + void setFrameColor(unsigned int frameColor) { mFrameColor = frameColor; } + + void render(const glm::mat4& parentTrans) override; + +private: + Renderer* mRenderer; + glm::vec2 mCornerSize; + unsigned int mFrameColor; + bool mCornerAntialias; +}; + +#endif // ES_CORE_COMPONENTS_BACKGROUND_COMPONENT_H