File: CMessage.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 11,136 kB
  • sloc: cpp: 142,615; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (490 lines) | stat: -rw-r--r-- 12,860 bytes parent folder | download | duplicates (2)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * CMessage.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

#include "StdInc.h"
#include "CMessage.h"

#include "CGameInfo.h"
#include "gui/SDL_Extensions.h"
#include "../lib/CGeneralTextHandler.h"
#include "CBitmapHandler.h"
#include "gui/CAnimation.h"

#include "widgets/CComponent.h"
#include "windows/InfoWindows.h"
#include "widgets/Buttons.h"
#include "widgets/TextControls.h"

const int BETWEEN_COMPS_ROWS = 10;
const int BEFORE_COMPONENTS = 30;
const int BETWEEN_COMPS = 30;
const int SIDE_MARGIN = 30;

template <typename T, typename U> std::pair<T,U> max(const std::pair<T,U> &x, const std::pair<T,U> &y)
{
	std::pair<T,U> ret;
	ret.first = std::max(x.first,y.first);
	ret.second = std::max(x.second,y.second);
	return ret;
}

//One image component + subtitles below it
class ComponentResolved : public CIntObject
{
public:
	std::shared_ptr<CComponent> comp;

	//blit component with image centered at this position
	void showAll(SDL_Surface * to) override;

	//ComponentResolved();
	ComponentResolved(std::shared_ptr<CComponent> Comp);
	~ComponentResolved();
};
// Full set of components for blitting on dialog box
struct ComponentsToBlit
{
	std::vector< std::vector<std::shared_ptr<ComponentResolved>>> comps;
	int w, h;

	void blitCompsOnSur(bool blitOr, int inter, int &curh, SDL_Surface *ret);
	ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr);
	~ComponentsToBlit();
};

namespace
{
	std::array<std::unique_ptr<CAnimation>, PlayerColor::PLAYER_LIMIT_I> dialogBorders;
	std::array<std::vector<std::shared_ptr<IImage>>, PlayerColor::PLAYER_LIMIT_I> piecesOfBox;

	SDL_Surface * background = nullptr;//todo: should be CFilledTexture
}

void CMessage::init()
{
	for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
	{
		dialogBorders[i] = make_unique<CAnimation>("DIALGBOX");
		dialogBorders[i]->preload();

        for(int j=0; j < dialogBorders[i]->size(0); j++)
		{
			auto image = dialogBorders[i]->getImage(j, 0);
			//assume blue color initially
			if(i != 1)
				image->playerColored(PlayerColor(i));
			piecesOfBox[i].push_back(image);
		}
	}

	background = BitmapHandler::loadBitmap("DIBOXBCK.BMP");
}

void CMessage::dispose()
{
	for(auto & item : dialogBorders)
		item.reset();
	SDL_FreeSurface(background);
}

SDL_Surface * CMessage::drawDialogBox(int w, int h, PlayerColor playerColor)
{
	//prepare surface
	SDL_Surface * ret = SDL_CreateRGBSurface(0, w, h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	for (int i=0; i<w; i+=background->w)//background
	{
		for (int j=0; j<h; j+=background->h)
		{
			Rect srcR(0,0,background->w, background->h);
			Rect dstR(i,j,w,h);
			CSDL_Ext::blitSurface(background, &srcR, ret, &dstR);
		}
	}
	drawBorder(playerColor, ret, w, h);
	return ret;
}

std::vector<std::string> CMessage::breakText( std::string text, size_t maxLineWidth, EFonts font )
{
	std::vector<std::string> ret;

	boost::algorithm::trim_right_if(text,boost::algorithm::is_any_of(std::string(" ")));

	// each iteration generates one output line
	while (text.length())
	{
		ui32 lineWidth = 0;    //in characters or given char metric
		ui32 wordBreak = -1;    //last position for line break (last space character)
		ui32 currPos = 0;       //current position in text
		bool opened = false;    //set to true when opening brace is found

		size_t symbolSize = 0; // width of character, in bytes
		size_t glyphWidth = 0; // width of printable glyph, pixels

		// loops till line is full or end of text reached
		while(currPos < text.length()  &&  text[currPos] != 0x0a  &&  lineWidth < maxLineWidth)
		{
			symbolSize = Unicode::getCharacterSize(text[currPos]);
			glyphWidth = graphics->fonts[font]->getGlyphWidth(text.data() + currPos);

			// candidate for line break
			if (ui8(text[currPos]) <= ui8(' '))
				wordBreak = currPos;

			/* We don't count braces in string length. */
			if (text[currPos] == '{')
				opened=true;
			else if (text[currPos]=='}')
				opened=false;
			else
				lineWidth += glyphWidth;
			currPos += symbolSize;
		}

		// long line, create line break
		if (currPos < text.length()  &&  (text[currPos] != 0x0a))
		{
			if (wordBreak != ui32(-1))
				currPos = wordBreak;
			else
				currPos -= symbolSize;
		}

		//non-blank line
		if(currPos != 0)
		{
			ret.push_back(text.substr(0, currPos));

			if (opened)
				/* Close the brace for the current line. */
				ret.back() += '}';

			text.erase(0, currPos);
		}
		else if(text[currPos] == 0x0a)
		{
			ret.push_back(""); //add empty string, no extra actions needed
		}

		if (text.length() != 0 && text[0] == 0x0a)
		{
			/* Remove LF */
			text.erase(0, 1);
		}
		else
		{
			// trim only if line does not starts with LF
			// FIXME: necessary? All lines will be trimmed before returning anyway
			boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(std::string(" ")));
		}

		if (opened)
		{
			/* Add an opening brace for the next line. */
			if (text.length() != 0)
				text.insert(0, "{");
		}
	}

	/* Trim whitespaces of every line. */
	for (auto & elem : ret)
		boost::algorithm::trim(elem);

	return ret;
}

void CMessage::drawIWindow(CInfoWindow * ret, std::string text, PlayerColor player)
{
	bool blitOr = false;
	if(dynamic_cast<CSelWindow*>(ret)) //it's selection window, so we'll blit "or" between components
		blitOr = true;

	const int sizes[][2] = {{400, 125}, {500, 150}, {600, 200}, {480, 400}};

	for(int i = 0;
		i < ARRAY_COUNT(sizes)
			&& sizes[i][0] < screen->w - 150
			&& sizes[i][1] < screen->h - 150
			&& ret->text->slider;
		i++)
	{
		ret->text->resize(Point(sizes[i][0], sizes[i][1]));
	}

	if(ret->text->slider)
	{
		ret->text->slider->addUsedEvents(CIntObject::WHEEL | CIntObject::KEYBOARD);
	}
	else
	{
		ret->text->resize(ret->text->label->textSize + Point(10, 10));
	}

	std::pair<int,int> winSize(ret->text->pos.w, ret->text->pos.h); //start with text size

	ComponentsToBlit comps(ret->components,500, blitOr);
	if (ret->components.size())
		winSize.second += 10 + comps.h; //space to first component

	int bw = 0;
	if (ret->buttons.size())
	{
		int bh = 0;
		// Compute total width of buttons
		bw = 20*(ret->buttons.size()-1); // space between all buttons
		for(auto & elem : ret->buttons) //and add buttons width
		{
			bw+=elem->pos.w;
			vstd::amax(bh, elem->pos.h);
		}
		winSize.second += 20 + bh;//before button + button
	}

	// Clip window size
	vstd::amax(winSize.second, 50);
	vstd::amax(winSize.first, 80);
	vstd::amax(winSize.first, comps.w);
	vstd::amax(winSize.first, bw);

	vstd::amin(winSize.first, screen->w - 150);

	ret->bitmap = drawDialogBox (winSize.first + 2*SIDE_MARGIN, winSize.second + 2*SIDE_MARGIN, player);
	ret->pos.h=ret->bitmap->h;
	ret->pos.w=ret->bitmap->w;
	ret->center();

	int curh = SIDE_MARGIN;
	int xOffset = (ret->pos.w - ret->text->pos.w)/2;

	if(!ret->buttons.size() && !ret->components.size()) //improvement for very small text only popups -> center text vertically
	{
		if(ret->bitmap->h > ret->text->pos.h + 2*SIDE_MARGIN)
			curh = (ret->bitmap->h - ret->text->pos.h)/2;
	}

	ret->text->moveBy(Point(xOffset, curh));

	curh += ret->text->pos.h;

	if (ret->components.size())
	{
		curh += BEFORE_COMPONENTS;
		comps.blitCompsOnSur (blitOr, BETWEEN_COMPS, curh, ret->bitmap);
	}
	if(ret->buttons.size())
	{
		// Position the buttons at the bottom of the window
		bw = (ret->bitmap->w/2) - (bw/2);
		curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->pos.h;

		for(auto & elem : ret->buttons)
		{
			elem->moveBy(Point(bw, curh));
			bw += elem->pos.w + 20;
		}
	}
	for(size_t i=0; i<ret->components.size(); i++)
		ret->components[i]->moveBy(Point(ret->pos.x, ret->pos.y));
}

void CMessage::drawBorder(PlayerColor playerColor, SDL_Surface * ret, int w, int h, int x, int y)
{
	if(playerColor.isSpectator())
		playerColor = PlayerColor(1);
	auto & box = piecesOfBox.at(playerColor.getNum());

	// Note: this code assumes that the corner dimensions are all the same.

	// Horizontal borders
	int start_x = x + box[0]->width();
	const int stop_x = x + w - box[1]->width();
	const int bottom_y = y+h-box[7]->height()+1;
	while (start_x < stop_x) {
		int cur_w = stop_x - start_x;
		if (cur_w > box[6]->width())
			cur_w = box[6]->width();

		// Top border
		Rect srcR(0, 0, cur_w, box[6]->height());
		Rect dstR(start_x, y, 0, 0);
		box[6]->draw(ret, &dstR, &srcR);

		// Bottom border
		dstR.y = bottom_y;
		box[7]->draw(ret, &dstR, &srcR);

		start_x += cur_w;
	}

	// Vertical borders
	int start_y = y + box[0]->height();
	const int stop_y = y + h - box[2]->height()+1;
	const int right_x = x+w-box[5]->width();
	while (start_y < stop_y) {
		int cur_h = stop_y - start_y;
		if (cur_h > box[4]->height())
			cur_h = box[4]->height();

		// Left border
		Rect srcR(0, 0, box[4]->width(), cur_h);
		Rect dstR(x, start_y, 0, 0);
		box[4]->draw(ret, &dstR, &srcR);

		// Right border
		dstR.x = right_x;
		box[5]->draw(ret, &dstR, &srcR);

		start_y += cur_h;
	}

	//corners
	Rect dstR(x, y, box[0]->width(), box[0]->height());
	box[0]->draw(ret, &dstR, nullptr);

	dstR=Rect(x+w-box[1]->width(), y,   box[1]->width(), box[1]->height());
	box[1]->draw(ret, &dstR, nullptr);

	dstR=Rect(x, y+h-box[2]->height()+1, box[2]->width(), box[2]->height());
	box[2]->draw(ret, &dstR, nullptr);

	dstR=Rect(x+w-box[3]->width(), y+h-box[3]->height()+1, box[3]->width(), box[3]->height());
	box[3]->draw(ret, &dstR, nullptr);
}

ComponentResolved::ComponentResolved(std::shared_ptr<CComponent> Comp):
	comp(Comp)
{
	//Temporary assign ownership on comp
	if (parent)
		parent->removeChild(this);
	if (comp->parent)
	{
		comp->parent->addChild(this);
		comp->parent->removeChild(comp.get());
	}

	addChild(comp.get());
	defActions = 255 - DISPOSE;
	pos.x = pos.y = 0;

	pos.w = comp->pos.w;
	pos.h = comp->pos.h;
}

ComponentResolved::~ComponentResolved()
{
	if (parent)
	{
		removeChild(comp.get());
		parent->addChild(comp.get());
	}
}

void ComponentResolved::showAll(SDL_Surface *to)
{
	CIntObject::showAll(to);
	comp->showAll(to);
}

ComponentsToBlit::~ComponentsToBlit() = default;

ComponentsToBlit::ComponentsToBlit(std::vector<std::shared_ptr<CComponent>> & SComps, int maxw, bool blitOr)
{
	int orWidth = graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]);

	w = h = 0;
	if(SComps.empty())
		return;

	comps.resize(1);
	int curw = 0;
	int curr = 0; //current row

	for(auto & SComp : SComps)
	{
		auto cur = std::make_shared<ComponentResolved>(SComp);

		int toadd = (cur->pos.w + BETWEEN_COMPS + (blitOr ? orWidth : 0));
		if (curw + toadd > maxw)
		{
			curr++;
			vstd::amax(w,curw);
			curw = cur->pos.w;
			comps.resize(curr+1);
		}
		else
		{
			curw += toadd;
			vstd::amax(w,curw);
		}

		comps[curr].push_back(cur);
	}

	for(auto & elem : comps)
	{
		int maxHeight = 0;
		for(size_t j=0;j<elem.size();j++)
			vstd::amax(maxHeight, elem[j]->pos.h);

		h += maxHeight + BETWEEN_COMPS_ROWS;
	}
}

void ComponentsToBlit::blitCompsOnSur( bool blitOr, int inter, int &curh, SDL_Surface *ret )
{
	int orWidth = graphics->fonts[FONT_MEDIUM]->getStringWidth(CGI->generaltexth->allTexts[4]);

	for (auto & elem : comps)//for each row
	{
		int totalw=0, maxHeight=0;
		for(size_t j=0;j<elem.size();j++)//find max height & total width in this row
		{
			auto cur = elem[j];
			totalw += cur->pos.w;
			vstd::amax(maxHeight, cur->pos.h);
		}

		//add space between comps in this row
		if(blitOr)
			totalw += (inter*2+orWidth) * (elem.size() - 1);
		else
			totalw += (inter) * (elem.size() - 1);

		int middleh = curh + maxHeight/2;//axis for image aligment
		int curw = ret->w/2 - totalw/2;

		for(size_t j=0;j<elem.size();j++)
		{
			auto cur = elem[j];
			cur->moveTo(Point(curw, curh));

			//blit component
			cur->showAll(ret);
			curw += cur->pos.w;

			//if there is subsequent component blit "or"
			if(j<(elem.size()-1))
			{
				if(blitOr)
				{
					curw+=inter;

					graphics->fonts[FONT_MEDIUM]->renderTextLeft(ret, CGI->generaltexth->allTexts[4], Colors::WHITE,
					        Point(curw,middleh-(graphics->fonts[FONT_MEDIUM]->getLineHeight()/2)));

					curw+=orWidth;
				}
				curw+=inter;
			}
		}
		curh += maxHeight + BETWEEN_COMPS_ROWS;
	}
}