This repository has been archived by the owner on May 14, 2021. It is now read-only.
/
display.hpp
127 lines (108 loc) · 3.32 KB
/
display.hpp
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
// sherpa_41's Display module, licensed under MIT. (c) hafiz, 2018
#ifndef DISPLAY_HPP
#define DISPLAY_HPP
#include "css.hpp"
#include "layout.hpp"
#include <memory>
#include <queue>
class Renderer;
/**
* The Display module issues commands for rendering various boxes with styled
* elements in the browser. This separates the concern of _what_ to render with
* _how_ to render it, with each command having the ability to accept any kind
* of renderer.
*
* The following rendering commands are supported:
* - RectangleCmd: a rectangle of a solid color
*/
namespace Display {
// forward declaration
class Command;
typedef std::unique_ptr<Command> CommandPtr;
typedef std::queue<CommandPtr> CommandQueue;
/**
* An abstract class describing a display command
*/
class Command {
public:
/**
* Pure virtual dtor
*/
virtual ~Command() = 0;
/**
* Accepts a renderer to the command
* @param renderer accepted renderer
*/
virtual void acceptRenderer(Renderer & renderer) const = 0;
/**
* Creates a queue of display commands to execute
* @param root root layout node
* @return queue of commands
*/
static CommandQueue createQueue(const Layout::BoxPtr & root);
private:
/**
* Creates the commands to render a box
* @param box box to render
* @param queue queue to add commands to
*/
static void renderBox(const Layout::BoxPtr & box, CommandQueue & queue);
/**
* Creates the commands to render the background of a box
* @param box box to render background of
* @param queue queue to add commands to
*/
static void renderBackground(const Layout::BoxPtr & box,
CommandQueue & queue);
/**
* Creates the commands to render the borders of a box
* @param box box to render borders of
* @param queue queue to add commands to
*/
static void renderBorders(const Layout::BoxPtr & box, CommandQueue & queue);
/**
* Gets the color value of a style from a box, or nullptr if no color is
* specified for that style
* @tparam Args variadic arguments, should be strings
* @param box box to get style from
* @param style style to look up
* @param backup backup styles to look up
* @return color value, or nullptr if it does not exist
*/
template <typename... Args>
static CSS::ValuePtr getColor(const Layout::BoxPtr & box,
const std::string & style,
const Args &... backup);
};
/**
* A command to render a rectangle
*/
class RectangleCmd : public Command {
public:
/**
* Command to create a rectangle of a color
* @param rectangle rectangle to create
* @param color color to color rectangle
*/
RectangleCmd(Layout::Rectangle rectangle, CSS::ColorValue color);
/**
* Accepts a renderer to the command
* @param renderer accepted renderer
*/
void acceptRenderer(Renderer & renderer) const override;
/**
* Returns encompassing rectangle
* @return rectangle
*/
Layout::Rectangle getRectangle() const;
/**
* Returns color
* @return color
*/
CSS::ColorValue getColor() const;
private:
Layout::Rectangle rectangle;
CSS::ColorValue color;
};
} // namespace Display
#endif