TermOx
glyph.hpp
1 #ifndef TERMOX_PAINTER_GLYPH_HPP
2 #define TERMOX_PAINTER_GLYPH_HPP
3 #include <utility>
4 
5 #include <termox/painter/brush.hpp>
6 
7 namespace ox {
8 
10 
11 struct Glyph {
12  public:
14  char32_t symbol = U'\0';
15 
18 
19  public:
21  constexpr Glyph() = default;
22 
24  constexpr Glyph(char32_t sym, Brush b) : symbol{sym}, brush{b} {}
25 
27  template <typename... Items>
28  constexpr Glyph(char32_t sym, Items... items) : symbol{sym}, brush{items...}
29  {}
30 
32  constexpr Glyph(char sym, Brush b)
33  : symbol{static_cast<char32_t>(sym)}, brush{b}
34  {}
35 
37  template <typename... Items>
38  constexpr Glyph(char sym, Items... items)
39  : symbol{static_cast<char32_t>(sym)}, brush{items...}
40  {}
41 };
42 
43 // Trait -------------------------------------------------------------------
44 
45 auto constexpr operator|=(Glyph& g, Traits ts) -> Glyph&
46 {
47  g.brush.traits |= ts;
48  return g;
49 }
50 
51 [[nodiscard]] auto constexpr operator|(Glyph g, Traits ts) -> Glyph
52 {
53  g.brush.traits |= ts;
54  return g;
55 }
56 
57 } // namespace ox
58 
59 namespace esc { // For ADL; Trait(s) is really in namespace::esc.
60 
61 [[nodiscard]] auto constexpr operator|(char32_t g, Trait t) -> ox::Glyph
62 {
63  return ox::Glyph{g} | t;
64 }
65 
66 [[nodiscard]] auto constexpr operator|(char32_t g, Traits ts) -> ox::Glyph
67 {
68  return ox::Glyph{g} | ts;
69 }
70 
71 [[nodiscard]] auto constexpr operator|(char g, Trait t) -> ox::Glyph
72 {
73  return ox::Glyph{g} | t;
74 }
75 
76 [[nodiscard]] auto constexpr operator|(char g, Traits ts) -> ox::Glyph
77 {
78  return ox::Glyph{g} | ts;
79 }
80 
81 } // namespace esc
82 
83 namespace ox {
84 
85 // Background_color ------------------------------------------------------------
86 
87 auto constexpr operator|=(Glyph& g, Background_color c) -> Glyph&
88 {
89  g.brush.background = Color{c.value};
90  return g;
91 }
92 
93 [[nodiscard]] auto constexpr operator|(Glyph g, Background_color c) -> Glyph
94 {
95  g.brush.background = Color{c.value};
96  return g;
97 }
98 
99 [[nodiscard]] auto constexpr operator|(char32_t g, Background_color c) -> Glyph
100 {
101  return Glyph{g} | c;
102 }
103 
104 [[nodiscard]] auto constexpr operator|(char g, Background_color c) -> ox::Glyph
105 {
106  return ox::Glyph{g} | c;
107 }
108 
109 // Foreground_color ------------------------------------------------------------
110 
111 auto constexpr operator|=(Glyph& g, Foreground_color c) -> Glyph&
112 {
113  g.brush.foreground = Color{c.value};
114  return g;
115 }
116 
117 [[nodiscard]] auto constexpr operator|(Glyph g, Foreground_color c) -> Glyph
118 {
119  g.brush.foreground = Color{c.value};
120  return g;
121 }
122 
123 [[nodiscard]] auto constexpr operator|(char32_t g, Foreground_color c) -> Glyph
124 {
125  return Glyph{g} | c;
126 }
127 
128 [[nodiscard]] auto constexpr operator|(char g, Foreground_color c) -> ox::Glyph
129 {
130  return ox::Glyph{g} | c;
131 }
132 
133 // Comparisons ----------------------------------------------------------------
134 
136 [[nodiscard]] auto constexpr operator==(Glyph lhs, Glyph rhs) -> bool
137 {
138  return (lhs.symbol == rhs.symbol) && (lhs.brush == rhs.brush);
139 }
140 
142 [[nodiscard]] auto constexpr operator!=(Glyph lhs, Glyph rhs) -> bool
143 {
144  return !(lhs == rhs);
145 }
146 
147 } // namespace ox
148 #endif // TERMOX_PAINTER_GLYPH_HPP
Holds the look of any paintable object with Traits and Colors.
Definition: brush.hpp:13
Holds a description of a paintable tile on the screen.
Definition: glyph.hpp:11
constexpr Glyph(char sym, Items... items)
Construct with the provided char32_t and list of Traits and Colors.
Definition: glyph.hpp:38
constexpr Glyph(char sym, Brush b)
Construct a Glyph with the provided char and Brush.
Definition: glyph.hpp:32
char32_t symbol
The Glyph's symbol is the wide character that will be displayed.
Definition: glyph.hpp:14
constexpr Glyph(char32_t sym, Brush b)
Construct a Glyph with the provided char32_t and Brush.
Definition: glyph.hpp:24
Brush brush
The Brush that will determine the Traits and Colors of the symbol.
Definition: glyph.hpp:17
constexpr Glyph(char32_t sym, Items... items)
Construct with the provided char32_t and list of Traits and Colors.
Definition: glyph.hpp:28
constexpr Glyph()=default
Construct an invisible Glyph, defaults to space and no traits/colors.