]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/part.cpp
Pass coordinates relative to the receiving widget's geometry
[libs/gltk.git] / source / part.cpp
index 1d965385e1372d2b3da5e765fac3e26351d4dc8c..a74c91141ca717c86262c8a634f8db75ff359226 100644 (file)
@@ -1,3 +1,11 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/gl/transform.h>
 #include "geometry.h"
 #include "part.h"
 #include "resources.h"
@@ -8,11 +16,7 @@ namespace Msp {
 namespace GLtk {
 
 Part::Part(const string &n):
-       name(n),
-       width(1),
-       height(1),
-       fill_x(true),
-       fill_y(true)
+       name(n)
 {
        for(unsigned i=0; i<N_STATES_; ++i)
                graphic[i]=0;
@@ -26,12 +30,15 @@ const Graphic *Part::get_graphic(State state) const
        return graphic[state];
 }
 
-void Part::render(const Geometry &geom, State state) const
+void Part::render(const Geometry &parent, State state) const
 {
-       unsigned gw=(fill_x ? geom.w : width);
-       unsigned gh=(fill_y ? geom.h : height);
-       align.apply(geom, gw, gh);
-       graphic[state]->render(gw, gh);
+       if(!graphic[state])
+               return;
+
+       Geometry rgeom=geom;
+       align.apply(rgeom, parent, margin);
+       GL::translate(rgeom.x, rgeom.y, 0);
+       graphic[state]->render(rgeom.w, rgeom.h);
 }
 
 
@@ -42,6 +49,8 @@ Part::Loader::Loader(Part &p, Resources &r):
        add("graphic", &Loader::graphic);
        add("align",   &Loader::align);
        add("fill",    &Loader::fill);
+       add("margin",  &Loader::margin);
+       add("size",    &Loader::size);
 }
 
 Part::Loader::~Loader()
@@ -51,8 +60,8 @@ Part::Loader::~Loader()
                if(part.graphic[i])
                {
                        const Sides &shadow=part.graphic[i]->get_shadow();
-                       part.width=max(part.width, part.graphic[i]->get_width()-shadow.left-shadow.right);
-                       part.height=max(part.height, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
+                       part.geom.w=max(part.geom.w, part.graphic[i]->get_width()-shadow.left-shadow.right);
+                       part.geom.h=max(part.geom.h, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
                }
                else
                        part.graphic[i]=part.graphic[NORMAL];
@@ -61,19 +70,30 @@ Part::Loader::~Loader()
 
 void Part::Loader::graphic(State s, const string &n)
 {
-       part.graphic[s]=&res.get<Graphic>(n);
+       part.graphic[s]=res.get<Graphic>(n);
 }
 
-void Part::Loader::align(int x, int y)
+void Part::Loader::align(float x, float y)
 {
        part.align.x=x;
        part.align.y=y;
 }
 
-void Part::Loader::fill(bool x, bool y)
+void Part::Loader::fill(float w, float h)
+{
+       part.align.w=w;
+       part.align.h=h;
+}
+
+void Part::Loader::margin()
+{
+       load_sub(part.margin);
+}
+
+void Part::Loader::size(unsigned w, unsigned h)
 {
-       part.fill_x=x;
-       part.fill_y=y;
+       part.geom.w=w;
+       part.geom.h=h;
 }
 
 } // namespace GLtk