Author Topic: Right click now working without extra hardware  (Read 11872 times)

Offline 7em1r

  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Reply #15 on: May 02, 2008, 02:56:36 PM
It would be great if you could explain the code, or update those files with some comments



Offline ujs

  • *
  • Posts: 134
  • Karma: +10/-0
    • View Profile
    • uweschmidt.org
Reply #16 on: May 04, 2008, 11:08:11 AM
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

Java WiimoteWhiteboard for Mac OS X, Windows and Linux.