From: Mikko Rasa Date: Mon, 11 Oct 2010 07:24:18 +0000 (+0000) Subject: Add Image widget X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=850fae783476edfdaf996a008c70c941afb39d62;p=libs%2Fgltk.git Add Image widget --- diff --git a/source/image.cpp b/source/image.cpp new file mode 100644 index 0000000..f7f61bb --- /dev/null +++ b/source/image.cpp @@ -0,0 +1,83 @@ +/* $Id$ + +This file is part of libmspgltk +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include +#include +#include "image.h" +#include "part.h" + +using namespace std; + +namespace Msp { +namespace GLtk { + +Image::Image(const Resources &r, const GL::Texture2D *i): + Widget(r), + image(i), + keep_aspect(true) +{ + focusable = false; + update_style(); +} + +void Image::set_image(const GL::Texture2D *i) +{ + image = i; +} + +void Image::set_keep_aspect(bool ka) +{ + keep_aspect = ka; +} + +void Image::render_special(const Part &part) const +{ + if(part.get_name()=="image") + { + if(!image) + return; + + const Alignment &align = part.get_alignment(); + Geometry rgeom = part.get_geometry(); + align.apply(rgeom, geom, part.get_margin()); + + if(keep_aspect) + { + float aspect = static_cast(image->get_width())/image->get_height(); + if(rgeom.w(rgeom.w/aspect); + rgeom.y += (rgeom.h-h)*align.y; + rgeom.h = h; + } + else + { + unsigned w = static_cast(rgeom.h*aspect); + rgeom.x += (rgeom.w-w)*align.x; + rgeom.w = w; + } + } + + GL::Bind _bind_tex(image); + GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); + imm.color(1.0f, 1.0f, 1.0f); + imm.begin(GL::QUADS); + imm.texcoord(0.0, 0.0); + imm.vertex(rgeom.x, rgeom.y); + imm.texcoord(1.0, 0.0); + imm.vertex(rgeom.x+rgeom.w, rgeom.y); + imm.texcoord(1.0, 1.0); + imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h); + imm.texcoord(0.0, 1.0); + imm.vertex(rgeom.x, rgeom.y+rgeom.h); + imm.end(); + } +} + +} // namespace GLtk +} // namespace Msp diff --git a/source/image.h b/source/image.h new file mode 100644 index 0000000..5d113cc --- /dev/null +++ b/source/image.h @@ -0,0 +1,39 @@ +/* $Id$ + +This file is part of libmspgltk +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GLTK_IMAGE_H_ +#define MSP_GLTK_IMAGE_H_ + +#include +#include "widget.h" + +namespace Msp { +namespace GLtk { + +/** +A widget for displaying images. +*/ +class Image: public Widget +{ +private: + const GL::Texture2D *image; + bool keep_aspect; + +public: + Image(const Resources &, const GL::Texture2D * = 0); + + void set_image(const GL::Texture2D *); + void set_keep_aspect(bool); +private: + virtual const char *get_class() const { return "image"; } + virtual void render_special(const Part &) const; +}; + +} // namespace GLtk +} // namespace Msp + +#endif