Mouse Design Pattern

I need some opinions on what a “perfect” design pattern is for general mouse interaction.

Here's a simplified task. I have a small 3d program (QT and openGL) and I use a mouse to interact. Each interaction, as a rule, is not only a call to one function, basically up to 3 function calls are made (initiate, execute, terminate). For example, camera rotation: here, the initial function call delivers the current first position of the mouse, while function function calls will update the camera, etc.

However, for only a few interactions, hardcoding them (inside MousePressEvent, MouseReleaseEvent MouseMoveEvent or MouseWheelEvent, etc.) does not really matter, but if I think of a more advanced program (for example, 20 or more interactions), then I need the right design .

Therefore, how would you create such interactions within QT.

I hope I made my problem clear enough, otherwise I won’t be bothered to complain :-)

thank

+3
source share
3 answers

I suggest using polymorphism and a factory method template. Here is an example:

In my Qt program, I have QGraphicsScenes and QGraphicsItems with mousePressEvent, mouseMoveEvent and mouseReleaseEvent, which look like this:

void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  // call factory method, which returns a subclass depending on where click occurred
  dragHandler = DragHandler::createDragHandler(event /* and other relevant stuff */);
}

void CustomItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
  dragHandler->onMouseMove(event);
}

void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  dragHandler->onMouseRelease(event);
  delete dragHandler;
}

, , CustomItem, , . , , , , . DragHandler:: onMouseMove DragHandler:: onMouseRelease - , , , , , . DragHandler:: onMousePress, .

, , , , , , , .

+4

Qt .

switch mouse_mode:, , , .. mouseDown/mouseUp/mousePosition /, .

(, , ..) / SLOTS , Mouse... Event()

+1

Apple UIGestureRecognizer .

, ( ) , .

GestureRecognizer, , MousePressEvent, MouseReleaseEvent MouseMoveEvent MouseWheelEvent .. GestureRecongnizers .

, : ( ++ ++... )

class Recognizer {
int state; // ex: 0:possible, 1:began, 2:changed, 3:ended/recognized 4:cancelled
protected:
void setTarget(void &theTarget); // or even better a touple, target/method. In this case target is assumed to have a method gestureHandle(Recognizer *r);
virtual void mouserPress() = 0;
virtual void mouserRelease() = 0;
virtual void mouserMove() = 0;
virtual void mouserWheel() = 0;
...
}

class SwipeRecognizer : Recognizer {
int direction; // ex: 0:left2right 1:bottom2top 2:...
private:
void mouserPress() {
    state = 0; // possible. You don't know yet is the mouse is going to swipe, simple click, long press, etc.
    // save some values so you can calculate the direction of the swipe later 
    target.gestureHandle(this);
};
void mouserMove() {
    if (state == 0) {
        state = 1; // it was possible now you know the swipe began!
        direction = ... // calculate the swipe direction here
    } else if (state == 1 || state == 2) {// state is began or changed
        state = 2; // changed ... which means is still mouse dragging
        // probably you want to make more checks here like you are still swiping in the same direction you started, maybe velocity thresholds, if any of your conditions are not met you should cancel the gesture recognizer by setting its state to 4
    }
    target.gestureHandler(this);
};
void mouserRelease() {
    if (state == 2) { // is swipping
        state = 3; // swipe ended
    } else {
        state = 4; // it was not swiping so simple cancel the tracking
    }
    target.gestureHandler(this);
};
void mouserWheel() {
    // if this method is called then this is definitely not a swipe right?
    state = 4; // cancelled
    target.gestureHandler(this);
}

, , , .

:

class Target {
...
void gestureHandler(Recognizer *r) {
    if (r->state == 2) {
        // Is swipping: move the opengl camera using some parameter your recognizer class brings
    } else if (r->state == 3) {
        // ended: stop moving the opengl camera
    } else if (r->state == 4) {
        // Cancelled, maybe restore camera to original position?
    }
}

UIGestureRecognizer / . UIGestureRecognizers -, , , , , , ..

, , PRO , : , ( ).

,

, :)

+1

All Articles