]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/part.cpp
Allow an empty graphic to be specified in part definitions
[libs/gltk.git] / source / part.cpp
index 38e75d7c034d4526f08662ec732deb7d8493f83d..72c8921b1c03663181645623ac8485386afe83e8 100644 (file)
@@ -1,5 +1,7 @@
+#include <msp/gl/meshbuilder.h>
 #include "geometry.h"
 #include "part.h"
+#include "partcache.h"
 #include "resources.h"
 
 using namespace std;
@@ -7,63 +9,89 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Part::Part(const Resources &r, const string &n):
-       res(r),
-       name(n),
-       fill_x(true),
-       fill_y(true)
+Part::Part(const string &n):
+       name(n)
 {
-       for(unsigned i=0; i<N_STATES_; ++i)
-               graphic[i]=0;
+       fill(graphic, graphic+N_STATES_, static_cast<Graphic *>(0));
 }
 
 const Graphic *Part::get_graphic(State state) const
 {
        if(state>N_STATES_)
-               throw InvalidParameterValue("Invalid state");
+               throw invalid_argument("Part::get_graphic");
 
        return graphic[state];
 }
 
-void Part::render(const Geometry &geom, State state) const
+void Part::build(const Geometry &parent, State state, PartCache &cache) const
 {
-       const Graphic::Sides &shadow=graphic[state]->get_shadow();
-       unsigned gw=(fill_x ? geom.w : graphic[state]->get_width())-shadow.left-shadow.right;
-       unsigned gh=(fill_y ? geom.h : graphic[state]->get_height())-shadow.top-shadow.bottom;
-       align.apply(geom, gw, gh);
-       graphic[state]->render(gw, gh);
+       if(!graphic[state] || !graphic[state]->get_texture())
+               return;
+
+       Geometry rgeom = geom;
+       align.apply(rgeom, parent, margin);
+       GL::MeshBuilder bld(cache.create_mesh(*this, *graphic[state]->get_texture()));
+       bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+       graphic[state]->build(rgeom.w, rgeom.h, bld);
 }
 
 
-Part::Loader::Loader(Part &p):
-       part(p)
+Part::Loader::Loader(Part &p, Resources &r):
+       DataFile::CollectionObjectLoader<Part>(p, &r)
 {
        add("graphic", &Loader::graphic);
        add("align",   &Loader::align);
        add("fill",    &Loader::fill);
+       add("margin",  &Loader::margin);
+       add("size",    &Loader::size);
+}
+
+Part::Loader::~Loader()
+{
+       for(unsigned i=0; i<N_STATES_; ++i)
+               if(obj.graphic[i])
+               {
+                       const Graphic &grph = *obj.graphic[i];
+                       const Sides &shadow = grph.get_shadow();
+                       obj.geom.w = max(obj.geom.w, grph.get_width()-shadow.left-shadow.right);
+                       obj.geom.h = max(obj.geom.h, grph.get_height()-shadow.bottom-shadow.top);
+               }
+}
+
+void Part::Loader::graphic_normal(const string &n)
+{
+       graphic(NORMAL, n);
 }
 
 void Part::Loader::graphic(State s, const string &n)
 {
-       part.graphic[s]=&part.res.get_graphic(n);
-       if(s==NORMAL)
-       {
-               for(unsigned i=0; i<N_STATES_; ++i)
-                       if(!part.graphic[i])
-                               part.graphic[i]=part.graphic[s];
-       }
+       Graphic *grph = (n.empty() ? 0 : &get_collection().get<Graphic>(n));
+       for(int i=0; i<N_STATES_; ++i)
+               if((i&s)==s)
+                       obj.graphic[i] = grph;
+}
+
+void Part::Loader::align(float x, float y)
+{
+       obj.align.x = x;
+       obj.align.y = y;
+}
+
+void Part::Loader::fill(float w, float h)
+{
+       obj.align.w = w;
+       obj.align.h = h;
 }
 
-void Part::Loader::align(int x, int y)
+void Part::Loader::margin()
 {
-       part.align.x=x;
-       part.align.y=y;
+       load_sub(obj.margin);
 }
 
-void Part::Loader::fill(bool x, bool y)
+void Part::Loader::size(unsigned w, unsigned h)
 {
-       part.fill_x=x;
-       part.fill_y=y;
+       obj.geom.w = w;
+       obj.geom.h = h;
 }
 
 } // namespace GLtk