major UIWidget rewrite with new features

This commit is contained in:
Eduardo Bart
2012-01-10 20:13:40 -02:00
parent 044213c6cd
commit a1374baee1
83 changed files with 1990 additions and 2010 deletions

View File

@@ -27,11 +27,7 @@
class UIManager;
class UIWidget;
class UILabel;
class UIButton;
class UILineEdit;
class UICheckBox;
class UIProgressBar;
class UIFrameCounter;
class UIWindow;
class UILayout;
@@ -41,11 +37,7 @@ class UIAnchorLayout;
typedef std::shared_ptr<UIWidget> UIWidgetPtr;
typedef std::weak_ptr<UIWidget> UIWidgetWeakPtr;
typedef std::shared_ptr<UILabel> UILabelPtr;
typedef std::shared_ptr<UIButton> UIButtonPtr;
typedef std::shared_ptr<UILineEdit> UILineEditPtr;
typedef std::shared_ptr<UICheckBox> UICheckBoxPtr;
typedef std::shared_ptr<UIProgressBar> UIProgressBarPtr;
typedef std::shared_ptr<UIFrameCounter> UIFrameCounterPtr;
typedef std::shared_ptr<UIWindow> UIWindowPtr;
typedef std::shared_ptr<UILayout> UILayoutPtr;

View File

@@ -28,8 +28,6 @@
#include "uilineedit.h"
#include "uiwindow.h"
#include "uiframecounter.h"
#include "uiprogressbar.h"
#include "uicheckbox.h"
#include "uilayout.h"
#include "uiverticallayout.h"
#include "uianchorlayout.h"

View File

@@ -1,79 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uicheckbox.h"
#include "uitranslator.h"
#include <framework/otml/otmlnode.h>
#include <framework/graphics/image.h>
#include <framework/graphics/font.h>
#include <framework/graphics/graphics.h>
#include <framework/core/eventdispatcher.h>
UICheckBox::UICheckBox()
{
m_focusable = false;
m_textAlign = Fw::AlignLeft;
}
void UICheckBox::render()
{
if(m_image) {
Rect boxRect;
boxRect.resize(m_boxSize);
boxRect.moveLeft(m_rect.left());
boxRect.moveVerticalCenter(m_rect.verticalCenter());
g_painter.setColor(m_backgroundColor);
m_image->draw(boxRect);
}
if(m_text.length()) {
Rect textRect(m_rect);
textRect.setTopLeft(textRect.topLeft() + m_textOffset);
m_font->renderText(m_text, textRect, m_textAlign, m_color);
}
}
void UICheckBox::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed() && getRect().contains(mousePos))
setChecked(!isChecked());
}
void UICheckBox::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleName, styleNode);
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "text-offset")
m_textOffset = node->value<Point>();
else if(node->tag() == "text")
m_text = node->value();
else if(node->tag() == "box-size")
m_boxSize = node->value<Size>();
else if(node->tag() == "text-align")
m_textAlign = Fw::translateAlignment(node->value());
else if(node->tag() == "checked") {
// must be scheduled because setChecked can change the style again
g_dispatcher.addEvent(std::bind(&UICheckBox::setChecked, asUICheckBox(), node->value<bool>()));
}
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef UICHECKBOX_H
#define UICHECKBOX_H
#include "uiwidget.h"
class UICheckBox : public UIWidget
{
public:
UICheckBox();
void render();
void setText(const std::string& text) { m_text = text; }
std::string getText() { return m_text; }
UICheckBoxPtr asUICheckBox() { return std::static_pointer_cast<UICheckBox>(shared_from_this()); }
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
std::string m_text;
Size m_boxSize;
Point m_textOffset;
Fw::AlignmentFlag m_textAlign;
};
#endif

View File

@@ -36,9 +36,9 @@ UIFrameCounter::UIFrameCounter()
m_frameCount = 0;
}
void UIFrameCounter::render()
void UIFrameCounter::draw()
{
UIWidget::render();
UIWidget::draw();
if(g_clock.ticksElapsed(m_lastFrameTicks) >= 1000) {
m_fpsText = Fw::mkstr("FPS: ", m_frameCount);

View File

@@ -29,7 +29,7 @@ class UIFrameCounter : public UIWidget
{
public:
UIFrameCounter();
virtual void render();
virtual void draw();
void setAlign(Fw::AlignmentFlag align) { m_align = align; }
Fw::AlignmentFlag getAlign() { return m_align; }

View File

@@ -1,41 +0,0 @@
#include "uiimage.h"
#include <framework/otml/otmlnode.h>
void UIImage::draw(const Rect& screenCoords)
{
}
void UIImage::applyStyle(const OTMLNodePtr& styleNode)
{
/*
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "image-source")
setImageSource(node->value());
else if(node->tag() == "image-clip")
setImageClip(node->value<Rect>());
else if(node->tag() == "image-rect")
setImageRect(node->value<Rect>());
else if(node->tag() == "image-fixed-ratio")
setImageFixedRatio(node->value<bool>());
else if(node->tag() == "image-repeated")
setImageRepeated(node->value<bool>());
else if(node->tag() == "image-smooth")
setImageSmooth(node->value<bool>());
else if(node->tag() == "image-color")
setImageColor(node->value<Color>());
else if(node->tag() == "image-border-top")
setImageBorderTop(node->value<int>());
else if(node->tag() == "image-border-right")
setImageBorderRight(node->value<int>());
else if(node->tag() == "image-border-bottom")
setImageBorderBottom(node->value<int>());
else if(node->tag() == "image-border-left")
setImageBorderLeft(node->value<int>());
else if(node->tag() == "image-border") {
}
}
*/
}

View File

@@ -1,69 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef UIIMAGE_H
#define UIIMAGE_H
#include <framework/graphics/declarations.h>
#include <framework/otml/declarations.h>
#include "declarations.h"
class UIImage
{
public:
void draw(const Rect& screenCoords);
void applyStyle(const OTMLNodePtr& styleNode);
void setImageSource(const std::string& source);
void setImageClip(const Rect& clipRect);
void setImageRect(const Rect& rect);
void setImageFixedRatio(bool fixedRatio);
void setImageRepeated(bool repeated);
void setImageSmooth(bool smooth);
void setImageColor(const Color& color);
void setImageBorderTop(int border);
void setImageBorderRight(int border);
void setImageBorderBottom(int border);
void setImageBorderLeft(int border);
protected:
TexturePtr m_imageTexture;
Rect m_imageClipRect;
Rect m_imageRect;
Boolean<false> m_imageFixedRatio;
Boolean<false> m_imageRepeated;
Color m_imageColor;
// border image coords
Rect m_leftBorderTexCoords;
Rect m_rightBorderTexCoords;
Rect m_topBorderTexCoords;
Rect m_bottomBorderTexCoords;
Rect m_topLeftCornerTexCoords;
Rect m_topRightCornerTexCoords;
Rect m_bottomLeftCornerTexCoords;
Rect m_bottomRightCornerTexCoords;
Rect m_centerTexCoords;
Size m_bordersSize;
};
#endif

View File

@@ -26,6 +26,9 @@
void UILayout::update()
{
if(m_updateDisabled)
return;
assert(!m_updating);
m_updating = true;
internalUpdate();
@@ -34,7 +37,7 @@ void UILayout::update()
void UILayout::updateLater()
{
if(m_updateScheduled)
if(m_updateDisabled || m_updateScheduled)
return;
if(!getParentWidget())

View File

@@ -38,9 +38,13 @@ public:
virtual void applyStyle(const OTMLNodePtr& styleNode) { }
virtual void addWidget(const UIWidgetPtr& widget) = 0;
virtual void removeWidget(const UIWidgetPtr& widget) = 0;
void disableUpdates() { m_updateDisabled = true; }
void enableUpdates() { m_updateDisabled = false; }
void setParent(UIWidgetPtr parentWidget) { m_parentWidget = parentWidget; }
UIWidgetPtr getParentWidget() { return m_parentWidget.lock(); }
bool isUpdateDisabled() { return m_updateDisabled; }
bool isUpdating() { return m_updating; }
virtual bool needsUpdatesOnChildChange() { return false; }
@@ -51,6 +55,7 @@ public:
protected:
virtual void internalUpdate() = 0;
Boolean<false> m_updateDisabled;
Boolean<false> m_updating;
Boolean<false> m_updateScheduled;
UIWidgetWeakPtr m_parentWidget;

View File

@@ -38,7 +38,7 @@ UILineEdit::UILineEdit()
blinkCursor();
}
void UILineEdit::renderSelf()
void UILineEdit::drawSelf()
{
drawBackground(m_rect);
drawBorder(m_rect);
@@ -140,8 +140,8 @@ void UILineEdit::update()
}
Rect textScreenCoords = m_rect;
textScreenCoords.addLeft(-m_textHorizontalMargin);
textScreenCoords.addRight(-m_textHorizontalMargin);
textScreenCoords.expandLeft(-m_textHorizontalMargin);
textScreenCoords.expandRight(-m_textHorizontalMargin);
m_drawArea = textScreenCoords;
if(m_textAlign & Fw::AlignBottom) {
@@ -339,8 +339,8 @@ int UILineEdit::getTextPos(Point pos)
int candidatePos = -1;
for(int i=0;i<textLength;++i) {
Rect clickGlyphRect = m_glyphsCoords[i];
clickGlyphRect.addTop(m_font->getYOffset() + m_font->getGlyphSpacing().height());
clickGlyphRect.addLeft(m_font->getGlyphSpacing().width()+1);
clickGlyphRect.expandTop(m_font->getYOffset() + m_font->getGlyphSpacing().height());
clickGlyphRect.expandLeft(m_font->getGlyphSpacing().width()+1);
if(clickGlyphRect.contains(pos))
return i;
else if(pos.y >= clickGlyphRect.top() && pos.y <= clickGlyphRect.bottom()) {

View File

@@ -30,7 +30,7 @@ class UILineEdit : public UIWidget
public:
UILineEdit();
virtual void renderSelf();
virtual void drawSelf();
private:
void update();

View File

@@ -47,12 +47,12 @@ void UIManager::terminate()
void UIManager::render()
{
m_rootWidget->render();
m_rootWidget->draw();
}
void UIManager::resize(const Size& size)
{
m_rootWidget->resize(g_window.getSize());
m_rootWidget->setSize(g_window.getSize());
}
void UIManager::inputEvent(const InputEvent& event)

View File

@@ -1,53 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uiprogressbar.h"
#include <framework/graphics/graphics.h>
#include <framework/otml/otmlnode.h>
UIProgressBar::UIProgressBar()
{
m_phantom = false;
m_focusable = false;
m_percent = 0;
}
void UIProgressBar::render()
{
UIWidget::render();
g_painter.setColor(m_color);
g_painter.drawBoundingRect(m_rect, 1);
Rect fillRect = m_rect.expanded(-1);
fillRect.setWidth(fillRect.width() * m_percent / 100.0);
g_painter.setColor(m_backgroundColor);
g_painter.drawFilledRect(fillRect);
}
void UIProgressBar::setPercent(double percent)
{
if(percent == NAN)
percent = 0;
m_percent = std::min(std::max(percent, 0.0), 100.0);
}

View File

@@ -1,41 +0,0 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef UIPROGRESSBAR_H
#define UIPROGRESSBAR_H
#include "uiwidget.h"
class UIProgressBar : public UIWidget
{
public:
UIProgressBar();
virtual void render();
void setPercent(double percent);
double getPercent() { return m_percent; }
private:
double m_percent;
};
#endif

View File

@@ -97,7 +97,7 @@ void UIVerticalLayout::internalUpdate()
pos.y += gap;
prefferedHeight += gap;
if(widget->isSizeFixed()) {
if(widget->isFixedSize()) {
// center it
pos.x = childrenRect.left() + (childrenRect.width() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2;
pos.x = std::max(pos.x, parentWidget->getX());

File diff suppressed because it is too large Load Diff

View File

@@ -28,154 +28,50 @@
#include <framework/graphics/declarations.h>
#include <framework/otml/otmlnode.h>
#include <framework/graphics/font.h>
#include <framework/graphics/coordsbuffer.h>
template<typename T = int>
struct EdgeGroup {
EdgeGroup() { top = right = bottom = left = T(0); }
void set(T value) { top = right = bottom = left = value; }
T top;
T right;
T bottom;
T left;
};
class UIWidget : public LuaObject
{
// widget core
public:
UIWidget();
virtual ~UIWidget() { }
void destroy();
virtual ~UIWidget();
protected:
virtual void render();
virtual void renderSelf();
virtual void renderChildren();
virtual void draw();
virtual void drawSelf();
virtual void drawChildren();
friend class UIManager;
void drawBackground(const Rect& screenCoords);
void drawBorder(const Rect& screenCoords);
void drawImage(const Rect& screenCoords);
void drawIcon(const Rect& screenCoords);
void drawText(const Rect& screenCoords);
std::string m_id;
Rect m_rect;
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_destroyed;
UILayoutPtr m_layout;
UIWidgetWeakPtr m_parent;
UIWidgetList m_children;
UIWidgetList m_lockedChildren;
UIWidgetPtr m_focusedChild;
OTMLNodePtr m_style;
Fw::FocusReason m_lastFocusReason;
public:
void setVisible(bool visible);
void setEnabled(bool enabled);
void setPressed(bool pressed);
void setOn(bool on);
void setChecked(bool checked);
void setId(const std::string& id) { m_id = id; }
void setFocusable(bool focusable);
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; }
void setParent(const UIWidgetPtr& parent);
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 setImage(const ImagePtr& image) { m_image = image; }
void setIcon(const std::string& iconFile);
void setOpacity(int opacity) { m_opacity = opacity; }
void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
void setColor(const Color& color) { m_color = color; }
void setMarginTop(int margin) { m_marginTop = margin; updateParentLayout(); }
void setMarginRight(int margin) { m_marginRight = margin; updateParentLayout(); }
void setMarginBottom(int margin) { m_marginBottom = margin; updateParentLayout(); }
void setMarginLeft(int margin) { m_marginLeft = margin; updateParentLayout(); }
void setPaddingTop(int padding) { m_paddingTop = padding; updateLayout(); }
void setPaddingRight(int padding) { m_paddingRight = padding; updateLayout(); }
void setPaddingBottom(int padding) { m_paddingBottom = padding; updateLayout(); }
void setPaddingLeft(int padding) { m_paddingLeft = padding; updateLayout(); }
void setText(const std::string& text);
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; }
void setTextOffset(const Point& offset) { m_textOffset = offset; }
void setFont(const std::string& fontName);
void setSizeFixed(bool fixed) { m_fixedSize = fixed; updateParentLayout(); }
void setLastFocusReason(Fw::FocusReason reason) { m_lastFocusReason = reason; }
void bindRectToParent();
void resize(const Size& size) { setRect(Rect(getPos(), size)); }
void resizeToText() { resize(getTextSize()); }
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); }
void lock();
void unlock();
void focus();
void grabMouse();
void ungrabMouse();
void grabKeyboard();
void ungrabKeyboard();
void clearText() { setText(""); }
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); }
bool isFirst() { return hasState(Fw::FirstState); }
bool isMiddle() { return hasState(Fw::MiddleState); }
bool isLast() { return hasState(Fw::LastState); }
bool isAlternate() { return hasState(Fw::AlternateState); }
bool isChecked() { return hasState(Fw::CheckedState); }
bool isOn() { return hasState(Fw::OnState); }
bool isVisible();
bool isHidden() { return !isVisible(); }
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 isDestroyed() { return m_destroyed; }
bool containsPoint(const Point& point) { return m_rect.contains(point); }
bool hasChildren() { return m_children.size() > 0; }
bool hasChild(const UIWidgetPtr& child);
std::string getId() { return m_id; }
int getChildCount() { return m_children.size(); }
UILayoutPtr getLayout() { return m_layout; }
UIWidgetPtr getParent() { return m_parent.lock(); }
UIWidgetPtr getRootParent();
Point getPos() { return m_rect.topLeft(); }
Size getSize() { return m_rect.size(); }
Rect getRect() { return m_rect; }
Rect getChildrenRect();
int getX() { return m_rect.x(); }
int getY() { return m_rect.y(); }
int getWidth() { return m_rect.width(); }
int getHeight() { return m_rect.height(); }
Color getColor() { return m_color; }
Color getBackgroundColor() { return m_backgroundColor; }
int getOpacity() { return m_opacity; }
int getMarginTop() { return m_marginTop; }
int getMarginRight() { return m_marginRight; }
int getMarginBottom() { return m_marginBottom; }
int getMarginLeft() { return m_marginLeft; }
int getPaddingTop() { return m_paddingTop; }
int getPaddingRight() { return m_paddingRight; }
int getPaddingBottom() { return m_paddingBottom; }
int getPaddingLeft() { return m_paddingLeft; }
std::string getText() { return m_text; }
Fw::AlignmentFlag getTextAlign() { return m_textAlign; }
Point getTextOffset() { return m_textOffset; }
std::string getFont() { return m_font->getName(); }
Size getTextSize() { return m_font->calculateTextRectSize(m_text); }
Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
OTMLNodePtr getStyle() { return m_style; }
std::string getStyleName() { return m_style->tag(); }
UIWidgetList getChildren() { return m_children; }
UIWidgetPtr getFocusedChild() { return m_focusedChild; }
UIWidgetPtr getChildAfter(const UIWidgetPtr& relativeChild);
UIWidgetPtr getChildBefore(const UIWidgetPtr& relativeChild);
UIWidgetPtr getChildById(const std::string& childId);
UIWidgetPtr getChildByPos(const Point& childPos);
UIWidgetPtr getChildByIndex(int index);
UIWidgetPtr getFirstChild() { return getChildByIndex(1); }
UIWidgetPtr getLastChild() { return getChildByIndex(-1); }
UIWidgetPtr recursiveGetChildById(const std::string& id);
UIWidgetPtr recursiveGetChildByPos(const Point& childPos);
UIWidgetPtr backwardsGetWidgetById(const std::string& id);
void addChild(const UIWidgetPtr& child);
void insertChild(int index, const UIWidgetPtr& child);
void removeChild(const UIWidgetPtr& child);
@@ -186,15 +82,63 @@ public:
void moveChildToIndex(const UIWidgetPtr& child, int index);
void lockChild(const UIWidgetPtr& child);
void unlockChild(const UIWidgetPtr& child);
bool isChildLocked(const UIWidgetPtr& child);
int getChildIndex(const UIWidgetPtr& child);
void applyStyle(const OTMLNodePtr& styleNode);
void addAnchor(Fw::AnchorEdge anchoredEdge, const std::string& hookedWidgetId, Fw::AnchorEdge hookedEdge);
void fill(const std::string& hookedWidgetId);
void centerIn(const std::string& hookedWidgetId);
void breakAnchors();
void updateParentLayout();
void updateLayout();
void applyStyle(const OTMLNodePtr& styleNode);
void lock();
void unlock();
void focus();
void grabMouse();
void ungrabMouse();
void grabKeyboard();
void ungrabKeyboard();
void bindRectToParent();
void destroy();
void setId(const std::string& id);
void setParent(const UIWidgetPtr& parent);
void setLayout(const UILayoutPtr& layout);
void setRect(const Rect& rect);
void setStyle(const std::string& styleName);
void setStyleFromNode(const OTMLNodePtr& styleNode);
void setEnabled(bool enabled);
void setVisible(bool visible);
void setPressed(bool pressed);
void setOn(bool on);
void setChecked(bool checked);
void setFocusable(bool focusable);
void setPhantom(bool phantom);
void setFixedSize(bool fixed);
void setLastFocusReason(Fw::FocusReason reason);
bool isVisible();
bool isChildLocked(const UIWidgetPtr& child);
bool hasChild(const UIWidgetPtr& child);
int getChildIndex(const UIWidgetPtr& child);
Rect getChildrenRect();
UIAnchorLayoutPtr getAnchoredLayout();
UIWidgetPtr getRootParent();
UIWidgetPtr getChildAfter(const UIWidgetPtr& relativeChild);
UIWidgetPtr getChildBefore(const UIWidgetPtr& relativeChild);
UIWidgetPtr getChildById(const std::string& childId);
UIWidgetPtr getChildByPos(const Point& childPos);
UIWidgetPtr getChildByIndex(int index);
UIWidgetPtr recursiveGetChildById(const std::string& id);
UIWidgetPtr recursiveGetChildByPos(const Point& childPos);
UIWidgetPtr backwardsGetWidgetById(const std::string& id);
UIWidgetPtr asUIWidget() { return std::static_pointer_cast<UIWidget>(shared_from_this()); }
private:
Boolean<false> m_updateEventScheduled;
Boolean<false> m_loadingStyle;
// state managment
protected:
bool setState(Fw::WidgetState state, bool on);
bool hasState(Fw::WidgetState state);
@@ -203,16 +147,20 @@ private:
void updateState(Fw::WidgetState state);
void updateStates();
void updateChildrenIndexStates();
void updateStyle();
Boolean<false> m_updateStyleScheduled;
Boolean<true> m_firstOnStyle;
OTMLNodePtr m_stateStyle;
int m_states;
// event processing
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
virtual void onFocusChange(bool focused, Fw::FocusReason reason);
virtual void onHoverChange(bool hovered);
virtual void onTextChange(const std::string& text);
virtual void onFontChange(const std::string& font);
virtual bool onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers);
virtual bool onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
@@ -227,46 +175,259 @@ protected:
bool propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved);
bool propagateOnMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
// function shortcuts
public:
void resize(int width, int height) { setRect(Rect(getPos(), Size(width, height))); }
void move(int x, int y) { setRect(Rect(x, y, getSize())); }
void hide() { setVisible(false); }
void show() { setVisible(true); }
void disable() { setEnabled(false); }
void enable() { setEnabled(true); }
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); }
bool isFirst() { return hasState(Fw::FirstState); }
bool isMiddle() { return hasState(Fw::MiddleState); }
bool isLast() { return hasState(Fw::LastState); }
bool isAlternate() { return hasState(Fw::AlternateState); }
bool isChecked() { return hasState(Fw::CheckedState); }
bool isOn() { return hasState(Fw::OnState); }
bool isHidden() { return !isVisible(); }
bool isExplicitlyEnabled() { return m_enabled; }
bool isExplicitlyVisible() { return m_visible; }
bool isFocusable() { return m_focusable; }
bool isPhantom() { return m_phantom; }
bool isFixedSize() { return m_fixedSize; }
bool isDestroyed() { return m_destroyed; }
bool hasChildren() { return m_children.size() > 0; }
bool containsPoint(const Point& point) { return m_rect.contains(point); }
std::string getId() { return m_id; }
UIWidgetPtr getParent() { return m_parent.lock(); }
UIWidgetPtr getFocusedChild() { return m_focusedChild; }
UIWidgetList getChildren() { return m_children; }
UIWidgetPtr getFirstChild() { return getChildByIndex(1); }
UIWidgetPtr getLastChild() { return getChildByIndex(-1); }
UILayoutPtr getLayout() { return m_layout; }
OTMLNodePtr getStyle() { return m_style; }
int getChildCount() { return m_children.size(); }
Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
std::string getStyleName() { return m_style->tag(); }
// base style
private:
void initBaseStyle();
void parseBaseStyle(const OTMLNodePtr& styleNode);
protected:
std::string m_id;
Fw::FocusReason m_lastFocusReason;
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<false> m_loadingStyle;
Boolean<false> m_updateStyleScheduled;
Boolean<true> m_firstOnStyle;
Boolean<false> m_destroyed;
Rect m_rect;
UILayoutPtr m_layout;
UIWidgetWeakPtr m_parent;
UIWidgetList m_children;
UIWidgetList m_lockedChildren;
UIWidgetPtr m_focusedChild;
OTMLNodePtr m_style;
OTMLNodePtr m_stateStyle;
ImagePtr m_image;
TexturePtr m_icon;
FontPtr m_font;
Color m_backgroundColor;
void drawBackground(const Rect& screenCoords);
void drawBorder(const Rect& screenCoords);
void drawIcon(const Rect& screenCoords);
Color m_color;
int m_states;
int m_opacity;
int m_marginTop;
int m_marginRight;
int m_marginBottom;
int m_marginLeft;
int m_paddingTop;
int m_paddingRight;
int m_paddingBottom;
int m_paddingLeft;
Color m_backgroundColor;
Rect m_backgroundRect;
TexturePtr m_icon;
Color m_iconColor;
Rect m_iconRect;
EdgeGroup<Color> m_borderColor;
EdgeGroup<int> m_borderWidth;
EdgeGroup<int> m_margin;
EdgeGroup<int> m_padding;
float m_opacity;
public:
void setX(int x) { move(x, getY()); }
void setY(int y) { move(getX(), y); }
void setWidth(int width) { resize(width, getHeight()); }
void setHeight(int height) { resize(getWidth(), height); }
void setSize(const Size& size) { resize(size.width(), size.height()); }
void setPos(const Point& pos) { move(pos.x, pos.y); }
void setColor(const Color& color) { m_color = color; }
void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
void setBackgroundOffsetX(int x) { m_backgroundRect.setX(x); }
void setBackgroundOffsetY(int y) { m_backgroundRect.setX(y); }
void setBackgroundOffset(const Point& pos) { m_backgroundRect.move(pos); }
void setBackgroundWidth(int width) { m_backgroundRect.setWidth(width); }
void setBackgroundHeight(int height) { m_backgroundRect.setHeight(height); }
void setBackgroundSize(const Size& size) { m_backgroundRect.resize(size); }
void setBackgroundRect(const Rect& rect) { m_backgroundRect = rect; }
void setIcon(const std::string& iconFile);
void setIconColor(const Color& color) { m_iconColor = color; }
void setIconOffsetX(int x) { m_iconRect.setX(x); }
void setIconOffsetY(int y) { m_iconRect.setX(y); }
void setIconOffset(const Point& pos) { m_iconRect.move(pos); }
void setIconWidth(int width) { m_iconRect.setWidth(width); }
void setIconHeight(int height) { m_iconRect.setHeight(height); }
void setIconSize(const Size& size) { m_iconRect.resize(size); }
void setIconRect(const Rect& rect) { m_iconRect = rect; }
void setBorderWidth(int width) { m_borderWidth.set(width); updateLayout(); }
void setBorderWidthTop(int width) { m_borderWidth.top = width; }
void setBorderWidthRight(int width) { m_borderWidth.right = width; }
void setBorderWidthBottom(int width) { m_borderWidth.bottom = width; }
void setBorderWidthLeft(int width) { m_borderWidth.left = width; }
void setBorderColor(const Color& color) { m_borderColor.set(color); updateLayout(); }
void setBorderColorTop(const Color& color) { m_borderColor.top = color; }
void setBorderColorRight(const Color& color) { m_borderColor.right = color; }
void setBorderColorBottom(const Color& color) { m_borderColor.bottom = color; }
void setBorderColorLeft(const Color& color) { m_borderColor.left = color; }
void setMargin(int margin) { m_margin.set(margin); updateParentLayout(); }
void setMarginHorizontal(int margin) { m_margin.right = m_margin.left = margin; updateParentLayout(); }
void setMarginVertical(int margin) { m_margin.bottom = m_margin.top = margin; updateParentLayout(); }
void setMarginTop(int margin) { m_margin.top = margin; updateParentLayout(); }
void setMarginRight(int margin) { m_margin.right = margin; updateParentLayout(); }
void setMarginBottom(int margin) { m_margin.bottom = margin; updateParentLayout(); }
void setMarginLeft(int margin) { m_margin.left = margin; updateParentLayout(); }
void setPadding(int padding) { m_padding.top = m_padding.right = m_padding.bottom = m_padding.left = padding; updateLayout(); }
void setPaddingHorizontal(int padding) { m_padding.right = m_padding.left = padding; updateLayout(); }
void setPaddingVertical(int padding) { m_padding.bottom = m_padding.top = padding; updateLayout(); }
void setPaddingTop(int padding) { m_padding.top = padding; updateLayout(); }
void setPaddingRight(int padding) { m_padding.right = padding; updateLayout(); }
void setPaddingBottom(int padding) { m_padding.bottom = padding; updateLayout(); }
void setPaddingLeft(int padding) { m_padding.left = padding; updateLayout(); }
void setOpacity(float opacity) { m_opacity = opacity; }
int getX() { return m_rect.x(); }
int getY() { return m_rect.y(); }
Point getPos() { return m_rect.topLeft(); }
int getWidth() { return m_rect.width(); }
int getHeight() { return m_rect.height(); }
Size getSize() { return m_rect.size(); }
Rect getRect() { return m_rect; }
Color getColor() { return m_color; }
Color getBackgroundColor() { return m_backgroundColor; }
int getBackgroundOffsetX() { return m_backgroundRect.x(); }
int getBackgroundOffsetY() { return m_backgroundRect.y(); }
Point getBackgroundOffset() { return m_backgroundRect.topLeft(); }
int getBackgroundWidth() { return m_backgroundRect.width(); }
int getBackgroundHeight() { return m_backgroundRect.height(); }
Size getBackgroundSize() { return m_backgroundRect.size(); }
Rect getBackgroundRect() { return m_backgroundRect; }
Color getIconColor() { return m_iconColor; }
int getIconOffsetX() { return m_iconRect.x(); }
int getIconOffsetY() { return m_iconRect.y(); }
Point getIconOffset() { return m_iconRect.topLeft(); }
int getIconWidth() { return m_iconRect.width(); }
int getIconHeight() { return m_iconRect.height(); }
Size getIconSize() { return m_iconRect.size(); }
Rect getIconRect() { return m_iconRect; }
Color getBorderTopColor() { return m_borderColor.top; }
Color getBorderRightColor() { return m_borderColor.right; }
Color getBorderBottomColor() { return m_borderColor.bottom; }
Color getBorderLeftColor() { return m_borderColor.left; }
int getBorderTopWidth() { return m_borderWidth.top; }
int getBorderRightWidth() { return m_borderWidth.right; }
int getBorderBottomWidth() { return m_borderWidth.bottom; }
int getBorderLeftWidth() { return m_borderWidth.left; }
int getMarginTop() { return m_margin.top; }
int getMarginRight() { return m_margin.right; }
int getMarginBottom() { return m_margin.bottom; }
int getMarginLeft() { return m_margin.left; }
int getPaddingTop() { return m_padding.top; }
int getPaddingRight() { return m_padding.right; }
int getPaddingBottom() { return m_padding.bottom; }
int getPaddingLeft() { return m_padding.left; }
float getOpacity() { return m_opacity; }
// image
private:
void initImage() { }
void parseImageStyle(const OTMLNodePtr& styleNode);
void updateImageCache() { m_imageMustRecache = true; }
void configureBorderImage() { m_imageBordered = true; updateImageCache(); }
CoordsBuffer m_imageCoordsBuffer;
Rect m_imageCachedScreenCoords;
Boolean<true> m_imageMustRecache;
Boolean<false> m_imageBordered;
protected:
void drawImage(const Rect& screenCoords);
TexturePtr m_imageTexture;
Rect m_imageClipRect;
Rect m_imageRect;
Color m_imageColor;
Boolean<false> m_imageFixedRatio;
Boolean<false> m_imageRepeated;
Boolean<false> m_imageSmooth;
EdgeGroup<int> m_imageBorder;
public:
void setImageSource(const std::string& source);
void setImageClip(const Rect& clipRect) { m_imageClipRect = clipRect; updateImageCache(); }
void setImageOffsetX(int x) { m_imageRect.setX(x); updateImageCache(); }
void setImageOffsetY(int y) { m_imageRect.setX(y); updateImageCache(); }
void setImageOffset(const Point& pos) { m_imageRect.move(pos); updateImageCache(); }
void setImageWidth(int width) { m_imageRect.setWidth(width); updateImageCache(); }
void setImageHeight(int height) { m_imageRect.setHeight(height); updateImageCache(); }
void setImageSize(const Size& size) { m_imageRect.resize(size); updateImageCache(); }
void setImageRect(const Rect& rect) { m_imageRect = rect; updateImageCache(); }
void setImageColor(const Color& color) { m_imageColor = color; updateImageCache(); }
void setImageFixedRatio(bool fixedRatio) { m_imageFixedRatio = fixedRatio; updateImageCache(); }
void setImageRepeated(bool repeated) { m_imageRepeated = repeated; updateImageCache(); }
void setImageSmooth(bool smooth) { m_imageSmooth = smooth; }
void setImageBorderTop(int border) { m_imageBorder.top = border; configureBorderImage(); }
void setImageBorderRight(int border) { m_imageBorder.right = border; configureBorderImage(); }
void setImageBorderBottom(int border) { m_imageBorder.bottom = border; configureBorderImage(); }
void setImageBorderLeft(int border) { m_imageBorder.left = border; configureBorderImage(); }
void setImageBorder(int border) { m_imageBorder.set(border); configureBorderImage(); }
Rect getImageClip() { return m_imageClipRect; }
int getImageOffsetX() { return m_imageRect.x(); }
int getImageOffsetY() { return m_imageRect.y(); }
Point getImageOffset() { return m_imageRect.topLeft(); }
int getImageWidth() { return m_imageRect.width(); }
int getImageHeight() { return m_imageRect.height(); }
Size getImageSize() { return m_imageRect.size(); }
Rect getImageRect() { return m_imageRect; }
Color getImageColor() { return m_imageColor; }
bool isImageFixedRatio() { return m_imageFixedRatio; }
bool isImageSmooth() { return m_imageSmooth; }
int getImageBorderTop() { return m_imageBorder.top; }
int getImageBorderRight() { return m_imageBorder.right; }
int getImageBorderBottom() { return m_imageBorder.bottom; }
int getImageBorderLeft() { return m_imageBorder.left; }
// text related
private:
void initText();
void parseTextStyle(const OTMLNodePtr& styleNode);
protected:
void drawText(const Rect& screenCoords);
virtual void onTextChange(const std::string& text);
virtual void onFontChange(const std::string& font);
std::string m_text;
Point m_textOffset;
Fw::AlignmentFlag m_textAlign;
Point m_textOffset;
FontPtr m_font;
public:
void resizeToText() { setSize(getTextSize()); }
void clearText() { setText(""); }
void setText(const std::string& text);
void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; }
void setTextOffset(const Point& offset) { m_textOffset = offset; }
void setFont(const std::string& fontName);
std::string getText() { return m_text; }
Fw::AlignmentFlag getTextAlign() { return m_textAlign; }
Point getTextOffset() { return m_textOffset; }
std::string getFont() { return m_font->getName(); }
Size getTextSize() { return m_font->calculateTextRectSize(m_text); }
};
#endif

View File

@@ -0,0 +1,366 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uiwidget.h"
#include "uianchorlayout.h"
#include "uiverticallayout.h"
#include "uitranslator.h"
#include <framework/graphics/painter.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
void UIWidget::initBaseStyle()
{
m_backgroundColor = Fw::alpha;
m_borderColor.set(Fw::black);
m_iconColor = Fw::white;
m_color = Fw::white;
m_opacity = 1.0f;
// generate an unique id, this is need because anchored layouts find widgets by id
static unsigned long id = 1;
m_id = Fw::mkstr("widget", id++);
}
void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
{
// load styles used by all widgets
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "color")
setColor(node->value<Color>());
else if(node->tag() == "x")
setX(node->value<int>());
else if(node->tag() == "y")
setY(node->value<int>());
else if(node->tag() == "pos")
setPos(node->value<Point>());
else if(node->tag() == "width")
setWidth(node->value<int>());
else if(node->tag() == "height")
setHeight(node->value<int>());
else if(node->tag() == "rect")
setRect(node->value<Rect>());
else if(node->tag() == "background")
setBackgroundColor(node->value<Color>());
else if(node->tag() == "background-color")
setBackgroundColor(node->value<Color>());
else if(node->tag() == "background-offset-x")
setBackgroundOffsetX(node->value<int>());
else if(node->tag() == "background-offset-y")
setBackgroundOffsetY(node->value<int>());
else if(node->tag() == "background-offset")
setBackgroundOffset(node->value<Point>());
else if(node->tag() == "background-width")
setBackgroundWidth(node->value<int>());
else if(node->tag() == "background-height")
setBackgroundHeight(node->value<int>());
else if(node->tag() == "background-size")
setBackgroundSize(node->value<Size>());
else if(node->tag() == "background-rect")
setBackgroundRect(node->value<Rect>());
else if(node->tag() == "icon")
setIcon(node->value());
else if(node->tag() == "icon-source")
setIcon(node->value());
else if(node->tag() == "icon-color")
setIconColor(node->value<Color>());
else if(node->tag() == "icon-offset-x")
setIconOffsetX(node->value<int>());
else if(node->tag() == "icon-offset-y")
setIconOffsetY(node->value<int>());
else if(node->tag() == "icon-offset")
setIconOffset(node->value<Point>());
else if(node->tag() == "icon-width")
setIconWidth(node->value<int>());
else if(node->tag() == "icon-height")
setIconHeight(node->value<int>());
else if(node->tag() == "icon-size")
setIconSize(node->value<Size>());
else if(node->tag() == "icon-rect")
setIconRect(node->value<Rect>());
else if(node->tag() == "opacity")
setOpacity(node->value<float>());
else if(node->tag() == "enabled")
setEnabled(node->value<bool>());
else if(node->tag() == "visible")
setVisible(node->value<bool>());
else if(node->tag() == "checked")
setChecked(node->value<bool>());
else if(node->tag() == "on")
setOn(node->value<bool>());
else if(node->tag() == "focusable")
setFocusable(node->value<bool>());
else if(node->tag() == "phantom")
setPhantom(node->value<bool>());
else if(node->tag() == "size")
setSize(node->value<Size>());
else if(node->tag() == "fixed-size")
setFixedSize(node->value<bool>());
else if(node->tag() == "border") {
auto split = Fw::split(node->value(), " ");
setBorderWidth(Fw::safeCast<int>(split[0]));
setBorderColor(Fw::safeCast<Color>(split[1]));
}
else if(node->tag() == "border-width")
setBorderWidth(node->value<int>());
else if(node->tag() == "border-width-top")
setBorderWidthTop(node->value<int>());
else if(node->tag() == "border-width-right")
setBorderWidthRight(node->value<int>());
else if(node->tag() == "border-width-bottom")
setBorderWidthBottom(node->value<int>());
else if(node->tag() == "border-width-left")
setBorderWidthLeft(node->value<int>());
else if(node->tag() == "border-color")
setBorderColor(node->value<Color>());
else if(node->tag() == "border-color-top")
setBorderColorTop(node->value<Color>());
else if(node->tag() == "border-color-right")
setBorderColorRight(node->value<Color>());
else if(node->tag() == "border-color-bottom")
setBorderColorBottom(node->value<Color>());
else if(node->tag() == "border-color-left")
setBorderColorLeft(node->value<Color>());
else if(node->tag() == "margin-top")
setMarginTop(node->value<int>());
else if(node->tag() == "margin-right")
setMarginRight(node->value<int>());
else if(node->tag() == "margin-bottom")
setMarginBottom(node->value<int>());
else if(node->tag() == "margin-left")
setMarginLeft(node->value<int>());
else if(node->tag() == "margin") {
std::string marginDesc = node->value();
std::vector<std::string> split;
boost::split(split, marginDesc, boost::is_any_of(std::string(" ")));
if(split.size() == 4) {
setMarginTop(Fw::safeCast<int>(split[0]));
setMarginRight(Fw::safeCast<int>(split[1]));
setMarginBottom(Fw::safeCast<int>(split[2]));
setMarginLeft(Fw::safeCast<int>(split[3]));
} else if(split.size() == 3) {
int marginTop = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
int marginBottom = Fw::safeCast<int>(split[2]);
setMarginTop(marginTop);
setMarginRight(marginHorizontal);
setMarginBottom(marginBottom);
setMarginLeft(marginHorizontal);
} else if(split.size() == 2) {
int marginVertical = Fw::safeCast<int>(split[0]);
int marginHorizontal = Fw::safeCast<int>(split[1]);
setMarginTop(marginVertical);
setMarginRight(marginHorizontal);
setMarginBottom(marginVertical);
setMarginLeft(marginHorizontal);
} else if(split.size() == 1) {
int margin = Fw::safeCast<int>(split[0]);
setMarginTop(margin);
setMarginRight(margin);
setMarginBottom(margin);
setMarginLeft(margin);
}
}
else if(node->tag() == "padding-top")
setPaddingTop(node->value<int>());
else if(node->tag() == "padding-right")
setPaddingRight(node->value<int>());
else if(node->tag() == "padding-bottom")
setPaddingBottom(node->value<int>());
else if(node->tag() == "padding-left")
setPaddingLeft(node->value<int>());
else if(node->tag() == "padding") {
std::string paddingDesc = node->value();
std::vector<std::string> split;
boost::split(split, paddingDesc, boost::is_any_of(std::string(" ")));
if(split.size() == 4) {
setPaddingTop(Fw::safeCast<int>(split[0]));
setPaddingRight(Fw::safeCast<int>(split[1]));
setPaddingBottom(Fw::safeCast<int>(split[2]));
setPaddingLeft(Fw::safeCast<int>(split[3]));
} else if(split.size() == 3) {
int paddingTop = Fw::safeCast<int>(split[0]);
int paddingHorizontal = Fw::safeCast<int>(split[1]);
int paddingBottom = Fw::safeCast<int>(split[2]);
setPaddingTop(paddingTop);
setPaddingRight(paddingHorizontal);
setPaddingBottom(paddingBottom);
setPaddingLeft(paddingHorizontal);
} else if(split.size() == 2) {
int paddingVertical = Fw::safeCast<int>(split[0]);
int paddingHorizontal = Fw::safeCast<int>(split[1]);
setPaddingTop(paddingVertical);
setPaddingRight(paddingHorizontal);
setPaddingBottom(paddingVertical);
setPaddingLeft(paddingHorizontal);
} else if(split.size() == 1) {
int padding = Fw::safeCast<int>(split[0]);
setPaddingTop(padding);
setPaddingRight(padding);
setPaddingBottom(padding);
setPaddingLeft(padding);
}
}
// layouts
else if(node->tag() == "layout") {
std::string layoutType;
if(node->hasValue())
layoutType = node->value();
else
layoutType = node->valueAt<std::string>("type", "");
if(!layoutType.empty()) {
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UIVerticalLayoutPtr(new UIVerticalLayout(asUIWidget()));
else if(layoutType == "anchor")
layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
else
throw OTMLException(node, "cannot determine layout type");
setLayout(layout);
}
if(node->hasChildren())
m_layout->applyStyle(node);
}
// anchors
else if(boost::starts_with(node->tag(), "anchors.")) {
UIWidgetPtr parent = getParent();
if(!parent) {
if(m_firstOnStyle)
throw OTMLException(node, "cannot create anchor, there is no parent widget!");
else
continue;
}
UIAnchorLayoutPtr anchorLayout = parent->getLayout()->asUIAnchorLayout();
if(!anchorLayout)
throw OTMLException(node, "cannot create anchor, the parent widget doesn't use anchor layout!");
std::string what = node->tag().substr(8);
if(what == "fill") {
fill(node->value());
} else if(what == "centerIn") {
centerIn(node->value());
} else {
Fw::AnchorEdge anchoredEdge = Fw::translateAnchorEdge(what);
std::vector<std::string> split = Fw::split(node->value(), ".");
if(split.size() != 2)
throw OTMLException(node, "invalid anchor description");
std::string hookedWidgetId = split[0];
Fw::AnchorEdge hookedEdge = Fw::translateAnchorEdge(split[1]);
if(anchoredEdge == Fw::AnchorNone)
throw OTMLException(node, "invalid anchor edge");
if(hookedEdge == Fw::AnchorNone)
throw OTMLException(node, "invalid anchor target edge");
addAnchor(anchoredEdge, hookedWidgetId, hookedEdge);
}
// lua functions
} else if(boost::starts_with(node->tag(), "@")) {
// load once
if(m_firstOnStyle) {
std::string funcName = node->tag().substr(1);
std::string funcOrigin = "@" + node->source() + "[" + node->tag() + "]";
g_lua.loadFunction(node->value(), funcOrigin);
luaSetField(funcName);
}
// lua fields value
} else if(boost::starts_with(node->tag(), "&")) {
std::string fieldName = node->tag().substr(1);
std::string fieldOrigin = "@" + node->source() + "[" + node->tag() + "]";
g_lua.evaluateExpression(node->value(), fieldOrigin);
luaSetField(fieldName);
}
}
}
void UIWidget::drawBackground(const Rect& screenCoords)
{
if(m_backgroundColor.a() > 0) {
Rect drawRect = screenCoords;
drawRect.translate(m_backgroundRect.topLeft());
if(m_backgroundRect.isValid())
drawRect.resize(m_backgroundRect.size());
g_painter.setColor(m_backgroundColor);
g_painter.drawFilledRect(drawRect);
}
}
void UIWidget::drawBorder(const Rect& screenCoords)
{
// top
if(m_borderWidth.top > 0 && m_borderColor.top.a() > 0) {
g_painter.setColor(m_borderColor.top);
Rect borderRect(screenCoords.topLeft(), screenCoords.width(), m_borderWidth.top);
g_painter.drawFilledRect(borderRect);
}
// right
if(m_borderWidth.right > 0 && m_borderColor.top.a() > 0) {
g_painter.setColor(m_borderColor.top);
Rect borderRect(screenCoords.topRight() - Point(m_borderWidth.right - 1, 0), m_borderWidth.right, screenCoords.height());
g_painter.drawFilledRect(borderRect);
}
// bottom
if(m_borderWidth.bottom > 0 && m_borderColor.top.a() > 0) {
g_painter.setColor(m_borderColor.top);
Rect borderRect(screenCoords.bottomLeft() - Point(0, m_borderWidth.bottom - 1), screenCoords.width(), m_borderWidth.bottom);
g_painter.drawFilledRect(borderRect);
}
// left
if(m_borderWidth.top > 0 && m_borderColor.top.a() > 0) {
g_painter.setColor(m_borderColor.top);
Rect borderRect(screenCoords.topLeft(), m_borderWidth.left, screenCoords.height());
g_painter.drawFilledRect(borderRect);
}
}
void UIWidget::drawIcon(const Rect& screenCoords)
{
if(m_icon && m_iconColor.a() > 0) {
Rect drawRect;
if(m_iconRect.isValid()) {
drawRect = screenCoords;
drawRect.translate(m_iconRect.topLeft());
drawRect.resize(m_iconRect.size());
} else {
drawRect.resize(m_icon->getSize());
drawRect.moveCenter(screenCoords.center());
}
g_painter.setColor(m_iconColor);
g_painter.drawTexturedRect(drawRect, m_icon);
}
}
void UIWidget::setIcon(const std::string& iconFile)
{
m_icon = g_textures.getTexture(iconFile);
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uiwidget.h"
#include <framework/graphics/painter.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode)
{
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "image")
setImageSource(node->value());
else if(node->tag() == "image-source")
setImageSource(node->value());
else if(node->tag() == "image-offset-x")
setImageOffsetX(node->value<int>());
else if(node->tag() == "image-offset-y")
setImageOffsetY(node->value<int>());
else if(node->tag() == "image-offset")
setImageOffset(node->value<Point>());
else if(node->tag() == "image-width")
setImageWidth(node->value<int>());
else if(node->tag() == "image-height")
setImageHeight(node->value<int>());
else if(node->tag() == "image-size")
setImageSize(node->value<Size>());
else if(node->tag() == "image-rect")
setImageRect(node->value<Rect>());
else if(node->tag() == "image-clip")
setImageClip(node->value<Rect>());
else if(node->tag() == "image-fixed-ratio")
setImageFixedRatio(node->value<bool>());
else if(node->tag() == "image-repeated")
setImageRepeated(node->value<bool>());
else if(node->tag() == "image-smooth")
setImageSmooth(node->value<bool>());
else if(node->tag() == "image-color")
setImageColor(node->value<Color>());
else if(node->tag() == "image-border-top")
setImageBorderTop(node->value<int>());
else if(node->tag() == "image-border-right")
setImageBorderRight(node->value<int>());
else if(node->tag() == "image-border-bottom")
setImageBorderBottom(node->value<int>());
else if(node->tag() == "image-border-left")
setImageBorderLeft(node->value<int>());
else if(node->tag() == "image-border") {
setImageBorder(node->value<int>());
}
}
}
void UIWidget::drawImage(const Rect& screenCoords)
{
if(!m_imageTexture || m_imageColor.a() == 0 || !screenCoords.isValid())
return;
// cache vertex buffers
if(m_imageCachedScreenCoords != screenCoords || m_imageMustRecache) {
m_imageCoordsBuffer.clear();
m_imageCachedScreenCoords = screenCoords;
m_imageMustRecache = false;
Rect drawRect = screenCoords;
drawRect.translate(m_imageRect.topLeft());
if(m_imageRect.isValid())
drawRect.resize(m_imageRect.size());
if(!m_imageBordered) {
if(m_imageFixedRatio) {
Size textureSize = m_imageTexture->getSize();
Size textureClipSize = drawRect.size();
textureClipSize.scale(textureSize, Fw::KeepAspectRatio);
Point texCoordsOffset;
if(textureSize.height() > textureClipSize.height())
texCoordsOffset.y = (textureSize.height() - textureClipSize.height())/2;
else if(textureSize.width() > textureClipSize.width())
texCoordsOffset.x = (textureSize.width() - textureClipSize.width())/2;
Rect textureClipRect(texCoordsOffset, textureClipSize);
m_imageCoordsBuffer.addRect(drawRect, textureClipRect);
} else {
if(m_imageRepeated)
m_imageCoordsBuffer.addRepeatedRects(drawRect, m_imageClipRect);
else
m_imageCoordsBuffer.addRect(drawRect, m_imageClipRect);
}
} else {
int top = m_imageBorder.top;
int bottom = m_imageBorder.bottom;
int left = m_imageBorder.left;
int right = m_imageBorder.right;
// calculates border coords
const Rect clip = m_imageClipRect;
Rect leftBorder(clip.left(), clip.top() + top, left, clip.height() - top - bottom);
Rect rightBorder(clip.right() - right + 1, clip.top() + top, right, clip.height() - top - bottom);
Rect topBorder(clip.left() + left, clip.top(), clip.width() - right - left, top);
Rect bottomBorder(clip.left() + left, clip.bottom() - bottom + 1, clip.width() - right - left, bottom);
Rect topLeftCorner(clip.left(), clip.top(), left, top);
Rect topRightCorner(clip.right() - right + 1, clip.top(), right, top);
Rect bottomLeftCorner(clip.left(), clip.bottom() - bottom + 1, left, bottom);
Rect bottomRightCorner(clip.right() - right + 1, clip.bottom() - bottom + 1, right, bottom);
Rect center(clip.left() + left, clip.top() + top, clip.width() - right - left, clip.height() - top - bottom);
Size bordersSize(leftBorder.width() + rightBorder.width(), topBorder.height() + bottomBorder.height());
Size centerSize = drawRect.size() - bordersSize;
Rect rectCoords;
// first the center
if(centerSize.area() > 0) {
rectCoords = Rect(drawRect.left() + leftBorder.width(), drawRect.top() + topBorder.height(), centerSize);
m_imageCoordsBuffer.addRepeatedRects(rectCoords, center);
}
// top left corner
rectCoords = Rect(drawRect.topLeft(), topLeftCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topLeftCorner);
// top
rectCoords = Rect(drawRect.left() + topLeftCorner.width(), drawRect.topLeft().y, centerSize.width(), topBorder.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topBorder);
// top right corner
rectCoords = Rect(drawRect.left() + topLeftCorner.width() + centerSize.width(), drawRect.top(), topRightCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topRightCorner);
// left
rectCoords = Rect(drawRect.left(), drawRect.top() + topLeftCorner.height(), leftBorder.width(), centerSize.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, leftBorder);
// right
rectCoords = Rect(drawRect.left() + leftBorder.width() + centerSize.width(), drawRect.top() + topRightCorner.height(), rightBorder.width(), centerSize.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, rightBorder);
// bottom left corner
rectCoords = Rect(drawRect.left(), drawRect.top() + topLeftCorner.height() + centerSize.height(), bottomLeftCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomLeftCorner);
// bottom
rectCoords = Rect(drawRect.left() + bottomLeftCorner.width(), drawRect.top() + topBorder.height() + centerSize.height(), centerSize.width(), bottomBorder.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomBorder);
// bottom right corner
rectCoords = Rect(drawRect.left() + bottomLeftCorner.width() + centerSize.width(), drawRect.top() + topRightCorner.height() + centerSize.height(), bottomRightCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomRightCorner);
}
}
m_imageTexture->setSmooth(m_imageSmooth);
g_painter.setColor(m_imageColor);
g_painter.drawTextureCoords(m_imageCoordsBuffer, m_imageTexture);
}
void UIWidget::setImageSource(const std::string& source)
{
m_imageTexture = g_textures.getTexture(source);
if(!m_imageClipRect.isValid())
m_imageClipRect = Rect(0, 0, m_imageTexture->getSize());
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2010-2012 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.
*/
#include "uiwidget.h"
#include "uitranslator.h"
#include <framework/graphics/fontmanager.h>
#include <framework/graphics/painter.h>
void UIWidget::initText()
{
m_font = g_fonts.getDefaultFont();
m_textAlign = Fw::AlignCenter;
}
void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
{
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "icon")
setIcon(node->value());
else if(node->tag() == "text")
setText(node->value());
else if(node->tag() == "text-align")
setTextAlign(Fw::translateAlignment(node->value()));
else if(node->tag() == "text-offset")
setTextOffset(node->value<Point>());
else if(node->tag() == "font")
setFont(node->value());
}
}
void UIWidget::drawText(const Rect& screenCoords)
{
g_painter.setColor(m_color);
if(m_text.length() > 0 && m_color.a() > 0) {
Rect textRect = screenCoords;
textRect.translate(m_textOffset);
m_font->renderText(m_text, textRect, m_textAlign, m_color);
}
}
void UIWidget::onTextChange(const std::string& text)
{
callLuaField("onTextChange", text);
}
void UIWidget::onFontChange(const std::string& font)
{
callLuaField("onFontChange", font);
}
void UIWidget::setText(const std::string& text)
{
if(m_text != text) {
m_text = text;
// update rect size
if(!m_rect.isValid()) {
Size textSize = m_font->calculateTextRectSize(m_text);
Size newSize = getSize();
if(newSize.width() <= 0)
newSize.setWidth(textSize.width());
if(newSize.height() <= 0)
newSize.setHeight(textSize.height());
setSize(newSize);
}
onTextChange(text);
}
}
void UIWidget::setFont(const std::string& fontName)
{
m_font = g_fonts.getFont(fontName);
onFontChange(fontName);
}

View File

@@ -22,7 +22,6 @@
#include "uiwindow.h"
#include "uitranslator.h"
#include <framework/graphics/borderimage.h>
#include <framework/graphics/font.h>
#include <framework/graphics/graphics.h>
#include <framework/otml/otml.h>
@@ -36,22 +35,22 @@ UIWindow::UIWindow()
m_oldIndex = -1;
}
void UIWindow::render()
void UIWindow::draw()
{
// render children
UIWidget::render();
UIWidget::draw();
// draw window head text
Rect headTextRect = m_rect;
headTextRect.addTop(-m_headTextOffset.y);
headTextRect.expandTop(-m_headTextOffset.y);
headTextRect.setHeight(m_headHeight);
if(m_titleAlign & Fw::AlignLeft)
headTextRect.addLeft(-m_headTextOffset.x);
headTextRect.expandLeft(-m_headTextOffset.x);
else if(m_titleAlign & Fw::AlignRight)
headTextRect.addRight(-m_headTextOffset.x);
headTextRect.expandRight(-m_headTextOffset.x);
else {
headTextRect.addLeft(-m_headTextOffset.x);
headTextRect.addRight(-m_headTextOffset.x);
headTextRect.expandLeft(-m_headTextOffset.x);
headTextRect.expandRight(-m_headTextOffset.x);
}
m_font->renderText(m_title, headTextRect, m_titleAlign, m_color);
}
@@ -111,7 +110,7 @@ void UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
// restore position before move
parent->moveChildToIndex(asUIWidget(), m_oldIndex);
moveTo(m_oldPos);
setPos(m_oldPos);
// calculate new index
int newIndex;
@@ -133,7 +132,7 @@ void UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
bool UIWindow::onMouseMove(const Point& mousePos, const Point& mouseMoved)
{
if(m_moving) {
moveTo(mousePos - m_movingReference);
setPos(mousePos - m_movingReference);
return true;
}
return UIWidget::onMouseMove(mousePos, mouseMoved);

View File

@@ -35,7 +35,7 @@ class UIWindow : public UIWidget
public:
UIWindow();
virtual void render();
virtual void draw();
void setTitle(const std::string& title) { m_title = title; }
std::string getTitle() const { return m_title; }