-
Notifications
You must be signed in to change notification settings - Fork 6
/
text.hpp
99 lines (85 loc) · 2.99 KB
/
text.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
//
// text.hpp
// Simpleton Engine
//
// Created by Indi Kernick on 17/6/18.
// Copyright © 2018 Indi Kernick. All rights reserved.
//
#ifndef engine_graphics_2d_text_hpp
#define engine_graphics_2d_text_hpp
#include <string_view>
#include <type_traits>
#include "quad writer.hpp"
namespace G2D {
/// Text alignment
enum class Align {
LEFT,
CENTER,
RIGHT
};
class Text {
template <typename T, typename Ret>
using EnableNotStr = std::enable_if_t<
!std::is_convertible_v<T, std::string_view>,
Ret
>;
public:
Text() = default;
explicit Text(Section &);
/// Set the section that glyphs will be written to. A pointer to the section
/// is stored interally.
void section(Section &);
/// Get a refernce to the section that is currently being written to.
Section §ion() const;
/// Set the size (in camera coordinates) of a glyph at a scale of 1.0
void glyphSize(glm::vec2);
/// Set the advance distance (in camera coordinates) for advancing to the
/// next character or next line. This class only supports monospaced text
void advance(glm::vec2);
/// Set the scale relative to the glyph size that the glyphs will be
/// written. If scale is 2.0f then glyphs will be written at a size of
/// glyphSize * 2.0f
void scale(float);
/// Set the depth of the text using a float
void depth(float);
/// Set the depth of the text using a depth enum
template <typename Enum>
void depth(Enum);
/// Set the color of the text
void color(glm::vec4);
/// Write a string of characters at a position and return the position of
/// the next character
template <Align ALIGN, PlusXY PLUS_XY>
glm::vec2 write(glm::vec2, std::string_view);
/// Write a single character at a position and return the position of the
/// next character
template <Align ALIGN, PlusXY PLUS_XY>
glm::vec2 write(glm::vec2, char);
/// Write a serializable object at a position and return the position of the
/// next character. The type must have an overloaded operator<<
template <Align ALIGN, PlusXY PLUS_XY, typename T>
EnableNotStr<T, glm::vec2> write(glm::vec2, const T &);
#define WRAPPER(NAME, ALIGN) \
template <PlusXY PLUS_XY = PlusXY::RIGHT_DOWN, typename Value> \
glm::vec2 write##NAME(const glm::vec2 pos, Value &&value) { \
return write<Align::ALIGN, PLUS_XY>(pos, std::forward<Value>(value)); \
}
WRAPPER(Left, LEFT)
WRAPPER(Center, CENTER)
WRAPPER(Right, RIGHT)
#undef WRAPPER
private:
Section *section_ {};
glm::vec4 color_ {1.0f};
glm::vec2 glyphSize_ {1.0f};
glm::vec2 advance_ {1.0f};
float scale_ {1.0f};
float depth_ {0.0f};
template <PlusXY PLUS_XY>
glm::vec2 writeLeftImpl(glm::vec2, std::string_view);
template <PlusXY PLUS_XY>
void writeChar(glm::vec2, glm::vec2, char);
};
}
#include "text.inl"
#endif