From 86d9edd57268b4ebdc92844d65db4469d516b1d1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 20 Jul 2008 19:11:43 +0000 Subject: [PATCH] Add UserInterface class --- source/userinterface.cpp | 36 +++++++++++++++++++++ source/userinterface.h | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 source/userinterface.cpp create mode 100644 source/userinterface.h diff --git a/source/userinterface.cpp b/source/userinterface.cpp new file mode 100644 index 0000000..7f976da --- /dev/null +++ b/source/userinterface.cpp @@ -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 index 0000000..c821320 --- /dev/null +++ b/source/userinterface.h @@ -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 widgets; + Root root; + Logic logic; + +public: + UserInterface(Resources &, Graphics::Window &); + Root &get_root() { return root; } + const Logic &get_logic() const { return logic; } + + template + W &get_widget(const std::string &n) const + { + std::map::const_iterator i=widgets.find(n); + if(i==widgets.end()) + throw KeyError("Unknown widget", n); + + W *w=dynamic_cast(i->second); + if(!w) + throw Exception("Widget type mismatch"); + + return *w; + } + + template + void get_widget(const std::string &n, W *&w) const + { + w=&get_widget(n); + } +}; + +} // namespace GLtk +} // namespace Msp + +#endif -- 2.43.0