]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/part.cpp
Enable loading of entry widgets from datafiles
[libs/gltk.git] / source / part.cpp
index f5e4b79ef5c36a674f329fd9ca3dbc9a575c5e1c..a92e7208ca6883015cd978eb5c9218700538faec 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/gl/transform.h>
 #include "geometry.h"
 #include "part.h"
 #include "resources.h"
@@ -16,8 +17,6 @@ namespace GLtk {
 
 Part::Part(const string &n):
        name(n),
-       width(1),
-       height(1),
        fill_x(true),
        fill_y(true)
 {
@@ -33,12 +32,19 @@ 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;
+       if(fill_x)
+               rgeom.w=parent.w-margin.left-margin.right;
+       if(fill_y)
+               rgeom.h=parent.h-margin.bottom-margin.top;
+       align.apply(rgeom, parent, margin);
+       GL::translate(rgeom.x, rgeom.y, 0);
+       graphic[state]->render(rgeom.w, rgeom.h);
 }
 
 
@@ -49,6 +55,7 @@ Part::Loader::Loader(Part &p, Resources &r):
        add("graphic", &Loader::graphic);
        add("align",   &Loader::align);
        add("fill",    &Loader::fill);
+       add("margin",  &Loader::margin);
 }
 
 Part::Loader::~Loader()
@@ -58,8 +65,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];
@@ -71,7 +78,7 @@ void Part::Loader::graphic(State s, const string &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;
@@ -83,5 +90,10 @@ void Part::Loader::fill(bool x, bool y)
        part.fill_y=y;
 }
 
+void Part::Loader::margin()
+{
+       load_sub(part.margin);
+}
+
 } // namespace GLtk
 } // namespace Msp