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
 */
55ea55dd sago007 2018-03-24 16:07 41
class SagoTextField {
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;
d91e7363 sago007 2018-03-26 13:09 46
	SagoTextField& operator=(const SagoTextField& base);
55ea55dd sago007 2018-03-24 16:07 47
	virtual ~SagoTextField();
55ea55dd sago007 2018-03-24 16:07 48
	/**
55ea55dd sago007 2018-03-24 16:07 49
	 * Sets the data holder. This is MANDATORY
55ea55dd sago007 2018-03-24 16:07 50
	 * @param holder The data holder to fetch the fonts from
55ea55dd sago007 2018-03-24 16:07 51
	 */
1a09fcd5 sago007 2018-03-25 13:32 52
	void SetHolder(const SagoDataHolder* holder);
55ea55dd sago007 2018-03-24 16:07 53
	/**
55ea55dd sago007 2018-03-24 16:07 54
	 * Set the text to display.
55ea55dd sago007 2018-03-24 16:07 55
	 * @param text The actual UTF-8 encoded text
55ea55dd sago007 2018-03-24 16:07 56
	 */
55ea55dd sago007 2018-03-24 16:07 57
	void SetText(const char* text);
55ea55dd sago007 2018-03-24 16:07 58
	void SetColor(const SDL_Color& color);
55ea55dd sago007 2018-03-24 16:07 59
	/**
55ea55dd sago007 2018-03-24 16:07 60
	 * Set the name of the font. Must be known to the data holder.
55ea55dd sago007 2018-03-24 16:07 61
	 * @param fontName Name of the font as required by SagoDataHolder
55ea55dd sago007 2018-03-24 16:07 62
	 */
55ea55dd sago007 2018-03-24 16:07 63
	void SetFont(const char* fontName);
55ea55dd sago007 2018-03-24 16:07 64
	void SetFontSize(int fontSize);
55ea55dd sago007 2018-03-24 16:07 65
	/**
55ea55dd sago007 2018-03-24 16:07 66
	 * Enable outline against the font.
55ea55dd sago007 2018-03-24 16:07 67
	 * @param outlineSize Number of pixels of outline.
55ea55dd sago007 2018-03-24 16:07 68
	 * @param color The color of the outline.
55ea55dd sago007 2018-03-24 16:07 69
	 */
55ea55dd sago007 2018-03-24 16:07 70
	void SetOutline(int outlineSize, const SDL_Color& color);
55ea55dd sago007 2018-03-24 16:07 71
	/**
55ea55dd sago007 2018-03-24 16:07 72
	 * Get the text we are currently drawing
55ea55dd sago007 2018-03-24 16:07 73
	 * @return The text
55ea55dd sago007 2018-03-24 16:07 74
	 */
55ea55dd sago007 2018-03-24 16:07 75
	const std::string& GetText() const;
d91e7363 sago007 2018-03-26 13:09 76
	enum class Alignment { left = 0, center = 2 };
d91e7363 sago007 2018-03-26 13:09 77
	enum class VerticalAlignment { top = 0, center = 1};
d91e7363 sago007 2018-03-26 13:09 78
	void Draw(SDL_Renderer* target, int x, int y, Alignment alignment = Alignment::left, VerticalAlignment verticalAlignment = VerticalAlignment::top);
55ea55dd sago007 2018-03-24 16:07 79
	/**
55ea55dd sago007 2018-03-24 16:07 80
	 * Updates the cache.
55ea55dd sago007 2018-03-24 16:07 81
	 * 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 82
	 * Unless you want to precache of course....
55ea55dd sago007 2018-03-24 16:07 83
	 * @param target Target the the text will eventually be rendered to
55ea55dd sago007 2018-03-24 16:07 84
	 */
55ea55dd sago007 2018-03-24 16:07 85
	void UpdateCache(SDL_Renderer* target);
55ea55dd sago007 2018-03-24 16:07 86
	/**
55ea55dd sago007 2018-03-24 16:07 87
	 * Clears the cache and forces the SagoTextField to render it again the next time it is drawn.
55ea55dd sago007 2018-03-24 16:07 88
	 * Can be used if you have changed font, color or sizes.
55ea55dd sago007 2018-03-24 16:07 89
	 * Changing the text implices a cache clear.
55ea55dd sago007 2018-03-24 16:07 90
	 */
55ea55dd sago007 2018-03-24 16:07 91
	void ClearCache();
55ea55dd sago007 2018-03-24 16:07 92
private:
55ea55dd sago007 2018-03-24 16:07 93
	SagoTextField(const SagoTextField& orig) = delete;
55ea55dd sago007 2018-03-24 16:07 94
	struct SagoTextFieldData;
55ea55dd sago007 2018-03-24 16:07 95
	SagoTextFieldData *data;
55ea55dd sago007 2018-03-24 16:07 96
};
55ea55dd sago007 2018-03-24 16:07 97
55ea55dd sago007 2018-03-24 16:07 98
}  //namespace sago
55ea55dd sago007 2018-03-24 16:07 99
55ea55dd sago007 2018-03-24 16:07 100
#endif /* SAGOTEXTFIELD_HPP */
55ea55dd sago007 2018-03-24 16:07 101
1970-01-01 00:00 102