AeonGUI
A portable video game graphic user interface library.
Loading...
Searching...
No Matches
Canvas.hpp
1/*
2Copyright (C) 2019,2020,2024,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGUI_CANVAS_H
17#define AEONGUI_CANVAS_H
18#include <array>
19#include <cstdint>
20#include <cstddef>
21#include <vector>
22#include <memory>
23#include <string>
24#include "aeongui/Platform.hpp"
25#include "aeongui/DrawType.hpp"
26#include "aeongui/Color.hpp"
27#include "aeongui/Attribute.hpp"
28#include "aeongui/Matrix2x3.hpp"
29#include "aeongui/TextLayout.hpp"
30namespace AeonGUI
31{
32 class Path;
39 class Canvas
40 {
41 public:
46 virtual void ResizeViewport ( uint32_t aWidth, uint32_t aHeight ) = 0;
50 virtual const uint8_t* GetPixels() const = 0;
54 virtual size_t GetWidth() const = 0;
58 virtual size_t GetHeight() const = 0;
62 virtual size_t GetStride() const = 0;
64 virtual void Clear() = 0;
68 virtual void SetFillColor ( const ColorAttr& aColor ) = 0;
72 virtual const ColorAttr& GetFillColor() const = 0;
76 virtual void SetStrokeColor ( const ColorAttr& aColor ) = 0;
80 virtual const ColorAttr& GetStrokeColor() const = 0;
84 virtual void SetStrokeWidth ( double aWidth ) = 0;
88 virtual double GetStrokeWidth () const = 0;
92 virtual void SetStrokeOpacity ( double aWidth ) = 0;
96 virtual double GetStrokeOpacity () const = 0;
100 virtual void SetFillOpacity ( double aWidth ) = 0;
104 virtual double GetFillOpacity () const = 0;
108 virtual void SetOpacity ( double aWidth ) = 0;
112 virtual double GetOpacity () const = 0;
116 virtual void Draw ( const Path& aPath ) = 0;
128 virtual void DrawImage ( const uint8_t* aPixels,
129 size_t aImageWidth,
130 size_t aImageHeight,
131 size_t aImageStride,
132 double aX,
133 double aY,
134 double aWidth,
135 double aHeight,
136 double aOpacity ) = 0;
146 virtual void DrawText ( const std::string& aText, double aX, double aY,
147 const std::string& aFontFamily, double aFontSize,
148 int aFontWeight, int aFontStyle ) = 0;
157 virtual double MeasureText ( const std::string& aText,
158 const std::string& aFontFamily, double aFontSize,
159 int aFontWeight, int aFontStyle ) const = 0;
172 virtual void DrawTextOnPath ( const std::string& aText,
173 const Path& aPath,
174 double aStartOffset,
175 const std::string& aFontFamily, double aFontSize,
176 int aFontWeight, int aFontStyle,
177 bool aReverse = false, bool aClosed = false ) = 0;
182 virtual void SetViewBox ( const ViewBox& aViewBox, const PreserveAspectRatio& aPreserveAspectRatio ) = 0;
186 virtual void SetTransform ( const Matrix2x3& aMatrix ) = 0;
190 virtual void Transform ( const Matrix2x3& aMatrix ) = 0;
193 virtual void Save() = 0;
196 virtual void Restore() = 0;
200 virtual void* GetNativeSurface() const = 0;
206 virtual void PushGroup() = 0;
212 virtual void PopGroup() = 0;
226 virtual void ApplyDropShadow ( double aDx, double aDy,
227 double aStdDeviationX, double aStdDeviationY,
228 const Color& aFloodColor, double aFloodOpacity ) = 0;
236 void SetHitTesting ( bool aHitTesting )
237 {
238 mHitTesting = aHitTesting;
239 }
240
243 bool IsHitTesting() const
244 {
245 return mHitTesting;
246 }
247
254 void SetPickId ( uint8_t aPickId )
255 {
256 mPickId = aPickId;
257 }
258
263 virtual uint8_t PickAtPoint ( double aX, double aY ) const = 0;
265 virtual void ResetPick() = 0;
274 virtual void SetClipRect ( double aX, double aY, double aWidth, double aHeight ) = 0;
278 virtual std::unique_ptr<Path> CreatePath() const = 0;
280 virtual ~Canvas() = 0;
281
284 {
285 double x1{0};
286 double y1{0};
287 double x2{0};
288 double y2{0};
289 };
290
294 const PickBounds& GetPickBounds ( uint8_t aId ) const
295 {
296 return mPickBounds[aId];
297 }
298 protected:
299 bool mHitTesting{false};
300 uint8_t mPickId{0};
301 std::array<PickBounds, 256> mPickBounds{};
302 };
303}
304#endif
Platform-specific DLL import/export macros and compiler helpers.
Abstract 2D rendering surface.
Definition Canvas.hpp:40
virtual double GetOpacity() const =0
Get the current global opacity.
virtual size_t GetStride() const =0
Get the stride (bytes per row) of the pixel buffer.
virtual const uint8_t * GetPixels() const =0
Get a pointer to the raw pixel data.
virtual const ColorAttr & GetFillColor() const =0
Get the current fill color.
virtual void SetStrokeColor(const ColorAttr &aColor)=0
Set the stroke color.
void SetPickId(uint8_t aPickId)
Set the current pick ID for subsequent Draw calls.
Definition Canvas.hpp:254
virtual std::unique_ptr< Path > CreatePath() const =0
Create a new Path object suitable for this canvas backend.
virtual void PopGroup()=0
End an offscreen group and composite back.
virtual void SetOpacity(double aWidth)=0
Set the global opacity.
virtual void Draw(const Path &aPath)=0
Draw a path using the current fill and stroke settings.
virtual void ApplyDropShadow(double aDx, double aDy, double aStdDeviationX, double aStdDeviationY, const Color &aFloodColor, double aFloodOpacity)=0
Apply a drop-shadow filter to the current group content.
virtual void PushGroup()=0
Begin an offscreen group for filter/compositing.
virtual double GetFillOpacity() const =0
Get the current fill opacity.
virtual ~Canvas()=0
Virtual destructor.
virtual void Save()=0
Save the current graphics state (transform, clipping, etc.).
const PickBounds & GetPickBounds(uint8_t aId) const
Get the cached device-space bounds for a pick ID.
Definition Canvas.hpp:294
void SetHitTesting(bool aHitTesting)
Enable or disable hit-testing mode.
Definition Canvas.hpp:236
virtual void Transform(const Matrix2x3 &aMatrix)=0
Pre-multiply the current transformation matrix.
virtual void * GetNativeSurface() const =0
Get the native rendering surface handle.
virtual size_t GetWidth() const =0
Get the width of the canvas in pixels.
virtual double GetStrokeOpacity() const =0
Get the current stroke opacity.
virtual void Restore()=0
Restore the previously saved graphics state.
virtual void ResetPick()=0
Clear the pick buffer and reset for a new frame.
virtual double GetStrokeWidth() const =0
Get the current stroke width.
virtual void SetStrokeWidth(double aWidth)=0
Set the stroke width.
virtual void ResizeViewport(uint32_t aWidth, uint32_t aHeight)=0
Resize the rendering viewport.
bool IsHitTesting() const
Query whether the canvas is in hit-testing mode.
Definition Canvas.hpp:243
virtual size_t GetHeight() const =0
Get the height of the canvas in pixels.
bool mHitTesting
True when in hit-testing mode.
Definition Canvas.hpp:299
virtual void SetViewBox(const ViewBox &aViewBox, const PreserveAspectRatio &aPreserveAspectRatio)=0
Set the SVG viewBox and preserveAspectRatio.
virtual void SetFillOpacity(double aWidth)=0
Set the fill opacity.
virtual void DrawTextOnPath(const std::string &aText, const Path &aPath, double aStartOffset, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle, bool aReverse=false, bool aClosed=false)=0
uint8_t mPickId
Current pick ID for Draw calls.
Definition Canvas.hpp:300
virtual void SetClipRect(double aX, double aY, double aWidth, double aHeight)=0
Set a device-space clip rectangle on both render and pick surfaces.
std::array< PickBounds, 256 > mPickBounds
Cached device-space bounds per pick ID.
Definition Canvas.hpp:301
virtual void Clear()=0
Clear the canvas to transparent.
virtual void SetFillColor(const ColorAttr &aColor)=0
Set the fill color.
virtual void DrawImage(const uint8_t *aPixels, size_t aImageWidth, size_t aImageHeight, size_t aImageStride, double aX, double aY, double aWidth, double aHeight, double aOpacity)=0
Draw a raster image.
virtual const ColorAttr & GetStrokeColor() const =0
Get the current stroke color.
virtual void DrawText(const std::string &aText, double aX, double aY, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle)=0
virtual void SetStrokeOpacity(double aWidth)=0
Set the stroke opacity.
virtual void SetTransform(const Matrix2x3 &aMatrix)=0
Replace the current transformation matrix.
virtual uint8_t PickAtPoint(double aX, double aY) const =0
Read the pick ID at the given viewport coordinates.
virtual double MeasureText(const std::string &aText, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle) const =0
2x3 affine transformation matrix.
Definition Matrix2x3.hpp:37
Abstract base class for renderable path data.
Definition Path.hpp:40
SVG preserveAspectRatio attribute.
Definition Attribute.hpp:44
Device-space bounding box for a pick-tracked element.
Definition Canvas.hpp:284
double x1
Left edge (device pixels).
Definition Canvas.hpp:285
double y1
Top edge (device pixels).
Definition Canvas.hpp:286
double y2
Bottom edge (device pixels).
Definition Canvas.hpp:288
double x2
Right edge (device pixels).
Definition Canvas.hpp:287
SVG viewBox attribute value.
Definition Attribute.hpp:31
Pixel Color Union. The Color union allows access to each unsigned 8 bit RGBA color component individu...
Definition Color.hpp:185