-/* $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 <msp/gl/meshbuilder.h>
#include "image.h"
#include "part.h"
+#include "style.h"
using namespace std;
namespace Msp {
namespace GLtk {
-Image::Image(const Resources &r, const GL::Texture2D *i):
- Widget(r),
+Image::Image(const GL::Texture2D *i):
image(i),
keep_aspect(true)
{
focusable = false;
- update_style();
+}
+
+void Image::autosize()
+{
+ if(!style)
+ return;
+
+ Widget::autosize();
+
+ if(const Part *image_part = style->get_part("image"))
+ {
+ const Sides &margin = image_part->get_margin();
+ if(image)
+ {
+ geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
+ geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
+ }
+ else
+ {
+ geom.w = max(geom.w, margin.left+margin.right);
+ geom.h = max(geom.h, margin.top+margin.bottom);
+ }
+ }
+
+ rebuild();
}
void Image::set_image(const GL::Texture2D *i)
{
image = i;
+ signal_autosize_changed.emit();
+ rebuild();
}
void Image::set_keep_aspect(bool ka)
{
keep_aspect = ka;
+ rebuild();
}
-void Image::render_special(const Part &part) const
+void Image::rebuild_special(const Part &part, CachedPart &cache)
{
if(part.get_name()=="image")
{
if(!image)
+ {
+ cache.texture = 0;
return;
+ }
const Alignment &align = part.get_alignment();
Geometry rgeom = part.get_geometry();
}
}
- 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();
+ cache.texture = image;
+ cache.clear_mesh();
+
+ GL::MeshBuilder bld(*cache.mesh);
+ bld.color(1.0f, 1.0f, 1.0f);
+ bld.begin(GL::QUADS);
+ bld.texcoord(0.0, 0.0);
+ bld.vertex(rgeom.x, rgeom.y);
+ bld.texcoord(1.0, 0.0);
+ bld.vertex(rgeom.x+rgeom.w, rgeom.y);
+ bld.texcoord(1.0, 1.0);
+ bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
+ bld.texcoord(0.0, 1.0);
+ bld.vertex(rgeom.x, rgeom.y+rgeom.h);
+ bld.end();
}
}