make menu work

This commit is contained in:
Eduardo Bart 2012-01-02 20:09:49 -02:00
parent 43c16a1643
commit 90600bbd69
10 changed files with 128 additions and 47 deletions

View File

@ -17,10 +17,10 @@ PopupMenuButton < UIButton
$disabled:
color: #555555
PopupMenuFirstButton < PopupMenuButton
$first:
margin-top: 3
PopupMenuLastButton < PopupMenuButton
$last:
margin-bottom: 3
PopupMenuSeparator < UIWidget

View File

@ -7,28 +7,50 @@ function UIPopupMenu.create()
local layout = UIVerticalLayout.create(menu)
layout:setFitParent(true)
menu:setLayout(layout)
menu:setStyle('PopupMenu')
return menu
end
function UIPopupMenu.display(otui, pos)
local menu = UI.display(otui, {x = pos.x, y = pos.y})
function UIPopupMenu.display(menu, pos)
UI.display(menu, {x = pos.x, y = pos.y})
menu:bindRectToParent()
menu:grabMouse()
menu:grabKeyboard()
return menu
end
function UIPopupMenu.addOption(menu, optionName, optionCallback)
local optionWidget = UIButton.create()
local lastOptionWidget = menu:getLastChild()
optionWidget.onClick = function()
optionCallback()
menu:destroy()
end
optionWidget:setText(optionName)
optionWidget:setStyle('PopupMenuButton')
menu:addChild(optionWidget)
end
function UIPopupMenu.addSeparator(menu)
local separatorWidget = UIWidget.create()
separatorWidget:setStyle('PopupMenuSeparator')
menu:addChild(separator)
end
-- hooked events
local function onWidgetStyleApply(widget, style)
if style and style.popupmenu then
widget.popupmenu = style.popupmenu
end
end
local function onWidgetMousePress(widget, mousePos, mouseButton)
if widget.popupmenu and mouseButton == MouseRightButton then
UIPopupMenu.display(widget.popupmenu, mousePos)
function UIPopupMenu.onMousePress(menu, mousePos, mouseButton)
-- clicks outside menu area destroys the menu
if not menu:containsPoint(mousePos) then
menu:destroy()
return true
end
return false
end
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
onMousePress = onWidgetMousePress })
function UIPopupMenu.onKeyPress(menu, keyCode, keyText, keyboardModifiers)
if keyCode == KeyEscape then
menu:destroy()
return true
end
return false
end

View File

@ -17,9 +17,6 @@ local InventorySlotAmmo = 10
-- public functions
function Inventory.create()
window = UI.display('inventory.otui', { parent = Game.gameRightPanel })
local itemWidget = window:getChildById('feet')
window:setHeight(itemWidget:getPosition().y + itemWidget:getHeight() - window:getPosition().y)
end
function Inventory.destroy()
@ -66,6 +63,13 @@ function Inventory.onSoulChange(soul)
widget:setText("Soul:\n" .. soul)
end
function Inventory.onInventoryItemMousePress(itemWidget, mousePos, mouseButton)
local menu = UIPopupMenu.create()
menu:addOption('Look', function() print('look') end)
menu:addOption('Use', function() print('use') end)
menu:display(mousePos)
end
connect(Game, { onLogin = Inventory.create,
onLogout = Inventory.destroy,
onInventoryChange = Inventory.onInventoryChange,

View File

@ -1,8 +1,9 @@
InvetoryItem < Item
popupmenu: /game_inventory/itempopupmenu.otui
&onMousePress: Inventory.onInventoryItemMousePress
UIWindow
width: 192
height: 148
margin-top: 10
margin-left: 6
margin-right: 6

View File

@ -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);

View File

@ -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;
};
}

View File

@ -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;
};

View File

@ -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);
}

View File

@ -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);

View File

@ -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)