mirror of
https://github.com/edubart/otclient.git
synced 2025-10-15 03:54:54 +02:00
make menu work
This commit is contained in:
@@ -63,6 +63,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setMarginLeft", &UIWidget::setMarginLeft);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setSizeFixed", &UIWidget::setSizeFixed);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("setLastFocusReason", &UIWidget::setLastFocusReason);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("bindRectToParent", &UIWidget::bindRectToParent);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("resize", &UIWidget::resize);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("moveTo", &UIWidget::moveTo);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hide", &UIWidget::hide);
|
||||
@@ -72,6 +73,10 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("lock", &UIWidget::lock);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("unlock", &UIWidget::unlock);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("focus", &UIWidget::focus);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("grabMouse", &UIWidget::grabMouse);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("ungrabMouse", &UIWidget::ungrabMouse);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("grabKeyboard", &UIWidget::grabKeyboard);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("ungrabKeyboard", &UIWidget::ungrabKeyboard);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isActive", &UIWidget::isActive);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isEnabled", &UIWidget::isEnabled);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isDisabled", &UIWidget::isDisabled);
|
||||
@@ -85,6 +90,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isFocusable", &UIWidget::isFocusable);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isPhantom", &UIWidget::isPhantom);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("isSizeFixed", &UIWidget::isSizeFixed);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("containsPoint", &UIWidget::containsPoint);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hasChildren", &UIWidget::hasChildren);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hasChild", &UIWidget::hasChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getId", &UIWidget::getId);
|
||||
@@ -117,6 +123,8 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getChildById", &UIWidget::getChildById);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getChildByPos", &UIWidget::getChildByPos);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getChildByIndex", &UIWidget::getChildByIndex);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getFirstChild", &UIWidget::getFirstChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getLastChild", &UIWidget::getLastChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("recursiveGetChildById", &UIWidget::recursiveGetChildById);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("recursiveGetChildByPos", &UIWidget::recursiveGetChildByPos);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("backwardsGetWidgetById", &UIWidget::backwardsGetWidgetById);
|
||||
|
@@ -35,6 +35,8 @@ void UIManager::init()
|
||||
m_rootWidget = UIWidget::create<UIWidget>();
|
||||
m_rootWidget->setId("root");
|
||||
m_rootWidget->resize(g_window.getSize());
|
||||
m_mouseReceiver = m_rootWidget;
|
||||
m_keyboardReceiver = m_rootWidget;
|
||||
}
|
||||
|
||||
void UIManager::terminate()
|
||||
@@ -58,23 +60,23 @@ void UIManager::inputEvent(const InputEvent& event)
|
||||
{
|
||||
switch(event.type) {
|
||||
case Fw::KeyPressInputEvent:
|
||||
m_rootWidget->onKeyPress(event.keyCode, event.keyText, event.keyboardModifiers);
|
||||
m_keyboardReceiver->onKeyPress(event.keyCode, event.keyText, event.keyboardModifiers);
|
||||
break;
|
||||
case Fw::KeyReleaseInputEvent:
|
||||
m_rootWidget->onKeyRelease(event.keyCode, event.keyText, event.keyboardModifiers);
|
||||
m_keyboardReceiver->onKeyRelease(event.keyCode, event.keyText, event.keyboardModifiers);
|
||||
break;
|
||||
case Fw::MousePressInputEvent:
|
||||
m_rootWidget->onMousePress(event.mousePos, event.mouseButton);
|
||||
m_keyboardReceiver->onMousePress(event.mousePos, event.mouseButton);
|
||||
break;
|
||||
case Fw::MouseReleaseInputEvent:
|
||||
m_rootWidget->onMouseRelease(event.mousePos, event.mouseButton);
|
||||
m_mouseReceiver->onMouseRelease(event.mousePos, event.mouseButton);
|
||||
break;
|
||||
case Fw::MouseMoveInputEvent:
|
||||
m_rootWidget->updateState(Fw::HoverState);
|
||||
m_rootWidget->onMouseMove(event.mousePos, event.mouseMoved);
|
||||
m_mouseReceiver->updateState(Fw::HoverState);
|
||||
m_mouseReceiver->onMouseMove(event.mousePos, event.mouseMoved);
|
||||
break;
|
||||
case Fw::MouseWheelInputEvent:
|
||||
m_rootWidget->onMouseWheel(event.mousePos, event.wheelDirection);
|
||||
m_mouseReceiver->onMouseWheel(event.mousePos, event.wheelDirection);
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
@@ -44,15 +44,19 @@ public:
|
||||
UIWidgetPtr loadUI(const std::string& file, const UIWidgetPtr& parent = nullptr);
|
||||
UIWidgetPtr loadWidgetFromOTML(const OTMLNodePtr& widgetNode, const UIWidgetPtr& parent);
|
||||
|
||||
//void setMouseGrabWidget();
|
||||
//void setKeyboardGrabWidget();
|
||||
void setMouseReceiver(const UIWidgetPtr& widget) { m_mouseReceiver = widget; }
|
||||
void setKeyboardReceiver(const UIWidgetPtr& widget) { m_keyboardReceiver = widget; }
|
||||
void resetMouseReceiver() { m_mouseReceiver = m_rootWidget; }
|
||||
void resetKeyboardReceiver() { m_keyboardReceiver = m_rootWidget; }
|
||||
UIWidgetPtr getMouseReceiver() { return m_mouseReceiver; }
|
||||
UIWidgetPtr getKeyboardReceiver() { return m_keyboardReceiver; }
|
||||
|
||||
UIWidgetPtr getRootWidget() { return m_rootWidget; }
|
||||
|
||||
private:
|
||||
UIWidgetPtr m_rootWidget;
|
||||
//UIWidgetPtr m_mouseGrabWidget;
|
||||
//UIWidgetPtr m_keyboardGrabWidget;
|
||||
UIWidgetPtr m_mouseReceiver;
|
||||
UIWidgetPtr m_keyboardReceiver;
|
||||
std::map<std::string, OTMLNodePtr> m_styles;
|
||||
};
|
||||
|
||||
|
@@ -52,6 +52,13 @@ void UIWidget::destroy()
|
||||
setVisible(false);
|
||||
setEnabled(false);
|
||||
|
||||
// release input grabs
|
||||
if(g_ui.getKeyboardReceiver() == asUIWidget())
|
||||
g_ui.resetKeyboardReceiver();
|
||||
|
||||
if(g_ui.getMouseReceiver() == asUIWidget())
|
||||
g_ui.resetMouseReceiver();
|
||||
|
||||
// remove itself from parent
|
||||
if(UIWidgetPtr parent = getParent()) {
|
||||
if(parent->hasChild(asUIWidget()))
|
||||
@@ -207,6 +214,18 @@ void UIWidget::setRect(const Rect& rect)
|
||||
m_updateEventScheduled = true;
|
||||
}
|
||||
|
||||
void UIWidget::bindRectToParent()
|
||||
{
|
||||
Rect boundRect = m_rect;
|
||||
UIWidgetPtr parent = getParent();
|
||||
if(parent) {
|
||||
Rect parentRect = parent->getRect();
|
||||
boundRect.bound(parentRect);
|
||||
}
|
||||
|
||||
setRect(boundRect);
|
||||
}
|
||||
|
||||
void UIWidget::lock()
|
||||
{
|
||||
if(UIWidgetPtr parent = getParent())
|
||||
@@ -227,6 +246,28 @@ void UIWidget::focus()
|
||||
parent->focusChild(asUIWidget(), Fw::ActiveFocusReason);
|
||||
}
|
||||
|
||||
void UIWidget::grabMouse()
|
||||
{
|
||||
g_ui.setMouseReceiver(asUIWidget());
|
||||
}
|
||||
|
||||
void UIWidget::ungrabMouse()
|
||||
{
|
||||
if(g_ui.getMouseReceiver() == asUIWidget())
|
||||
g_ui.resetMouseReceiver();
|
||||
}
|
||||
|
||||
void UIWidget::grabKeyboard()
|
||||
{
|
||||
g_ui.setKeyboardReceiver(asUIWidget());
|
||||
}
|
||||
|
||||
void UIWidget::ungrabKeyboard()
|
||||
{
|
||||
if(g_ui.getKeyboardReceiver() == asUIWidget())
|
||||
g_ui.resetKeyboardReceiver();
|
||||
}
|
||||
|
||||
bool UIWidget::isVisible()
|
||||
{
|
||||
if(!m_visible)
|
||||
@@ -282,7 +323,7 @@ UIWidgetPtr UIWidget::getChildByPos(const Point& childPos)
|
||||
{
|
||||
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
const UIWidgetPtr& widget = (*it);
|
||||
if(widget->isExplicitlyVisible() && widget->getRect().contains(childPos))
|
||||
if(widget->isExplicitlyVisible() && widget->containsPoint(childPos))
|
||||
return widget;
|
||||
}
|
||||
|
||||
@@ -313,7 +354,7 @@ UIWidgetPtr UIWidget::recursiveGetChildById(const std::string& id)
|
||||
UIWidgetPtr UIWidget::recursiveGetChildByPos(const Point& childPos)
|
||||
{
|
||||
for(const UIWidgetPtr& child : m_children) {
|
||||
if(child->getRect().contains(childPos)) {
|
||||
if(child->containsPoint(childPos)) {
|
||||
if(UIWidgetPtr subChild = child->recursiveGetChildByPos(childPos))
|
||||
return subChild;
|
||||
return child;
|
||||
@@ -677,7 +718,7 @@ void UIWidget::updateState(Fw::WidgetState state)
|
||||
UIWidgetPtr parent;
|
||||
do {
|
||||
parent = widget->getParent();
|
||||
if(!widget->isExplicitlyEnabled() || !widget->isExplicitlyVisible() || !widget->getRect().contains(mousePos) ||
|
||||
if(!widget->isExplicitlyEnabled() || !widget->isExplicitlyVisible() || !widget->containsPoint(mousePos) ||
|
||||
(parent && widget != parent->getChildByPos(mousePos))) {
|
||||
newStatus = false;
|
||||
break;
|
||||
@@ -1035,7 +1076,7 @@ bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
||||
continue;
|
||||
|
||||
// mouse press events only go to children that contains the mouse position
|
||||
if(child->getRect().contains(mousePos) && child == getChildByPos(mousePos))
|
||||
if(child->containsPoint(mousePos) && child == getChildByPos(mousePos))
|
||||
children.push_back(child);
|
||||
}
|
||||
|
||||
@@ -1119,7 +1160,7 @@ bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direc
|
||||
continue;
|
||||
|
||||
// mouse wheel events only go to children that contains the mouse position
|
||||
if(child->getRect().contains(mousePos) && child == getChildByPos(mousePos))
|
||||
if(child->containsPoint(mousePos) && child == getChildByPos(mousePos))
|
||||
children.push_back(child);
|
||||
}
|
||||
|
||||
|
@@ -69,6 +69,7 @@ public:
|
||||
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(getPosition(), size)); }
|
||||
void moveTo(const Point& pos) { setRect(Rect(pos, getSize())); }
|
||||
void hide() { setVisible(false); }
|
||||
@@ -78,6 +79,10 @@ public:
|
||||
void lock();
|
||||
void unlock();
|
||||
void focus();
|
||||
void grabMouse();
|
||||
void ungrabMouse();
|
||||
void grabKeyboard();
|
||||
void ungrabKeyboard();
|
||||
|
||||
bool isActive() { return hasState(Fw::ActiveState); }
|
||||
bool isEnabled() { return !hasState(Fw::DisabledState); }
|
||||
@@ -92,6 +97,7 @@ public:
|
||||
bool isFocusable() { return m_focusable; }
|
||||
bool isPhantom() { return m_phantom; }
|
||||
bool isSizeFixed() { return m_fixedSize; }
|
||||
bool containsPoint(const Point& point) { return m_rect.contains(point); }
|
||||
bool hasChildren() { return m_children.size() > 0; }
|
||||
bool hasChild(const UIWidgetPtr& child);
|
||||
|
||||
@@ -126,6 +132,8 @@ public:
|
||||
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);
|
||||
|
@@ -82,16 +82,7 @@ void UIWindow::onStyleApply(const OTMLNodePtr& styleNode)
|
||||
|
||||
void UIWindow::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
|
||||
{
|
||||
// bind window rect to parent rect
|
||||
Rect boundRect = newRect;
|
||||
UIWidgetPtr parent = getParent();
|
||||
if(parent) {
|
||||
Rect parentRect = parent->getRect();
|
||||
boundRect.bound(parentRect);
|
||||
}
|
||||
|
||||
if(boundRect != newRect)
|
||||
setRect(boundRect);
|
||||
bindRectToParent();
|
||||
}
|
||||
|
||||
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
|
||||
|
Reference in New Issue
Block a user