git repos / blockattack-game

blame: source/code/sago/SagoTextField.hpp

normal view · raw

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