Added a BackgroundComponent to replace NinePatchComponent for rendering menu and popup backgrounds

This commit is contained in:
Leon Styhre 2025-02-09 17:43:30 +01:00
parent ba5f85c74c
commit 4e12ad52b6
3 changed files with 81 additions and 0 deletions

View file

@ -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

View file

@ -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());
}

View file

@ -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