// // Copyright (c) 2008-2013 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #pragma once #include "Object.h" #include "Cursor.h" #include "UIBatch.h" namespace Urho3D { class Cursor; class Graphics; class ResourceCache; class Timer; class UIBatch; class UIElement; class VertexBuffer; class XMLElement; class XMLFile; /// %UI subsystem. Manages the graphical user interface. class URHO3D_API UI : public Object { OBJECT(UI); public: /// Construct. UI(Context* context); /// Destruct. virtual ~UI(); /// Set cursor UI element. void SetCursor(Cursor* cursor); /// Set focused UI element. void SetFocusElement(UIElement* element); /// Set modal element. Until all the modal elements are dismissed, all the inputs and events are only sent to them. Return true when successful. /// Only the modal element can clear its modal status or when it is being destructed. /// UI subystem auto-removes modal element when an ESC key is pressed, however if this is not desirable, setting a user-defined variable "NoAutoRemove" in the modal element would prevent this. /// In that case, the modal element will only have its modal flag reset and reparented back to its original parent. bool SetModalElement(UIElement* modalElement, bool enable); /// Clear the UI (excluding the cursor.) void Clear(); /// Update the UI logic. Called by HandlePostUpdate(). void Update(float timeStep); /// Update the UI for rendering. Called by HandleRenderUpdate(). void RenderUpdate(); /// Render the UI. void Render(); /// Debug draw a UI element. void DebugDraw(UIElement* element); /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element. SharedPtr LoadLayout(Deserializer& source, XMLFile* styleFile = 0); /// Load a UI layout from an XML file. Optionally specify another XML file for element style. Return the root element. SharedPtr LoadLayout(XMLFile* file, XMLFile* styleFile = 0); /// Save a UI layout to an XML file. Return true if successful. bool SaveLayout(Serializer& dest, UIElement* element); /// Set clipboard text. void SetClipBoardText(const String& text); /// Set UI element double click interval in seconds. void SetDoubleClickInterval(float interval); /// Set maximum font face texture size. Must be a power of two. Default is 2048. void SetMaxFontTextureSize(int size); /// Set whether mouse wheel can control also a non-focused element. void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel); /// Set whether to use system clipboard. Default false. void SetUseSystemClipBoard(bool enable); /// Set whether to use mutable (eraseable) glyphs to ensure a font face never expands to more than one texture. Default false. void SetUseMutableGlyphs(bool enable); /// Set whether to force font autohinting instead of using FreeType's TTF bytecode interpreter. void SetForceAutoHint(bool enable); /// Return root UI element. UIElement* GetRoot() const { return rootElement_; } /// Return root modal element. UIElement* GetRootModalElement() const { return rootModalElement_; } /// Return cursor. Cursor* GetCursor() const { return cursor_; } /// Return cursor position. IntVector2 GetCursorPosition() const; /// Return UI element at screen coordinates. UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true); /// Return UI element at screen coordinates. UIElement* GetElementAt(int x, int y, bool enabledOnly = true); /// Return focused element. UIElement* GetFocusElement() const { return focusElement_; } /// Return topmost enabled root-level non-modal element. UIElement* GetFrontElement() const; /// Return clipboard text. const String& GetClipBoardText() const; /// Return UI element double click interval in seconds. float GetDoubleClickInterval() const { return doubleClickInterval_; } /// Return font texture maximum size. int GetMaxFontTextureSize() const { return maxFontTextureSize_; } /// Return whether mouse wheel can control also a non-focused element. bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; } /// Return whether is using the system clipboard. bool GetUseSystemClipBoard() const { return useSystemClipBoard_; } /// Return whether is using mutable (eraseable) glyphs for fonts. bool GetUseMutableGlyphs() const { return useMutableGlyphs_; } /// Return whether is using forced autohinting. bool GetForceAutoHint() const { return forceAutoHint_; } /// Return true when UI has modal element(s). bool HasModalElement() const; private: /// Initialize when screen mode initially set. void Initialize(); /// Update UI element logic recursively. void Update(float timeStep, UIElement* element); /// Upload UI geometry into a vertex buffer. void SetVertexData(VertexBuffer* dest, const PODVector& vertexData); /// Render UI batches. Geometry must have been uploaded first. void Render(VertexBuffer* buffer, const PODVector& batches, unsigned batchStart, unsigned batchEnd); /// Generate batches from an UI element recursively. Skip the cursor element. void GetBatches(UIElement* element, IntRect currentScissor); /// Return UI element at screen position recursively. void GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly); /// Return the first element in hierarchy that can alter focus. UIElement* GetFocusableElement(UIElement* element); /// Return cursor position and visibility either from the cursor element, or the Input subsystem. void GetCursorPositionAndVisible(IntVector2& pos, bool& visible); /// Set active cursor's shape. void SetCursorShape(CursorShape shape); /// Force release of font faces when global font properties change. void ReleaseFontFaces(); /// Handle button or touch begin. void ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible); /// Handle button or touch end. void ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible); /// Handle mouse or touch move. void ProcessMove(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible); /// Send a UI element drag event. void SendDragEvent(StringHash eventType, UIElement* element, const IntVector2& screenPos); /// Send a UI click or double click event. void SendClickEvent(StringHash eventType, UIElement* element, const IntVector2& pos, int button, int buttons, int qualifiers); /// Handle screen mode event. void HandleScreenMode(StringHash eventType, VariantMap& eventData); /// Handle mouse button down event. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData); /// Handle mouse button up event. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData); /// Handle mouse move event. void HandleMouseMove(StringHash eventType, VariantMap& eventData); /// Handle mouse wheel event. void HandleMouseWheel(StringHash eventType, VariantMap& eventData); /// Handle touch begin event. void HandleTouchBegin(StringHash eventType, VariantMap& eventData); /// Handle touch end event. void HandleTouchEnd(StringHash eventType, VariantMap& eventData); /// Handle touch move event. void HandleTouchMove(StringHash eventType, VariantMap& eventData); /// Handle keypress event. void HandleKeyDown(StringHash eventType, VariantMap& eventData); /// Handle character event. void HandleChar(StringHash eventType, VariantMap& eventData); /// Handle logic post-update event. void HandlePostUpdate(StringHash eventType, VariantMap& eventData); /// Handle render update event. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData); /// Handle a file being drag-dropped into the application window. void HandleDropFile(StringHash eventType, VariantMap& eventData); /// Graphics subsystem. WeakPtr graphics_; /// Vertex shader for no texture. SharedPtr noTextureVS_; /// Vertex shader for diffuse texture. SharedPtr diffTextureVS_; /// Pixel shader for no texture. SharedPtr noTexturePS_; /// Pixel shader for diffuse texture. SharedPtr diffTexturePS_; /// Pixel shader for diffuse texture masking. SharedPtr diffMaskTexturePS_; /// Pixel shader for alpha texture. SharedPtr alphaTexturePS_; /// UI root element. SharedPtr rootElement_; /// UI root modal element. SharedPtr rootModalElement_; /// Cursor. SharedPtr cursor_; /// UI element being dragged. WeakPtr dragElement_; /// Currently focused element WeakPtr focusElement_; /// UI rendering batches. PODVector batches_; /// UI rendering vertex data. PODVector vertexData_; /// UI rendering batches for debug draw. PODVector debugDrawBatches_; /// UI rendering vertex data for debug draw. PODVector debugVertexData_; /// UI vertex buffer. SharedPtr vertexBuffer_; /// UI debug geometry vertex buffer. SharedPtr debugVertexBuffer_; /// UI element query vector. PODVector tempElements_; /// Clipboard text. mutable String clipBoard_; /// Mouse buttons held down. int mouseButtons_; /// Qualifier keys held down. int qualifiers_; /// Font texture maximum size. int maxFontTextureSize_; /// Initialized flag. bool initialized_; /// Touch used flag. bool usingTouchInput_; /// Flag to switch mouse wheel event to be sent to non-focused element at cursor. bool nonFocusedMouseWheel_; /// Flag for using operating system clipboard instead of internal. bool useSystemClipBoard_; /// Flag for using mutable (eraseable) font glyphs. bool useMutableGlyphs_; /// Flag for forcing FreeType autohinting. bool forceAutoHint_; /// Non-modal batch size (used internally for rendering). unsigned nonModalBatchSize_; /// Timer used to trigger double click. Timer* clickTimer_; /// UI element last clicked for tracking click end. WeakPtr clickElement_; /// UI element last clicked for tracking double clicks. WeakPtr doubleClickElement_; /// Last mouse button pressed. int lastMouseButtons_; /// Seconds between clicks to register a double click. float doubleClickInterval_; }; /// Register UI library objects. void URHO3D_API RegisterUILibrary(Context* context); }