/* 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/state.h" #include "render/text.h" #include "render/textures.h" #include "ui/paint.h" #include namespace ui { void Paint::set_color(float r, float g, float b, float a) { gl::color(r, g, b, a); } void Paint::set_color(math::Color const & color) { gl::color(color); } void Paint::assign_system_color(const char c, const math::Color &color) { render::Text::assign_color(c, color); } void Paint::set_system_color(const char c) { render::Text::setcolor(c); } void Paint::draw_border(const math::Vector2f &global_location, const math::Vector2f &size) { using namespace gl; begin(LineLoop); vertex(global_location.x(), global_location.y()); vertex(global_location.x() + size.width(), global_location.y()); vertex(global_location.x() + size.width(), global_location.y() + size.height()); vertex(global_location.x(), global_location.y() + size.height()); end(); } void Paint::draw_rectangle(const math::Vector2f &global_location, const math::Vector2f &size) { using namespace gl; begin(Quads); vertex(global_location.x(), global_location.y()); vertex(global_location.x() + size.width(), global_location.y()); vertex(global_location.x() + size.width(), global_location.y() + size.height()); vertex(global_location.x(), global_location.y() + size.height()); end(); } void Paint::draw_rectangle_gradient(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color_left, const math::Color & color_right) { gl::begin(gl::Quads); gl::color(color_left); gl::vertex(global_location.x(), global_location.y()); gl::color(color_right); gl::vertex(global_location.x() + size.width(), global_location.y()); gl::color(color_right); gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); gl::color(color_left); gl::vertex(global_location.x(), global_location.y() + size.height()); gl::end(); } void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture, const float preserve_aspect) { model::Material *material = model::Material::load(texture, true); math::Vector2f bitmap_location; math::Vector2f bitmap_size(size); if (preserve_aspect) { const float s = math::min(size.width() / material->size().width(), size.height() / material->size().height()); bitmap_size.assign(material->size().width() * s, material->size().height() * s); bitmap_location.assign((size.width() - bitmap_size.width()) * 0.5f, (size.height() - bitmap_size.height()) * 0.5f); bitmap_location += global_location; } else { bitmap_location.assign(global_location); } render::State::set_color(math::Color()); render::State::set_color_second(math::Color()); for (model::Material::Layers::const_iterator lit = material->layers().begin(); lit != material->layers().end(); ++lit) { render::State::use_material_layer(material, *lit); gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); } render::State::reset(); } // draw a bitmap and override material color void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture, const float preserve_aspect) { model::Material *material = model::Material::load(texture, true); math::Vector2f bitmap_location; math::Vector2f bitmap_size(size); if (preserve_aspect) { const float s = math::min(size.width() / material->size().width(), size.height() / material->size().height()); bitmap_size.assign(material->size().width() * s, material->size().height() * s); bitmap_location.assign((size.width() - bitmap_size.width()) * 0.5f, (size.height() - bitmap_size.height()) * 0.5f); bitmap_location += global_location; } else { bitmap_location.assign(global_location); } render::State::set_power(true); for (model::Material::Layers::const_iterator lit = material->layers().begin(); lit != material->layers().end(); ++lit) { render::State::use_material_layer(material, *lit); // this overrides material color gl::color(color); gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); render::State::reset(); } } void Paint::draw_material(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture) { model::Material *material = model::Material::load(texture, true); // use global coordinates const float w0 = global_location.x() / material->size().width(); const float h0 = global_location.y() / material->size().height(); const float w1 = (global_location.x() + size.width()) / material->size().width(); const float h1 = (global_location.y() + size.height()) / material->size().height(); render::State::set_power(true); render::State::set_color(math::Color()); render::State::set_color_second(math::Color()); for (model::Material::Layers::const_iterator lit = material->layers().begin(); lit != material->layers().end(); ++lit) { render::State::use_material_layer(material, *lit); gl::begin(gl::Quads); glTexCoord2f(w0, h0); gl::vertex(global_location.x(), global_location.y()); glTexCoord2f(w1, h0); gl::vertex(global_location.x() + size.width(), global_location.y()); glTexCoord2f(w1, h1); gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); glTexCoord2f(w0, h1); gl::vertex(global_location.x(), global_location.y() + size.height()); gl::end(); render::State::reset(); } } // draw unaligned text void Paint::draw_text(const math::Vector2f &global_location, const Font *font, const std::string &text) { render::Text::setfont(font->name().c_str(), font->width(), font->height()); // enable OpenGL textures gl::enable(GL_TEXTURE_2D); render::Text::draw(global_location.x(), global_location.y(), text); // disable OpenGL textures gl::disable(GL_TEXTURE_2D); } // draw aligned text void Paint::draw_label(const math::Vector2f &global_location, const math::Vector2f &size, const Font *font, const std::string &text, const unsigned int alignment) { unsigned int align_horizontal = (alignment & 0x000F); if (!align_horizontal) align_horizontal = AlignLeft; unsigned int align_vertical = (alignment & 0x00F0); if (!align_vertical) align_vertical = AlignTop; // apply text font render::Text::setfont(font->name().c_str(), font->width(), font->height()); // enable OpenGL textures gl::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(global_location); switch (align_horizontal) { case AlignLeft: v[0] += font->width(); break; case AlignHCenter: v[0] += (size.width() - text_width) / 2.0f; break; case AlignRight: v[0] += size.width() - text_width - font->width(); break; } switch (align_vertical) { case AlignTop: v[1] += font->height() * 0.5f; break; case AlignVCenter: v[1] += (size.height() - text_height) / 2.0f; break; case AlignBottom: v[1] += size.height() - text_height - font->height() * 0.5f; break; } render::Text::draw(v.x(), v.y(), text); // disable OpenGL textures gl::disable(GL_TEXTURE_2D); } /* // draw unaligned text void text(const math::Vector2f &location, const math::Vector2f &size, const Font *font, std::stringstream & textstream) { render::Text::setfont(font->name().c_str(), font->width(), font->height()); // enable OpenGL textures gl::enable(GL_TEXTURE_2D); render::Text::draw(location.x(), location.y(), textstream); // disable OpenGL textures gl::disable(GL_TEXTURE_2D); } */ } //namespace ui