summaryrefslogtreecommitdiff
path: root/src/interface/Textblock.cpp
blob: f003a6aeefce8a2a5787e074c4b9c92a69ee148d (plain)
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
/*
 * Textblock.cpp
 *
 *  Created on: Jan 29, 2012
 *      Author: Simon
 */

#include <iostream>
#include "Textblock.h"

using namespace ui;

Textblock::Textblock(Point position, Point size, std::string textboxText):
	Label(position, size, textboxText)
{
	if(size.Y==-1)
		autoHeight = true;
	else
		autoHeight = false;
	updateMultiline();
}

void Textblock::SetText(std::string text)
{
	this->text = text;
	updateMultiline();
}

void Textblock::updateMultiline()
{
	char * rawText = new char[text.length()+1];
	std::copy(text.begin(), text.end(), rawText);
	rawText[text.length()] = 0;

	int lines = 1;
	int currentWidth = 0;
	char * lastSpace = NULL;
	char * currentWord = rawText;
	char * nextSpace;
	while(true)
	{
		nextSpace = strchr(currentWord+1, ' ');
		if(nextSpace)
			nextSpace[0] = 0;
		int width = Graphics::textwidth(currentWord);
		if(width+currentWidth > Size.X-6)
		{
			currentWidth = width;
			currentWord[0] = '\n';
			lines++;
		}
		else
			currentWidth += width;
		if(nextSpace)
			nextSpace[0] = ' ';
		if(!(currentWord = strchr(currentWord+1, ' ')))
			break;
	}
	if(autoHeight)
	{
		Size.Y = lines*12;
	}
	textLines = std::string(rawText);
	delete[] rawText;
}

void Textblock::Draw(const Point &screenPos)
{
	Graphics * g = ui::Engine::Ref().g;
	//g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, textColour.Red, textColour.Green, textColour.Blue, 255);
	g->drawtext(screenPos.X+3, screenPos.Y+3, textLines, textColour.Red, textColour.Green, textColour.Blue, 255);
}

Textblock::~Textblock() {
	// TODO Auto-generated destructor stub
}