7 std::shared_ptr<business_logic::BlocksManager> blocksManager)
8 :
gui::renderer::delegate::UIRendererDelegate<SkCanvas>(),
10 blocksManager(
std::move(blocksManager)),
12 uiRendererDelegatePtr(
13 static_cast<
gui::renderer::delegate::UIRendererDelegate<SkCanvas>*>(this)) {
15 throw std::runtime_error(
"Invalid window handle provided to SkiaRendererImpl");
21 throw std::runtime_error(
22 "Failed to create Skia surface (SkSurfaces::RenderTarget returned null)");
29 if (windowSize.width == 0 && windowSize.height == 0) {
33 logger->info(
"Reinitializing Skia surface with width {} and height {}",
37 sk_sp<const GrGLInterface>
const interface = GrGLMakeNativeInterface();
39 throw std::runtime_error(
"Failed to create GL interface");
42 grContext = GrDirectContexts::MakeGL(interface);
44 GrGLFramebufferInfo framebufferInfo;
45 framebufferInfo.fFBOID = 0;
46 framebufferInfo.fFormat = GL_RGBA8;
49 GrBackendRenderTarget
const renderTarget = GrBackendRenderTargets::MakeGL(
50 framebufferSize.width, framebufferSize.height, 0, 8, framebufferInfo);
54 kBottomLeft_GrSurfaceOrigin,
55 kRGBA_8888_SkColorType,
66 SkCanvas* canvas =
skSurface->getCanvas();
81 skSurface->getCanvas()->scale(xScale, yScale);
86 static_cast<float>(windowSize.width) /
88 static_cast<float>(windowSize.height) /
97 const std::vector<components::UITextsRow>& rows) {
99 auto rowsHeights = std::vector<SkScalar>(rows.size());
102 SkScalar const maxHeight = std::accumulate(
103 row.getUiTexts().begin(),
104 row.getUiTexts().end(),
106 [](SkScalar currentMax, const auto& text) {
109 FontManager::getFontForVariant(text.getVariant()).getSize());
118 std::accumulate(rowsHeights.begin(), rowsHeights.end(),
static_cast<SkScalar
>(0.0F));
120 auto screenCenterX =
static_cast<SkScalar
>(size.width) / 2.0F;
121 auto screenCenterY =
static_cast<SkScalar
>(size.height) / 2.0F;
124 auto renderY = screenCenterY -
130 for (
const auto& row : rows) {
133 auto textsWidths = std::vector<SkScalar>(row.getUiTexts().size());
134 std::transform(row.getUiTexts().begin(),
135 row.getUiTexts().end(),
138 return FontManager::getFontForVariant(uiText.getVariant())
139 .measureText(uiText.getText().c_str(),
140 uiText.getText().length(),
141 SkTextEncoding::kUTF8);
144 const auto sumOfWidths = std::accumulate(
145 textsWidths.begin(), textsWidths.end(),
static_cast<SkScalar
>(0.0F));
158 renderY += halfMarginY;
162 for (
const auto& uiText : row.getUiTexts()) {
164 const auto& text = uiText.getText();
166 auto halfMarginX = textsWidths[textIndex] *
169 renderX += halfMarginX;
180 renderX += textsWidths[textIndex] + halfMarginX;
186 renderY += rowsHeights[rowIndex] + halfMarginY;
constexpr float CENTERED_TEXT_ROWS_MARGIN_HORIZONTAL_NORM_PERCENT
constexpr float CENTERED_TEXT_ROWS_MARGIN_VERTICAL_NORM_PERCENT
std::shared_ptr< spdlog::logger > logger
The UI renderable text primitive.
The UI renderable row of UITexts primitive.
Class that extends business_logic::BlocksManager and adds rendering logic.
static void recalculateFontSizes(float aspectX, float aspectY, float xScale, float yScale)
Recalculates the font sizes.
static SkPaint textFontFillPaint
The paint for the text font fill.
static const SkFont & getFontForVariant(const business_logic::components::UIText::Variant &variant)
Gets the font for the given variant.
static SkPaint textFontStrokePaint
The paint for the text font stroke.
void render()
Renders the current frame.
void handleWindowResized(gui::window::WindowBase< SkCanvas > *window, float xScale, float yScale)
Handles window resize events.
std::shared_ptr< business_logic::BlocksManager > blocksManager
The blocks manager.
void reinitializeSurface()
Reinitializes the Skia surface using the current width and height.
void renderCenteredTextsRows(SkCanvas *canvas, const business_logic::geometry::Size2D &size, const std::vector< components::UITextsRow > &rows) override
Renders the texts on the canvas around the center horizontally, centered vertically.
sk_sp< GrDirectContext > grContext
The Skia context.
gui::window::WindowBase< SkCanvas > * window
The window.
SkiaRendererImpl(gui::window::WindowBase< SkCanvas > *window, std::shared_ptr< business_logic::BlocksManager > blocksManager)
Constructs a new SkiaRendererImpl.
~SkiaRendererImpl() override
Destructor.
sk_sp< SkSurface > skSurface
The Skia surface.
gui::renderer::delegate::UIRendererDelegate< SkCanvas > * uiRendererDelegatePtr
The UI renderer delegate.
The abstract base class for implementing a window.
business_logic::geometry::Size2D getFramebufferSize() override
Get the framebuffer size.
business_logic::geometry::Size2D getWindowSize() override
Get the window size.
constexpr int FONT_ASPECT_BASE_WINDOW_WIDTH
The base window width that the current width is divided by to obtain a scaling factor for font size.
constexpr int FONT_ASPECT_BASE_WINDOW_HEIGHT
The base window height that the current height is divided by to obtain a scaling factor for font size...
const SkColor WINDOW_BACKGROUND_COLOR
The GUI renderer and renderer-supporting implementation module.
The actual GUI / renderer implementation counterpart.
A basic struct representing a 2D size.