]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Remove dead code from Panel::Child
[libs/gltk.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include "resources.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 Resources::Resources():
10         path("."),
11         default_font(0)
12 {
13         init();
14 }
15
16 Resources::Resources(const FS::Path &fn):
17         path(FS::dirname(fn)),
18         default_font(0)
19 {
20         init();
21
22         DataFile::load(*this, fn.str());
23 }
24
25 void Resources::init()
26 {
27         add_type<Graphic>().keyword("graphic");
28         add_type<GL::Texture2D>().keyword("texture").creator(&Resources::create_texture);
29         add_type<GL::Font>().creator(&Resources::create_font);
30 }
31
32 void Resources::set_path(const FS::Path &p)
33 {
34         /* XXX bad, should change Collection API to allow creators to form paths
35         relative to the datafile location */
36         path = p;
37 }
38
39 const GL::Font &Resources::get_default_font() const
40 {
41         if(!default_font)
42                 throw logic_error("!default_font");
43
44         return *default_font;
45 }
46
47 GL::Font *Resources::create_font(const string &name)
48 {
49         RefPtr<GL::Font> fnt = new GL::Font;
50         DataFile::load(*fnt, (path/name).str(), *this);
51         if(!default_font)
52                 default_font = fnt.get();
53         return fnt.release();
54 }
55
56 GL::Texture2D *Resources::create_texture(const string &name)
57 {
58         RefPtr<GL::Texture2D> tex = new GL::Texture2D;
59         tex->load_image((path/name).str());
60         tex->set_min_filter(GL::LINEAR);
61         return tex.release();
62 }
63
64
65 Resources::Loader::Loader(Resources &r):
66         Collection::Loader(r),
67         res(r)
68 {
69         add("default_font", &Loader::default_font);
70         add("font", &Loader::font);
71         add("style", &Loader::style);
72 }
73
74 void Resources::Loader::default_font(const string &name)
75 {
76         res.default_font = &res.get<GL::Font>(name);
77 }
78
79 void Resources::Loader::font(const string &name)
80 {
81         RefPtr<GL::Font> fnt = new GL::Font;
82         load_sub(*fnt, res);
83         res.add(name, fnt.get());
84         if(!res.default_font)
85                 res.default_font = fnt.get();
86         fnt.release();
87 }
88
89 void Resources::Loader::style(const string &name)
90 {
91         RefPtr<Style> stl = new Style(res);
92         load_sub(*stl, res);
93         res.add(name, stl.get());
94         stl.release();
95 }
96
97 } // namespace GLtk
98 } // namespace Msp