1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __C_GUI_ENVIRONMENT_H_INCLUDED__
6 #define __C_GUI_ENVIRONMENT_H_INCLUDED__
7 
8 #include "IrrCompileConfig.h"
9 #ifdef _IRR_COMPILE_WITH_GUI_
10 
11 #include "IGUIEnvironment.h"
12 #include "IGUIElement.h"
13 #include "irrArray.h"
14 #include "IFileSystem.h"
15 #include "IOSOperator.h"
16 
17 namespace irr
18 {
19 namespace io
20 {
21 	class IXMLWriter;
22 }
23 namespace gui
24 {
25 
26 class CGUIEnvironment : public IGUIEnvironment, public IGUIElement
27 {
28 public:
29 
30 	//! constructor
31 	CGUIEnvironment(io::IFileSystem* fs, video::IVideoDriver* driver, IOSOperator* op);
32 
33 	//! destructor
34 	virtual ~CGUIEnvironment();
35 
36 	//! draws all gui elements
37 	virtual void drawAll();
38 
39 	//! returns the current video driver
40 	virtual video::IVideoDriver* getVideoDriver() const;
41 
42 	//! returns pointer to the filesystem
43 	virtual io::IFileSystem* getFileSystem() const;
44 
45 	//! returns a pointer to the OS operator
46 	virtual IOSOperator* getOSOperator() const;
47 
48 	//! posts an input event to the environment
49 	virtual bool postEventFromUser(const SEvent& event);
50 
51 	//! This sets a new event receiver for gui events. Usually you do not have to
52 	//! use this method, it is used by the internal engine.
53 	virtual void setUserEventReceiver(IEventReceiver* evr);
54 
55 	//! removes all elements from the environment
56 	virtual void clear();
57 
58 	//! called if an event happened.
59 	virtual bool OnEvent(const SEvent& event);
60 
61 	//! returns the current gui skin
62 	virtual IGUISkin* getSkin() const;
63 
64 	//! Sets a new GUI Skin
65 	virtual void setSkin(IGUISkin* skin);
66 
67 	//! Creates a new GUI Skin based on a template.
68 	/** \return Returns a pointer to the created skin.
69 	If you no longer need the skin, you should call IGUISkin::drop().
70 	See IReferenceCounted::drop() for more information. */
71 	virtual IGUISkin* createSkin(EGUI_SKIN_TYPE type);
72 
73 	//! Creates the image list from the given texture.
74 	virtual IGUIImageList* createImageList( video::ITexture* texture,
75 					core::dimension2d<s32> imageSize, bool useAlphaChannel );
76 
77 	//! returns the font
78 	virtual IGUIFont* getFont(const io::path& filename);
79 
80 	//! add an externally loaded font
81 	virtual IGUIFont* addFont(const io::path& name, IGUIFont* font);
82 
83 	//! remove loaded font
84 	virtual void removeFont(IGUIFont* font);
85 
86 	//! returns default font
87 	virtual IGUIFont* getBuiltInFont() const;
88 
89 	//! returns the sprite bank
90 	virtual IGUISpriteBank* getSpriteBank(const io::path& filename);
91 
92 	//! returns the sprite bank
93 	virtual IGUISpriteBank* addEmptySpriteBank(const io::path& name);
94 
95 	//! adds an button. The returned pointer must not be dropped.
96 	virtual IGUIButton* addButton(const core::rect<s32>& rectangle, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0,const wchar_t* tooltiptext = 0);
97 
98 	//! adds a window. The returned pointer must not be dropped.
99 	virtual IGUIWindow* addWindow(const core::rect<s32>& rectangle, bool modal = false,
100 		const wchar_t* text=0, IGUIElement* parent=0, s32 id=-1);
101 
102 	//! adds a modal screen. The returned pointer must not be dropped.
103 	virtual IGUIElement* addModalScreen(IGUIElement* parent);
104 
105 	//! Adds a message box.
106 	virtual IGUIWindow* addMessageBox(const wchar_t* caption, const wchar_t* text=0,
107 		bool modal = true, s32 flag = EMBF_OK, IGUIElement* parent=0, s32 id=-1, video::ITexture* image=0);
108 
109 	//! adds a scrollbar. The returned pointer must not be dropped.
110 	virtual IGUIScrollBar* addScrollBar(bool horizontal, const core::rect<s32>& rectangle,
111 		IGUIElement* parent=0, s32 id=-1);
112 
113 	//! Adds an image element.
114 	virtual IGUIImage* addImage(video::ITexture* image, core::position2d<s32> pos,
115 		bool useAlphaChannel=true, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0);
116 
117 	//! adds an image. The returned pointer must not be dropped.
118 	virtual IGUIImage* addImage(const core::rect<s32>& rectangle,
119 		IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0, bool useAlphaChannel=true);
120 
121 	//! adds a checkbox
122 	virtual IGUICheckBox* addCheckBox(bool checked, const core::rect<s32>& rectangle, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0);
123 
124 	//! adds a list box
125 	virtual IGUIListBox* addListBox(const core::rect<s32>& rectangle,
126 		IGUIElement* parent=0, s32 id=-1, bool drawBackground=false);
127 
128 	//! adds a tree view
129 	virtual IGUITreeView* addTreeView(const core::rect<s32>& rectangle,
130 		IGUIElement* parent=0, s32 id=-1, bool drawBackground=false,
131 		bool scrollBarVertical = true, bool scrollBarHorizontal = false);
132 
133 	//! adds an mesh viewer. The returned pointer must not be dropped.
134 	virtual IGUIMeshViewer* addMeshViewer(const core::rect<s32>& rectangle, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0);
135 
136 	//! Adds a file open dialog.
137 	virtual IGUIFileOpenDialog* addFileOpenDialog(const wchar_t* title = 0,
138 			bool modal=true, IGUIElement* parent=0, s32 id=-1,
139 			bool restoreCWD=false, io::path::char_type* startDir=0);
140 
141 	//! Adds a color select dialog.
142 	virtual IGUIColorSelectDialog* addColorSelectDialog(const wchar_t* title = 0, bool modal=true, IGUIElement* parent=0, s32 id=-1);
143 
144 	//! adds a static text. The returned pointer must not be dropped.
145 	virtual IGUIStaticText* addStaticText(const core::stringw& text, const core::rect<s32>& rectangle,
146 		bool border=false, bool wordWrap=true, IGUIElement* parent=0, s32 id=-1, bool drawBackground = false);
147 
148 	//! Adds an edit box. The returned pointer must not be dropped.
149 	virtual IGUIEditBox* addEditBox(const wchar_t* text, const core::rect<s32>& rectangle,
150 		bool border=false, IGUIElement* parent=0, s32 id=-1);
151 
152 	//! Adds a spin box to the environment
153 	virtual IGUISpinBox* addSpinBox(const wchar_t* text, const core::rect<s32>& rectangle,
154 		bool border=false,IGUIElement* parent=0, s32 id=-1);
155 
156 	//! Adds a tab control to the environment.
157 	virtual IGUITabControl* addTabControl(const core::rect<s32>& rectangle,
158 		IGUIElement* parent=0, bool fillbackground=false, bool border=true, s32 id=-1);
159 
160 	//! Adds tab to the environment.
161 	virtual IGUITab* addTab(const core::rect<s32>& rectangle,
162 		IGUIElement* parent=0, s32 id=-1);
163 
164 	//! Adds a context menu to the environment.
165 	virtual IGUIContextMenu* addContextMenu(const core::rect<s32>& rectangle,
166 		IGUIElement* parent=0, s32 id=-1);
167 
168 	//! Adds a menu to the environment.
169 	virtual IGUIContextMenu* addMenu(IGUIElement* parent=0, s32 id=-1);
170 
171 	//! Adds a toolbar to the environment. It is like a menu is always placed on top
172 	//! in its parent, and contains buttons.
173 	virtual IGUIToolBar* addToolBar(IGUIElement* parent=0, s32 id=-1);
174 
175 	//! Adds a combo box to the environment.
176 	virtual IGUIComboBox* addComboBox(const core::rect<s32>& rectangle,
177 		IGUIElement* parent=0, s32 id=-1);
178 
179 	//! Adds a table element.
180 	virtual IGUITable* addTable(const core::rect<s32>& rectangle,
181 		IGUIElement* parent=0, s32 id=-1, bool drawBackground=false);
182 
183 	//! sets the focus to an element
184 	virtual bool setFocus(IGUIElement* element);
185 
186 	//! removes the focus from an element
187 	virtual bool removeFocus(IGUIElement* element);
188 
189 	//! Returns if the element has focus
190 	virtual bool hasFocus(IGUIElement* element) const;
191 
192 	//! Returns the element with the focus
193 	virtual IGUIElement* getFocus() const;
194 
195 	//! Returns the element last known to be under the mouse
196 	virtual IGUIElement* getHovered() const;
197 
198 	//! Adds an element for fading in or out.
199 	virtual IGUIInOutFader* addInOutFader(const core::rect<s32>* rectangle=0, IGUIElement* parent=0, s32 id=-1);
200 
201 	//! Returns the root gui element.
202 	virtual IGUIElement* getRootGUIElement();
203 
204 	virtual void OnPostRender( u32 time );
205 
206 	//! Returns the default element factory which can create all built in elements
207 	virtual IGUIElementFactory* getDefaultGUIElementFactory() const;
208 
209 	//! Adds an element factory to the gui environment.
210 	/** Use this to extend the gui environment with new element types which it should be
211 	able to create automaticly, for example when loading data from xml files. */
212 	virtual void registerGUIElementFactory(IGUIElementFactory* factoryToAdd);
213 
214 	//! Returns amount of registered scene node factories.
215 	virtual u32 getRegisteredGUIElementFactoryCount() const;
216 
217 	//! Returns a scene node factory by index
218 	virtual IGUIElementFactory* getGUIElementFactory(u32 index) const;
219 
220 	//! Adds a GUI Element by its name
221 	virtual IGUIElement* addGUIElement(const c8* elementName, IGUIElement* parent=0);
222 
223 	//! Saves the current gui into a file.
224 	/** \param filename: Name of the file.
225 	\param start: The element to start saving from.
226 	if not specified, the root element will be used */
227 	virtual bool saveGUI( const io::path& filename, IGUIElement* start=0);
228 
229 	//! Saves the current gui into a file.
230 	/** \param file: The file to save the GUI to.
231 	\param start: The element to start saving from.
232 	if not specified, the root element will be used */
233 	virtual bool saveGUI(io::IWriteFile* file, IGUIElement* start=0);
234 
235 	//! Loads the gui. Note that the current gui is not cleared before.
236 	/** \param filename: Name of the file.
237 	\param parent: The parent of all loaded GUI elements,
238 	if not specified, the root element will be used */
239 	virtual bool loadGUI(const io::path& filename, IGUIElement* parent=0);
240 
241 	//! Loads the gui. Note that the current gui is not cleared before.
242 	/** \param file: IReadFile to load the GUI from
243 	\param parent: The parent of all loaded GUI elements,
244 	if not specified, the root element will be used */
245 	virtual bool loadGUI(io::IReadFile* file, IGUIElement* parent=0);
246 
247 	//! Writes attributes of the environment
248 	virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
249 
250 	//! Reads attributes of the environment.
251 	virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
252 
253 	//! writes an element
254 	virtual void writeGUIElement(io::IXMLWriter* writer, IGUIElement* node);
255 
256 	//! reads an element
257 	virtual void readGUIElement(io::IXMLReader* reader, IGUIElement* node);
258 
259 	virtual void removeHovered(IGUIElement* element);
260 private:
261 
262 	IGUIElement* getNextElement(bool reverse=false, bool group=false);
263 
264 	void updateHoveredElement(core::position2d<s32> mousePos);
265 
266 	struct SFont
267 	{
268 		io::SNamedPath NamedPath;
269 		IGUIFont* Font;
270 
271 		bool operator < (const SFont& other) const
272 		{
273 			return (NamedPath < other.NamedPath);
274 		}
275 	};
276 
277 	struct SSpriteBank
278 	{
279 		io::SNamedPath NamedPath;
280 		IGUISpriteBank* Bank;
281 
282 		bool operator < (const SSpriteBank& other) const
283 		{
284 			return (NamedPath < other.NamedPath);
285 		}
286 	};
287 
288 	struct SToolTip
289 	{
290 		IGUIStaticText* Element;
291 		u32 LastTime;
292 		u32 EnterTime;
293 		u32 LaunchTime;
294 		u32 RelaunchTime;
295 	};
296 
297 	SToolTip ToolTip;
298 
299 	core::array<IGUIElementFactory*> GUIElementFactoryList;
300 
301 	core::array<SFont> Fonts;
302 	core::array<SSpriteBank> Banks;
303 	video::IVideoDriver* Driver;
304 	IGUIElement* Hovered;
305 	IGUIElement* HoveredNoSubelement;	// subelements replaced by their parent, so you only have 'real' elements here
306 	IGUIElement* Focus;
307 	core::position2d<s32> LastHoveredMousePos;
308 	IGUISkin* CurrentSkin;
309 	io::IFileSystem* FileSystem;
310 	IEventReceiver* UserReceiver;
311 	IOSOperator* Operator;
312 	static const io::path DefaultFontName;
313 };
314 
315 } // end namespace gui
316 } // end namespace irr
317 
318 #endif // _IRR_COMPILE_WITH_GUI_
319 
320 #endif // __C_GUI_ENVIRONMENT_H_INCLUDED__
321 
322 
323