Ignore:
Timestamp:
01/24/13 18:20:26 (11 years ago)
Author:
pharms
Message:
  • added detection of double clicks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/eventcore/gui/MouseClickCondenser.java

    r995 r1042  
    7272                } 
    7373                else if (mouseClickSequenceFound(mbDown, mbUp)) { 
    74                     // skip the mouse button down and mouse button up event and add a mouse click 
     74                    // replace the mouse button down and mouse button up event with a generated 
     75                    // mouse click 
    7576                    index += 2; 
    7677                    resultingSequence.add(createClick(mbDown, mbUp)); 
     
    7879                } 
    7980                else if (mouseDragAndDropSequenceFound(mbDown, mbUp)) { 
    80                     // skip the mouse button down and mouse button up event and add a mouse click 
     81                    // replace the mouse button down and mouse button up event with a generated 
     82                    // mouse drag and drop 
    8183                    index += 2; 
    8284                    resultingSequence.add(createDragAndDrop(mbDown, mbUp)); 
    8385                    mouseClickHandled = true; 
    8486                } 
     87                else if (mouseDoubleClickSequenceFound(mbDown, mbUp)) { 
     88                    // replace the two mouse click events with a generated mouse double click 
     89                    index += 2; 
     90                    resultingSequence.add(createDoubleClick(mbDown, mbUp)); 
     91                    mouseClickHandled = true; 
     92                } 
    8593            } 
    8694 
     
    8896                resultingSequence.add(sequence.get(index)); 
    8997                index++; 
     98            } 
     99             
     100            if (resultingSequence.size() > 1) { 
     101                // check for double clicks 
     102                int resultingSequenceIndex = resultingSequence.size() - 1; 
     103                Event click1 = resultingSequence.get(resultingSequenceIndex - 1); 
     104                Event click2 = resultingSequence.get(resultingSequenceIndex); 
     105                if (mouseDoubleClickSequenceFound(click1, click2)) { 
     106                    resultingSequence.remove(resultingSequenceIndex); 
     107                    resultingSequence.remove(resultingSequenceIndex - 1); 
     108                    resultingSequence.add(createDoubleClick(click1, click2)); 
     109                } 
    90110            } 
    91111        } 
     
    195215 
    196216    /** 
     217     *  
     218     */ 
     219    private boolean mouseDoubleClickSequenceFound(Event click1, 
     220                                                  Event click2) 
     221    { 
     222        // check the first in a row of three for validity 
     223        if (!(click1.getType() instanceof MouseClick)) { 
     224            return false; 
     225        } 
     226 
     227        if (((MouseClick) click1.getType()).getButton() != MouseButtonInteraction.Button.LEFT) { 
     228            return false; 
     229        } 
     230         
     231        // check the second node for validity 
     232        if (!(click2.getType() instanceof MouseClick)) { 
     233            return false; 
     234        } 
     235 
     236        // use 500 milliseconds as timestamp difference as this is more or less similar to default 
     237        // values in Microsoft Windows  
     238        if (!timestampDifferenceSmallerThan(click1, click2, 500)) { 
     239            return false; 
     240        } 
     241         
     242        if (!targetsEqual(click1, click2)) { 
     243            return false; 
     244        } 
     245 
     246        if (!buttonsEqual(click1, click2)) { 
     247            return false; 
     248        } 
     249 
     250        if (!coordinatesEqual(click1, click2)) { 
     251            return false; 
     252        } 
     253         
     254        return true; 
     255    } 
     256 
     257    /** 
    197258     * 
    198259     */ 
     
    204265        int y = ((MouseButtonDown) mouseButtonDown.getType()).getY(); 
    205266 
    206         return new Event(new MouseClick(button, x, y), mouseButtonDown.getTarget()); 
     267        Event click = new Event(new MouseClick(button, x, y), mouseButtonDown.getTarget()); 
     268        click.setTimestamp(mouseButtonDown.getTimestamp()); 
     269        return click; 
     270    } 
     271 
     272    /** 
     273     * 
     274     */ 
     275    private Event createDoubleClick(Event click1, Event click2) { 
     276        MouseButtonInteraction.Button button = ((MouseClick) click1.getType()).getButton(); 
     277        
     278        int x = ((MouseClick) click1.getType()).getX(); 
     279        int y = ((MouseClick) click1.getType()).getY(); 
     280 
     281        Event doubleClick = new Event(new MouseDoubleClick(button, x, y), click1.getTarget()); 
     282        doubleClick.setTimestamp(click1.getTimestamp()); 
     283        return doubleClick; 
    207284    } 
    208285 
     
    216293        int yEnd = ((MouseButtonUp) mouseButtonUp.getType()).getY(); 
    217294 
    218         return new Event 
     295        Event dragAndDrop = new Event 
    219296            (new MouseDragAndDrop(xStart, yStart, xEnd, yEnd), mouseButtonDown.getTarget()); 
     297         
     298        dragAndDrop.setTimestamp(mouseButtonDown.getTimestamp()); 
     299        return dragAndDrop; 
    220300    } 
    221301 
     
    248328     * 
    249329     */ 
     330    private boolean timestampDifferenceSmallerThan(Event event1, Event event2, long difference) { 
     331        long timestamp1 = event1.getTimestamp(); 
     332         
     333        if (timestamp1 < 0) { 
     334            return false; 
     335        } 
     336         
     337        long timestamp2 = event2.getTimestamp(); 
     338         
     339        if (timestamp2 < 0) { 
     340            return false; 
     341        } 
     342         
     343        return (Math.abs((timestamp2 - timestamp1))) < difference; 
     344    } 
     345 
     346    /** 
     347     * 
     348     */ 
    250349    private boolean coordinatesEqual(Event event1, Event event2) { 
    251350        int x1 = 
     
    256355            (event2.getType() instanceof MouseButtonInteraction) ? 
    257356                ((MouseButtonInteraction) event2.getType()).getX() : -1; 
    258                  
    259         if ((x1 == -1) || (x1 != x2)) { 
     357         
     358        // allow a deviation of one pixel to identify it as click anyway 
     359        if ((x1 == -1) || (x2 == -1) || (x2 < (x1 - 1)) || ((x1 + 1) < x2)) { 
    260360            return false; 
    261361        } 
     
    269369                ((MouseButtonInteraction) event2.getType()).getY() : -1; 
    270370 
    271         return (y1 != -1) && (y1 == y2); 
     371        return (y1 != -1) && (y2 != -1) && ((y1 - 1) < y2) && (y2 < (y1 + 1)); 
    272372   } 
    273373 
Note: See TracChangeset for help on using the changeset viewer.