improve window moving, minor changes to enable others apps use otclient framework

This commit is contained in:
Eduardo Bart
2011-11-11 18:26:10 -02:00
parent 3f4ad7977c
commit fc65f99ead
31 changed files with 96 additions and 83 deletions

View File

@@ -29,7 +29,7 @@
ResourceManager g_resources;
void ResourceManager::init(const char* argv0)
void ResourceManager::init(const char* argv0, const char *appName)
{
PHYSFS_init(argv0);
@@ -39,11 +39,12 @@ void ResourceManager::init(const char* argv0)
std::string possibleDirs[] = { "modules",
baseDir + "modules",
baseDir + "../modules",
baseDir + "../share/otclient/modules",
baseDir + "../share/" + appName + "/otclient/modules",
"" };
bool found = false;
for(const std::string& dir : possibleDirs) {
dump << dir;
if(g_resources.addToSearchPath(dir)) {
logInfo("Using modules directory '", dir.c_str(), "'");
found = true;

View File

@@ -28,7 +28,7 @@
class ResourceManager
{
public:
void init(const char* argv0);
void init(const char* argv0, const char *appName);
void terminate();
/// Set output files directory

View File

@@ -49,6 +49,7 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberFunction<UIWidget>("setSize", &UIWidget::resize);
g_lua.bindClassMemberFunction<UIWidget>("getPosition", &UIWidget::getPosition);
g_lua.bindClassMemberFunction<UIWidget>("moveTo", &UIWidget::moveTo);
g_lua.bindClassMemberFunction<UIWidget>("moveChildToIndex", &UIWidget::moveChildToIndex);
g_lua.bindClassMemberFunction<UIWidget>("getParent", &UIWidget::getParent);
g_lua.bindClassMemberFunction<UIWidget>("setParent", &UIWidget::setParent);
g_lua.bindClassMemberFunction<UIWidget>("getBackgroundColor", &UIWidget::getBackgroundColor);

View File

@@ -28,6 +28,7 @@ UIVerticalLayout::UIVerticalLayout(UIWidgetPtr parentWidget)
: UILayout(parentWidget)
{
m_alignBottom = false;
m_padding = 0;
}
void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
@@ -37,6 +38,8 @@ void UIVerticalLayout::applyStyle(const OTMLNodePtr& styleNode)
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "align bottom")
m_alignBottom = node->value<bool>();
else if(node->tag() == "padding")
m_padding = node->value<int>();
}
}
@@ -45,12 +48,6 @@ void UIVerticalLayout::update()
UIWidgetPtr parentWidget = getParentWidget();
UIWidgetList widgets = parentWidget->getChildren();
// sort by Y pos
std::sort(widgets.begin(), widgets.end(),
[](const UIWidgetPtr& first, const UIWidgetPtr& second) -> bool {
return first->getY() < second->getY();
});
if(m_alignBottom)
std::reverse(widgets.begin(), widgets.end());
@@ -68,13 +65,12 @@ void UIVerticalLayout::update()
}
widget->setRect(Rect(pos, size));
pos.y += (m_alignBottom) ? -widget->getMarginTop() : (widget->getHeight() + widget->getMarginBottom());
pos.y += m_padding;
}
}
void UIVerticalLayout::addWidget(const UIWidgetPtr& widget)
{
// needed to be correctly sorted on the following update
widget->setY(9999);
update();
}

View File

@@ -37,6 +37,7 @@ public:
private:
bool m_alignBottom;
int m_padding;
};
#endif

View File

@@ -497,6 +497,18 @@ void UIWidget::moveChildToTop(const UIWidgetPtr& child)
m_children.push_back(child);
}
void UIWidget::moveChildToIndex(const UIWidgetPtr& child, int index)
{
if(!child)
return;
// remove and push child again
auto it = std::find(m_children.begin(), m_children.end(), child);
assert(it != m_children.end());
m_children.erase(it);
m_children.insert(m_children.begin() + index - 1, child);
}
void UIWidget::lockChild(const UIWidgetPtr& child)
{
if(!child)
@@ -572,6 +584,17 @@ bool UIWidget::isChildLocked(const UIWidgetPtr& child)
return it != m_lockedChildren.end();
}
int UIWidget::getChildIndex(const UIWidgetPtr& child)
{
int index = 1;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if(*it == child)
return index;
++index;
}
return -1;
}
void UIWidget::updateParentLayout()
{
if(UIWidgetPtr parent = getParent())

View File

@@ -139,9 +139,11 @@ public:
void focusNextChild(Fw::FocusReason reason);
void focusPreviousChild(Fw::FocusReason reason);
void moveChildToTop(const UIWidgetPtr& child);
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 updateParentLayout();
void updateLayout();

View File

@@ -32,6 +32,7 @@ void UIWindow::setup()
m_moving = false;
m_movePolicy = DONT_MOVE;
m_headHeight = 0;
m_oldIndex = -1;
m_titleAlign = Fw::AlignCenter;
}
@@ -103,15 +104,6 @@ void UIWindow::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
setRect(boundRect);
}
void UIWindow::onFocusChange(bool focused, Fw::FocusReason reason)
{
// when a window is focused it goes to the top
if(focused) {
if(UIWidgetPtr parent = getParent())
parent->moveChildToTop(asUIWidget());
}
}
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
if(m_movePolicy != DONT_MOVE) {
@@ -120,6 +112,9 @@ bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
if(!clickedChild || clickedChild->isPhantom()) {
m_moving = true;
m_movingReference = mousePos - getRect().topLeft();
m_oldIndex = getParent()->getChildIndex(asUIWidget());
m_oldPos = getPosition();
getParent()->moveChildToTop(asUIWidget());
}
}
return UIWidget::onMousePress(mousePos, button);
@@ -128,6 +123,25 @@ bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
bool UIWindow::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(m_moving) {
if(m_movePolicy == FREE_UPDATED_MOVE) {
UIWidgetPtr parent = getParent();
// restore position before move
parent->moveChildToIndex(asUIWidget(), m_oldIndex);
moveTo(m_oldPos);
// calculate new index
int newIndex;
for(newIndex=parent->getChildCount();newIndex>1;--newIndex) {
UIWidgetPtr child = parent->getChildByIndex(newIndex);
if(mousePos.y >= child->getRect().top())
break;
}
// set the new index
parent->moveChildToIndex(asUIWidget(), newIndex);
updateParentLayout();
}
m_moving = false;
return true;
}
@@ -138,8 +152,6 @@ bool UIWindow::onMouseMove(const Point& mousePos, const Point& mouseMoved)
{
if(m_moving) {
moveTo(mousePos - m_movingReference);
if(m_movePolicy == FREE_UPDATED_MOVE)
updateParentLayout();
return true;
}
return UIWidget::onMouseMove(mousePos, mouseMoved);

View File

@@ -43,7 +43,6 @@ public:
protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
virtual void onFocusChange(bool focused, Fw::FocusReason reason);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
@@ -54,6 +53,8 @@ private:
bool m_moving;
MovePolicy m_movePolicy;
Point m_movingReference;
Point m_oldPos;
int m_oldIndex;
// styling
BorderImagePtr m_headImage;