Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / gui / gui.h
blob1b7a3cf21bd7fe1d8a2ca0909db9d5b9a85e5415
1 //
2 // Copyright (C) 2008 by Martin Moracek
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /**
20 * @file gui.h
22 * Ble.
25 #pragma once
27 #include <string>
28 #include <boost/signal.hpp>
29 #include <boost/utility.hpp>
30 #include <boost/smart_ptr.hpp>
32 #include "platform.h"
34 #ifdef PLATFORM_MSVC
35 # include <unordered_map>
36 #else /* PLATFORM_MSVC */
37 # include <tr1/unordered_map>
38 #endif /* PLATFORM_MSVC */
40 #include "math/volumes.h"
41 #include "input/input.h"
43 class TiXmlElement;
45 namespace tre {
47 class Widget;
48 class Application;
49 class Gui;
50 class GuiSkin;
51 class Canvas;
52 class Cursor;
53 class CursorSet;
55 class EventCombiner;
57 typedef boost::shared_ptr<GuiSkin> GuiSkinPtr;
58 typedef boost::shared_ptr<Canvas> CanvasPtr;
59 typedef boost::shared_ptr<CursorSet> CursorSetPtr;
61 typedef boost::shared_ptr<Gui> GuiPtr;
62 typedef boost::weak_ptr<Gui> GuiRef;
64 class GuiFactory {
65 public:
66 GuiPtr CreateInstance(Application & app, const CanvasPtr & canvas,
67 const std::string & filename, const std::string & skin);
70 class Gui : private boost::noncopyable {
71 public:
72 static GuiFactory & Factory(void)
74 static GuiFactory fac;
75 return fac;
78 public:
79 class EventCombiner {
80 public:
81 typedef bool result_type;
83 template<typename InputIterator>
84 bool operator () (InputIterator first, InputIterator last) const
86 while(first != last) {
87 if(*first)
88 return true;
89 ++first;
91 return false;
95 public:
96 // public signals
97 boost::signal<void (uint)> buttonOn;
98 boost::signal<void (uint)> buttonOff;
99 boost::signal<bool (char chr, KeySym sym), EventCombiner> keyOn;
100 boost::signal<bool (char chr, KeySym sym), EventCombiner> keyOff;
101 boost::signal<void (const Vector2f &)> mouseMove;
103 public:
104 Gui(Application & app, const CanvasPtr & cnv);
105 ~Gui();
107 Application & GetApplication(void) {return app_;}
109 // size & scale
110 Vector2f GetSize(void) const {return size_;}
111 Vector2f GetScaleFactor(void) const;
113 void SetCanvas(const CanvasPtr & cnv);
115 const GuiSkinPtr & GetSkin(void) const {return skin_;}
116 const Vector3f GetMousePosition(void) const
117 {return mousePos_.GetWPosition();};
119 // widget queries
120 const Widget * GetRootWidget(void) const {return rootWdg_;}
121 Widget * GetRootWidget(void) {return rootWdg_;}
123 const Widget * FindWidget(const std::string & name) const;
124 Widget * FindWidget(const std::string & name);
126 bool RegisterWidget(Widget * wdg, const std::string & name);
127 void UnregisterWidget(const std::string & name);
129 // skin manipulation
130 bool UseSkin(const std::string & filename);
131 void ResetSkin(void);
133 // cursor manipulation
134 void ShowCursor(bool on);
135 void SetCursor(const std::string & name);
137 // input handling
138 void MouseMove(const Vector2f & abs, const Vector2f & rel);
139 bool ProcessInput(const InputEvent & event);
141 // draws recursively the entire gui
142 void Draw(void);
144 void LoadFromFile(const std::string & filename);
146 // TODO: drag & drop - StartDrag(dragwidget)
148 private:
149 typedef std::tr1::unordered_map<std::string, Widget*> WidgetMap;
151 private:
152 Application & app_;
153 CanvasPtr canvas_;
155 // dimensions & scaling
156 bool scale_; /// gui scales to fit the canvas, keeping it's proportions
157 Vector2f size_;
158 Vector2f scaleBase_;
160 // widget templates & cursor set
161 GuiSkinPtr skin_;
162 CursorSetPtr cursors_;
164 // root widget
165 Widget * rootWdg_;
167 // various states
168 Widget * lastTop_;
169 Transformf mousePos_;
171 // default 2d camera
172 PVolume cam_;
174 // cursor
175 const Cursor * actCursor_;
176 std::string curName_;
177 bool showCursor_;
179 // widget map
180 WidgetMap wdgRegistry_;