]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / resources.cpp
index aad671c393729adc4608d3d6024378bcf44bbe35..dc678a9480d74aa276039d65b5b0b5cf1c0ed8d5 100644 (file)
@@ -1,4 +1,6 @@
-#include <msp/core/error.h>
+#include <msp/fs/utils.h>
+#include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
 #include "resources.h"
 
 using namespace std;
@@ -6,116 +8,116 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Resources::Resources():
-       default_font(0)
-{ }
-
-Resources::~Resources()
+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_type<Graphic>().keyword("graphic");
+       add_type<GL::Module>().creator([this](const string &n){ return create_module(n); });
+       add_type<GL::Sampler>().creator([this](const string &n){ return create_sampler(n); });
+       add_type<GL::Program>().creator([this](const string &n){ return create_program(n); });
+       add_type<GL::Texture2D>().keyword("texture").creator([this](const string &n){ return create_texture(n); });
+       add_type<GL::Font>().keyword("font");
+       add_type<Style>().keyword("style");
 }
 
-const GL::Font &Resources::get_font(const string &name) const
+Resources::Resources(const FS::Path &fn):
+       Resources()
 {
-       FontMap::const_iterator i=fonts.find(name);
-       if(i==fonts.end())
-               throw KeyError("Unknown font "+name);
+       dir_src = make_unique<DataFile::DirectorySource>();
+       dir_src->add_directory(FS::dirname(fn));
+       add_source(*dir_src);
 
-       return *i->second;
+       DataFile::load(*this, fn.str());
 }
 
 const GL::Font &Resources::get_default_font() const
 {
        if(!default_font)
-               throw InvalidState("No default font");
+               throw logic_error("!default_font");
 
        return *default_font;
 }
 
-const GL::Texture2D &Resources::get_texture(const string &name) const
+GL::Module *Resources::create_module(const string &name)
 {
-       TextureMap::const_iterator i=textures.find(name);
-       if(i==textures.end())
-               throw KeyError("Unknown texture "+name);
+       if(name!="ui.glsl")
+               return nullptr;
 
-       return *i->second;
-}
+       unique_ptr<GL::Module> mod;
+       if(GL::get_backend_api()==GL::VULKAN)
+               mod = make_unique<GL::SpirVModule>();
+       else
+               mod = make_unique<GL::GlslModule>();
 
-const Graphic &Resources::get_graphic(const string &name) const
-{
-       GraphicMap::const_iterator i=graphics.find(name);
-       if(i==graphics.end())
-               throw KeyError("Unknown graphic "+name);
+       mod->set_source("import msp_interface; import common;\n"
+               "uniform sampler2D ui_tex;\n"
+               "#pragma MSP stage(fragment)\n"
+               "void main() { frag_color = texture(ui_tex, texcoord.xy)*color; }\n");
 
-       return i->second;
+       return mod.release();
 }
 
-const Style &Resources::get_style(const string &wdg, const string &name) const
+GL::Program *Resources::create_program(const string &name)
 {
-       StyleMap::const_iterator i=styles.find(StyleId(wdg, name));
-       if(i==styles.end())
-               throw KeyError("Unknown style "+name+" for widget "+wdg);
+       if(name!="ui.shader")
+               return nullptr;
 
-       return i->second;
+       return new GL::Program(get<GL::Module>("ui.glsl"));
 }
 
-
-Resources::Loader::Loader(Resources &r):
-       res(r)
+GL::Sampler *Resources::create_sampler(const string &name)
 {
-       add("font",    &Loader::font);
-       add("texture", &Loader::texture);
-       add("graphic", &Loader::graphic);
-       add("style",   &Loader::style);
+       static const Regex r_name("^(linear|nearest)_clamp.samp$");
+       if(RegMatch m = r_name.match(name))
+       {
+               unique_ptr<GL::Sampler> sampler = make_unique<GL::Sampler>();
+               sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
+               sampler->set_wrap(GL::CLAMP_TO_EDGE);
+               return sampler.release();
+       }
+
+       return nullptr;
 }
 
-void Resources::Loader::font(const string &fn)
+GL::Texture2D *Resources::create_texture(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()));
-       if(!res.default_font)
-               res.default_font=fnt.get();
-       fnt.release();
+       string ext = FS::extpart(name);
+       if(ext==".png" || ext==".jpg")
+               if(auto io = unique_ptr<IO::Seekable>(open_raw(name)))
+               {
+                       Graphics::Image image;
+                       image.load_io(*io);
+                       io.reset();
+
+                       unique_ptr<GL::Texture2D> tex = make_unique<GL::Texture2D>();
+                       tex->image(image);
+                       return tex.release();
+               }
+
+       return nullptr;
 }
 
-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)
+Resources::Loader::Loader(Resources &r):
+       Collection::Loader(r),
+       res(r)
 {
-       Graphic graph(res, n);
-       load_sub(graph);
-       if(!graph.get_texture())
-               throw Exception("Graphic without texture");
-
-       res.graphics.insert(GraphicMap::value_type(n, graph));
+       add("default_font", &Loader::default_font);
+       add("font", &Loader::font);
 }
 
-void Resources::Loader::style(const string &w, const string &n)
+void Resources::Loader::default_font(const string &name)
 {
-       Style stl(res, w, n);
-       load_sub(stl);
-
-       res.styles.insert(StyleMap::value_type(StyleId(w, n), stl));
+       res.default_font = &res.get<GL::Font>(name);
 }
 
-
-bool Resources::StyleId::operator<(const StyleId &other) const
+void Resources::Loader::font(const string &name)
 {
-       if(widget<other.widget)
-               return true;
-       return widget==other.widget && name<other.name;
+       unique_ptr<GL::Font> fnt = make_unique<GL::Font>();
+       load_sub(*fnt, res);
+       res.add(name, fnt.get());
+       if(!res.default_font)
+               res.default_font = fnt.get();
+       fnt.release();
 }
 
 } // namespace GLtk