+++ /dev/null
-#ifndef MSP_GLTK_COLOR_H_
-#define MSP_GLTK_COLOR_H_
-
-namespace Msp {
-namespace GLtk {
-
-struct Color
-{
- float r, g, b;
-
- Color(): r(1), g(1), b(1) { }
- Color(float r_, float g_, float b_): r(r_), g(g_), b(b_) { }
-};
-
-} // namespace GLtk
-} // namespace Msp
-
-#endif
+#include <msp/gl/immediate.h>
#include "graphic.h"
#include "resources.h"
using namespace std;
-#include <iostream>
-
namespace Msp {
namespace GLtk {
-Graphic::Graphic(const Resources &r, const string &n):
- res(r),
- name(n),
+Graphic::Graphic():
texture(0)
{ }
unsigned ymin=border.bottom ? 0 : 1;
unsigned ymax=border.top ? 3 : 2;
+ GL::Immediate imm((GL::TEXCOORD2,GL::VERTEX2));
for(unsigned i=ymin; i<ymax; ++i)
{
- glBegin(GL_QUAD_STRIP);
+ imm.begin(GL::QUAD_STRIP);
for(unsigned j=xmin; j<=xmax; ++j)
{
- glTexCoord2f(u[j], v[i]);
- glVertex2f(x[j], y[i]);
- glTexCoord2f(u[j], v[i+1]);
- glVertex2f(x[j], y[i+1]);
+ imm.texcoord(u[j], v[i]);
+ imm.vertex(x[j], y[i]);
+ imm.texcoord(u[j], v[i+1]);
+ imm.vertex(x[j], y[i+1]);
}
- glEnd();
+ imm.end();
}
}
-Graphic::Loader::Loader(Graphic &g):
- graph(g)
+Graphic::Loader::Loader(Graphic &g, Resources &r):
+ graph(g),
+ res(r)
{
add("texture", &Loader::texture);
add("slice", &Loader::slice);
void Graphic::Loader::texture(const string &n)
{
- graph.texture=&graph.res.get_texture(n);
+ graph.texture=&res.get<GL::Texture2D>(n);
graph.slice=Geometry(0, 0, graph.texture->get_width(), graph.texture->get_height());
}
public:
class Loader: public DataFile::Loader
{
- public:
- Loader(Graphic &);
private:
Graphic &graph;
+ Resources &res;
+
+ public:
+ typedef Resources Collection;
+ Loader(Graphic &, Resources &);
+ private:
void texture(const std::string &);
void slice(unsigned, unsigned, unsigned, unsigned);
void border();
void shadow();
};
- Graphic(const Resources &, const std::string &);
+ Graphic();
const Sides &get_border() const { return border; }
const Sides &get_shadow() const { return shadow; }
const GL::Texture2D *get_texture() const { return texture; }
unsigned get_height() const { return slice.h; }
void render(unsigned, unsigned) const;
private:
- const Resources &res;
- std::string name;
Sides border;
Sides shadow;
const GL::Texture2D *texture;
namespace Msp {
namespace GLtk {
-Part::Part(const Resources &r, const string &n):
- res(r),
+Part::Part(const string &n):
name(n),
width(1),
height(1),
}
-Part::Loader::Loader(Part &p):
- part(p)
+Part::Loader::Loader(Part &p, Resources &r):
+ part(p),
+ res(r)
{
add("graphic", &Loader::graphic);
add("align", &Loader::align);
void Part::Loader::graphic(State s, const string &n)
{
- part.graphic[s]=&part.res.get_graphic(n);
+ part.graphic[s]=&res.get<Graphic>(n);
}
void Part::Loader::align(int x, int y)
public:
class Loader: public DataFile::Loader
{
- public:
- Loader(Part &);
- ~Loader();
private:
Part ∂
+ Resources &res;
+ public:
+ Loader(Part &, Resources &);
+ ~Loader();
+ private:
void graphic(State, const std::string &);
void align(int, int);
void fill(bool, bool);
};
- Part(const Resources &, const std::string &);
+ Part(const std::string &);
const std::string &get_name() const { return name; }
const Graphic *get_graphic(State) const;
unsigned get_width() const { return width; }
bool get_fill_y() const { return fill_y; }
void render(const Geometry &, State) const;
private:
- const Resources &res;
std::string name;
const Graphic *graphic[N_STATES_];
unsigned width;
-#include <msp/core/error.h>
+#include <msp/core/except.h>
#include "resources.h"
using namespace std;
namespace GLtk {
Resources::Resources():
+ path("."),
default_font(0)
-{ }
-
-Resources::~Resources()
{
- for(FontMap::iterator i=fonts.begin(); i!=fonts.end(); ++i)
- delete i->second;
- for(TextureMap::iterator i=textures.begin(); i!=textures.end(); ++i)
- delete i->second;
+ add_keyword<GL::Font>("font");
+ add_keyword<Graphic>("graphic");
+ add_keyword<Style>("style");
+
+ add_creator(&Resources::create_font);
+ add_creator(&Resources::create_texture);
}
-const GL::Font &Resources::get_font(const string &name) const
+void Resources::set_path(const Path &p)
{
- FontMap::const_iterator i=fonts.find(name);
- if(i==fonts.end())
- throw KeyError("Unknown font "+name);
-
- return *i->second;
+ path=p;
}
const GL::Font &Resources::get_default_font() const
return *default_font;
}
-const GL::Texture2D &Resources::get_texture(const string &name) const
-{
- TextureMap::const_iterator i=textures.find(name);
- if(i==textures.end())
- throw KeyError("Unknown texture "+name);
-
- return *i->second;
-}
-
-const Graphic &Resources::get_graphic(const string &name) const
+GL::Font *Resources::create_font(const string &name)
{
- GraphicMap::const_iterator i=graphics.find(name);
- if(i==graphics.end())
- throw KeyError("Unknown graphic "+name);
-
- return i->second;
+ RefPtr<GL::Font> fnt=new GL::Font;
+ DataFile::load(*fnt, (path/(name+".font")).str());
+ return fnt.release();
}
-const Style &Resources::get_style(const string &wdg, const string &name) const
+GL::Texture2D *Resources::create_texture(const string &name)
{
- StyleMap::const_iterator i=styles.find(StyleId(wdg, name));
- if(i==styles.end())
- throw KeyError("Unknown style "+name+" for widget "+wdg);
-
- return i->second;
+ RefPtr<GL::Texture2D> tex=new GL::Texture2D;
+ tex->image((path/(name+".png")).str());
+ tex->set_min_filter(GL::LINEAR);
+ return tex.release();
}
Resources::Loader::Loader(Resources &r):
+ Collection::Loader(r),
res(r)
{
- add("font", &Loader::font);
- add("texture", &Loader::texture);
- add("graphic", &Loader::graphic);
- add("style", &Loader::style);
+ add("font", &Loader::font);
}
-void Resources::Loader::font(const string &fn)
+void Resources::Loader::font(const string &name)
{
RefPtr<GL::Font> fnt=new GL::Font;
- DataFile::load(*fnt, fn);
-
- res.fonts.insert(FontMap::value_type(fn.substr(0, fn.rfind('.')), fnt.get()));
+ load_sub(*fnt);
+ res.add(name, fnt.get());
if(!res.default_font)
res.default_font=fnt.get();
fnt.release();
}
-void Resources::Loader::texture(const string &fn)
-{
- RefPtr<GL::Texture2D> tex=new GL::Texture2D;
- tex->image(fn);
- tex->set_min_filter(GL::LINEAR);
-
- res.textures.insert(TextureMap::value_type(fn.substr(0, fn.rfind('.')), tex.release()));
-}
-
-void Resources::Loader::graphic(const std::string &n)
-{
- Graphic graph(res, n);
- load_sub(graph);
- if(!graph.get_texture())
- throw Exception("Graphic without texture");
-
- res.graphics.insert(GraphicMap::value_type(n, graph));
-}
-
-void Resources::Loader::style(const string &w, const string &n)
-{
- Style stl(res, w, n);
- load_sub(stl);
-
- res.styles.insert(StyleMap::value_type(StyleId(w, n), stl));
-}
-
-
-bool Resources::StyleId::operator<(const StyleId &other) const
-{
- if(widget<other.widget)
- return true;
- return widget==other.widget && name<other.name;
-}
-
} // namespace GLtk
} // namespace Msp
#include <msp/gl/font.h>
#include <msp/gl/texture.h>
-#include <msp/datafile/loader.h>
+#include <msp/datafile/collection.h>
+#include <msp/path/path.h>
#include "graphic.h"
#include "style.h"
namespace Msp {
namespace GLtk {
-class Resources
+class Resources: public DataFile::Collection
{
-public:
+private:
+ Path path;
+ GL::Font *default_font;
- class Loader: public Msp::DataFile::Loader
+public:
+ class Loader: public Collection::Loader
{
- public:
- Loader(Resources &);
private:
Resources &res;
+ public:
+ Loader(Resources &);
+ private:
void font(const std::string &);
- void texture(const std::string &);
- void graphic(const std::string &);
- void style(const std::string &, const std::string &);
};
Resources();
- ~Resources();
- const GL::Font &get_font(const std::string &) const;
+
+ void set_path(const Path &);
const GL::Font &get_default_font() const;
- const GL::Texture2D &get_texture(const std::string &) const;
- const Graphic &get_graphic(const std::string &) const;
- const Style &get_style(const std::string &, const std::string &) const;
private:
- struct StyleId
- {
- std::string widget;
- std::string name;
-
- StyleId(const std::string &w, const std::string &n): widget(w), name(n) { }
- bool operator<(const StyleId &) const;
- };
-
- typedef std::map<std::string, GL::Font *> FontMap;
- typedef std::map<std::string, GL::Texture2D *> TextureMap;
- typedef std::map<std::string, Graphic> GraphicMap;
- typedef std::map<StyleId, Style> StyleMap;
-
- FontMap fonts;
- GL::Font *default_font;
- TextureMap textures;
- GraphicMap graphics;
- StyleMap styles;
-
- Resources(const Resources &);
- Resources &operator=(const Resources &);
+ GL::Font *create_font(const std::string &);
+ GL::Texture2D *create_texture(const std::string &);
};
} // namespace GLtk
extern std::istream &operator>>(std::istream &, State &);
} // namespace GLtk
-
-namespace DataFile {
-
-template<>
-struct TypeResolver<GLtk::State> { static const Value::Type type=Value::ENUM; };
-
-} // namespace Parser
} // namespace Msp
#endif
namespace Msp {
namespace GLtk {
-Style::Style(const Resources &r, const string &w, const string &n):
- res(r),
- widget(w),
- name(n),
- font(&res.get_default_font())
+Style::Style():
+ font(0)
{ }
-Style::Loader::Loader(Style &s):
- style(s)
+Style::Loader::Loader(Style &s, Resources &r):
+ style(s),
+ res(r)
{
- add("font", &Loader::font);
+ add("font", &Style::font);
add("font_color", &Loader::font_color);
- add("part", &Loader::part);
-}
-
-void Style::Loader::font(const string &f)
-{
- style.font=&style.res.get_font(f);
+ add("part", &Loader::part);
}
void Style::Loader::font_color(float r, float g, float b)
{
- style.font_color=Color(r, g, b);
+ style.font_color=GL::Color(r, g, b);
}
void Style::Loader::part(const string &n)
{
- Part p(style.res, n);
- load_sub(p);
+ Part p(n);
+ load_sub(p, res);
style.parts.push_back(p);
}
#ifndef MSP_GLTK_STYLE_H_
#define MSP_GLTK_STYLE_H_
+#include <msp/gl/color.h>
#include <msp/gl/font.h>
#include <msp/datafile/loader.h>
-#include "color.h"
#include "part.h"
namespace Msp {
public:
class Loader: public DataFile::Loader
{
- public:
- Loader(Style &);
private:
Style &style;
+ Resources &res;
+
+ public:
+ typedef Resources Collection;
+ Loader(Style &, Resources &);
+ Style &get_object() const { return style; }
+ Resources &get_collection() const { return res; }
+ private:
void font(const std::string &);
void font_color(float, float, float);
void part(const std::string &);
};
- Style(const Resources &, const std::string &, const std::string &);
+ Style();
const GL::Font *get_font() const { return font; }
- const Color &get_font_color() const { return font_color; }
- const PartSeq &get_parts() const { return parts; }
+ const GL::Color &get_font_color() const { return font_color; }
+ const PartSeq &get_parts() const { return parts; }
private:
- const Resources &res;
- std::string widget;
- std::string name;
const GL::Font *font;
- Color font_color;
- PartSeq parts;
+ GL::Color font_color;
+ PartSeq parts;
};
} // namespace GLtk
void Widget::update_style()
{
- style=&res.get_style(get_class(), style_name);
+ string sname=get_class();
+ if(!style_name.empty())
+ {
+ sname+='-';
+ sname+=style_name;
+ }
+ style=&res.get<Style>(sname);
}
void Widget::render_part(const Part &part) const
part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
GL::scale_uniform(font_size);
- const Color &color=style->get_font_color();
+ const GL::Color &color=style->get_font_color();
glColor3f(color.r, color.g, color.b);
font->draw_string(text);
glColor3f(1, 1, 1);