]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Implement autosize() method for most widgets
[libs/gltk.git] / source / resources.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/except.h>
9 #include <msp/fs/utils.h>
10 #include "resources.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GLtk {
16
17 Resources::Resources():
18         path("."),
19         default_font(0)
20 {
21         init();
22 }
23
24 Resources::Resources(const FS::Path &fn):
25         path(FS::dirname(fn)),
26         default_font(0)
27 {
28         init();
29
30         DataFile::load(*this, fn.str());
31 }
32
33 void Resources::init()
34 {
35         add_keyword<Graphic>("graphic");
36         add_keyword<GL::Texture2D>("texture");
37
38         add_creator(&Resources::create_font);
39         add_creator(&Resources::create_texture);
40 }
41
42 void Resources::set_path(const FS::Path &p)
43 {
44         /* XXX bad, should change Collection API to allow creators to form paths
45         relative to the datafile location */
46         path = p;
47 }
48
49 const GL::Font &Resources::get_default_font() const
50 {
51         if(!default_font)
52                 throw InvalidState("No default font");
53
54         return *default_font;
55 }
56
57 GL::Font *Resources::create_font(const string &name)
58 {
59         RefPtr<GL::Font> fnt = new GL::Font;
60         DataFile::load(*fnt, (path/name).str(), *this);
61         if(!default_font)
62                 default_font = fnt.get();
63         return fnt.release();
64 }
65
66 GL::Texture2D *Resources::create_texture(const string &name)
67 {
68         RefPtr<GL::Texture2D> tex = new GL::Texture2D;
69         tex->load_image((path/name).str());
70         tex->set_min_filter(GL::LINEAR);
71         return tex.release();
72 }
73
74
75 Resources::Loader::Loader(Resources &r):
76         Collection::Loader(r),
77         res(r)
78 {
79         add("default_font", &Loader::default_font);
80         add("font", &Loader::font);
81         add("style", &Loader::style);
82 }
83
84 void Resources::Loader::default_font(const string &name)
85 {
86         res.default_font = res.get<GL::Font>(name);
87 }
88
89 void Resources::Loader::font(const string &name)
90 {
91         RefPtr<GL::Font> fnt = new GL::Font;
92         load_sub(*fnt, res);
93         res.add(name, fnt.get());
94         if(!res.default_font)
95                 res.default_font = fnt.get();
96         fnt.release();
97 }
98
99 void Resources::Loader::style(const string &name)
100 {
101         RefPtr<Style> stl = new Style(res);
102         load_sub(*stl, res);
103         res.add(name, stl.get());
104         stl.release();
105 }
106
107 } // namespace GLtk
108 } // namespace Msp