X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fimage.cpp;h=1da782d28a2579e66d4079d8ffb915f703627255;hb=78e05360d70542d343401241ff9ddddfed18c5cd;hp=11003f4d1af56acbfb6b7367ec680f6e86bb1533;hpb=91997dd3189b93a67179822ec2fed5f2a7bddb74;p=libs%2Fgltk.git diff --git a/source/image.cpp b/source/image.cpp index 11003f4..1da782d 100644 --- a/source/image.cpp +++ b/source/image.cpp @@ -1,15 +1,9 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include -#include +#include #include "image.h" #include "part.h" +#include "resources.h" +#include "root.h" +#include "style.h" using namespace std; @@ -20,20 +14,67 @@ Image::Image(const GL::Texture2D *i): image(i), keep_aspect(true) { - focusable = false; +} + +void Image::autosize_special(const Part &part, Geometry &ageom) const +{ + if(part.get_name()=="image") + { + const Sides &margin = part.get_margin(); + if(image) + { + ageom.w = max(ageom.w, image->get_width()+margin.left+margin.right); + ageom.h = max(ageom.h, image->get_height()+margin.top+margin.bottom); + } + else + { + ageom.w = max(ageom.w, margin.left+margin.right); + ageom.h = max(ageom.h, margin.top+margin.bottom); + } + } } void Image::set_image(const GL::Texture2D *i) { image = i; + icon_name = string(); + signal_autosize_changed.emit(); + rebuild(); +} + +void Image::set_icon(const string &n) +{ + icon_name = n; + update_icon(); } void Image::set_keep_aspect(bool ka) { keep_aspect = ka; + rebuild(); } -void Image::render_special(const Part &part) const +void Image::update_icon() +{ + if(style) + { + Root *root = find_ancestor(); + if(root) + { + if(icon_name.empty()) + image = 0; + else + image = &root->get_resources().get(icon_name); + signal_autosize_changed.emit(); + rebuild(); + return; + } + } + + image = 0; +} + +void Image::rebuild_special(const Part &part) { if(part.get_name()=="image") { @@ -61,21 +102,39 @@ void Image::render_special(const Part &part) const } } - 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(); + GL::MeshBuilder bld(part_cache.create_mesh(part, *image)); + bld.color(1.0f, 1.0f, 1.0f); + bld.begin(GL::TRIANGLE_STRIP); + bld.texcoord(0.0, 1.0); + bld.vertex(rgeom.x, rgeom.y+rgeom.h); + bld.texcoord(0.0, 0.0); + bld.vertex(rgeom.x, rgeom.y); + bld.texcoord(1.0, 1.0); + bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h); + bld.texcoord(1.0, 0.0); + bld.vertex(rgeom.x+rgeom.w, rgeom.y); + bld.end(); } } +void Image::on_style_change() +{ + if(!icon_name.empty()) + update_icon(); +} + +void Image::on_reparent() +{ + if(!icon_name.empty()) + update_icon(); +} + + +Image::Loader::Loader(Image &img): + DataFile::DerivedObjectLoader(img) +{ + add("icon", &Image::icon_name); +} + } // namespace GLtk } // namespace Msp