tibia-client/src/framework/ui/uiwidget.h

213 lines
9.2 KiB
C
Raw Normal View History

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-08-14 04:09:11 +02:00
virtual ~UIWidget();
template<class T>
static std::shared_ptr<T> create() { auto t = std::shared_ptr<T>(new T); t->setup(); return t; }
2011-08-14 04:09:11 +02:00
void destroy();
2011-08-20 22:30:41 +02:00
virtual void setup();
2011-08-14 04:09:11 +02:00
virtual void render();
void renderSelf();
void renderChildren();
2011-08-14 04:09:11 +02:00
2011-08-30 01:40:56 +02:00
void setVisible(bool visible);
void setEnabled(bool enabled) { m_enabled = enabled; updateState(Fw::DisabledState); }
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; }
void setFocusable(bool focusable) { m_focusable = focusable; }
2011-11-01 17:41:15 +01:00
void setPhantom(bool phantom) { m_phantom = phantom; }
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; }
virtual void setFont(const FontPtr& font) { m_font = font; }
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; }
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(Fw::FocusReason reason) { m_lastFocusReason = reason; }
2011-08-14 04:09:11 +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); }
void lock();
void unlock();
void focus();
2011-08-14 04:09:11 +02:00
bool isActive() const { return hasState(Fw::ActiveState); }
bool isEnabled() const { return !hasState(Fw::DisabledState); }
bool isDisabled() const { return hasState(Fw::DisabledState); }
bool isFocused() const { return hasState(Fw::FocusState); }
bool isHovered() const { return hasState(Fw::HoverState); }
bool isPressed() const { return hasState(Fw::PressedState); }
2011-08-22 21:13:52 +02:00
bool isVisible();
bool isHidden() { return !isVisible(); }
bool isExplicitlyEnabled() const { return m_enabled; }
2011-08-21 03:01:46 +02:00
bool isExplicitlyVisible() const { return m_visible; }
2011-08-14 04:09:11 +02:00
bool isFocusable() const { return m_focusable; }
2011-11-01 17:41:15 +01:00
bool isPhantom() const { return m_phantom; }
bool isSizeFixed() const { return m_fixedSize; }
2011-08-14 04:09:11 +02:00
bool hasChildren() const { return m_children.size() > 0; }
bool hasChild(const UIWidgetPtr& child);
bool hasState(Fw::WidgetState state) const { return m_states & state; }
2011-08-14 04:09:11 +02:00
std::string getId() const { return m_id; }
int getChildCount() const { return m_children.size(); }
UILayoutPtr getLayout() const { return m_layout; }
2011-08-14 04:09:11 +02:00
UIWidgetPtr getParent() const { return m_parent.lock(); }
2011-08-21 21:43:05 +02:00
UIWidgetPtr getRootParent();
2011-08-14 04:09:11 +02:00
Point getPosition() const { return m_rect.topLeft(); }
Size getSize() const { return m_rect.size(); }
2011-08-21 21:43:05 +02:00
Rect getRect() const { return m_rect; }
2011-08-14 04:09:11 +02:00
int getX() const { return m_rect.x(); }
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; }
2011-08-20 22:30:41 +02:00
Color getForegroundColor() const { return m_foregroundColor; }
Color getBackgroundColor() const { return m_backgroundColor; }
int getOpacity() const { return m_opacity; }
2011-08-14 04:09:11 +02:00
int getMarginLeft() const { return m_marginLeft; }
int getMarginRight() const { return m_marginRight; }
int getMarginTop() const { return m_marginTop; }
int getMarginBottom() const { return m_marginBottom; }
Fw::FocusReason getLastFocusReason() const { return m_lastFocusReason; }
2011-08-14 04:09:11 +02:00
UIWidgetList getChildren() const { return m_children; }
UIWidgetPtr getFocusedChild() const { return m_focusedChild; }
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);
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);
void lockChild(const UIWidgetPtr& child);
void unlockChild(const UIWidgetPtr& child);
2011-08-29 20:38:01 +02:00
bool isChildLocked(const UIWidgetPtr& child);
2011-08-14 04:09:11 +02:00
void updateParentLayout();
2011-08-21 21:43:05 +02:00
void updateLayout();
virtual void updateState(Fw::WidgetState state);
void updateStates();
virtual void updateStyle();
void applyStyle(const OTMLNodePtr& styleNode);
2011-08-14 04:09:11 +02:00
UIWidgetPtr asUIWidget() { return std::static_pointer_cast<UIWidget>(shared_from_this()); }
2011-08-21 21:43:05 +02:00
private:
bool m_updateEventScheduled;
2011-08-14 04:09:11 +02:00
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
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-08-22 14:44:26 +02:00
virtual bool onKeyPress(uchar keyCode, char keyChar, int keyboardModifiers);
2011-08-14 04:09:11 +02:00
/// Triggered when user releases key while widget has focus
2011-08-22 14:44:26 +02:00
virtual bool onKeyRelease(uchar keyCode, char keyChar, 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
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
2011-08-14 04:09:11 +02:00
/// Triggered when a mouse button is released
virtual bool 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
virtual bool onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
2011-08-14 04:09:11 +02:00
friend class UIManager;
protected:
std::string m_id;
Fw::FocusReason m_lastFocusReason;
2011-08-14 04:09:11 +02:00
bool m_enabled;
bool m_visible;
bool m_focusable;
bool m_fixedSize;
bool m_pressed;
2011-11-01 17:41:15 +01:00
bool m_phantom;
2011-08-14 04:09:11 +02:00
Rect m_rect;
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;
OTMLNodePtr m_style;
OTMLNodePtr m_stateStyle;
uint m_states;
2011-08-14 04:09:11 +02:00
// basic style components used by all widgets
ImagePtr m_image;
FontPtr m_font;
int m_opacity;
2011-08-20 22:30:41 +02:00
Color m_backgroundColor;
Color m_foregroundColor;
2011-08-14 04:09:11 +02:00
int m_marginLeft;
int m_marginRight;
int m_marginTop;
int m_marginBottom;
};
#endif