implement grid layout

This commit is contained in:
Eduardo Bart
2012-01-12 17:20:18 -02:00
parent 8db565f456
commit c1cf53829e
21 changed files with 340 additions and 147 deletions

View File

@@ -189,8 +189,9 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgettext.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgetbasestyle.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uilineedit.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uianchorlayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiverticallayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uigridlayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uianchorlayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uilayout.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uiframecounter.cpp
${CMAKE_CURRENT_LIST_DIR}/ui/uitranslator.cpp

View File

@@ -281,6 +281,17 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction<UIVerticalLayout>("create", [](UIWidgetPtr parent){ return UIVerticalLayoutPtr(new UIVerticalLayout(parent)); } );
g_lua.bindClassMemberFunction<UIVerticalLayout>("setFitParent", &UIVerticalLayout::setFitParent);
// UIGridLayout
g_lua.registerClass<UIGridLayout, UILayout>();
g_lua.bindClassStaticFunction<UIGridLayout>("create", [](UIWidgetPtr parent){ return UIGridLayoutPtr(new UIGridLayout(parent)); });
g_lua.bindClassMemberFunction<UIGridLayout>("setCellSize", &UIGridLayout::setCellSize);
g_lua.bindClassMemberFunction<UIGridLayout>("setCellWidth", &UIGridLayout::setCellWidth);
g_lua.bindClassMemberFunction<UIGridLayout>("setCellHeight", &UIGridLayout::setCellHeight);
g_lua.bindClassMemberFunction<UIGridLayout>("setCellSpacing", &UIGridLayout::setCellSpacing);
g_lua.bindClassMemberFunction<UIGridLayout>("setNumColumns", &UIGridLayout::setNumColumns);
g_lua.bindClassMemberFunction<UIGridLayout>("setNumLines", &UIGridLayout::setNumLines);
g_lua.bindClassMemberFunction<UIGridLayout>("asUIGridLayout", &UIGridLayout::asUIGridLayout);
// UIAnchorLayout
g_lua.registerClass<UIAnchorLayout, UILayout>();
g_lua.bindClassStaticFunction<UIAnchorLayout>("create", [](UIWidgetPtr parent){ return UIAnchorLayoutPtr(new UIAnchorLayout(parent)); } );

View File

@@ -31,6 +31,7 @@ class UILineEdit;
class UIFrameCounter;
class UILayout;
class UIVerticalLayout;
class UIGridLayout;
class UIAnchorLayout;
typedef std::shared_ptr<UIWidget> UIWidgetPtr;
@@ -40,6 +41,7 @@ typedef std::shared_ptr<UILineEdit> UILineEditPtr;
typedef std::shared_ptr<UIFrameCounter> UIFrameCounterPtr;
typedef std::shared_ptr<UILayout> UILayoutPtr;
typedef std::shared_ptr<UIVerticalLayout> UIVerticalLayoutPtr;
typedef std::shared_ptr<UIGridLayout> UIGridLayoutPtr;
typedef std::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;
typedef std::deque<UIWidgetPtr> UIWidgetList;

View File

@@ -29,6 +29,7 @@
#include "uiframecounter.h"
#include "uilayout.h"
#include "uiverticallayout.h"
#include "uigridlayout.h"
#include "uianchorlayout.h"
#endif

View File

@@ -0,0 +1,91 @@
/*
* 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 "uigridlayout.h"
#include "uiwidget.h"
UIGridLayout::UIGridLayout(UIWidgetPtr parentWidget): UILayout(parentWidget)
{
m_cellSize = Size(16,16);
m_cellSpacing = 0;
m_numColumns = 1;
m_numLines = 1;
}
void UIGridLayout::applyStyle(const OTMLNodePtr& styleNode)
{
UILayout::applyStyle(styleNode);
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "cell-size")
setCellSize(node->value<Size>());
else if(node->tag() == "cell-width")
setCellWidth(node->value<int>());
else if(node->tag() == "cell-height")
setCellHeight(node->value<int>());
else if(node->tag() == "cell-spacing")
setCellSpacing(node->value<int>());
else if(node->tag() == "num-columns")
setNumColumns(node->value<int>());
else if(node->tag() == "num-lines")
setNumLines(node->value<int>());
}
}
void UIGridLayout::removeWidget(const UIWidgetPtr& widget)
{
update();
}
void UIGridLayout::addWidget(const UIWidgetPtr& widget)
{
update();
}
void UIGridLayout::internalUpdate()
{
UIWidgetPtr parentWidget = getParentWidget();
UIWidgetList widgets = parentWidget->getChildren();
Rect childrenRect = parentWidget->getChildrenRect();
Point topLeft = childrenRect.topLeft();
int index = 0;
for(const UIWidgetPtr& widget : widgets) {
if(!widget->isExplicitlyVisible())
continue;
int line = index / m_numLines;
int column = index % m_numLines;
Point virtualPos = Point(column * (m_cellSize.width() + m_cellSpacing), line * (m_cellSize.height() + m_cellSpacing));
Point pos = topLeft + virtualPos;
widget->setRect(Rect(pos, m_cellSize));
index++;
if(index >= m_numColumns * m_numLines)
break;
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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 UIGRIDLAYOUT_H
#define UIGRIDLAYOUT_H
#include <framework/ui/uilayout.h>
class UIGridLayout : public UILayout
{
public:
UIGridLayout(UIWidgetPtr parentWidget);
void applyStyle(const OTMLNodePtr& styleNode);
void removeWidget(const UIWidgetPtr& widget);
void addWidget(const UIWidgetPtr& widget);
void setCellSize(const Size& size) { m_cellSize = size; update(); }
void setCellWidth(int width) { m_cellSize.setWidth(width); update(); }
void setCellHeight(int height) { m_cellSize.setHeight(height); update(); }
void setCellSpacing(int spacing) { m_cellSpacing = spacing; update(); }
void setNumColumns(int columns) { m_numColumns = columns; update(); }
void setNumLines(int lines) { m_numLines = lines; update(); }
virtual UIGridLayoutPtr asUIGridLayout() { return nullptr; }
protected:
void internalUpdate();
private:
Size m_cellSize;
int m_cellSpacing;
int m_numColumns;
int m_numLines;
};
#endif

View File

@@ -25,7 +25,7 @@
#include "declarations.h"
#include <framework/luascript/luaobject.h>
#include <framework/otml/declarations.h>
#include <framework/otml/otml.h>
class UILayout : public LuaObject
{
@@ -46,11 +46,11 @@ public:
bool isUpdateDisabled() { return m_updateDisabled; }
bool isUpdating() { return m_updating; }
virtual bool needsUpdatesOnChildChange() { return false; }
UILayoutPtr asUILayout() { return std::static_pointer_cast<UILayout>(shared_from_this()); }
virtual UIAnchorLayoutPtr asUIAnchorLayout() { return nullptr; }
virtual UIVerticalLayoutPtr asUIVerticalLayout() { return nullptr; }
virtual UIGridLayoutPtr asUIGridLayout() { return nullptr; }
protected:
virtual void internalUpdate() = 0;

View File

@@ -0,0 +1,24 @@
/*
* 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 "uirichtext.h"

View File

@@ -0,0 +1,33 @@
/*
* 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 UIRICHTEXT_H
#define UIRICHTEXT_H
#include <framework/ui/uiwidget.h>
class UIRichText : public UIWidget
{
};
#endif // UIRICHTEXT_H

View File

@@ -22,7 +22,6 @@
#include "uiverticallayout.h"
#include "uiwidget.h"
#include <framework/otml/otml.h>
#include <framework/core/eventdispatcher.h>
UIVerticalLayout::UIVerticalLayout(UIWidgetPtr parentWidget)
@@ -45,35 +44,6 @@ void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
}
}
void UIVerticalLayout::addWidget(const UIWidgetPtr& widget)
{
update();
}
void UIVerticalLayout::removeWidget(const UIWidgetPtr& widget)
{
update();
}
void UIVerticalLayout::setAlignBottom(bool aliginBottom)
{
m_alignBottom = aliginBottom;
update();
}
void UIVerticalLayout::setSpacing(int spacing)
{
m_spacing = spacing;
update();
}
void UIVerticalLayout::setFitParent(bool fitParent)
{
m_fitParent = fitParent;
update();
}
void UIVerticalLayout::internalUpdate()
{
UIWidgetPtr parentWidget = getParentWidget();

View File

@@ -31,14 +31,12 @@ public:
UIVerticalLayout(UIWidgetPtr parentWidget);
void applyStyle(const OTMLNodePtr& styleNode);
void addWidget(const UIWidgetPtr& widget);
void removeWidget(const UIWidgetPtr& widget);
void addWidget(const UIWidgetPtr& widget) { update(); }
void removeWidget(const UIWidgetPtr& widget) { update(); }
void setAlignBottom(bool aliginBottom);
void setSpacing(int spacing);
void setFitParent(bool fitParent);
bool needsUpdatesOnChildChange() { return m_fitParent; }
void setAlignBottom(bool aliginBottom) { m_alignBottom = aliginBottom; update(); }
void setSpacing(int spacing) { m_spacing = spacing; update(); }
void setFitParent(bool fitParent) { m_fitParent = fitParent; update(); }
UIVerticalLayoutPtr asUIVerticalLayout() { return std::static_pointer_cast<UIVerticalLayout>(shared_from_this()); }

View File

@@ -364,8 +364,8 @@ void UIWidget::unlockChild(const UIWidgetPtr& child)
void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
{
m_loadingStyle = true;
try {
m_loadingStyle = true;
onStyleApply(styleNode->tag(), styleNode);
callLuaField("onStyleApply", styleNode->tag(), styleNode);
@@ -377,10 +377,10 @@ void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
}
m_firstOnStyle = false;
m_loadingStyle = false;
} catch(Exception& e) {
logError("Failed to apply style to widget '", m_id, "' style: ", e.what());
}
m_loadingStyle = false;
}
void UIWidget::addAnchor(Fw::AnchorEdge anchoredEdge, const std::string& hookedWidgetId, Fw::AnchorEdge hookedEdge)
{

View File

@@ -21,8 +21,9 @@
*/
#include "uiwidget.h"
#include "uianchorlayout.h"
#include "uiverticallayout.h"
#include "uigridlayout.h"
#include "uianchorlayout.h"
#include "uitranslator.h"
#include <framework/graphics/painter.h>
@@ -233,6 +234,8 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
UILayoutPtr layout;
if(layoutType == "verticalBox")
layout = UIVerticalLayoutPtr(new UIVerticalLayout(asUIWidget()));
else if(layoutType == "grid")
layout = UIGridLayoutPtr(new UIGridLayout(asUIWidget()));
else if(layoutType == "anchor")
layout = UIAnchorLayoutPtr(new UIAnchorLayout(asUIWidget()));
else