git repos / blockattack-game

blame: source/code/sago/SagoTextBox.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 SAGOTEXTBOX_HPP
55ea55dd sago007 2018-03-24 16:07 26
#define SAGOTEXTBOX_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
#include "SagoTextField.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
2d137b45 sago007 2018-04-02 13:59 33
class SagoTextBox final {
55ea55dd sago007 2018-03-24 16:07 34
public:
55ea55dd sago007 2018-03-24 16:07 35
	SagoTextBox();
2d137b45 sago007 2018-04-02 13:59 36
	~SagoTextBox();
1a09fcd5 sago007 2018-03-25 13:32 37
	void SetHolder(const SagoDataHolder* holder);
55ea55dd sago007 2018-03-24 16:07 38
	void SetText(const char* text);
0b8fb137 sago007 2018-03-31 19:53 39
	void SetText(const std::string& text);
55ea55dd sago007 2018-03-24 16:07 40
	void SetColor(const SDL_Color& color);
e94af580 sago007 2020-12-30 17:45 41
eb0258c8 sago007 2018-04-02 13:23 42
	/**
eb0258c8 sago007 2018-04-02 13:23 43
	 * Set the name of the font. Must be known to the data holder.
eb0258c8 sago007 2018-04-02 13:23 44
	 * The name could for instance be "freeserif".
eb0258c8 sago007 2018-04-02 13:23 45
	 * @param fontName Name of the font as required by SagoDataHolder
eb0258c8 sago007 2018-04-02 13:23 46
	 */
55ea55dd sago007 2018-03-24 16:07 47
	void SetFont(const char* fontName);
55ea55dd sago007 2018-03-24 16:07 48
	void SetFontSize(int fontSize);
55ea55dd sago007 2018-03-24 16:07 49
	void SetOutline(int outlineSize, const SDL_Color& color);
e94af580 sago007 2020-12-30 17:45 50
55ea55dd sago007 2018-03-24 16:07 51
	/**
55ea55dd sago007 2018-03-24 16:07 52
	 * Sets the max width to generate. SagoTextBox will insert line breaks to keep the width below this number.
55ea55dd sago007 2018-03-24 16:07 53
	 * Outline is not included in the width: If you have a 2 pixels outline the rendere may go 2 pixels beyond.
55ea55dd sago007 2018-03-24 16:07 54
	 * You will always need to generate at least one char per line. If max width is too low one char will be drawn per line even if it goes above max width.
55ea55dd sago007 2018-03-24 16:07 55
	 * Setting this to 0 will disable the feature.
55ea55dd sago007 2018-03-24 16:07 56
	 * @param width The maximum width before forcing a line break
55ea55dd sago007 2018-03-24 16:07 57
	 */
55ea55dd sago007 2018-03-24 16:07 58
	void SetMaxWidth(int width);
55ea55dd sago007 2018-03-24 16:07 59
	const std::string& GetText() const;
4b21c89c Poul Sander 2025-03-04 20:33 60
	void Draw(SDL_Renderer* target, int x, int y, SagoTextField::Alignment alignment = SagoTextField::Alignment::left, SagoLogicalResize* resize = nullptr);
55ea55dd sago007 2018-03-24 16:07 61
	void UpdateCache();
55ea55dd sago007 2018-03-24 16:07 62
private:
55ea55dd sago007 2018-03-24 16:07 63
	void AppendLineToCache(const std::string& text);
55ea55dd sago007 2018-03-24 16:07 64
	void SplitAndAppendLineToCache(TTF_Font* font, const std::string& text);
55ea55dd sago007 2018-03-24 16:07 65
	SagoTextBox(const SagoTextBox& orig) = delete;
55ea55dd sago007 2018-03-24 16:07 66
	SagoTextBox& operator=(const SagoTextBox& base) = delete;
55ea55dd sago007 2018-03-24 16:07 67
	struct SagoTextBoxData;
e94af580 sago007 2020-12-30 17:45 68
	SagoTextBoxData* data;
55ea55dd sago007 2018-03-24 16:07 69
};
55ea55dd sago007 2018-03-24 16:07 70
55ea55dd sago007 2018-03-24 16:07 71
}  //namespace sago
55ea55dd sago007 2018-03-24 16:07 72
55ea55dd sago007 2018-03-24 16:07 73
#endif /* SAGOTEXTBOX_HPP */
55ea55dd sago007 2018-03-24 16:07 74
1970-01-01 00:00 75