| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- $#include "UIElement.h"
- /// %UI element horizontal alignment.
- enum HorizontalAlignment
- {
- HA_LEFT = 0,
- HA_CENTER,
- HA_RIGHT
- };
- /// %UI element vertical alignment.
- enum VerticalAlignment
- {
- VA_TOP = 0,
- VA_CENTER,
- VA_BOTTOM
- };
- /// %UI element corners.
- enum Corner
- {
- C_TOPLEFT = 0,
- C_TOPRIGHT,
- C_BOTTOMLEFT,
- C_BOTTOMRIGHT,
- MAX_UIELEMENT_CORNERS
- };
- /// %UI element orientation.
- enum Orientation
- {
- O_HORIZONTAL = 0,
- O_VERTICAL
- };
- /// %UI element focus mode.
- enum FocusMode
- {
- /// Is not focusable and does not affect existing focus.
- FM_NOTFOCUSABLE = 0,
- /// Resets focus when clicked.
- FM_RESETFOCUS,
- /// Is focusable.
- FM_FOCUSABLE,
- /// Is focusable and also defocusable by pressing ESC.
- FM_FOCUSABLE_DEFOCUSABLE
- };
- /// Layout operation mode.
- enum LayoutMode
- {
- /// No layout operations will be performed.
- LM_FREE = 0,
- /// Layout child elements horizontally and resize them to fit. Resize element if necessary.
- LM_HORIZONTAL,
- /// Layout child elements vertically and resize them to fit. Resize element if necessary.
- LM_VERTICAL
- };
- /// Traversal mode.
- enum TraversalMode
- {
- /// Traverse thru children having same priority first and recurse into their children before traversing children having higher priority.
- TM_BREADTH_FIRST = 0,
- /// Traverse thru each child and its children immediately after in sequence.
- TM_DEPTH_FIRST
- };
- /// Drag and drop disabled.
- static const unsigned DD_DISABLED;
- /// Drag and drop source flag.
- static const unsigned DD_SOURCE;
- /// Drag and drop target flag.
- static const unsigned DD_TARGET;
- /// Drag and drop source and target.
- static const unsigned DD_SOURCE_AND_TARGET;
- /// Base class for %UI elements.
- class UIElement : public Serializable
- {
- public:
- /// Construct.
- UIElement(Context* context);
- /// Destruct.
- virtual ~UIElement();
- /// Set name.
- void SetName(const String& name);
- /// Set position.
- void SetPosition(const IntVector2& position);
- /// Set position.
- void SetPosition(int x, int y);
- /// Set size.
- void SetSize(const IntVector2& size);
- /// Set size.
- void SetSize(int width, int height);
- /// Set width only.
- void SetWidth(int width);
- /// Set height only.
- void SetHeight(int height);
- /// Set minimum size.
- void SetMinSize(const IntVector2& minSize);
- /// Set minimum size.
- void SetMinSize(int width, int height);
- /// Set minimum width.
- void SetMinWidth(int width);
- /// Set minimum height.
- void SetMinHeight(int height);
- /// Set maximum size.
- void SetMaxSize(const IntVector2& maxSize);
- /// Set maximum size.
- void SetMaxSize(int width, int height);
- /// Set maximum width.
- void SetMaxWidth(int width);
- /// Set maximum height.
- void SetMaxHeight(int height);
- /// Set fixed size.
- void SetFixedSize(const IntVector2& size);
- /// Set fixed size.
- void SetFixedSize(int width, int height);
- /// Set fixed width.
- void SetFixedWidth(int width);
- /// Set fixed height.
- void SetFixedHeight(int height);
- /// Set horizontal and vertical alignment.
- void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign);
- /// Set horizontal alignment.
- void SetHorizontalAlignment(HorizontalAlignment align);
- /// Set vertical alignment.
- void SetVerticalAlignment(VerticalAlignment align);
- /// Set child element clipping border.
- void SetClipBorder(const IntRect& rect);
- /// Set color on all corners.
- void SetColor(const Color& color);
- /// Set color on one corner.
- void SetColor(Corner corner, const Color& color);
- /// Set priority.
- void SetPriority(int priority);
- /// Set opacity.
- void SetOpacity(float opacity);
- /// Set whether should be brought to front when focused.
- void SetBringToFront(bool enable);
- /// Set whether should be put to background when another element is focused.
- void SetBringToBack(bool enable);
- /// Set whether should clip child elements. Default false.
- void SetClipChildren(bool enable);
- /// Set whether should sort child elements according to priority. Default true.
- void SetSortChildren(bool enable);
- /// Set whether parent elements' opacity affects opacity. Default true.
- void SetUseDerivedOpacity(bool enable);
- /// Set whether reacts to input.
- void SetEnabled(bool enable);
- /// Set whether is focused. Only one element can be focused at a time.
- void SetFocus(bool enable);
- /// Set selected mode. Actual meaning is element dependent, for example constant hover or pressed effect.
- void SetSelected(bool enable);
- /// Set whether is visible.
- void SetVisible(bool enable);
- /// Set focus mode.
- void SetFocusMode(FocusMode mode);
- /// Set drag and drop flags.
- void SetDragDropMode(unsigned mode);
- /// Set style from an XML file. Find the style element by name. If the style file is not explicitly provided, use the default style from parental chain. Return true if the style is applied successfully.
- bool SetStyle(const String& styleName, XMLFile* file = 0);
- /// Set style from an XML element. Return true if the style is applied successfully.
- bool SetStyle(const XMLElement& element);
- /// Set style from an XML file. Find the style element automatically. If the style file is not explicitly provided, use the default style from parental chain. Return true if the style is applied successfully.
- bool SetStyleAuto(XMLFile* file = 0);
- /// Set default style file for later use by children elements.
- void SetDefaultStyle(XMLFile* style);
- /// Set layout.
- void SetLayout(LayoutMode mode, int spacing = 0, const IntRect& border = IntRect::ZERO);
- /// Set layout mode only.
- void SetLayoutMode(LayoutMode mode);
- /// Set layout spacing.
- void SetLayoutSpacing(int spacing);
- /// Set layout border.
- void SetLayoutBorder(const IntRect& border);
- /// Set horizontal indentation.
- void SetIndent(int indent);
- /// Set indent spacing (number of pixels per indentation level).
- void SetIndentSpacing(int indentSpacing);
- /// Manually update layout. Should not be necessary in most cases, but is provided for completeness.
- void UpdateLayout();
- /// Disable automatic layout update. Should only be used if there are performance problems.
- void DisableLayoutUpdate();
- /// Enable automatic layout update.
- void EnableLayoutUpdate();
- /// Bring UI element to front.
- void BringToFront();
- /// Create and add a child element and return it.
- UIElement* CreateChild(ShortStringHash type, const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
- /// Add a child element.
- void AddChild(UIElement* element);
- /// Insert a child element into a specific position in the child list.
- void InsertChild(unsigned index, UIElement* element);
- /// Remove a child element. Starting search at specified index if provided.
- void RemoveChild(UIElement* element, unsigned index = 0);
- /// Remove a child element at index.
- void RemoveChildAtIndex(unsigned index);
- /// Remove all child elements.
- void RemoveAllChildren();
- /// Remove from the parent element. If no other shared pointer references exist, causes immediate deletion.
- void Remove();
- /// Find child index. Return M_MAX_UNSIGNED if not found.
- unsigned FindChild(UIElement* element) const;
- /// Set parent element. Same as parent->InsertChild(index, this).
- void SetParent(UIElement* parent, unsigned index = M_MAX_UNSIGNED);
- /// Set a user variable.
- void SetVar(ShortStringHash key, const Variant& value);
- /// Mark as internally (programmatically) created. Used when an element composes itself out of child elements.
- void SetInternal(bool enable);
- /// Set traversal mode. The default traversal mode is TM_BREADTH_FIRST for non-root element. Root element should be set to TM_DEPTH_FIRST to avoid artifacts during rendering.
- void SetTraversalMode(TraversalMode traversalMode);
- /// Set element event sender flag. When child element is added or deleted, the event would be sent using UIElement found in the parental chain having this flag set. If not set, the event is sent using UI's root as per normal.
- void SetElementEventSender(bool flag);
-
- /// Return name.
- const String& GetName() const { return name_; }
- /// Return position.
- const IntVector2& GetPosition() const { return position_; }
- /// Return size.
- const IntVector2& GetSize() const { return size_; }
- /// Return width.
- int GetWidth() const { return size_.x_; }
- /// Return height.
- int GetHeight() const { return size_.y_; }
- /// Return minimum size.
- const IntVector2& GetMinSize() const { return minSize_; }
- /// Return minimum width.
- int GetMinWidth() const { return minSize_.x_; }
- /// Return minimum height.
- int GetMinHeight() const { return minSize_.y_; }
- /// Return maximum size.
- const IntVector2& GetMaxSize() const { return maxSize_; }
- /// Return minimum width.
- int GetMaxWidth() const { return maxSize_.x_; }
- /// Return minimum height.
- int GetMaxHeight() const { return maxSize_.y_; }
- /// Return true if size is fixed.
- bool IsFixedSize() const { return minSize_ == maxSize_; }
- /// Return true if width is fixed.
- bool IsFixedWidth() const { return minSize_.x_ == maxSize_.x_; }
- /// Return true if height is fixed.
- bool IsFixedHeight() const { return minSize_.y_ == maxSize_.y_; }
- /// Return child element offset.
- const IntVector2& GetChildOffset() const { return childOffset_; }
- /// Return horizontal alignment.
- HorizontalAlignment GetHorizontalAlignment() const { return horizontalAlignment_; }
- /// Return vertical alignment.
- VerticalAlignment GetVerticalAlignment() const { return verticalAlignment_; }
- /// Return child element clipping border.
- const IntRect& GetClipBorder() const { return clipBorder_; }
- /// Return corner color.
- const Color& GetColor(Corner corner) const { return color_[corner]; }
- /// Return priority.
- int GetPriority() const { return priority_; }
- /// Return opacity.
- float GetOpacity() const { return opacity_; }
- /// Return derived opacity (affected by parent elements.) If UseDerivedOpacity is false, returns same as element's own opacity.
- float GetDerivedOpacity() const;
- /// Return whether should be brought to front when focused.
- bool GetBringToFront() const { return bringToFront_; }
- /// Return whether should be put to background when another element is focused.
- bool GetBringToBack() const { return bringToBack_; }
- /// Return whether should clip child elements.
- bool GetClipChildren() const { return clipChildren_; }
- /// Return whether should sort child elements according to priority.
- bool GetSortChildren() const { return sortChildren_; }
- /// Return whether parent elements' opacity affects opacity.
- bool GetUseDerivedOpacity() const { return useDerivedOpacity_; }
- /// Return whether has focus.
- bool HasFocus() const;
- /// Return whether reacts to input.
- bool IsEnabled() const { return enabled_; }
- /// Return whether is selected. Actual meaning is element dependent.
- bool IsSelected() const { return selected_; }
- /// Return whether is visible.
- bool IsVisible() const { return visible_; }
- /// Return whether the cursor is hovering on this element.
- bool IsHovering() const { return hovering_; }
- /// Return whether is internally created.
- bool IsInternal() const { return internal_; }
- /// Return whether has different color in at least one corner.
- bool HasColorGradient() const { return colorGradient_; }
- /// Return focus mode.
- FocusMode GetFocusMode() const { return focusMode_; }
- /// Return drag and drop flags.
- unsigned GetDragDropMode() const { return dragDropMode_; }
- /// Return applied style name. Return an empty string when the applied style is an 'auto' style (i.e. style derived from instance's type).
- const String& GetAppliedStyle() const;
- /// Return default style.
- XMLFile* GetDefaultStyle(bool recursiveUp = true) const;
- /// Return layout mode.
- LayoutMode GetLayoutMode() const { return layoutMode_; }
- /// Return layout spacing.
- int GetLayoutSpacing() const { return layoutSpacing_; }
- /// Return layout border.
- const IntRect& GetLayoutBorder() const { return layoutBorder_; }
- /// Return number of child elements.
- unsigned GetNumChildren(bool recursive = false) const;
- /// Return child element by index.
- UIElement* GetChild(unsigned index) const;
- /// Return child element by name.
- UIElement* GetChild(const String& name, bool recursive = false) const;
- /// Return child element by variable. If only key is provided, return the first child having the matching variable key. If value is also provided then the actual variable value would also be checked against.
- UIElement* GetChild(const ShortStringHash& key, const Variant& value = Variant::EMPTY, bool recursive = false) const;
- /// Return parent element.
- UIElement* GetParent() const { return parent_; }
- /// Return root element.
- UIElement* GetRoot() const;
- /// Return derived color. Only valid when no gradient.
- const Color& GetDerivedColor() const;
- /// Convert screen coordinates to element coordinates.
- IntVector2 ScreenToElement(const IntVector2& screenPosition);
- /// Convert element coordinates to screen coordinates.
- IntVector2 ElementToScreen(const IntVector2& position);
- /// Return whether a point (either in element or screen coordinates) is inside the element.
- bool IsInside(IntVector2 position, bool isScreen);
- /// Return whether a point (either in element or screen coordinates) is inside the combined rect of the element and its children.
- bool IsInsideCombined(IntVector2 position, bool isScreen);
- /// Return combined screen coordinate rect of element and its children.
- IntRect GetCombinedScreenRect();
- /// Sort child elements if sorting enabled and order dirty. Called by UI.
- void SortChildren();
- /// Return minimum layout element size in the layout direction. Only valid after layout has been calculated.
- int GetLayoutMinSize() const { return layoutMinSize_; }
- /// Return horizontal indentation.
- int GetIndent() const { return indent_; }
- /// Return indent spacing (number of pixels per indentation level).
- int GetIndentSpacing() const { return indentSpacing_; }
- /// Return indent width in pixels.
- int GetIndentWidth() const { return indent_ * indentSpacing_; }
- /// Set child offset.
- void SetChildOffset(const IntVector2& offset);
- /// Set hovering state.
- void SetHovering(bool enable);
- /// Set temporary visibility status without updating layout or sending events. Used internally.
- void SetTempVisible(bool enable);
- /// Adjust scissor for rendering.
- void AdjustScissor(IntRect& currentScissor);
- /// Get color attribute. Uses just the top-left color.
- const Color& GetColorAttr() const { return color_[0]; }
- /// Get traversal mode.
- TraversalMode GetTraversalMode() const { return traversalMode_; }
- /// Get element event sender flag.
- bool IsElementEventSender() const { return elementEventSender_; }
- /// Get element event sender.
- UIElement* GetElementEventSender() const;
- };
|