Sponsors of Wiimoteproject.com
IR Pens for Wiimote Whiteboard
Wiimote Project » Wiimote Projects » Wiimote Interactive Whiteboard » Right click now working without extra hardware
Pages: 1 [2]
  Print  
Author Topic: Right click now working without extra hardware  (Read 7317 times)
0 Members and 1 Guest are viewing this topic.
*
Karma: +0/-0
Posts: 5
Offline Offline
View Profile Email
« Reply #15 on: May 02, 2008, 11:56:36 PM »

It would be great if you could explain the code, or update those files with some comments
Logged
*
Karma: +10/-0
Posts: 134
Offline Offline
View Profile WWW
« Reply #16 on: May 04, 2008, 08:08:11 PM »

It would be great if you could explain the code, or update those files with some comments

Here's the main code with some additional comments. I really doesn't do something special:

public abstract class AbstractRightClick implements RightClickStrategy {

   private static final int PIXEL_MOVE_TOLERANCE = 10;
   
   // access to preferences, here to get the right click delay (e.g. 1000ms)
   private final WWPreferences prefs = WWPreferences.getPreferences();

   private long lightStartTime = 0;
   private Point lightStartPoint = null;
   private boolean active = false;

   /*
      Current activate() implementation (right click):
      Mouse.RIGHT_BUTTON.setPressed(true);
      Mouse.RIGHT_BUTTON.setPressed(false);
   */
   protected abstract void activate();
   
   
   /*
      Current deactivate() implementation (left click):
      Mouse.LEFT_BUTTON.setPressed(true);
      Mouse.LEFT_BUTTON.setPressed(false);
   */
   protected abstract void deactivate();

   /*
      This is called when the mouse is moved to the given Point p
      p == null means that no IR light is currently detected
   */
   public void process(Point p) {
      if (p != null) {
         /*
            reset start point for potential right click, if currently not active
            and either IR light was off previously (lightStartPoint = null) or
            mouse has been moved (outside the tolerance area)
            
         */
         if (!active && (lightStartPoint == null || lightStartPoint.distance(p) > PIXEL_MOVE_TOLERANCE)) {
            // save current cursor position and time
            lightStartPoint = p;
            lightStartTime = System.currentTimeMillis();
         }
      } else {
         // IR light not detected (IR pen off)
         // reset start point
         lightStartPoint = null;
         // deactivate(), if has been active
         if (active) {
            active = false;
            deactivate();
         }
      }
   }

   /*
      This gets called after process(Point), but only if right-clicks are enabled in the GUI
      returns true if the right-click is activated, false otherwise
   */
   public boolean trigger() {
      // light hasn't moved in the same area for long enough to trigger right click
      if (lightStartPoint != null && System.currentTimeMillis() - lightStartTime > prefs.getRightClickDelay()) {
         if (!active) {
            active = true;
            Mouse.LEFT_BUTTON.setPressed(false);
            activate();
         }
         return true;
      }
      return false;
   }

}


Hope it helps,
Uwe
Logged

Java WiimoteWhiteboard for Mac OS X, Windows and Linux.
Pages: 1 [2]
  Print  
 
Jump to:  

Clicky Web Analytics