File: paragraph.h

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (214 lines) | stat: -rw-r--r-- 5,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
	This file is part of Warzone 2100.
	Copyright (C) 2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __INCLUDED_LIB_WIDGET_PARAGRAPH_H__
#define __INCLUDED_LIB_WIDGET_PARAGRAPH_H__

#include <string>
#include "lib/ivis_opengl/textdraw.h"
#include "lib/gamelib/gtime.h"
#include "widget.h"
#include "widgbase.h"

class Paragraph;
struct FlowLayout;
struct FlowLayoutFragment;

/**
 * Provide information about an element appended to the paragraph.
 **/
struct ParagraphElement
{
	virtual ~ParagraphElement() = default;

	virtual void appendTo(FlowLayout &layout) = 0;
	virtual std::shared_ptr<WIDGET> createFragmentWidget(Paragraph &paragraph, FlowLayoutFragment const &fragment) = 0;
	virtual void destroyFragments(Paragraph &paragraph) = 0;
	virtual bool isLayoutDirty() const = 0;
	virtual int32_t getAboveBase() const = 0;
};

struct ParagraphTextStyle
{
	iV_fonts font = font_regular;
	PIELIGHT shadeColour = pal_RGBA(0, 0, 0, 0);
	PIELIGHT fontColour = WZCOL_BLACK;
};

class WzCachedText
{
public:
	WzCachedText(uint32_t cacheDurationMs = 100):
		font(font_regular),
		cacheDurationMs(cacheDurationMs)
	{}
	WzCachedText(WzString text, iV_fonts font, uint32_t cacheDurationMs = 100):
		text(text),
		font(font),
		cacheDurationMs(cacheDurationMs)
	{}

	void tick()
	{
		if (cachedText && cacheExpireAt < realTime)
		{
			cachedText = nullptr;
		}
	}

	void setText(const WzString &_text, iV_fonts _fontID)
	{
		if (text == _text && font == _fontID)
		{
			return;
		}
		text = _text;
		font = _fontID;
		cachedTextWidth.reset();
		if (cachedText)
		{
			cachedText->setText(text, font);
		}
	}

	void resetCachedDimensions()
	{
		cachedTextWidth.reset();
	}

	inline const WzString& getText() const { return text; }
	inline iV_fonts getFontID() const { return font; }
	inline int32_t getTextWidth()
	{
		if (!cachedText)
		{
			if (!cachedTextWidth.has_value())
			{
				cachedTextWidth = iV_GetTextWidth(text, font);
			}
			return cachedTextWidth.value();
		}
		return cachedText->width();
	}
	inline int32_t getTextLineSize() const
	{
		if (!cachedText)
		{
			return iV_GetTextLineSize(font);
		}
		return cachedText->lineSize();
	}

	WzText *operator ->()
	{
		if (!cachedText)
		{
			cachedText = std::make_unique<WzText>(text, font);
		}

		cacheExpireAt = realTime + (cacheDurationMs * GAME_TICKS_PER_SEC) / 1000;
		return cachedText.get();
	}

private:
	WzString text;
	iV_fonts font;
	optional<unsigned int> cachedTextWidth = nullopt;
	uint32_t cacheDurationMs;
	std::unique_ptr<WzText> cachedText = nullptr;
	uint32_t cacheExpireAt = 0;
};

struct FlowLayoutFragment
{
    size_t elementId;
    size_t begin;
    size_t length;
    unsigned int width;
    unsigned int offset;
};

class Paragraph : public WIDGET
{
public:
	Paragraph(): WIDGET() {}

	void addText(const WzString &text);
	void addWidget(const std::shared_ptr<WIDGET> &widget, int32_t aboveBase);

	void setFont(iV_fonts font)
	{
		textStyle.font = font;
	}

	void setFontColour(PIELIGHT colour)
	{
		textStyle.fontColour = colour;
	}

	void setShadeColour(PIELIGHT colour)
	{
		textStyle.shadeColour = colour;
	}

	void setLineSpacing(uint32_t newLineSpacing)
	{
		lineSpacing = newLineSpacing;
		layoutDirty = true;
	}

	ParagraphTextStyle const &getTextStyle() const
	{
		return textStyle;
	}

	void geometryChanged() override;
	void displayRecursive(WidgetGraphicsContext const &context) override;
	void display(int xOffset, int yOffset) override;

	/* The optional "onClick" callback function */
	typedef std::function<void (Paragraph& paragraph, WIDGET_KEY key)> W_PARAGRAPH_ONCLICK_FUNC;
	void addOnClickHandler(const W_PARAGRAPH_ONCLICK_FUNC& onClickFunc);

	void clicked(W_CONTEXT *, WIDGET_KEY key) override;
	void released(W_CONTEXT *, WIDGET_KEY key) override;
	void highlightLost() override;

	nonstd::optional<std::vector<uint32_t>> getScrollSnapOffsets() override;

	void forceSetAllFontColor(PIELIGHT colour);

private:
	std::vector<std::unique_ptr<ParagraphElement>> elements;
	bool layoutDirty = true;
	uint32_t layoutWidth = 0;
	ParagraphTextStyle textStyle;
	uint32_t lineSpacing = 0;

	bool hasElementWithLayoutDirty() const;
	void updateLayout();
	std::vector<std::vector<FlowLayoutFragment>> calculateLinesLayout();

	std::vector<uint32_t> scrollSnapOffsets;

	bool isMouseDown = false;
	std::vector<W_PARAGRAPH_ONCLICK_FUNC> onClickHandlers;
};

#endif // __INCLUDED_LIB_WIDGET_PARAGRAPH_H__