mirror of
https://github.com/edubart/otclient.git
synced 2025-12-21 07:57:12 +01:00
new layout system, new UI state/styles system
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#include "declarations.h"
|
||||
#include <framework/luascript/luaobject.h>
|
||||
#include <framework/graphics/declarations.h>
|
||||
#include <framework/otml/declarations.h>
|
||||
|
||||
class UIWidget : public LuaObject
|
||||
{
|
||||
@@ -12,60 +11,65 @@ public:
|
||||
UIWidget();
|
||||
virtual ~UIWidget();
|
||||
|
||||
static UIWidgetPtr create() { return UIWidgetPtr(new UIWidget); }
|
||||
template<class T>
|
||||
static std::shared_ptr<T> create() { auto t = std::shared_ptr<T>(new T); t->setup(); return t; }
|
||||
|
||||
/// Must be called just after the widget creation
|
||||
virtual void setup() { }
|
||||
void destroy();
|
||||
|
||||
/// Remove this widget from parent then destroy it and its children
|
||||
virtual void destroy();
|
||||
|
||||
/// Draw widget on screen
|
||||
virtual void setup();
|
||||
virtual void render();
|
||||
|
||||
void setEnabled(bool enable) { m_enabled = enable; }
|
||||
void setEnabled(bool enabled) { m_enabled = enabled; updateState(DisabledState); }
|
||||
void setVisible(bool visible) { m_visible = visible; }
|
||||
void setPressed(bool pressed) { m_pressed = pressed; updateState(PressedState); }
|
||||
void setId(const std::string& id) { m_id = id; }
|
||||
void setFocusable(bool focusable) { m_focusable = focusable; }
|
||||
void setHovered(bool hovered) { m_hovered = hovered; }
|
||||
void setVisible(bool visible) { m_visible = visible; }
|
||||
void setStyle(const std::string& styleName);
|
||||
void setStyleFromNode(const OTMLNodePtr& styleNode);
|
||||
void setLayout(const UILayoutPtr& layout) { m_layout = layout; }
|
||||
void setParent(const UIWidgetPtr& parent);
|
||||
void applyStyle(const std::string& styleName);
|
||||
void setRect(const Rect& rect);
|
||||
void setX(int x) { moveTo(Point(x, getY())); }
|
||||
void setY(int y) { moveTo(Point(getX(), y)); }
|
||||
void setWidth(int width) { resize(Size(width, getHeight())); }
|
||||
void setHeight(int height) { resize(Size(getWidth(), height)); }
|
||||
void resize(const Size& size) { setRect(Rect(getPosition(), size)); }
|
||||
void moveTo(const Point& pos) { setRect(Rect(pos, getSize())); }
|
||||
|
||||
void setImage(const ImagePtr& image) { m_image = image; }
|
||||
virtual void setFont(const FontPtr& font) { m_font = font; }
|
||||
void setOpacity(int opacity) { m_opacity = opacity; }
|
||||
void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
|
||||
void setForegroundColor(const Color& color) { m_foregroundColor = color; }
|
||||
void setMarginLeft(int margin) { m_marginLeft = margin; updateLayout(); }
|
||||
void setMarginRight(int margin) { m_marginRight = margin; updateLayout(); }
|
||||
void setMarginTop(int margin) { m_marginTop = margin; updateLayout(); }
|
||||
void setMarginBottom(int margin) { m_marginBottom = margin; updateLayout(); }
|
||||
void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); }
|
||||
void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); }
|
||||
void setMarginTop(int margin) { m_marginTop = margin; updateParentLayout(); }
|
||||
void setMarginBottom(int margin) { m_marginBottom = margin; updateParentLayout(); }
|
||||
void setSizeFixed(bool fixed) { m_fixedSize = fixed; updateParentLayout(); }
|
||||
void setLastFocusReason(FocusReason reason) { m_lastFocusReason = reason; }
|
||||
|
||||
void resize(const Size& size) { setRect(Rect(getPosition(), size)); }
|
||||
void moveTo(const Point& pos) { setRect(Rect(pos, getSize())); }
|
||||
void hide() { setVisible(false); }
|
||||
void show() { setVisible(true); }
|
||||
void disable() { setEnabled(false); }
|
||||
void enable() { setEnabled(true); }
|
||||
|
||||
bool isEnabled();
|
||||
bool isActive() const { return hasState(ActiveState); }
|
||||
bool isEnabled() const { return !hasState(DisabledState); }
|
||||
bool isDisabled() const { return hasState(DisabledState); }
|
||||
bool isFocused() const { return hasState(FocusState); }
|
||||
bool isHovered() const { return hasState(HoverState); }
|
||||
bool isPressed() const { return hasState(PressedState); }
|
||||
bool isVisible();
|
||||
bool isExplicitlyEnabled() const { return m_enabled; }
|
||||
bool isExplicitlyVisible() const { return m_visible; }
|
||||
bool isHovered() const { return m_hovered; }
|
||||
bool isFocusable() const { return m_focusable; }
|
||||
bool isDestroyed() const { return m_destroyed; }
|
||||
bool isSizeFixed() const { return m_fixedSize; }
|
||||
bool hasChildren() const { return m_children.size() > 0; }
|
||||
bool hasFocus();
|
||||
bool hasChild(const UIWidgetPtr& child);
|
||||
bool hasState(WidgetState state) const { return m_states & state; }
|
||||
|
||||
std::string getId() const { return m_id; }
|
||||
int getChildCount() const { return m_children.size(); }
|
||||
UILayoutPtr getLayout() const { return m_layout; }
|
||||
UIWidgetPtr getParent() const { return m_parent.lock(); }
|
||||
UIWidgetPtr getRootParent();
|
||||
Point getPosition() const { return m_rect.topLeft(); }
|
||||
@@ -75,7 +79,6 @@ public:
|
||||
int getY() const { return m_rect.y(); }
|
||||
int getWidth() const { return m_rect.width(); }
|
||||
int getHeight() const { return m_rect.height(); }
|
||||
|
||||
ImagePtr getImage() const { return m_image; }
|
||||
FontPtr getFont() const { return m_font; }
|
||||
Color getForegroundColor() const { return m_foregroundColor; }
|
||||
@@ -85,6 +88,7 @@ public:
|
||||
int getMarginRight() const { return m_marginRight; }
|
||||
int getMarginTop() const { return m_marginTop; }
|
||||
int getMarginBottom() const { return m_marginBottom; }
|
||||
FocusReason getLastFocusReason() const { return m_lastFocusReason; }
|
||||
|
||||
UIWidgetList getChildren() const { return m_children; }
|
||||
UIWidgetPtr getFocusedChild() const { return m_focusedChild; }
|
||||
@@ -100,18 +104,18 @@ public:
|
||||
void addChild(const UIWidgetPtr& child);
|
||||
void insertChild(int index, const UIWidgetPtr& child);
|
||||
void removeChild(const UIWidgetPtr& child);
|
||||
void focusChild(const UIWidgetPtr& child, UI::FocusReason reason);
|
||||
void focusNextChild(UI::FocusReason reason);
|
||||
void focusChild(const UIWidgetPtr& child, FocusReason reason);
|
||||
void focusNextChild(FocusReason reason);
|
||||
void moveChildToTop(const UIWidgetPtr& child);
|
||||
void lockChild(const UIWidgetPtr& child);
|
||||
void unlockChild(const UIWidgetPtr& child);
|
||||
|
||||
void updateParentLayout();
|
||||
void updateLayout();
|
||||
void updateChildrenLayout();
|
||||
|
||||
bool addAnchor(AnchorEdge edge, const std::string& hookedWidgetId, AnchorEdge hookedEdge);
|
||||
void centerIn(const std::string& hookedWidgetId);
|
||||
void fill(const std::string& hookedWidgetId);
|
||||
virtual void updateState(WidgetState state);
|
||||
void updateStates();
|
||||
virtual void updateStyle();
|
||||
void applyStyle(const OTMLNodePtr& styleNode);
|
||||
|
||||
UIWidgetPtr asUIWidget() { return std::static_pointer_cast<UIWidget>(shared_from_this()); }
|
||||
|
||||
@@ -119,19 +123,7 @@ private:
|
||||
void internalDestroy();
|
||||
void internalDestroyCheck();
|
||||
|
||||
void internalUpdateLayout();
|
||||
void internalUpdateChildrenLayout();
|
||||
|
||||
void addAnchoredWidget(const UIWidgetPtr& widget);
|
||||
void removeAnchoredWidget(const UIWidgetPtr& widget);
|
||||
void computeHookedWidgets();
|
||||
void clearHookedWidgets();
|
||||
void resetLayoutUpdateState(bool resetOwn);
|
||||
|
||||
bool m_layoutUpdated;
|
||||
bool m_updateEventScheduled;
|
||||
bool m_layoutUpdateScheduled;
|
||||
bool m_childrenLayoutUpdateScheduled;
|
||||
|
||||
protected:
|
||||
/// Triggered when widget style is changed
|
||||
@@ -139,7 +131,7 @@ protected:
|
||||
/// Triggered when widget is moved or resized
|
||||
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
|
||||
/// Triggered when widget gets or loses focus
|
||||
virtual void onFocusChange(bool focused, UI::FocusReason reason);
|
||||
virtual void onFocusChange(bool focused, FocusReason reason);
|
||||
/// Triggered when the mouse enters or leaves widget area
|
||||
virtual void onHoverChange(bool hovered);
|
||||
/// Triggered when user presses key while widget has focus
|
||||
@@ -147,30 +139,33 @@ protected:
|
||||
/// Triggered when user releases key while widget has focus
|
||||
virtual bool onKeyRelease(uchar keyCode, char keyChar, int keyboardModifiers);
|
||||
/// Triggered when a mouse button is pressed down while mouse pointer is inside widget area
|
||||
virtual bool onMousePress(const Point& mousePos, UI::MouseButton button);
|
||||
virtual bool onMousePress(const Point& mousePos, MouseButton button);
|
||||
/// Triggered when a mouse button is released
|
||||
virtual bool onMouseRelease(const Point& mousePos, UI::MouseButton button);
|
||||
virtual bool onMouseRelease(const Point& mousePos, MouseButton button);
|
||||
/// Triggered when mouse moves (even when the mouse is outside widget area)
|
||||
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
|
||||
/// Triggered when mouse middle button wheels inside widget area
|
||||
virtual bool onMouseWheel(const Point& mousePos, UI::MouseWheelDirection direction);
|
||||
virtual bool onMouseWheel(const Point& mousePos, MouseWheelDirection direction);
|
||||
|
||||
friend class UIManager;
|
||||
|
||||
protected:
|
||||
std::string m_id;
|
||||
FocusReason m_lastFocusReason;
|
||||
bool m_enabled;
|
||||
bool m_visible;
|
||||
bool m_hovered;
|
||||
bool m_focusable;
|
||||
bool m_destroyed;
|
||||
bool m_fixedSize;
|
||||
bool m_pressed;
|
||||
Rect m_rect;
|
||||
UIAnchorList m_anchors;
|
||||
UIWidgetList m_anchoredWidgets;
|
||||
UILayoutPtr m_layout;
|
||||
UIWidgetWeakPtr m_parent;
|
||||
UIWidgetList m_children;
|
||||
UIWidgetList m_lockedChildren;
|
||||
UIWidgetPtr m_focusedChild;
|
||||
std::string m_id;
|
||||
OTMLNodePtr m_style;
|
||||
OTMLNodePtr m_stateStyle;
|
||||
uint m_states;
|
||||
|
||||
// basic style components used by all widgets
|
||||
ImagePtr m_image;
|
||||
|
||||
Reference in New Issue
Block a user