Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 33ee54f7c4df2ce3137e918c799761e566727dba (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
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
/*
   ui/paint.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "auxiliary/functions.h"
#include "math/vector2f.h"
#include "render/gl.h"
#include "render/text.h"
#include "render/textures.h"
#include "ui/paint.h"

namespace ui
{

// contains the interface between the user interface and the render library
namespace paint {

void color(float r, float g, float b, float a)
{
	render::gl::color(r, g, b, a);
}

void color(math::Color const & color)
{
	render::gl::color(color);
}

void color_code(const char *c)
{
	render::Text::setcolor(*c);
}

void border(math::Vector2f const &location, math::Vector2f const &size)
{
	using namespace render::gl;
	
	begin(LineLoop);
	vertex(location.x +1 , location.y);
	vertex(location.x + size.x, location.y);
	vertex(location.x + size.x, location.y + size.y -1);
	vertex(location.x +1, location.y + size.y - 1);
	end();
}

void rectangle(math::Vector2f const &location, math::Vector2f const &size)
{
	using namespace render::gl;
	
	begin(Quads);
	vertex(location.x +1 , location.y);
	vertex(location.x + size.x, location.y);
	vertex(location.x + size.x, location.y + size.y -1);
	vertex(location.x +1, location.y + size.y - 1);
	end();
}

void bitmap(math::Vector2f const &location, math::Vector2f const &size, std::string const &texture)
{

	using namespace render::gl;
	
	render::Textures::bind("bitmaps/" + texture);
	enable(GL_TEXTURE_2D);
	
	begin(Quads);
	
	glTexCoord2f(0.0f, 0.0f);
	vertex(location.x +1 , location.y);
	
	glTexCoord2f(1.0f, 0.0f);
	vertex(location.x + size.x, location.y);
	
	glTexCoord2f(1.0f, 1.0f);
	vertex(location.x + size.x, location.y + size.y -1);
	
	glTexCoord2f(0.0f, 1.0f);
	vertex(location.x +1, location.y + size.y - 1);
	end();
	
	disable(GL_TEXTURE_2D);
}

void text_centered(math::Vector2f const &location, math::Vector2f const &size, std::string const &text, Font const *font)
{
	using namespace render::gl;
	
	render::Text::setfont(font->name().c_str(), font->width(), font->height());
	enable(GL_TEXTURE_2D);
	
	math::Vector2f v(location);
	
	v.x += (size.x - aux::text_strip(text).size() * font->width()) /2.0f;
	v.y += (size.y - font->height()) / 2.0f;
	
	render::Text::draw(v.x, v.y, text);
	
	disable(GL_TEXTURE_2D);
}

void text(math::Vector2f const &location, Font const *font,  std::stringstream & textstream)
{
	using namespace render::gl;
	render::Text::setfont(font->name().c_str(), font->width(), font->height());
	
	// enable OpenGL textures
	enable(GL_TEXTURE_2D);
	
	render::Text::draw(location.x, location.y, textstream);
	
	// disable texturing
	disable(GL_TEXTURE_2D);
}

void text(math::Vector2f const &location, math::Vector2f const &size,
	  Font const *font,
	  std::string const &text,
	  unsigned int align)
{
	unsigned int align_horizontal = (align & 0x000F);
	if (!align_horizontal)
		align_horizontal = AlignLeft;
		
	unsigned int align_vertical = (align & 0x00F0);
	if (!align_vertical)
		align_vertical = AlignTop;
		
	// apply text font
	using namespace render::gl;
	render::Text::setfont(font->name().c_str(), font->width(), font->height());
	
	// enable OpenGL textures
	enable(GL_TEXTURE_2D);
	
	// determine the width and height of the text
	// FIXME support multiline text
	float text_height = 1.0f * font->height();
	float text_width = (float) aux::text_strip(text).size() * font->width();
	
	// calculate drawing position
	math::Vector2f v(location);
	
	switch (align_horizontal) {
		case AlignLeft:
			v.x += font->width();
			break;
		case AlignHCenter:
			v.x += (size.x - text_width) / 2.0f;
			break;
		case AlignRight:
			v.x += size.x - text_width - font->width();
			break;
	}
	
	switch (align_vertical) {
		case AlignTop:
			v.y += font->height()*0.5f;
			break;
		case AlignVCenter:
			v.y += (size.y - text_height) / 2.0f;
			break;
		case AlignBottom:
			v.y += size.y - text_height - font->height()*0.5f;
			break;
	}
	
	render::Text::draw(v.x, v.y, text);
	
	// disable texturing
	disable(GL_TEXTURE_2D);
	
}

}

}