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"
55ea55dd sago007 2018-03-24 16:07 29
55ea55dd sago007 2018-03-24 16:07 30
namespace sago {
55ea55dd sago007 2018-03-24 16:07 31
55ea55dd sago007 2018-03-24 16:07 32
/**
55ea55dd sago007 2018-03-24 16:07 33
 * This is a text field.
55ea55dd sago007 2018-03-24 16:07 34
 * 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 35
 * If line breaks are needed use SagoTextBox instead.
55ea55dd sago007 2018-03-24 16:07 36
 * 
55ea55dd sago007 2018-03-24 16:07 37
 * 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 38
 * Normally all values will be set at the beginning before text is drawn.
55ea55dd sago007 2018-03-24 16:07 39
 * SetHolder MUST be called before the field is drawn!
55ea55dd sago007 2018-03-24 16:07 40
 */
2d137b45 sago007 2018-04-02 13:59 41
class SagoTextField final {
55ea55dd sago007 2018-03-24 16:07 42
public:
55ea55dd sago007 2018-03-24 16:07 43
	SagoTextField();
55ea55dd sago007 2018-03-24 16:07 44
	SagoTextField(SagoTextField&& o) noexcept;
55ea55dd sago007 2018-03-24 16:07 45
	SagoTextField& operator=(const SagoTextField&& base) = delete;
eb0258c8 sago007 2018-04-02 13:23 46
	SagoTextField& operator=(const SagoTextField& base) = delete;
2d137b45 sago007 2018-04-02 13:59 47
	~SagoTextField();
55ea55dd sago007 2018-03-24 16:07 48
	/**
eb0258c8 sago007 2018-04-02 13:23 49
	 * This method creates a copy of a given font.
eb0258c8 sago007 2018-04-02 13:23 50
	 * The cache will not be copied.
eb0258c8 sago007 2018-04-02 13:23 51
	 * This is ALMOST like the "= operator" but given its own name to prevent implicit calling. 
eb0258c8 sago007 2018-04-02 13:23 52
	 * @param base The object to copy from
eb0258c8 sago007 2018-04-02 13:23 53
	 * @return A reference to this object.
eb0258c8 sago007 2018-04-02 13:23 54
	 */
eb0258c8 sago007 2018-04-02 13:23 55
	SagoTextField& CopyFrom(const SagoTextField& base);
eb0258c8 sago007 2018-04-02 13:23 56
	/**
55ea55dd sago007 2018-03-24 16:07 57
	 * Sets the data holder. This is MANDATORY
55ea55dd sago007 2018-03-24 16:07 58
	 * @param holder The data holder to fetch the fonts from
55ea55dd sago007 2018-03-24 16:07 59
	 */
1a09fcd5 sago007 2018-03-25 13:32 60
	void SetHolder(const SagoDataHolder* holder);
55ea55dd sago007 2018-03-24 16:07 61
	/**
55ea55dd sago007 2018-03-24 16:07 62
	 * Set the text to display.
55ea55dd sago007 2018-03-24 16:07 63
	 * @param text The actual UTF-8 encoded text
55ea55dd sago007 2018-03-24 16:07 64
	 */
55ea55dd sago007 2018-03-24 16:07 65
	void SetText(const char* text);
eb0258c8 sago007 2018-04-02 13:23 66
	/**
eb0258c8 sago007 2018-04-02 13:23 67
	 * Set the text to display.
eb0258c8 sago007 2018-04-02 13:23 68
	 * @param text The actual UTF-8 encoded text
eb0258c8 sago007 2018-04-02 13:23 69
	 */
0b8fb137 sago007 2018-03-31 19:53 70
	void SetText(const std::string& text);
55ea55dd sago007 2018-03-24 16:07 71
	void SetColor(const SDL_Color& color);
55ea55dd sago007 2018-03-24 16:07 72
	/**
55ea55dd sago007 2018-03-24 16:07 73
	 * Set the name of the font. Must be known to the data holder.
eb0258c8 sago007 2018-04-02 13:23 74
	 * The name could for instance be "freeserif".
55ea55dd sago007 2018-03-24 16:07 75
	 * @param fontName Name of the font as required by SagoDataHolder
55ea55dd sago007 2018-03-24 16:07 76
	 */
55ea55dd sago007 2018-03-24 16:07 77
	void SetFont(const char* fontName);
55ea55dd sago007 2018-03-24 16:07 78
	void SetFontSize(int fontSize);
55ea55dd sago007 2018-03-24 16:07 79
	/**
55ea55dd sago007 2018-03-24 16:07 80
	 * Enable outline against the font.
55ea55dd sago007 2018-03-24 16:07 81
	 * @param outlineSize Number of pixels of outline.
55ea55dd sago007 2018-03-24 16:07 82
	 * @param color The color of the outline.
55ea55dd sago007 2018-03-24 16:07 83
	 */
55ea55dd sago007 2018-03-24 16:07 84
	void SetOutline(int outlineSize, const SDL_Color& color);
55ea55dd sago007 2018-03-24 16:07 85
	/**
55ea55dd sago007 2018-03-24 16:07 86
	 * Get the text we are currently drawing
55ea55dd sago007 2018-03-24 16:07 87
	 * @return The text
55ea55dd sago007 2018-03-24 16:07 88
	 */
55ea55dd sago007 2018-03-24 16:07 89
	const std::string& GetText() const;
eb0258c8 sago007 2018-04-02 13:23 90
	/**
eb0258c8 sago007 2018-04-02 13:23 91
	 * A Shorthand for calling TTF_SizeUTF8 on the right font
eb0258c8 sago007 2018-04-02 13:23 92
	 * The size is measuered WITHOUT the outline!
eb0258c8 sago007 2018-04-02 13:23 93
	 * 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 94
	 * @param text The text to check the rendered size for
eb0258c8 sago007 2018-04-02 13:23 95
	 * @param w Pointer to an int where the width of the text will be stored. Maybe null.
eb0258c8 sago007 2018-04-02 13:23 96
	 * @param h Pointer to an int where the hight of the text will be stored. Maybe null.
eb0258c8 sago007 2018-04-02 13:23 97
	 */
bd788178 sago007 2018-03-31 16:03 98
	void GetRenderedSize(const char* text, int* w = nullptr, int* h = nullptr);
fc338170 sago007 2018-03-28 14:31 99
	enum class Alignment { left = 0, right=1, center = 2 };
492b2753 sago007 2018-04-07 14:37 100
	enum class VerticalAlignment { top = 0, center = 1, bottom = 2};
d91e7363 sago007 2018-03-26 13:09 101
	void Draw(SDL_Renderer* target, int x, int y, Alignment alignment = Alignment::left, VerticalAlignment verticalAlignment = VerticalAlignment::top);
55ea55dd sago007 2018-03-24 16:07 102
	/**
55ea55dd sago007 2018-03-24 16:07 103
	 * Updates the cache.
55ea55dd sago007 2018-03-24 16:07 104
	 * 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 105
	 * Unless you want to precache of course....
55ea55dd sago007 2018-03-24 16:07 106
	 * @param target Target the the text will eventually be rendered to
55ea55dd sago007 2018-03-24 16:07 107
	 */
55ea55dd sago007 2018-03-24 16:07 108
	void UpdateCache(SDL_Renderer* target);
55ea55dd sago007 2018-03-24 16:07 109
	/**
55ea55dd sago007 2018-03-24 16:07 110
	 * Clears the cache and forces the SagoTextField to render it again the next time it is drawn.
55ea55dd sago007 2018-03-24 16:07 111
	 * Can be used if you have changed font, color or sizes.
55ea55dd sago007 2018-03-24 16:07 112
	 * Changing the text implices a cache clear.
55ea55dd sago007 2018-03-24 16:07 113
	 */
55ea55dd sago007 2018-03-24 16:07 114
	void ClearCache();
55ea55dd sago007 2018-03-24 16:07 115
private:
55ea55dd sago007 2018-03-24 16:07 116
	SagoTextField(const SagoTextField& orig) = delete;
55ea55dd sago007 2018-03-24 16:07 117
	struct SagoTextFieldData;
55ea55dd sago007 2018-03-24 16:07 118
	SagoTextFieldData *data;
55ea55dd sago007 2018-03-24 16:07 119
};
55ea55dd sago007 2018-03-24 16:07 120
55ea55dd sago007 2018-03-24 16:07 121
}  //namespace sago
55ea55dd sago007 2018-03-24 16:07 122
55ea55dd sago007 2018-03-24 16:07 123
#endif /* SAGOTEXTFIELD_HPP */
55ea55dd sago007 2018-03-24 16:07 124
1970-01-01 00:00 125