invert widget event propagation order

This commit is contained in:
Eduardo Bart 2012-01-05 00:42:17 -02:00
parent 24c1f05d66
commit c9280ecf9d
11 changed files with 112 additions and 67 deletions

View File

@ -69,4 +69,4 @@ local function onRootGeometryUpdate()
end
end
connect(rootWidget, { onGeometryUpdate = onRootGeometryUpdate} )
connect(rootWidget, { onGeometryChange = onRootGeometryUpdate} )

View File

@ -1,6 +1,7 @@
VipListLabel < GameLabel
font: verdana-11px-monochrome
margin-left: 5
phantom: false
&onMousePress: VipList.onVipListLabelMousePress
MiniWindow

View File

@ -392,9 +392,10 @@ void UILineEdit::onStyleApply(const std::string& styleName, const OTMLNodePtr& s
}
}
void UILineEdit::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
void UILineEdit::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{
update();
UIWidget::onGeometryChange(oldRect, newRect);
}
void UILineEdit::onFocusChange(bool focused, Fw::FocusReason reason)
@ -405,6 +406,7 @@ void UILineEdit::onFocusChange(bool focused, Fw::FocusReason reason)
else
blinkCursor();
}
UIWidget::onFocusChange(focused, reason);
}
bool UILineEdit::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
@ -431,7 +433,7 @@ bool UILineEdit::onKeyPress(uchar keyCode, std::string keyText, int keyboardModi
} else if(!keyText.empty() && (keyboardModifiers == Fw::KeyboardNoModifier || keyboardModifiers == Fw::KeyboardShiftModifier))
appendText(keyText);
else
return false;
return UIWidget::onKeyPress(keyCode, keyText, keyboardModifiers);
return true;
}

View File

@ -59,7 +59,7 @@ protected:
virtual void onTextChange(const std::string& text);
virtual void onFontChange(const std::string& font);
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
virtual void onFocusChange(bool focused, Fw::FocusReason reason);
virtual bool onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);

View File

@ -60,23 +60,23 @@ void UIManager::inputEvent(const InputEvent& event)
m_isOnInputEvent = true;
switch(event.type) {
case Fw::KeyPressInputEvent:
m_keyboardReceiver->onKeyPress(event.keyCode, event.keyText, event.keyboardModifiers);
m_keyboardReceiver->propagateOnKeyPress(event.keyCode, event.keyText, event.keyboardModifiers);
break;
case Fw::KeyReleaseInputEvent:
m_keyboardReceiver->onKeyRelease(event.keyCode, event.keyText, event.keyboardModifiers);
m_keyboardReceiver->propagateOnKeyRelease(event.keyCode, event.keyText, event.keyboardModifiers);
break;
case Fw::MousePressInputEvent:
m_keyboardReceiver->onMousePress(event.mousePos, event.mouseButton);
m_keyboardReceiver->propagateOnMousePress(event.mousePos, event.mouseButton);
break;
case Fw::MouseReleaseInputEvent:
m_mouseReceiver->onMouseRelease(event.mousePos, event.mouseButton);
m_mouseReceiver->propagateOnMouseRelease(event.mousePos, event.mouseButton);
break;
case Fw::MouseMoveInputEvent:
m_mouseReceiver->updateState(Fw::HoverState);
m_mouseReceiver->onMouseMove(event.mousePos, event.mouseMoved);
m_mouseReceiver->propagateOnMouseMove(event.mousePos, event.mouseMoved);
break;
case Fw::MouseWheelInputEvent:
m_mouseReceiver->onMouseWheel(event.mousePos, event.wheelDirection);
m_mouseReceiver->propagateOnMouseWheel(event.mousePos, event.wheelDirection);
break;
};
m_isOnInputEvent = false;

View File

@ -165,17 +165,6 @@ void UIWidget::drawText(const Rect& screenCoords)
}
}
void UIWidget::setEnabled(bool enabled)
{
if(enabled != m_enabled) {
m_enabled = enabled;
updateState(Fw::DisabledState);
updateState(Fw::ActiveState);
updateState(Fw::HoverState);
}
}
void UIWidget::setVisible(bool visible)
{
if(m_visible != visible) {
@ -192,6 +181,25 @@ void UIWidget::setVisible(bool visible)
}
}
void UIWidget::setEnabled(bool enabled)
{
if(enabled != m_enabled) {
m_enabled = enabled;
updateState(Fw::DisabledState);
updateState(Fw::ActiveState);
updateState(Fw::HoverState);
}
}
void UIWidget::setPressed(bool pressed)
{
if(pressed != m_pressed) {
m_pressed = pressed;
updateState(Fw::PressedState);
}
}
void UIWidget::setFocusable(bool focusable)
{
if(m_focusable != focusable) {
@ -268,7 +276,7 @@ void UIWidget::setRect(const Rect& rect)
UIWidgetPtr self = asUIWidget();
g_dispatcher.addEvent([self, oldRect]() {
self->m_updateEventScheduled = false;
self->onGeometryUpdate(oldRect, self->getRect());
self->onGeometryChange(oldRect, self->getRect());
});
}
m_updateEventScheduled = true;
@ -1129,9 +1137,9 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
m_firstOnStyle = false;
}
void UIWidget::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{
callLuaField("onGeometryUpdate", oldRect, newRect);
callLuaField("onGeometryChange", oldRect, newRect);
}
void UIWidget::onFocusChange(bool focused, Fw::FocusReason reason)
@ -1161,9 +1169,39 @@ void UIWidget::onFontChange(const std::string& font)
bool UIWidget::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
{
if(callLuaField<bool>("onKeyPress", keyCode, keyText, keyboardModifiers))
return true;
return callLuaField<bool>("onKeyPress", keyCode, keyText, keyboardModifiers);
}
bool UIWidget::onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
{
return callLuaField<bool>("onKeyRelease", keyCode, keyText, keyboardModifiers);
}
bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
return callLuaField<bool>("onMousePress", mousePos, button);
}
void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed() && getRect().contains(mousePos))
callLuaField("onClick");
callLuaField("onMouseRelease", mousePos, button);
}
bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)
{
return callLuaField<bool>("onMouseMove", mousePos, mouseMoved);
}
bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
{
return callLuaField<bool>("onMouseWheel", mousePos, direction);
}
bool UIWidget::propagateOnKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers)
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1177,18 +1215,15 @@ bool UIWidget::onKeyPress(uchar keyCode, std::string keyText, int keyboardModifi
}
for(const UIWidgetPtr& child : children) {
if(child->onKeyPress(keyCode, keyText, keyboardModifiers))
if(child->propagateOnKeyPress(keyCode, keyText, keyboardModifiers))
return true;
}
return false;
return onKeyPress(keyCode, keyText, keyboardModifiers);
}
bool UIWidget::onKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
bool UIWidget::propagateOnKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers)
{
if(callLuaField<bool>("onKeyRelease", keyCode, keyText, keyboardModifiers))
return true;
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1202,18 +1237,15 @@ bool UIWidget::onKeyRelease(uchar keyCode, std::string keyText, int keyboardModi
}
for(const UIWidgetPtr& child : children) {
if(child->onKeyRelease(keyCode, keyText, keyboardModifiers))
if(child->propagateOnKeyRelease(keyCode, keyText, keyboardModifiers))
return true;
}
return false;
return onKeyRelease(keyCode, keyText, keyboardModifiers);
}
bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
bool UIWidget::propagateOnMousePress(const Point& mousePos, Fw::MouseButton button)
{
if(callLuaField<bool>("onMousePress", mousePos, button))
return true;
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1231,7 +1263,7 @@ bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
if(child->isFocusable())
focusChild(child, Fw::MouseFocusReason);
bool mustEnd = child->onMousePress(mousePos, button);
bool mustEnd = child->propagateOnMousePress(mousePos, button);
if(button == Fw::MouseLeftButton && !child->isPressed()) {
UIWidgetPtr clickedChild = child->getChildByPos(mousePos);
@ -1243,16 +1275,14 @@ bool UIWidget::onMousePress(const Point& mousePos, Fw::MouseButton button)
return true;
}
return false;
if(!isPhantom())
return onMousePress(mousePos, button);
else
return false;
}
void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
void UIWidget::propagateOnMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed() && getRect().contains(mousePos))
callLuaField("onClick");
callLuaField("onMouseRelease", mousePos, button);
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1265,18 +1295,19 @@ void UIWidget::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
}
for(const UIWidgetPtr& child : children) {
child->onMouseRelease(mousePos, button);
child->propagateOnMouseRelease(mousePos, button);
if(child->isPressed())
if(child->isPressed() && button == Fw::MouseLeftButton)
child->setPressed(false);
}
// fire release events only when pressed
if(isPressed())
onMouseRelease(mousePos, button);
}
bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)
bool UIWidget::propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved)
{
if(callLuaField<bool>("onMouseMove", mousePos, mouseMoved))
return true;
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1289,18 +1320,18 @@ bool UIWidget::onMouseMove(const Point& mousePos, const Point& mouseMoved)
}
for(const UIWidgetPtr& child : children) {
if(child->onMouseMove(mousePos, mouseMoved))
if(child->propagateOnMouseMove(mousePos, mouseMoved))
return true;
}
return false;
if(!isPhantom())
return onMouseMove(mousePos, mouseMoved);
else
return false;
}
bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
bool UIWidget::propagateOnMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction)
{
if(callLuaField<bool>("onMouseWheel", mousePos, direction))
return true;
// do a backup of children list, because it may change while looping it
UIWidgetList children;
for(const UIWidgetPtr& child : m_children) {
@ -1314,9 +1345,12 @@ bool UIWidget::onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direc
}
for(const UIWidgetPtr& child : children) {
if(child->onMouseWheel(mousePos, direction))
if(child->propagateOnMouseWheel(mousePos, direction))
return true;
}
return false;
if(!isPhantom())
return onMouseWheel(mousePos, direction);
else
return false;
}

View File

@ -56,7 +56,7 @@ protected:
public:
void setVisible(bool visible);
void setEnabled(bool enabled);
void setPressed(bool pressed) { m_pressed = pressed; updateState(Fw::PressedState); }
void setPressed(bool pressed);
void setId(const std::string& id) { m_id = id; }
void setFocusable(bool focusable);
void setPhantom(bool phantom) { m_phantom = phantom; }
@ -197,7 +197,7 @@ private:
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
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);
@ -209,6 +209,13 @@ protected:
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
virtual bool onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
bool propagateOnKeyPress(uchar keyCode, std::string keyText, int keyboardModifiers);
bool propagateOnKeyRelease(uchar keyCode, std::string keyText, int keyboardModifiers);
bool propagateOnMousePress(const Point& mousePos, Fw::MouseButton button);
void propagateOnMouseRelease(const Point& mousePos, Fw::MouseButton button);
bool propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved);
bool propagateOnMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
protected:
std::string m_id;
Fw::FocusReason m_lastFocusReason;

View File

@ -80,14 +80,15 @@ void UIWindow::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty
}
}
void UIWindow::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
void UIWindow::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{
bindRectToParent();
UIWidget::onGeometryChange(oldRect, newRect);
}
bool UIWindow::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
if(m_movePolicy != DONT_MOVE) {
if(m_movePolicy != DONT_MOVE && button == Fw::MouseLeftButton) {
UIWidgetPtr clickedChild = getChildByPos(mousePos);
//FIXME: recursively check for non phantom children
if(!clickedChild || clickedChild->isPhantom()) {

View File

@ -42,7 +42,7 @@ public:
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);

View File

@ -100,7 +100,7 @@ bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button)
return true;
}
void UIMap::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
void UIMap::onGeometryChange(const Rect& oldRect, const Rect& newRect)
{
Rect mapRect = newRect.expanded(-m_mapMargin-1);
Size mapSize(g_map.getVibibleSize().width() * Map::NUM_TILE_PIXELS, g_map.getVibibleSize().height() * Map::NUM_TILE_PIXELS);

View File

@ -35,7 +35,7 @@ public:
protected:
virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual void onGeometryUpdate(const Rect& oldRect, const Rect& newRect);
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
private:
int m_mapMargin;