]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add Image widget
authorMikko Rasa <tdb@tdb.fi>
Mon, 11 Oct 2010 07:24:18 +0000 (07:24 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 11 Oct 2010 07:24:18 +0000 (07:24 +0000)
source/image.cpp [new file with mode: 0644]
source/image.h [new file with mode: 0644]

diff --git a/source/image.cpp b/source/image.cpp
new file mode 100644 (file)
index 0000000..f7f61bb
--- /dev/null
@@ -0,0 +1,83 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/gl/bindable.h>
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#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<float>(image->get_width())/image->get_height();
+                       if(rgeom.w<rgeom.h*aspect)
+                       {
+                               unsigned h = static_cast<unsigned>(rgeom.w/aspect);
+                               rgeom.y += (rgeom.h-h)*align.y;
+                               rgeom.h = h;
+                       }
+                       else
+                       {
+                               unsigned w = static_cast<unsigned>(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 (file)
index 0000000..5d113cc
--- /dev/null
@@ -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 <msp/gl/texture2d.h>
+#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