IGUIEnvironment.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #pragma once
  5. #include "IReferenceCounted.h"
  6. #include "IGUISkin.h"
  7. #include "rect.h"
  8. #include "EFocusFlags.h"
  9. #include "IEventReceiver.h"
  10. #include "path.h"
  11. namespace irr
  12. {
  13. class IOSOperator;
  14. class IEventReceiver;
  15. namespace io
  16. {
  17. class IReadFile;
  18. class IWriteFile;
  19. class IFileSystem;
  20. } // end namespace io
  21. namespace video
  22. {
  23. class IVideoDriver;
  24. class ITexture;
  25. } // end namespace video
  26. namespace gui
  27. {
  28. class IGUIElement;
  29. class IGUIFont;
  30. class IGUISpriteBank;
  31. class IGUIScrollBar;
  32. class IGUIImage;
  33. class IGUICheckBox;
  34. class IGUIListBox;
  35. class IGUIImageList;
  36. class IGUIFileOpenDialog;
  37. class IGUIStaticText;
  38. class IGUIEditBox;
  39. class IGUITabControl;
  40. class IGUITab;
  41. class IGUIComboBox;
  42. class IGUIButton;
  43. class IGUIWindow;
  44. //! GUI Environment. Used as factory and manager of all other GUI elements.
  45. /** \par This element can create the following events of type EGUI_EVENT_TYPE (which are passed on to focused sub-elements):
  46. \li EGET_ELEMENT_FOCUS_LOST
  47. \li EGET_ELEMENT_FOCUSED
  48. \li EGET_ELEMENT_LEFT
  49. \li EGET_ELEMENT_HOVERED
  50. */
  51. class IGUIEnvironment : public virtual IReferenceCounted
  52. {
  53. public:
  54. //! Draws all gui elements by traversing the GUI environment starting at the root node.
  55. /** \param When true ensure the GuiEnvironment (aka the RootGUIElement) has the same size as the current driver screensize.
  56. Can be set to false to control that size yourself, p.E when not the full size should be used for UI. */
  57. virtual void drawAll(bool useScreenSize = true) = 0;
  58. //! Sets the focus to an element.
  59. /** Causes a EGET_ELEMENT_FOCUS_LOST event followed by a
  60. EGET_ELEMENT_FOCUSED event. If someone absorbed either of the events,
  61. then the focus will not be changed.
  62. \param element Pointer to the element which shall get the focus.
  63. \return True on success, false on failure */
  64. virtual bool setFocus(IGUIElement *element) = 0;
  65. //! Returns the element which holds the focus.
  66. /** \return Pointer to the element with focus. */
  67. virtual IGUIElement *getFocus() const = 0;
  68. //! Returns the element which was last under the mouse cursor
  69. /** NOTE: This information is updated _after_ the user-eventreceiver
  70. received it's mouse-events. To find the hovered element while catching
  71. mouse events you have to use instead:
  72. IGUIEnvironment::getRootGUIElement()->getElementFromPoint(mousePos);
  73. \return Pointer to the element under the mouse. */
  74. virtual IGUIElement *getHovered() const = 0;
  75. //! Removes the focus from an element.
  76. /** Causes a EGET_ELEMENT_FOCUS_LOST event. If the event is absorbed
  77. then the focus will not be changed.
  78. \param element Pointer to the element which shall lose the focus.
  79. \return True on success, false on failure */
  80. virtual bool removeFocus(IGUIElement *element) = 0;
  81. //! Returns whether the element has focus
  82. /** \param element Pointer to the element which is tested.
  83. \param checkSubElements When true and focus is on a sub-element of element then it will still count as focused and return true
  84. \return True if the element has focus, else false. */
  85. virtual bool hasFocus(const IGUIElement *element, bool checkSubElements = false) const = 0;
  86. //! Returns the current video driver.
  87. /** \return Pointer to the video driver. */
  88. virtual video::IVideoDriver *getVideoDriver() const = 0;
  89. //! Returns the file system.
  90. /** \return Pointer to the file system. */
  91. virtual io::IFileSystem *getFileSystem() const = 0;
  92. //! returns a pointer to the OS operator
  93. /** \return Pointer to the OS operator. */
  94. virtual IOSOperator *getOSOperator() const = 0;
  95. //! Removes all elements from the environment.
  96. virtual void clear() = 0;
  97. //! Posts an input event to the environment.
  98. /** Usually you do not have to
  99. use this method, it is used by the engine internally.
  100. \param event The event to post.
  101. \return True if succeeded, else false. */
  102. virtual bool postEventFromUser(const SEvent &event) = 0;
  103. //! This sets a new event receiver for gui events.
  104. /** Usually you do not have to
  105. use this method, it is used by the engine internally.
  106. \param evr Pointer to the new receiver. */
  107. virtual void setUserEventReceiver(IEventReceiver *evr) = 0;
  108. //! Returns pointer to the current gui skin.
  109. /** \return Pointer to the GUI skin. */
  110. virtual IGUISkin *getSkin() const = 0;
  111. //! Sets a new GUI Skin
  112. /** You can use this to change the appearance of the whole GUI
  113. Environment. You can set one of the built-in skins or implement your
  114. own class derived from IGUISkin and enable it using this method.
  115. To set for example the built-in Windows classic skin, use the following
  116. code:
  117. \code
  118. gui::IGUISkin* newskin = environment->createSkin(gui::EGST_WINDOWS_CLASSIC);
  119. environment->setSkin(newskin);
  120. newskin->drop();
  121. \endcode
  122. \param skin New skin to use.
  123. */
  124. virtual void setSkin(IGUISkin *skin) = 0;
  125. //! Creates a new GUI Skin based on a template.
  126. /** Use setSkin() to set the created skin.
  127. \param type The type of the new skin.
  128. \return Pointer to the created skin.
  129. If you no longer need it, you should call IGUISkin::drop().
  130. See IReferenceCounted::drop() for more information. */
  131. virtual IGUISkin *createSkin(EGUI_SKIN_TYPE type) = 0;
  132. //! Creates the image list from the given texture.
  133. /** \param texture Texture to split into images
  134. \param imageSize Dimension of each image
  135. \param useAlphaChannel Flag whether alpha channel of the texture should be honored.
  136. \return Pointer to the font. Returns 0 if the font could not be loaded.
  137. This pointer should not be dropped. See IReferenceCounted::drop() for
  138. more information. */
  139. virtual IGUIImageList *createImageList(video::ITexture *texture,
  140. core::dimension2d<s32> imageSize,
  141. bool useAlphaChannel) = 0;
  142. //! Returns pointer to the font with the specified filename.
  143. /** Loads the font if it was not loaded before.
  144. \param filename Filename of the Font.
  145. \return Pointer to the font. Returns 0 if the font could not be loaded.
  146. This pointer should not be dropped. See IReferenceCounted::drop() for
  147. more information. */
  148. virtual IGUIFont *getFont(const io::path &filename) = 0;
  149. //! Adds an externally loaded font to the font list.
  150. /** This method allows to attach an already loaded font to the list of
  151. existing fonts. The font is grabbed if non-null and adding was successful.
  152. \param name Name the font should be stored as.
  153. \param font Pointer to font to add.
  154. \return Pointer to the font stored. This can differ from given parameter if the name previously existed. */
  155. virtual IGUIFont *addFont(const io::path &name, IGUIFont *font) = 0;
  156. //! remove loaded font
  157. virtual void removeFont(IGUIFont *font) = 0;
  158. //! Returns the default built-in font.
  159. /** \return Pointer to the default built-in font.
  160. This pointer should not be dropped. See IReferenceCounted::drop() for
  161. more information. */
  162. virtual IGUIFont *getBuiltInFont() const = 0;
  163. //! Returns pointer to the sprite bank which was added with addEmptySpriteBank
  164. /** TODO: This should load files in the future, but not implemented so far.
  165. \param filename Name of a spritebank added with addEmptySpriteBank
  166. \return Pointer to the sprite bank. Returns 0 if it could not be loaded.
  167. This pointer should not be dropped. See IReferenceCounted::drop() for more information. */
  168. virtual IGUISpriteBank *getSpriteBank(const io::path &filename) = 0;
  169. //! Adds an empty sprite bank to the manager
  170. /** \param name Name of the new sprite bank.
  171. \return Pointer to the sprite bank.
  172. This pointer should not be dropped. See IReferenceCounted::drop() for more information. */
  173. virtual IGUISpriteBank *addEmptySpriteBank(const io::path &name) = 0;
  174. //! Returns the root gui element.
  175. /** This is the first gui element, the (direct or indirect) parent of all
  176. other gui elements. It is a valid IGUIElement, with dimensions the same
  177. size as the screen.
  178. \return Pointer to the root element of the GUI. The returned pointer
  179. should not be dropped. See IReferenceCounted::drop() for more
  180. information. */
  181. virtual IGUIElement *getRootGUIElement() = 0;
  182. //! Adds a button element.
  183. /** \param rectangle Rectangle specifying the borders of the button.
  184. \param parent Parent gui element of the button.
  185. \param id Id with which the gui element can be identified.
  186. \param text Text displayed on the button.
  187. \param tooltiptext Text displayed in the tooltip.
  188. \return Pointer to the created button. Returns 0 if an error occurred.
  189. This pointer should not be dropped. See IReferenceCounted::drop() for
  190. more information. */
  191. virtual IGUIButton *addButton(const core::rect<s32> &rectangle,
  192. IGUIElement *parent = 0, s32 id = -1, const wchar_t *text = 0, const wchar_t *tooltiptext = 0) = 0;
  193. //! Adds a scrollbar.
  194. /** \param horizontal Specifies if the scroll bar is drawn horizontal
  195. or vertical.
  196. \param rectangle Rectangle specifying the borders of the scrollbar.
  197. \param parent Parent gui element of the scroll bar.
  198. \param id Id to identify the gui element.
  199. \return Pointer to the created scrollbar. Returns 0 if an error
  200. occurred. This pointer should not be dropped. See
  201. IReferenceCounted::drop() for more information. */
  202. virtual IGUIScrollBar *addScrollBar(bool horizontal, const core::rect<s32> &rectangle,
  203. IGUIElement *parent = 0, s32 id = -1) = 0;
  204. //! Adds an image element.
  205. /** \param image Image to be displayed.
  206. \param pos Position of the image. The width and height of the image is
  207. taken from the image.
  208. \param useAlphaChannel Sets if the image should use the alpha channel
  209. of the texture to draw itself.
  210. \param parent Parent gui element of the image.
  211. \param id Id to identify the gui element.
  212. \param text Title text of the image (not displayed).
  213. \return Pointer to the created image element. Returns 0 if an error
  214. occurred. This pointer should not be dropped. See
  215. IReferenceCounted::drop() for more information. */
  216. virtual IGUIImage *addImage(video::ITexture *image, core::position2d<s32> pos,
  217. bool useAlphaChannel = true, IGUIElement *parent = 0, s32 id = -1, const wchar_t *text = 0) = 0;
  218. //! Adds an image element.
  219. /** Use IGUIImage::setImage later to set the image to be displayed.
  220. \param rectangle Rectangle specifying the borders of the image.
  221. \param parent Parent gui element of the image.
  222. \param id Id to identify the gui element.
  223. \param text Title text of the image (not displayed).
  224. \param useAlphaChannel Sets if the image should use the alpha channel
  225. of the texture to draw itself.
  226. \return Pointer to the created image element. Returns 0 if an error
  227. occurred. This pointer should not be dropped. See
  228. IReferenceCounted::drop() for more information. */
  229. virtual IGUIImage *addImage(const core::rect<s32> &rectangle,
  230. IGUIElement *parent = 0, s32 id = -1, const wchar_t *text = 0, bool useAlphaChannel = true) = 0;
  231. //! Adds a checkbox element.
  232. /** \param checked Define the initial state of the check box.
  233. \param rectangle Rectangle specifying the borders of the check box.
  234. \param parent Parent gui element of the check box.
  235. \param id Id to identify the gui element.
  236. \param text Title text of the check box.
  237. \return Pointer to the created check box. Returns 0 if an error
  238. occurred. This pointer should not be dropped. See
  239. IReferenceCounted::drop() for more information. */
  240. virtual IGUICheckBox *addCheckBox(bool checked, const core::rect<s32> &rectangle,
  241. IGUIElement *parent = 0, s32 id = -1, const wchar_t *text = 0) = 0;
  242. //! Adds a list box element.
  243. /** \param rectangle Rectangle specifying the borders of the list box.
  244. \param parent Parent gui element of the list box.
  245. \param id Id to identify the gui element.
  246. \param drawBackground Flag whether the background should be drawn.
  247. \return Pointer to the created list box. Returns 0 if an error occurred.
  248. This pointer should not be dropped. See IReferenceCounted::drop() for
  249. more information. */
  250. virtual IGUIListBox *addListBox(const core::rect<s32> &rectangle,
  251. IGUIElement *parent = 0, s32 id = -1, bool drawBackground = false) = 0;
  252. //! Adds a file open dialog.
  253. /** \param title Text to be displayed as the title of the dialog.
  254. \param modal Defines if the dialog is modal. This means, that all other
  255. gui elements which were created before the message box cannot be used
  256. until this messagebox is removed.
  257. \param parent Parent gui element of the dialog.
  258. \param id Id to identify the gui element.
  259. \param restoreCWD If set to true, the current working directory will be
  260. restored after the dialog is closed in some way. Otherwise the working
  261. directory will be the one that the file dialog was last showing.
  262. \param startDir Optional path for which the file dialog will be opened.
  263. \return Pointer to the created file open dialog. Returns 0 if an error
  264. occurred. This pointer should not be dropped. See
  265. IReferenceCounted::drop() for more information. */
  266. virtual IGUIFileOpenDialog *addFileOpenDialog(const wchar_t *title = 0,
  267. bool modal = true, IGUIElement *parent = 0, s32 id = -1,
  268. bool restoreCWD = false, io::path::char_type *startDir = 0) = 0;
  269. //! Adds a static text.
  270. /** \param text Text to be displayed. Can be altered after creation by SetText().
  271. \param rectangle Rectangle specifying the borders of the static text
  272. \param border Set to true if the static text should have a 3d border.
  273. \param wordWrap Enable if the text should wrap into multiple lines.
  274. \param parent Parent item of the element, e.g. a window.
  275. \param id The ID of the element.
  276. \param fillBackground Enable if the background shall be filled.
  277. Defaults to false.
  278. \return Pointer to the created static text. Returns 0 if an error
  279. occurred. This pointer should not be dropped. See
  280. IReferenceCounted::drop() for more information. */
  281. virtual IGUIStaticText *addStaticText(const wchar_t *text, const core::rect<s32> &rectangle,
  282. bool border = false, bool wordWrap = true, IGUIElement *parent = 0, s32 id = -1,
  283. bool fillBackground = false) = 0;
  284. //! Adds an edit box.
  285. /** Supports Unicode input from every keyboard around the world,
  286. scrolling, copying and pasting (exchanging data with the clipboard
  287. directly), maximum character amount, marking, and all shortcuts like
  288. ctrl+X, ctrl+V, ctrl+C, shift+Left, shift+Right, Home, End, and so on.
  289. \param text Text to be displayed. Can be altered after creation
  290. by setText().
  291. \param rectangle Rectangle specifying the borders of the edit box.
  292. \param border Set to true if the edit box should have a 3d border.
  293. \param parent Parent item of the element, e.g. a window.
  294. Set it to 0 to place the edit box directly in the environment.
  295. \param id The ID of the element.
  296. \return Pointer to the created edit box. Returns 0 if an error occurred.
  297. This pointer should not be dropped. See IReferenceCounted::drop() for
  298. more information. */
  299. virtual IGUIEditBox *addEditBox(const wchar_t *text, const core::rect<s32> &rectangle,
  300. bool border = true, IGUIElement *parent = 0, s32 id = -1) = 0;
  301. //! Adds a tab control to the environment.
  302. /** \param rectangle Rectangle specifying the borders of the tab control.
  303. \param parent Parent item of the element, e.g. a window.
  304. Set it to 0 to place the tab control directly in the environment.
  305. \param fillbackground Specifies if the background of the tab control
  306. should be drawn.
  307. \param border Specifies if a flat 3d border should be drawn. This is
  308. usually not necessary unless you place the control directly into
  309. the environment without a window as parent.
  310. \param id An identifier for the tab control.
  311. \return Pointer to the created tab control element. Returns 0 if an
  312. error occurred. This pointer should not be dropped. See
  313. IReferenceCounted::drop() for more information. */
  314. virtual IGUITabControl *addTabControl(const core::rect<s32> &rectangle,
  315. IGUIElement *parent = 0, bool fillbackground = false,
  316. bool border = true, s32 id = -1) = 0;
  317. //! Adds tab to the environment.
  318. /** You can use this element to group other elements. This is not used
  319. for creating tabs on tab controls, please use IGUITabControl::addTab()
  320. for this instead.
  321. \param rectangle Rectangle specifying the borders of the tab.
  322. \param parent Parent item of the element, e.g. a window.
  323. Set it to 0 to place the tab directly in the environment.
  324. \param id An identifier for the tab.
  325. \return Pointer to the created tab. Returns 0 if an
  326. error occurred. This pointer should not be dropped. See
  327. IReferenceCounted::drop() for more information. */
  328. virtual IGUITab *addTab(const core::rect<s32> &rectangle,
  329. IGUIElement *parent = 0, s32 id = -1) = 0;
  330. //! Adds a combo box to the environment.
  331. /** \param rectangle Rectangle specifying the borders of the combo box.
  332. \param parent Parent item of the element, e.g. a window.
  333. Set it to 0 to place the combo box directly in the environment.
  334. \param id An identifier for the combo box.
  335. \return Pointer to the created combo box. Returns 0 if an
  336. error occurred. This pointer should not be dropped. See
  337. IReferenceCounted::drop() for more information. */
  338. virtual IGUIComboBox *addComboBox(const core::rect<s32> &rectangle,
  339. IGUIElement *parent = 0, s32 id = -1) = 0;
  340. //! Find the next element which would be selected when pressing the tab-key
  341. /** If you set the focus for the result you can manually force focus-changes like they
  342. would happen otherwise by the tab-keys.
  343. \param reverse When true it will search backward (toward lower TabOrder numbers, like shift+tab)
  344. \param group When true it will search for the next tab-group (like ctrl+tab)
  345. */
  346. virtual IGUIElement *getNextElement(bool reverse = false, bool group = false) = 0;
  347. //! Set the way the gui will handle automatic focus changes
  348. /** The default is (EFF_SET_ON_LMOUSE_DOWN | EFF_SET_ON_TAB).
  349. with the left mouse button.
  350. This does not affect the setFocus function itself - users can still call that whenever they want on any element.
  351. \param flags A bitmask which is a combination of ::EFOCUS_FLAG flags.*/
  352. virtual void setFocusBehavior(u32 flags) = 0;
  353. //! Get the way the gui does handle focus changes
  354. /** \returns A bitmask which is a combination of ::EFOCUS_FLAG flags.*/
  355. virtual u32 getFocusBehavior() const = 0;
  356. //! Adds a IGUIElement to deletion queue.
  357. /** Queued elements will be removed at the end of each drawAll call.
  358. Or latest in the destructor of the GUIEnvironment.
  359. This can be used to allow an element removing itself safely in a function
  360. iterating over gui elements, like an overloaded IGUIElement::draw or
  361. IGUIElement::OnPostRender function.
  362. Note that in general just calling IGUIElement::remove() is enough.
  363. Unless you create your own GUI elements removing themselves you won't need it.
  364. \param element: Element to remove */
  365. virtual void addToDeletionQueue(IGUIElement *element) = 0;
  366. };
  367. } // end namespace gui
  368. } // end namespace irr