~aleteoryx/muditaos

muditaos/module-gui/gui/core/DrawCommand.hpp -rw-r--r-- 4.5 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <string>
#include <cstdint>
#include <Math.hpp>
#include <utf8/UTF8.hpp>
#include <gui/Common.hpp>

#include "Color.hpp"
#include "Context.hpp"
#include <FontGlyph.hpp>

#include "PixMap.hpp"
#include "VecMap.hpp"

namespace gui
{
    /**
     * @brief Draw command interface.
     */
    class DrawCommand
    {
      public:
        std::int16_t areaX{0};
        std::int16_t areaY{0};
        Length areaW{0};
        Length areaH{0};

        virtual ~DrawCommand() = default;

        virtual void draw(Context *ctx) const = 0;
    };

    class Clear : public DrawCommand
    {
        void draw(Context *ctx) const override;
    };

    /**
     * @brief Draw command for line.
     */
    class DrawLine : public DrawCommand
    {
      public:
        Point start{0, 0};
        Point end{0, 0};
        Color color{ColorFullBlack};
        uint8_t penWidth{1};

        void draw(Context *ctx) const override;
    };

    /**
     * @brief Draw command for rectangle.
     */
    class DrawRectangle : public DrawCommand
    {
      public:
        Point origin{0, 0};
        Length width{0};
        Length height{0};
        Length radius{0};

        // Flags that defines whether paint given border
        RectangleEdge edges{RectangleEdge::All};
        // Flags that defines which edge should be flat. This will disable roundness on both sides of the edge.
        RectangleFlatEdge flatEdges{RectangleFlatEdge::None};
        // Flags that defines whether paint given corner (only for rounded corners)
        RectangleRoundedCorner corners{RectangleRoundedCorner::All};
        // Flags indicating yaps for speech bubbles, it takes precedence over other properties
        RectangleYap yaps{RectangleYap::None};
        // Defines which of the edges and corners are painted
        unsigned short yapSize{0};

        bool filled{false};
        std::uint8_t penWidth{1};
        Color fillColor{ColorFullBlack};
        Color borderColor{ColorFullBlack};

        void draw(Context *ctx) const override;
    };

    /**
     * @brief Draw command for arc.
     */
    class DrawArc : public DrawCommand
    {
      public:
        const trigonometry::Degrees start;
        const trigonometry::Degrees sweep;
        const Length width;
        const Color borderColor;
        const Point center;
        const Length radius;

        DrawArc(Point _center,
                Length _radius,
                trigonometry::Degrees _start,
                trigonometry::Degrees _sweep,
                Length _width,
                Color _color)
            : start{_start}, sweep{_sweep}, width{_width}, borderColor{_color}, center{_center}, radius{_radius}
        {}

        void draw(Context *ctx) const override;
    };

    /**
     * @brief Draw command for circle.
     */
    class DrawCircle : public DrawArc
    {
      public:
        const bool filled;
        const Color fillColor;

        DrawCircle(Point _center,
                   Length _radius,
                   Length _borderWidth,
                   Color _borderColor,
                   bool _filled     = false,
                   Color _fillColor = {})
            : DrawArc{_center, _radius, 0, trigonometry::FullAngle, _borderWidth, _borderColor}, filled{_filled},
              fillColor{_fillColor}
        {}

        void draw(Context *ctx) const override;
    };

    /**
     * @brief Draw command for text line.
     */
    class DrawText : public DrawCommand
    {
      public:
        // Area where label will be drawn
        Point origin{0, 0};
        Length width{0};
        Length height{0};

        // Area occupied by text inside the label
        Point textOrigin{0, 0};
        Length textHeight{0};

        UTF8 str{};
        uint8_t fontID{0};
        Color color{ColorFullBlack};

        void draw(Context *ctx) const override;

      private:
        void drawChar(Context *ctx, Point glyphOrigin, FontGlyph *glyph) const;
    };

    /**
     * @brief Draw command for image.
     */
    class DrawImage : public DrawCommand
    {
      public:
        Point origin{0, 0};

        // ID of the image
        std::uint16_t imageID{0};

        void draw(Context *ctx) const override;

      private:
        void drawPixMap(Context *ctx, PixMap *pixMap) const;
        void drawVecMap(Context *ctx, VecMap *vecMap) const;
        inline void checkImageSize(Context *ctx, ImageMap *image) const;
    };
} /* namespace gui */