Commit 4d342a31 authored by Stefan Majewsky's avatar Stefan Majewsky

This is r1119463 done right.

Previously, when one pressed Ctrl+LMB in the default trigger
configuration, MovePiece would be triggered because LMB matches Ctrl+LMB.
This is clearly wrong, we want SelectPiece instead which matches
Ctrl+LMB exactly. So now, those interactors which have exactly matching
triggers are prioritized compared to those who do not match.

svn path=/trunk/KDE/kdegames/palapeli/; revision=1130156
parent a98be2bd
......@@ -53,8 +53,9 @@ namespace Palapeli
enum EventProcessingFlag {
EventMatches = 1 << 0,
EventStartsInteraction = 1 << 1,
EventConcludesInteraction = 1 << 2
EventMatchesExactly = (1 << 1) + EventMatches,
EventStartsInteraction = 1 << 2,
EventConcludesInteraction = 1 << 3
};
Q_DECLARE_FLAGS(EventProcessingFlags, EventProcessingFlag)
......
......@@ -23,6 +23,8 @@
#include <QMouseEvent>
#include <QWheelEvent>
static const int AdditionalPriorityForExactMatches = 10000;
Palapeli::InteractorManager::InteractorManager(QGraphicsView* view)
: QObject(view)
, m_view(view)
......@@ -55,11 +57,28 @@ void Palapeli::InteractorManager::handleEvent(QWheelEvent* event)
{
//convert event
Palapeli::WheelEvent pEvent(m_view, event->pos(), event->delta());
//check interactors
//check which interactors are triggered by this event
Palapeli::Interactor* bestMatchInteractor = 0;
int bestMatchPriority = -1;
QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = m_interactors.begin(), it2 = m_interactors.end();
for (; it1 != it2; ++it1)
if (Palapeli::TriggerMapper::instance()->testTrigger(it1.key(), event) & Palapeli::EventMatches)
it1.value()->sendEvent(pEvent);
{
Palapeli::Interactor* const interactor = it1.value();
const Palapeli::EventProcessingFlags flags = Palapeli::TriggerMapper::instance()->testTrigger(it1.key(), event);
if (!(flags & Palapeli::EventMatches))
continue;
int priority = interactor->priority();
if ((flags & Palapeli::EventMatchesExactly) == Palapeli::EventMatchesExactly)
priority += AdditionalPriorityForExactMatches;
if (priority > bestMatchPriority)
{
bestMatchInteractor = interactor;
bestMatchPriority = priority;
}
}
//activate matching interactor with highest priority
if (bestMatchInteractor)
bestMatchInteractor->sendEvent(pEvent);
}
/*
......@@ -79,7 +98,7 @@ void Palapeli::InteractorManager::handleEvent(QMouseEvent* event)
m_buttons |= event->button();
m_mousePos = event->pos();
//check which interactors are triggered by this event
QMap<Palapeli::Interactor*, EventContext> interactorData;
QMap<Palapeli::Interactor*, Palapeli::EventContext> interactorData;
QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = m_interactors.begin(), it2 = m_interactors.end();
for (; it1 != it2; ++it1)
interactorData[it1.value()] = Palapeli::TriggerMapper::instance()->testTrigger(it1.key(), event);
......@@ -95,11 +114,11 @@ void Palapeli::InteractorManager::handleEvent(QKeyEvent* event)
//convert event
Palapeli::MouseEvent pEvent(m_view, m_mousePos);
//check which interactors are triggered by this event
QMap<Palapeli::Interactor*, EventContext> interactorData;
QMap<Palapeli::Interactor*, Palapeli::EventContext> interactorData;
QMap<QByteArray, Palapeli::Interactor*>::const_iterator it1 = m_interactors.begin(), it2 = m_interactors.end();
for (; it1 != it2; ++it1)
interactorData[it1.value()] = Palapeli::TriggerMapper::instance()->testTrigger(it1.key(), event, m_buttons);
//further processing in a method which is shared with the KeyEvent handler
//further processing in a method which is shared with the MouseEvent handler
handleEventCommon(pEvent, interactorData, m_buttons);
}
......@@ -129,8 +148,11 @@ void Palapeli::InteractorManager::handleEventCommon(const Palapeli::MouseEvent&
while (iter1.hasNext())
{
Palapeli::Interactor* interactor = iter1.next().key();
int priority = interactor->priority();
if ((iter1.value().flags & Palapeli::EventMatchesExactly) == Palapeli::EventMatchesExactly)
priority += AdditionalPriorityForExactMatches;
//NOTE: The minus below implements a descending sort order.
sortedInteractors.insertMulti(-interactor->priority(), interactor);
sortedInteractors.insertMulti(-priority, interactor);
}
//try to activate interactors with matching triggers
foreach (Palapeli::Interactor* interactor, sortedInteractors)
......
......@@ -181,7 +181,7 @@ void Palapeli::MoveViewportInteractor::continueInteraction(const Palapeli::Mouse
//BEGIN Palapeli::ZoomViewportInteractor
Palapeli::ZoomViewportInteractor::ZoomViewportInteractor(QGraphicsView* view)
: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
: Palapeli::Interactor(2, Palapeli::WheelInteractor, view) //priority: more important than ScrollViewport
{
setMetadata(ViewportInteraction, i18nc("Description (used like a name) for a mouse interaction method", "Zoom viewport"), KIcon("zoom-in"));
}
......@@ -197,7 +197,7 @@ void Palapeli::ZoomViewportInteractor::doInteraction(const Palapeli::WheelEvent&
//BEGIN Palapeli::ScrollViewportInteractor
Palapeli::ScrollViewportInteractor::ScrollViewportInteractor(Qt::Orientation orientation, QGraphicsView* view)
: Palapeli::Interactor(0, Palapeli::WheelInteractor, view) //priority: unused for wheel interactors ATM
: Palapeli::Interactor(1, Palapeli::WheelInteractor, view) //priority: less important than ZoomViewport
, m_orientation(orientation)
{
QString description;
......
......@@ -162,11 +162,16 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
{
if (trigger.isValid())
{
const bool testModifiers = trigger.modifiers() == event->modifiers();
const bool testModifiers = trigger.modifiers() == Qt::NoModifier || trigger.modifiers() == event->modifiers();
const bool checkDirection = trigger.wheelDirection() != 0;
const bool testDirection = trigger.wheelDirection() == event->orientation();
if (testModifiers && checkDirection && testDirection)
return Palapeli::EventMatches;
{
if (trigger.modifiers() == event->modifiers())
return Palapeli::EventMatchesExactly;
else
return Palapeli::EventMatches;
}
}
//if execution comes to this point, trigger does not match
return 0;
......@@ -177,17 +182,18 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
if (trigger.isValid())
{
const bool testModifiers = trigger.modifiers() == Qt::NoModifier || trigger.modifiers() == event->modifiers();
const Palapeli::EventProcessingFlags positiveResult = (trigger.modifiers() == event->modifiers()) ? Palapeli::EventMatchesExactly : Palapeli::EventMatches;
const bool checkDirection = trigger.wheelDirection() == 0;
if (testModifiers && checkDirection)
{
if (trigger.button() == Qt::NoButton)
//trigger matches
return Palapeli::EventMatches;
return positiveResult;
const bool checkButtons = (event->button() | event->buttons()) & trigger.button();
if (checkButtons)
{
//trigger matches - construct result
Palapeli::EventProcessingFlags result = Palapeli::EventMatches;
Palapeli::EventProcessingFlags result = positiveResult;
if (event->button() == trigger.button())
{
if (event->type() == QEvent::MouseButtonPress)
......@@ -212,12 +218,13 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
const Qt::KeyboardModifiers modifiers = keyModifier | event->modifiers();
//checking
const bool testModifiers = trigger.modifiers() == Qt::NoModifier || trigger.modifiers() == modifiers;
const Palapeli::EventProcessingFlags positiveResult = trigger.modifiers() == event->modifiers() ? Palapeli::EventMatchesExactly : Palapeli::EventMatches;
const bool checkDirection = trigger.wheelDirection() == 0;
const bool checkButton = (trigger.button() & buttons) || trigger.button() == Qt::NoButton;
if (testModifiers && checkDirection && checkButton)
{
//trigger matches - construct result
Palapeli::EventProcessingFlags result = Palapeli::EventMatches;
Palapeli::EventProcessingFlags result = positiveResult;
if (keyModifier != Qt::NoModifier)
{
if (event->type() == QEvent::KeyPress)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment