Debian Codegraph @

source/code/sago/SagoTextField.hpp

raw symbols SCIP
1 /*
2 Copyright (c) 2018 Poul Sander
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #ifndef SAGOTEXTFIELD_HPP
26 #define SAGOTEXTFIELD_HPP
27
28 #include "SagoDataHolder.hpp"
29 #include "SagoLogicalResize.hpp"
30
31 namespace sago {
32
33 /**
34 * This is a text field.
35 * It represents a line of text to be drawn on screen. It is not possible to have line breaks.
36 * If line breaks are needed use SagoTextBox instead.
37 *
38 * This object renderes to a texture and cahces the texture. The texture will be automatically refreshed if the text changes, the SagoDataHolder is invalidated or ClearCache is called.
39 * Normally all values will be set at the beginning before text is drawn.
40 * SetHolder MUST be called before the field is drawn!
41 */
42 class SagoTextField final {
43 public:
44 SagoTextField();
45 SagoTextField(SagoTextField&& o) noexcept;
46 SagoTextField& operator=(const SagoTextField&& base) = delete;
47 SagoTextField& operator=(const SagoTextField& base) = delete;
48 ~SagoTextField();
49
50 /**
51 * This method creates a copy of a given font.
52 * The cache will not be copied.
53 * This is ALMOST like the "= operator" but given its own name to prevent implicit calling.
54 * @param base The object to copy from
55 * @return A reference to this object.
56 */
57 SagoTextField& CopyFrom(const SagoTextField& base);
58
59 /**
60 * Sets the data holder. This is MANDATORY
61 * @param holder The data holder to fetch the fonts from
62 */
63 void SetHolder(const SagoDataHolder* holder);
64
65 /**
66 * Set the text to display.
67 * @param text The actual UTF-8 encoded text
68 */
69 void SetText(const char* text);
70
71 /**
72 * Set the text to display.
73 * @param text The actual UTF-8 encoded text
74 */
75 void SetText(const std::string& text);
76
77 void SetColor(const SDL_Color& color);
78
79 /**
80 * Set the name of the font. Must be known to the data holder.
81 * The name could for instance be "freeserif".
82 * @param fontName Name of the font as required by SagoDataHolder
83 */
84 void SetFont(const char* fontName);
85
86 void SetFontSize(int fontSize);
87
88 /**
89 * Enable outline against the font.
90 * @param outlineSize Number of pixels of outline.
91 * @param color The color of the outline.
92 */
93 void SetOutline(int outlineSize, const SDL_Color& color);
94
95 /**
96 * Get the text we are currently drawing
97 * @return The text
98 */
99 const std::string& GetText() const;
100
101 /**
102 * A Shorthand for calling TTF_SizeUTF8 on the right font
103 * The size is measuered WITHOUT the outline!
104 * Will fail silently on error (except writing to stderr) and set w and h to 0 if they are not null.
105 * @param text The text to check the rendered size for
106 * @param w Pointer to an int where the width of the text will be stored. Maybe null.
107 * @param h Pointer to an int where the hight of the text will be stored. Maybe null.
108 */
109 void GetRenderedSize(const char* text, int* w = nullptr, int* h = nullptr);
110
111 enum class Alignment { left = 0, right=1, center = 2 };
112 enum class VerticalAlignment { top = 0, center = 1, bottom = 2};
113 void Draw(SDL_Renderer* target, int x, int y, Alignment alignment = Alignment::left, VerticalAlignment verticalAlignment = VerticalAlignment::top, SagoLogicalResize* resize = nullptr);
114
115 /**
116 * Updates the cache.
117 * You normally do not want to call this from the outside as it is done just in time.
118 * Unless you want to precache of course....
119 * @param target Target the the text will eventually be rendered to
120 */
121 void UpdateCache(SDL_Renderer* target);
122
123 /**
124 * Clears the cache and forces the SagoTextField to render it again the next time it is drawn.
125 * Can be used if you have changed font, color or sizes.
126 * Changing the text implices a cache clear.
127 */
128 void ClearCache();
129 private:
130 SagoTextField(const SagoTextField& orig) = delete;
131 struct SagoTextFieldData;
132 SagoTextFieldData* data;
133 };
134
135 } //namespace sago
136
137 #endif /* SAGOTEXTFIELD_HPP */
138