FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_objText.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_OBJTEXT_HPP_INCLUDED
18#define _FGE_C_OBJTEXT_HPP_INCLUDED
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "C_object.hpp"
29#include "FastEngine/accessor/C_font.hpp"
30#include "FastEngine/graphic/C_glyph.hpp"
31#include "tinyutf8.h"
32#include <vector>
33
34#define FGE_OBJTEXT_CLASSNAME "FGE:OBJ:TEXT"
35
36namespace fge
37{
38
39class ObjText;
40
41class FGE_API Character : public fge::Transformable
42{
43public:
44 Character();
45 Character(fge::Color const& fillColor, fge::Color const& outlineColor);
46
47 void clear();
48 void addLine(bool outlineVertices,
49 float lineLength,
50 float lineTop,
51 float offset,
52 float thickness,
53 float outlineThickness = 0.0f);
54 void addGlyphQuad(bool outlineVertices,
55 fge::Vector2f const& size,
56 fge::Glyph const& glyph,
57 fge::Vector2i const& textureSize,
58 float italicShear);
59
60 void draw(fge::TransformUboData const& externalTransform,
61 fge::RenderTarget const& target,
62 fge::RenderStates const& states) const;
63
64 void setFillColor(fge::Color const& color);
65 void setOutlineColor(fge::Color const& color);
66
67 [[nodiscard]] fge::Color const& getFillColor() const;
68 [[nodiscard]] fge::Color const& getOutlineColor() const;
69
70 void setVisibility(bool visibility);
71 [[nodiscard]] bool isVisible() const;
72
73 [[nodiscard]] uint32_t getUnicode() const;
74
75private:
76 friend ObjText;
77
78 fge::vulkan::VertexBuffer g_vertices;
79 fge::vulkan::VertexBuffer g_outlineVertices;
80
81 fge::Color g_fillColor{255, 255, 255};
82 fge::Color g_outlineColor{0, 0, 0};
83
84 uint32_t g_unicodeChar{0};
85
86 bool g_visibility{true};
87};
88
89class FGE_API ObjText : public fge::Object
90{
91public:
92 enum Style : uint8_t
93 {
94 Regular = 0,
95 Bold = 1 << 0,
96 Italic = 1 << 1,
97 Underlined = 1 << 2,
98 StrikeThrough = 1 << 3
99 };
100
101 ObjText() = default;
102 ObjText(tiny_utf8::string string,
103 fge::Font font,
104 fge::Vector2f const& position = {},
105 fge::CharacterSize characterSize = 30);
106 explicit ObjText(fge::Font font, fge::Vector2f const& position = {}, fge::CharacterSize characterSize = 30);
107
108 FGE_OBJ_DEFAULT_COPYMETHOD(fge::ObjText)
109
110 void setFont(fge::Font font);
111 fge::Font const& getFont() const;
112
113 void setString(tiny_utf8::string string);
114
115 void setCharacterSize(fge::CharacterSize size);
116
117 void setLineSpacingFactor(float spacingFactor);
118 void setLetterSpacingFactor(float spacingFactor);
119
120 void setStyle(std::underlying_type<Style>::type style);
121
122 void setFillColor(fge::Color const& color);
123 void setOutlineColor(fge::Color const& color);
124
125 void setOutlineThickness(float thickness);
126
127 tiny_utf8::string const& getString() const;
128
129 fge::CharacterSize getCharacterSize() const;
130
131 [[nodiscard]] float getLetterSpacingFactor() const;
132 [[nodiscard]] float getLineSpacingFactor() const;
133 [[nodiscard]] float getLineSpacing() const;
134 [[nodiscard]] float getGlyphAdvance(uint32_t c) const;
135
136 std::underlying_type<Style>::type getStyle() const;
137
138 fge::Color const& getFillColor() const;
139 fge::Color const& getOutlineColor() const;
140
141 float getOutlineThickness() const;
142
143 fge::Vector2f findCharacterPos(std::size_t index) const;
144
145 std::vector<fge::Character>& getCharacters();
146 std::vector<fge::Character> const& getCharacters() const;
147
148 FGE_OBJ_DRAW_DECLARE
149
150 void save(nlohmann::json& jsonObject, fge::Scene* scene) override;
151 void load(nlohmann::json& jsonObject, fge::Scene* scene) override;
152 void pack(fge::net::Packet& pck) override;
153 void unpack(fge::net::Packet const& pck) override;
154
155 char const* getClassName() const override;
156 char const* getReadableClassName() const override;
157
160
161private:
162 void ensureGeometryUpdate() const;
163
164 tiny_utf8::string g_string;
165 fge::Font g_font;
166 fge::CharacterSize g_characterSize{30};
167 float g_letterSpacingFactor{1.0f};
168 float g_lineSpacingFactor{1.0f};
169 std::underlying_type<Style>::type g_style{Regular};
170 fge::Color g_fillColor{255, 255, 255};
171 fge::Color g_outlineColor{0, 0, 0};
172 float g_outlineThickness{0.0f};
173
174 mutable std::vector<Character> g_characters;
175 mutable fge::RectFloat g_bounds;
176 mutable bool g_geometryNeedUpdate{false};
177 mutable uint32_t g_fontTextureModificationCount{0};
178};
179
180} // namespace fge
181
182#endif // _FGE_C_OBJTEXT_HPP_INCLUDED
Definition C_objText.hpp:42
Definition C_color.hpp:35
This class is a wrapper for the font manager to allow easy manipulation.
Definition C_font.hpp:35
Definition C_glyph.hpp:33
Definition C_objText.hpp:90
void unpack(fge::net::Packet const &pck) override
Unpack the object from a packet.
Style
Definition C_objText.hpp:93
char const * getReadableClassName() const override
Get a readable version of the class name.
fge::RectFloat getGlobalBounds() const override
Get the global bounds of the object.
void pack(fge::net::Packet &pck) override
Pack the object into a packet.
void save(nlohmann::json &jsonObject, fge::Scene *scene) override
Save the object to a json object.
fge::RectFloat getLocalBounds() const override
Get the local bounds of the object (without any transformations)
char const * getClassName() const override
Get the unique class name of the object.
void load(nlohmann::json &jsonObject, fge::Scene *scene) override
Load the object from a json object.
The Object class is the base class for all objects in the engine.
Definition C_object.hpp:102
The RenderStates class contains all the information needed to render something.
Definition C_renderStates.hpp:412
Definition C_renderTarget.hpp:56
A scene contain a collection of object and handle them.
Definition C_scene.hpp:450
Definition C_transformable.hpp:34
Definition C_packet.hpp:70
Definition C_vertexBuffer.hpp:45
Definition C_transform.hpp:26