2011-08-28 15:17:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#ifndef UIWIDGET_H
|
|
|
|
#define UIWIDGET_H
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include "declarations.h"
|
|
|
|
#include <framework/luascript/luaobject.h>
|
|
|
|
#include <framework/graphics/declarations.h>
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
class UIWidget : public LuaObject
|
|
|
|
{
|
|
|
|
public:
|
2011-08-22 14:44:26 +02:00
|
|
|
UIWidget();
|
2011-11-17 18:43:41 +01:00
|
|
|
virtual ~UIWidget() { }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
template<class T>
|
2011-11-17 18:43:41 +01:00
|
|
|
static std::shared_ptr<T> create() { auto t = std::shared_ptr<T>(new T); return t; }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void destroy();
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
virtual void render();
|
2011-11-03 10:59:11 +01:00
|
|
|
void renderSelf();
|
|
|
|
void renderChildren();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-30 01:40:56 +02:00
|
|
|
void setVisible(bool visible);
|
2011-11-16 22:00:40 +01:00
|
|
|
void setEnabled(bool enabled);
|
2011-08-28 18:02:26 +02:00
|
|
|
void setPressed(bool pressed) { m_pressed = pressed; updateState(Fw::PressedState); }
|
2011-08-14 04:09:11 +02:00
|
|
|
void setId(const std::string& id) { m_id = id; }
|
2011-11-16 22:00:40 +01:00
|
|
|
void setFocusable(bool focusable);
|
2011-11-01 17:41:15 +01:00
|
|
|
void setPhantom(bool phantom) { m_phantom = phantom; }
|
2011-08-26 17:06:52 +02:00
|
|
|
void setStyle(const std::string& styleName);
|
|
|
|
void setStyleFromNode(const OTMLNodePtr& styleNode);
|
|
|
|
void setLayout(const UILayoutPtr& layout) { m_layout = layout; }
|
2011-08-14 04:09:11 +02:00
|
|
|
void setParent(const UIWidgetPtr& parent);
|
2011-08-21 21:43:05 +02:00
|
|
|
void setRect(const Rect& rect);
|
2011-08-22 21:13:52 +02:00
|
|
|
void setX(int x) { moveTo(Point(x, getY())); }
|
|
|
|
void setY(int y) { moveTo(Point(getX(), y)); }
|
2011-08-14 04:09:11 +02:00
|
|
|
void setWidth(int width) { resize(Size(width, getHeight())); }
|
|
|
|
void setHeight(int height) { resize(Size(getWidth(), height)); }
|
|
|
|
void setImage(const ImagePtr& image) { m_image = image; }
|
2011-08-14 16:09:26 +02:00
|
|
|
virtual void setFont(const FontPtr& font) { m_font = font; }
|
2011-08-14 19:45:25 +02:00
|
|
|
void setOpacity(int opacity) { m_opacity = opacity; }
|
2011-08-20 22:30:41 +02:00
|
|
|
void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
|
|
|
|
void setForegroundColor(const Color& color) { m_foregroundColor = color; }
|
2011-08-26 17:06:52 +02:00
|
|
|
void setMarginTop(int margin) { m_marginTop = margin; updateParentLayout(); }
|
2011-11-17 22:41:02 +01:00
|
|
|
void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); }
|
2011-08-26 17:06:52 +02:00
|
|
|
void setMarginBottom(int margin) { m_marginBottom = margin; updateParentLayout(); }
|
2011-11-17 22:41:02 +01:00
|
|
|
void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); }
|
2011-08-26 17:06:52 +02:00
|
|
|
void setSizeFixed(bool fixed) { m_fixedSize = fixed; updateParentLayout(); }
|
2011-08-28 18:02:26 +02:00
|
|
|
void setLastFocusReason(Fw::FocusReason reason) { m_lastFocusReason = reason; }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void resize(const Size& size) { setRect(Rect(getPosition(), size)); }
|
|
|
|
void moveTo(const Point& pos) { setRect(Rect(pos, getSize())); }
|
2011-08-14 04:09:11 +02:00
|
|
|
void hide() { setVisible(false); }
|
|
|
|
void show() { setVisible(true); }
|
|
|
|
void disable() { setEnabled(false); }
|
|
|
|
void enable() { setEnabled(true); }
|
2011-08-29 16:14:21 +02:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
2011-11-03 10:59:11 +01:00
|
|
|
void focus();
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-11-16 00:47:32 +01:00
|
|
|
bool isActive() { return hasState(Fw::ActiveState); }
|
|
|
|
bool isEnabled() { return !hasState(Fw::DisabledState); }
|
|
|
|
bool isDisabled() { return hasState(Fw::DisabledState); }
|
|
|
|
bool isFocused() { return hasState(Fw::FocusState); }
|
|
|
|
bool isHovered() { return hasState(Fw::HoverState); }
|
|
|
|
bool isPressed() { return hasState(Fw::PressedState); }
|
2011-08-22 21:13:52 +02:00
|
|
|
bool isVisible();
|
2011-08-29 16:14:21 +02:00
|
|
|
bool isHidden() { return !isVisible(); }
|
2011-11-16 00:47:32 +01:00
|
|
|
bool isExplicitlyEnabled() { return m_enabled; }
|
|
|
|
bool isExplicitlyVisible() { return m_visible; }
|
|
|
|
bool isFocusable() { return m_focusable; }
|
|
|
|
bool isPhantom() { return m_phantom; }
|
|
|
|
bool isSizeFixed() { return m_fixedSize; }
|
|
|
|
bool hasChildren() { return m_children.size() > 0; }
|
2011-08-14 04:09:11 +02:00
|
|
|
bool hasChild(const UIWidgetPtr& child);
|
|
|
|
|
2011-11-16 00:47:32 +01:00
|
|
|
std::string getId() { return m_id; }
|
|
|
|
int getChildCount() { return m_children.size(); }
|
|
|
|
UILayoutPtr getLayout() { return m_layout; }
|
|
|
|
UIWidgetPtr getParent() { return m_parent.lock(); }
|
2011-08-21 21:43:05 +02:00
|
|
|
UIWidgetPtr getRootParent();
|
2011-11-16 00:47:32 +01:00
|
|
|
Point getPosition() { return m_rect.topLeft(); }
|
|
|
|
Size getSize() { return m_rect.size(); }
|
|
|
|
Rect getRect() { return m_rect; }
|
|
|
|
int getX() { return m_rect.x(); }
|
|
|
|
int getY() { return m_rect.y(); }
|
|
|
|
int getWidth() { return m_rect.width(); }
|
|
|
|
int getHeight() { return m_rect.height(); }
|
|
|
|
ImagePtr getImage() { return m_image; }
|
|
|
|
FontPtr getFont() { return m_font; }
|
|
|
|
Color getForegroundColor() { return m_foregroundColor; }
|
|
|
|
Color getBackgroundColor() { return m_backgroundColor; }
|
|
|
|
int getOpacity() { return m_opacity; }
|
|
|
|
int getMarginTop() { return m_marginTop; }
|
2011-11-17 22:41:02 +01:00
|
|
|
int getMarginRight() { return m_marginRight; }
|
2011-11-16 00:47:32 +01:00
|
|
|
int getMarginBottom() { return m_marginBottom; }
|
2011-11-17 22:41:02 +01:00
|
|
|
int getMarginLeft() { return m_marginLeft; }
|
2011-11-16 00:47:32 +01:00
|
|
|
Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
|
|
|
|
OTMLNodePtr getStyle() { return m_style; }
|
|
|
|
|
|
|
|
UIWidgetList getChildren() { return m_children; }
|
|
|
|
UIWidgetPtr getFocusedChild() { return m_focusedChild; }
|
2011-08-14 04:09:11 +02:00
|
|
|
UIWidgetPtr getChildAfter(const UIWidgetPtr& relativeChild);
|
|
|
|
UIWidgetPtr getChildBefore(const UIWidgetPtr& relativeChild);
|
|
|
|
UIWidgetPtr getChildById(const std::string& childId);
|
|
|
|
UIWidgetPtr getChildByPos(const Point& childPos);
|
2011-08-23 03:08:36 +02:00
|
|
|
UIWidgetPtr getChildByIndex(int index);
|
2011-08-21 21:43:05 +02:00
|
|
|
UIWidgetPtr recursiveGetChildById(const std::string& id);
|
2011-08-14 04:09:11 +02:00
|
|
|
UIWidgetPtr recursiveGetChildByPos(const Point& childPos);
|
|
|
|
UIWidgetPtr backwardsGetWidgetById(const std::string& id);
|
|
|
|
|
2011-08-22 21:13:52 +02:00
|
|
|
void addChild(const UIWidgetPtr& child);
|
2011-08-23 03:08:36 +02:00
|
|
|
void insertChild(int index, const UIWidgetPtr& child);
|
2011-08-22 21:13:52 +02:00
|
|
|
void removeChild(const UIWidgetPtr& child);
|
2011-08-28 18:02:26 +02:00
|
|
|
void focusChild(const UIWidgetPtr& child, Fw::FocusReason reason);
|
|
|
|
void focusNextChild(Fw::FocusReason reason);
|
2011-08-29 04:18:13 +02:00
|
|
|
void focusPreviousChild(Fw::FocusReason reason);
|
2011-08-22 21:13:52 +02:00
|
|
|
void moveChildToTop(const UIWidgetPtr& child);
|
2011-11-11 21:26:10 +01:00
|
|
|
void moveChildToIndex(const UIWidgetPtr& child, int index);
|
2011-08-22 21:13:52 +02:00
|
|
|
void lockChild(const UIWidgetPtr& child);
|
|
|
|
void unlockChild(const UIWidgetPtr& child);
|
2011-08-29 20:38:01 +02:00
|
|
|
bool isChildLocked(const UIWidgetPtr& child);
|
2011-11-11 21:26:10 +01:00
|
|
|
int getChildIndex(const UIWidgetPtr& child);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void updateParentLayout();
|
2011-08-21 21:43:05 +02:00
|
|
|
void updateLayout();
|
2011-11-16 00:47:32 +01:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
void updateStates();
|
2011-11-16 00:47:32 +01:00
|
|
|
virtual void updateState(Fw::WidgetState state);
|
2011-11-16 18:03:11 +01:00
|
|
|
bool setState(Fw::WidgetState state, bool on);
|
2011-11-16 00:47:32 +01:00
|
|
|
bool hasState(Fw::WidgetState state);
|
|
|
|
|
|
|
|
void updateStyle();
|
2011-08-26 17:06:52 +02:00
|
|
|
void applyStyle(const OTMLNodePtr& styleNode);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
UIWidgetPtr asUIWidget() { return std::static_pointer_cast<UIWidget>(shared_from_this()); }
|
|
|
|
|
|
|
|
protected:
|
2011-08-22 14:44:26 +02:00
|
|
|
/// Triggered when widget style is changed
|
|
|
|
virtual void onStyleApply(const OTMLNodePtr& styleNode);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when widget is moved or resized
|
2011-08-22 14:44:26 +02:00
|
|
|
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when widget gets or loses focus
|
2011-08-28 18:02:26 +02:00
|
|
|
virtual void onFocusChange(bool focused, Fw::FocusReason reason);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when the mouse enters or leaves widget area
|
2011-08-22 14:44:26 +02:00
|
|
|
virtual void onHoverChange(bool hovered);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when user presses key while widget has focus
|
2011-12-03 22:41:37 +01:00
|
|
|
virtual bool onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when user releases key while widget has focus
|
2011-12-03 22:41:37 +01:00
|
|
|
virtual bool onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when a mouse button is pressed down while mouse pointer is inside widget area
|
2011-08-28 18:02:26 +02:00
|
|
|
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when a mouse button is released
|
2011-11-13 06:11:47 +01:00
|
|
|
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when mouse moves (even when the mouse is outside widget area)
|
2011-08-22 14:44:26 +02:00
|
|
|
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
|
2011-08-14 04:09:11 +02:00
|
|
|
/// Triggered when mouse middle button wheels inside widget area
|
2011-08-28 18:02:26 +02:00
|
|
|
virtual bool onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
friend class UIManager;
|
|
|
|
|
2011-08-14 16:09:26 +02:00
|
|
|
protected:
|
2011-08-26 17:06:52 +02:00
|
|
|
std::string m_id;
|
2011-08-28 18:02:26 +02:00
|
|
|
Fw::FocusReason m_lastFocusReason;
|
2011-11-19 01:12:17 +01:00
|
|
|
Boolean<true> m_enabled;
|
|
|
|
Boolean<true> m_visible;
|
|
|
|
Boolean<true> m_focusable;
|
|
|
|
Boolean<false> m_fixedSize;
|
|
|
|
Boolean<false> m_pressed;
|
|
|
|
Boolean<false> m_phantom;
|
|
|
|
Boolean<false> m_updateEventScheduled;
|
|
|
|
Boolean<true> m_firstOnStyle;
|
2011-08-14 04:09:11 +02:00
|
|
|
Rect m_rect;
|
2011-08-26 17:06:52 +02:00
|
|
|
UILayoutPtr m_layout;
|
2011-08-14 04:09:11 +02:00
|
|
|
UIWidgetWeakPtr m_parent;
|
|
|
|
UIWidgetList m_children;
|
2011-08-22 21:13:52 +02:00
|
|
|
UIWidgetList m_lockedChildren;
|
2011-08-14 04:09:11 +02:00
|
|
|
UIWidgetPtr m_focusedChild;
|
2011-08-26 17:06:52 +02:00
|
|
|
OTMLNodePtr m_style;
|
|
|
|
OTMLNodePtr m_stateStyle;
|
2011-08-14 04:09:11 +02:00
|
|
|
ImagePtr m_image;
|
|
|
|
FontPtr m_font;
|
2011-08-20 22:30:41 +02:00
|
|
|
Color m_backgroundColor;
|
|
|
|
Color m_foregroundColor;
|
2011-11-17 18:43:41 +01:00
|
|
|
int m_states;
|
|
|
|
int m_opacity;
|
2011-08-14 04:09:11 +02:00
|
|
|
int m_marginTop;
|
2011-11-17 22:41:02 +01:00
|
|
|
int m_marginRight;
|
2011-08-14 04:09:11 +02:00
|
|
|
int m_marginBottom;
|
2011-11-17 22:41:02 +01:00
|
|
|
int m_marginLeft;
|
2011-08-14 04:09:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|