forked from acaudwell/Logstalgia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextarea.cpp
116 lines (88 loc) · 3.06 KB
/
textarea.cpp
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
/*
Copyright (C) 2008 Andrew Caudwell ([email protected])
This program 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
3 of the License, or (at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "textarea.h"
TextArea::TextArea() {
}
TextArea::TextArea(FXFont font) {
this->colour = vec3(1.0f,1.0f,1.0f);
this->font = font;
this->corner = vec2(0.0f,0.0f);
this->visible = false;
this->font.dropShadow(true);
}
TextArea::TextArea(std::vector<std::string>& content, FXFont font, vec3 colour) {
this->colour = colour;
this->font = font;
this->corner = vec2(0.0f,0.0f);
setText(content);
}
void TextArea::hide() {
this->visible=false;
}
void TextArea::setColour(vec3 colour) {
this->colour = colour;
}
void TextArea::setText(std::vector<std::string>& content_) {
this->content.clear();
//calculate area
rectwidth = 0;
rectheight = content_.size() * (font.getMaxHeight()+4) + 2;
std::vector<std::string>::iterator it;
for(it = content_.begin(); it != content_.end(); it++) {
std::string s = *it;
if(s.size() > 100) {
s = s.substr(0,100);
}
int width = font.getWidth(s) + 6;
if(width>rectwidth) rectwidth = width;
this->content.push_back(s);
}
this->visible=true;
}
void TextArea::setPos(vec2 pos) {
corner = pos;
int fontheight = font.getMaxHeight() + 4;
corner.y -= rectheight;
if((corner.x + rectwidth) > display.width) {
if((corner.x - rectwidth - fontheight )>0) {
corner.x -= rectwidth;
} else {
corner.x = display.width - rectwidth;
}
}
if(corner.y < 0) corner.y += rectheight + fontheight;
if(corner.y +rectheight > display.height) corner.y -= rectheight;
}
void TextArea::draw() {
if(!visible) return;
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
vec4 col(colour, 0.2f);
glColor4fv(glm::value_ptr(col));
glBegin(GL_QUADS);
glVertex2f(corner.x, corner.y);
glVertex2f(corner.x, corner.y + rectheight);
glVertex2f(corner.x+rectwidth, corner.y + rectheight);
glVertex2f(corner.x+rectwidth, corner.y);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
int yinc = 2;
std::vector<std::string>::iterator it;
for(it = content.begin(); it != content.end(); it++) {
font.draw((int)corner.x+2, (int)corner.y+yinc, (*it).c_str());
yinc += font.getMaxHeight() + 4;
}
}