diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/bitmap.cc | 8 | ||||
-rw-r--r-- | src/ui/bitmap.h | 12 | ||||
-rw-r--r-- | src/ui/paint.cc | 48 | ||||
-rw-r--r-- | src/ui/paint.h | 9 |
4 files changed, 59 insertions, 18 deletions
diff --git a/src/ui/bitmap.cc b/src/ui/bitmap.cc index 03d7166..5caf966 100644 --- a/src/ui/bitmap.cc +++ b/src/ui/bitmap.cc @@ -16,6 +16,7 @@ Bitmap::Bitmap(Widget *parent, const char *texture) : Widget(parent) { set_border(false); set_background(true); + set_preserve_aspect(false); set_label("bitmap"); set_texture(texture); @@ -48,10 +49,15 @@ void Bitmap::set_color(const math::Color & color) bitmap_color.assign(color); } +void Bitmap::set_preserve_aspect(const bool preserve_aspect) +{ + bitmap_preserve_aspect = preserve_aspect; +} + void Bitmap::draw_background() { if (bitmap_texture.size()) { - Paint::draw_bitmap(global_location(), size(),bitmap_color, bitmap_texture); + Paint::draw_bitmap(global_location(), size(), bitmap_color, bitmap_texture, bitmap_preserve_aspect); } } diff --git a/src/ui/bitmap.h b/src/ui/bitmap.h index 4838ea2..123f12c 100644 --- a/src/ui/bitmap.h +++ b/src/ui/bitmap.h @@ -18,18 +18,25 @@ public: Bitmap(Widget *parent, const char *texture = 0); ~Bitmap(); - inline std::string const &texture() const { + inline const std::string & texture() const { return bitmap_texture; } - inline math::Color const &color() const { + inline const math::Color & color() const { return bitmap_color; } + + inline const bool preserve_aspect() const { + return bitmap_preserve_aspect; + } void set_texture(const std::string & texture); + void set_texture(const char *texture); void set_color(const math::Color &color); + + void set_preserve_aspect(const bool preserve_aspect); /// print bitmap description virtual void print(const size_t indent) const; @@ -41,6 +48,7 @@ protected: private: std::string bitmap_texture; math::Color bitmap_color; + bool bitmap_preserve_aspect; }; } diff --git a/src/ui/paint.cc b/src/ui/paint.cc index 6dd85b8..cea363c 100644 --- a/src/ui/paint.cc +++ b/src/ui/paint.cc @@ -12,6 +12,8 @@ #include "render/textures.h" #include "ui/paint.h" +#include <cassert> + namespace ui { void Paint::set_color(float r, float g, float b, float a) @@ -72,8 +74,7 @@ void Paint::draw_rectangle_gradient(const math::Vector2f &global_location, const gl::end(); } -// draw a bitmap -void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture) +void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture, const float preserve_aspect) { // find the material model::Material *material = model::Material::find(texture); @@ -85,6 +86,19 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto material->set_flags(model::Material::FlagBright); } + 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()); render::State::use_material(material); @@ -92,16 +106,16 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); - gl::vertex(global_location.x(), global_location.y()); + gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); - gl::vertex(global_location.x() + size.width(), global_location.y()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); - gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); - gl::vertex(global_location.x(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); @@ -109,7 +123,7 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto } // 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) +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) { // find the material model::Material *material = model::Material::find(texture); @@ -121,6 +135,18 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto material->set_flags(model::Material::FlagBright); } + 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); render::State::use_material(material); @@ -130,16 +156,16 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); - gl::vertex(global_location.x(), global_location.y()); + gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); - gl::vertex(global_location.x() + size.width(), global_location.y()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); - gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); - gl::vertex(global_location.x(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); diff --git a/src/ui/paint.h b/src/ui/paint.h index b2829ea..76cd8bf 100644 --- a/src/ui/paint.h +++ b/src/ui/paint.h @@ -8,6 +8,7 @@ #define __INCLUDED_UI_PAINT_H__ #include "ui/widget.h" +#include "model/material.h" namespace ui { @@ -53,17 +54,17 @@ public: * @brief draw a color gradient rectangle * */ static void draw_rectangle_gradient(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color_left, const math::Color & color_right); - + /** * @brief draw a rectangular bitmap * @param texture the name of material to be used - **/ - static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture); + **/ + static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture, const float preserve_aspect = false); /** * @brief draw a rectangular bitmap and overrride material color * */ - static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture); + static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture, const float preserve_aspect = false); /** * @brief draw a tiled bitmap |