]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add UserInterface class
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Jul 2008 19:11:43 +0000 (19:11 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Jul 2008 19:11:43 +0000 (19:11 +0000)
source/userinterface.cpp [new file with mode: 0644]
source/userinterface.h [new file with mode: 0644]

diff --git a/source/userinterface.cpp b/source/userinterface.cpp
new file mode 100644 (file)
index 0000000..7f976da
--- /dev/null
@@ -0,0 +1,36 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "userinterface.h"
+
+namespace Msp {
+namespace GLtk {
+
+UserInterface::UserInterface(Resources &r, Graphics::Window &w):
+       root(r, w)
+{ }
+
+
+UserInterface::Loader::Loader(UserInterface &u):
+       ui(u)
+{
+       add("logic", &Loader::logic);
+       add("root",  &Loader::root);
+}
+
+void UserInterface::Loader::logic()
+{
+       load_sub(ui.logic, ui.widgets);
+}
+
+void UserInterface::Loader::root()
+{
+       load_sub(ui.root, ui.widgets);
+}
+
+} // namespace GLtk
+} // namespace Msp
diff --git a/source/userinterface.h b/source/userinterface.h
new file mode 100644 (file)
index 0000000..c821320
--- /dev/null
@@ -0,0 +1,70 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef MSP_GLTK_USERINTERFACE_H_
+#define MSP_GLTK_USERINTERFACE_H_
+
+#include "logic.h"
+#include "root.h"
+
+namespace Msp {
+namespace GLtk {
+
+/**
+Encapsulates a Root widget and Logic associated with it.  Allows looking up
+widgets by name.
+*/
+class UserInterface
+{
+public:
+       class Loader: public DataFile::Loader
+       {
+       private:
+               UserInterface &ui;
+
+       public:
+               Loader(UserInterface &);
+       private:
+               void logic();
+               void root();
+       };
+
+private:
+       std::map<std::string, Widget *> widgets;
+       Root root;
+       Logic logic;
+
+public:
+       UserInterface(Resources &, Graphics::Window &);
+       Root &get_root() { return root; }
+       const Logic &get_logic() const { return logic; }
+
+       template<typename W>
+       W &get_widget(const std::string &n) const
+       {
+               std::map<std::string, Widget *>::const_iterator i=widgets.find(n);
+               if(i==widgets.end())
+                       throw KeyError("Unknown widget", n);
+
+               W *w=dynamic_cast<W *>(i->second);
+               if(!w)
+                       throw Exception("Widget type mismatch");
+
+               return *w;
+       }
+
+       template<typename W>
+       void get_widget(const std::string &n, W *&w) const
+       {
+               w=&get_widget<W>(n);
+       }
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif