SystemUI.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../../Core/Object.h"
  24. #include "Cursor.h"
  25. #include "SystemUIBatch.h"
  26. namespace Atomic
  27. {
  28. class Graphics;
  29. class ResourceCache;
  30. class Timer;
  31. class VertexBuffer;
  32. class XMLElement;
  33. class XMLFile;
  34. namespace SystemUI
  35. {
  36. class Cursor;
  37. class SystemUIBatch;
  38. class UIElement;
  39. /// %UI subsystem. Manages the graphical user interface.
  40. class ATOMIC_API SystemUI : public Object
  41. {
  42. OBJECT(SystemUI);
  43. public:
  44. /// Construct.
  45. SystemUI(Context* context);
  46. /// Destruct.
  47. virtual ~SystemUI();
  48. /// Set cursor UI element.
  49. void SetCursor(Cursor* cursor);
  50. /// Set focused UI element.
  51. void SetFocusElement(UIElement* element, bool byKey = false);
  52. /// Set modal element. Until all the modal elements are dismissed, all the inputs and events are only sent to them. Return true when successful.
  53. /// Only the modal element can clear its modal status or when it is being destructed.
  54. bool SetModalElement(UIElement* modalElement, bool enable);
  55. /// Clear the UI (excluding the cursor.)
  56. void Clear();
  57. /// Update the UI logic. Called by HandlePostUpdate().
  58. void Update(float timeStep);
  59. /// Update the UI for rendering. Called by HandleRenderUpdate().
  60. void RenderUpdate();
  61. /// Render the UI. If resetRenderTargets is true, is assumed to be the default UI render to backbuffer called by Engine, and will be performed only once. Additional UI renders to a different rendertarget may be triggered from the renderpath.
  62. void Render(bool resetRenderTargets = true);
  63. /// Debug draw a UI element.
  64. void DebugDraw(UIElement* element);
  65. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  66. SharedPtr<UIElement> LoadLayout(Deserializer& source, XMLFile* styleFile = 0);
  67. /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element.
  68. SharedPtr<UIElement> LoadLayout(XMLFile* file, XMLFile* styleFile = 0);
  69. /// Save a UI layout to an XML file. Return true if successful.
  70. bool SaveLayout(Serializer& dest, UIElement* element);
  71. /// Set clipboard text.
  72. void SetClipboardText(const String& text);
  73. /// Set UI element double click interval in seconds.
  74. void SetDoubleClickInterval(float interval);
  75. /// Set UI drag event start interval in seconds.
  76. void SetDragBeginInterval(float interval);
  77. /// Set UI drag event start distance threshold in pixels.
  78. void SetDragBeginDistance(int pixels);
  79. /// Set tooltip default display delay in seconds.
  80. void SetDefaultToolTipDelay(float delay);
  81. /// Set maximum font face texture size. Must be a power of two. Default is 2048.
  82. void SetMaxFontTextureSize(int size);
  83. /// Set whether mouse wheel can control also a non-focused element.
  84. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
  85. /// Set whether to use system clipboard. Default false.
  86. void SetUseSystemClipboard(bool enable);
  87. /// Set whether to show the on-screen keyboard (if supported) when a %LineEdit is focused. Default true on mobile devices.
  88. void SetUseScreenKeyboard(bool enable);
  89. /// Set whether to use mutable (eraseable) glyphs to ensure a font face never expands to more than one texture. Default false.
  90. void SetUseMutableGlyphs(bool enable);
  91. /// Set whether to force font autohinting instead of using FreeType's TTF bytecode interpreter.
  92. void SetForceAutoHint(bool enable);
  93. /// Return root UI element.
  94. UIElement* GetRoot() const { return rootElement_; }
  95. /// Return root modal element.
  96. UIElement* GetRootModalElement() const { return rootModalElement_; }
  97. /// Return cursor.
  98. Cursor* GetCursor() const { return cursor_; }
  99. /// Return cursor position.
  100. IntVector2 GetCursorPosition() const;
  101. /// Return UI element at screen coordinates. By default returns only input-enabled elements.
  102. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true);
  103. /// Return UI element at screen coordinates. By default returns only input-enabled elements.
  104. UIElement* GetElementAt(int x, int y, bool enabledOnly = true);
  105. /// Return focused element.
  106. UIElement* GetFocusElement() const { return focusElement_; }
  107. /// Return topmost enabled root-level non-modal element.
  108. UIElement* GetFrontElement() const;
  109. /// Return currently dragged elements.
  110. const Vector<UIElement*> GetDragElements();
  111. /// Return the number of currently dragged elements.
  112. unsigned GetNumDragElements() const { return (unsigned)dragConfirmedCount_; }
  113. /// Return the drag element at index.
  114. UIElement* GetDragElement(unsigned index);
  115. /// Return clipboard text.
  116. const String& GetClipboardText() const;
  117. /// Return UI element double click interval in seconds.
  118. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  119. /// Return UI drag start event interval in seconds.
  120. float GetDragBeginInterval() const { return dragBeginInterval_; }
  121. /// Return UI drag start event distance threshold in pixels.
  122. int GetDragBeginDistance() const { return dragBeginDistance_; }
  123. /// Return tooltip default display delay in seconds.
  124. float GetDefaultToolTipDelay() const { return defaultToolTipDelay_; }
  125. /// Return font texture maximum size.
  126. int GetMaxFontTextureSize() const { return maxFontTextureSize_; }
  127. /// Return whether mouse wheel can control also a non-focused element.
  128. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
  129. /// Return whether is using the system clipboard.
  130. bool GetUseSystemClipboard() const { return useSystemClipboard_; }
  131. /// Return whether focusing a %LineEdit will show the on-screen keyboard.
  132. bool GetUseScreenKeyboard() const { return useScreenKeyboard_; }
  133. /// Return whether is using mutable (eraseable) glyphs for fonts.
  134. bool GetUseMutableGlyphs() const { return useMutableGlyphs_; }
  135. /// Return whether is using forced autohinting.
  136. bool GetForceAutoHint() const { return forceAutoHint_; }
  137. /// Return true when UI has modal element(s).
  138. bool HasModalElement() const;
  139. /// Return whether a drag is in progress.
  140. bool IsDragging() const { return dragConfirmedCount_ > 0; }
  141. /// Data structure used to represent the drag data associated to a UIElement.
  142. struct DragData
  143. {
  144. /// Which button combo initiated the drag.
  145. int dragButtons;
  146. /// How many buttons initiated the drag.
  147. int numDragButtons;
  148. /// Sum of all touch locations
  149. IntVector2 sumPos;
  150. /// Flag for a drag start event pending.
  151. bool dragBeginPending;
  152. /// Timer used to trigger drag begin event.
  153. Timer dragBeginTimer;
  154. /// Drag start position.
  155. IntVector2 dragBeginSumPos;
  156. };
  157. void CreateConsoleAndDebugHud();
  158. private:
  159. /// Initialize when screen mode initially set.
  160. void Initialize();
  161. /// Update UI element logic recursively.
  162. void Update(float timeStep, UIElement* element);
  163. /// Upload UI geometry into a vertex buffer.
  164. void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
  165. /// Render UI batches. Geometry must have been uploaded first.
  166. void Render
  167. (bool resetRenderTargets, VertexBuffer* buffer, const PODVector<SystemUIBatch>& batches, unsigned batchStart, unsigned batchEnd);
  168. /// Generate batches from an UI element recursively. Skip the cursor element.
  169. void GetBatches(UIElement* element, IntRect currentScissor);
  170. /// Return UI element at screen position recursively.
  171. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly);
  172. /// Return the first element in hierarchy that can alter focus.
  173. UIElement* GetFocusableElement(UIElement* element);
  174. /// Return cursor position and visibility either from the cursor element, or the Input subsystem.
  175. void GetCursorPositionAndVisible(IntVector2& pos, bool& visible);
  176. /// Set active cursor's shape.
  177. void SetCursorShape(CursorShape shape);
  178. /// Force release of font faces when global font properties change.
  179. void ReleaseFontFaces();
  180. /// Handle button or touch hover
  181. void ProcessHover(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor);
  182. /// Handle button or touch begin.
  183. void
  184. ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  185. /// Handle button or touch end.
  186. void ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible);
  187. /// Handle mouse or touch move.
  188. void ProcessMove(const IntVector2& cursorPos, const IntVector2& cursorDeltaPos, int buttons, int qualifiers, Cursor* cursor,
  189. bool cursorVisible);
  190. /// Send a UI element drag or hover begin event.
  191. void SendDragOrHoverEvent
  192. (StringHash eventType, UIElement* element, const IntVector2& screenPos, const IntVector2& deltaPos, SystemUI::DragData* dragData);
  193. /// Send a UI click or double click event.
  194. void SendClickEvent
  195. (StringHash eventType, UIElement* beginElement, UIElement* endElement, const IntVector2& pos, int button, int buttons,
  196. int qualifiers);
  197. /// Handle screen mode event.
  198. void HandleScreenMode(StringHash eventType, VariantMap& eventData);
  199. /// Handle mouse button down event.
  200. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  201. /// Handle mouse button up event.
  202. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  203. /// Handle mouse move event.
  204. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  205. /// Handle mouse wheel event.
  206. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  207. /// Handle touch begin event.
  208. void HandleTouchBegin(StringHash eventType, VariantMap& eventData);
  209. /// Handle touch end event.
  210. void HandleTouchEnd(StringHash eventType, VariantMap& eventData);
  211. /// Handle touch move event.
  212. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  213. /// Handle keypress event.
  214. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  215. /// Handle text input event.
  216. void HandleTextInput(StringHash eventType, VariantMap& eventData);
  217. /// Handle frame begin event.
  218. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  219. /// Handle logic post-update event.
  220. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  221. /// Handle render update event.
  222. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  223. /// Handle a file being drag-dropped into the application window.
  224. void HandleDropFile(StringHash eventType, VariantMap& eventData);
  225. /// Remove drag data and return next iterator.
  226. HashMap<WeakPtr<UIElement>, DragData*>::Iterator DragElementErase(HashMap<WeakPtr<UIElement>, DragData*>::Iterator dragElement);
  227. /// Handle clean up on a drag cancel.
  228. void ProcessDragCancel();
  229. /// Sum touch positions and return the begin position ready to send.
  230. IntVector2 SumTouchPositions(SystemUI::DragData* dragData, const IntVector2& oldSendPos);
  231. /// Graphics subsystem.
  232. WeakPtr<Graphics> graphics_;
  233. /// UI root element.
  234. SharedPtr<UIElement> rootElement_;
  235. /// UI root modal element.
  236. SharedPtr<UIElement> rootModalElement_;
  237. /// Cursor.
  238. SharedPtr<Cursor> cursor_;
  239. /// Currently focused element.
  240. WeakPtr<UIElement> focusElement_;
  241. /// UI rendering batches.
  242. PODVector<SystemUIBatch> batches_;
  243. /// UI rendering vertex data.
  244. PODVector<float> vertexData_;
  245. /// UI rendering batches for debug draw.
  246. PODVector<SystemUIBatch> debugDrawBatches_;
  247. /// UI rendering vertex data for debug draw.
  248. PODVector<float> debugVertexData_;
  249. /// UI vertex buffer.
  250. SharedPtr<VertexBuffer> vertexBuffer_;
  251. /// UI debug geometry vertex buffer.
  252. SharedPtr<VertexBuffer> debugVertexBuffer_;
  253. /// UI element query vector.
  254. PODVector<UIElement*> tempElements_;
  255. /// Clipboard text.
  256. mutable String clipBoard_;
  257. /// Seconds between clicks to register a double click.
  258. float doubleClickInterval_;
  259. /// Seconds from mouse button down to begin a drag if there has been no movement exceeding pixel threshold.
  260. float dragBeginInterval_;
  261. /// Tooltip default display delay in seconds.
  262. float defaultToolTipDelay_;
  263. /// Drag begin event distance threshold in pixels.
  264. int dragBeginDistance_;
  265. /// Mouse buttons held down.
  266. int mouseButtons_;
  267. /// Last mouse button pressed.
  268. int lastMouseButtons_;
  269. /// Qualifier keys held down.
  270. int qualifiers_;
  271. /// Font texture maximum size.
  272. int maxFontTextureSize_;
  273. /// Initialized flag.
  274. bool initialized_;
  275. /// Touch used flag.
  276. bool usingTouchInput_;
  277. /// Flag to switch mouse wheel event to be sent to non-focused element at cursor.
  278. bool nonFocusedMouseWheel_;
  279. /// Flag for using operating system clipboard instead of internal.
  280. bool useSystemClipboard_;
  281. /// Flag for showing the on-screen keyboard on focusing a %LineEdit.
  282. bool useScreenKeyboard_;
  283. /// Flag for using mutable (erasable) font glyphs.
  284. bool useMutableGlyphs_;
  285. /// Flag for forcing FreeType auto hinting.
  286. bool forceAutoHint_;
  287. /// Flag for UI already being rendered this frame.
  288. bool uiRendered_;
  289. /// Non-modal batch size (used internally for rendering).
  290. unsigned nonModalBatchSize_;
  291. /// Timer used to trigger double click.
  292. Timer clickTimer_;
  293. /// UI element last clicked for tracking double clicks.
  294. WeakPtr<UIElement> doubleClickElement_;
  295. /// Currently hovered elements.
  296. HashMap<WeakPtr<UIElement>, bool> hoveredElements_;
  297. /// Currently dragged elements.
  298. HashMap<WeakPtr<UIElement>, DragData*> dragElements_;
  299. /// Number of elements in dragElements_.
  300. int dragElementsCount_;
  301. /// Number of elements in dragElements_ with dragPending = false.
  302. int dragConfirmedCount_;
  303. /// UI elements that are being touched with touch input.
  304. HashMap<WeakPtr<UIElement>, int> touchDragElements_;
  305. /// Confirmed drag elements cache.
  306. Vector<UIElement*> dragElementsConfirmed_;
  307. };
  308. /// Register UI library objects.
  309. void ATOMIC_API RegisterSystemUILibrary(Context* context);
  310. }
  311. }