1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
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.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef ui_graphical_applicationH
21 #define ui_graphical_applicationH
22 
23 #include <SDL.h>
24 
25 #include <vector>
26 #include <list>
27 #include <memory>
28 
29 #include "input/keyboard/keysequence.h"
30 #include "utility/thread/concurrentqueue.h"
31 #include "utility/signal/signalconnectionmanager.h"
32 #include "input/mouse/mousebuttontype.h"
33 
34 class cMouse;
35 class cKeyboard;
36 class cWidget;
37 class cWindow;
38 class cPosition;
39 class cRunnable;
40 class cShortcut;
41 class cFrameCounter;
42 
43 class cApplication
44 {
45 public:
46 	cApplication();
47 	~cApplication();
48 
49 	void execute();
50 
51 	template<typename WindowType>
52 	WindowType* show (std::shared_ptr<WindowType> window);
53 
54 	/**
55 	 * Marks all windows to be closed that are above the passed one
56 	 * on the window stack.
57 	 *
58 	 * If the passed window is not on the applications window stack
59 	 * all windows will be marked to be closed.
60 	 *
61 	 * @param window The reference window to search for.
62 	 *               This window will not be marked to be closed.
63 	 */
64 	void closeTill (const cWindow& window);
65 
66 	void registerMouse (cMouse& mouse);
67 	void registerKeyboard (cKeyboard& keyboard);
68 
69 	void grapMouseFocus (cWidget& widget);
70 	void releaseMouseFocus (const cWidget& widget);
71 	bool hasMouseFocus (const cWidget& widget) const;
72 	bool hasMouseFocus() const;
73 
74 	void grapKeyFocus (cWidget& widget);
75 	void releaseKeyFocus (const cWidget& widget);
76 	bool hasKeyFocus (const cWidget& widget) const;
77 	bool hasKeyFocus() const;
78 
79 	void addRunnable (std::shared_ptr<cRunnable> runnable);
80 	std::shared_ptr<cRunnable> removeRunnable (const cRunnable& runnable);
81 
82 	cShortcut* addShortcut (std::unique_ptr<cShortcut> shortcut);
83 
84 	cMouse* getActiveMouse();
85 	cKeyboard* getActiveKeyboard();
86 
87 	std::shared_ptr<cFrameCounter> frameCounter;
88 private:
89 	static const size_t maximalShortcutSequenceLength = 4;
90 
91 	std::vector<std::shared_ptr<cWindow>> modalWindows;
92 
93 	cSignalConnectionManager signalConnectionManager;
94 
95 	cKeySequence currentKeySequence;
96 
97 	std::vector<std::unique_ptr<cShortcut>> shortcuts;
98 
99 	cMouse* activeMouse;
100 	cKeyboard* activeKeyboard;
101 
102 	cWidget* keyFocusWidget;
103 	cWidget* mouseFocusWidget;
104 	//cWidget* underMouseWidget;
105 
106 	std::list<std::shared_ptr<cRunnable>> runnables;
107 
108 	bool shouldDrawFramesPerSecond;
109 
110 	void center (cWindow& window);
111 
112 	cWindow* getActiveWindow();
113 
114 	cWidget* getMouseEventFirstTarget (const cPosition& position);
115 
116 	cWidget* getKeyFocusWidget() const;
117 
118 	void mousePressed (cMouse& mouse, eMouseButtonType button);
119 	void mouseReleased (cMouse& mouse, eMouseButtonType button);
120 	void mouseWheelMoved (cMouse& mouse, const cPosition& amount);
121 	void mouseMoved (cMouse& mouse, const cPosition& offset);
122 
123 	void keyPressed (cKeyboard& keyboard, SDL_Keycode key);
124 	void keyReleased (cKeyboard& keyboard, SDL_Keycode key);
125 	void textEntered (cKeyboard& keyboard, const char* text);
126 
127 	void assignKeyFocus (cWidget* widget);
128 
129 	bool hitShortcuts (const cKeySequence& keySequence);
130 
131 	void drawFramesPerSecond (unsigned int fps, bool draw = true);
132 };
133 
134 //------------------------------------------------------------------------------
135 template<typename WindowType>
show(std::shared_ptr<WindowType> window)136 WindowType* cApplication::show (std::shared_ptr<WindowType> window)
137 {
138 	if (window == nullptr) return nullptr;
139 
140 	if (window->wantsCentered())
141 	{
142 		center (*window);
143 	}
144 
145 	modalWindows.push_back (std::move (window));
146 
147 	return static_cast<WindowType*> (modalWindows.back().get());
148 }
149 
150 #endif // ui_graphical_applicationH
151