Package jpotter

Source Code of jpotter.GestureManager

package jpotter;

import java.awt.Point;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.wiigee.control.WiimoteWiigee;
import org.wiigee.device.Device;
import org.wiigee.device.Wiimote;
import org.wiigee.event.ButtonListener;
import org.wiigee.event.ButtonPressedEvent;
import org.wiigee.event.ButtonReleasedEvent;
import org.wiigee.event.GestureEvent;
import org.wiigee.event.GestureListener;
import org.wiigee.event.InfraredEvent;
import org.wiigee.event.InfraredListener;
import org.wiigee.logic.ProcessingUnit;

/**
* Date: 27 Mar 2011 Time: 11:34:46
*
* @author Thomas Michel
*/
@SuppressWarnings("boxing")
public class GestureManager {

  // TODO change this
  private static final GestureManager instance = new GestureManager();

  private final WiimoteWiigee wiigee;
  private final Map<Integer, String> gestureMap;
  private Wiimote wiimote;
  private ButtonListener spellListener;

  private GestureListener gestureListener;

  private Vector<?> theVector, theVector2;

  public static GestureManager getInstance() {
    return instance;
  }

  /**
   *
   * @author Thomas Michel
   */
  private GestureManager() {
    if (instance != null)
      throw new AssertionError();
    gestureMap = new HashMap<Integer, String>();
    wiigee = new WiimoteWiigee();
    try {
      Wiimote[] motes = wiigee.getDevices();
      for (Wiimote m : motes) {
        if (m != null) {
          wiimote = m;
          break;
        }
      }
      wiimote.setInfraredCameraEnabled(true, Wiimote.IR_MODE_EXTENDED);
      wiimote.setLED(2);
      wiimote.vibrateForTime(1000);

      int id = 0;
      for (File f : new File("gesture").listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
          String path = pathname.getName();
          if (path.length() < 4)
            return false;
          return path.substring(path.length() - 3, path.length()).equals("txt");
        }

      })) {
        String store = "";
        if (f.getName().indexOf('.') <= 0)
          store = f.getName();
        else
          store = f.getName().substring(0, f.getName().indexOf('.'));
        gestureMap.put(id++, store);
        wiimote.loadGesture("gesture/" + store);
      }
      gestureListener = new GestureListener() {

        @Override
        public void gestureReceived(GestureEvent event) {
          if (FusionManager.getInstance().getLastReceivedGesture() == null) {
            FusionManager.getInstance().postGesture(event.getId());
            System.out.println("gesture:" + gestureMap.get(event.getId()));
          }
          // else
          // System.out.println("already sent a gesture for this incantation!");
        }
      };

      wiimote.addInfraredListener(new InfraredListener() {

        @Override
        public void infraredReceived(InfraredEvent event) {
          int[][] coord = event.getCoordinates();
          IHM.getInstance().setCurs(new Point(-coord[0][0] + 1023, coord[0][1]));
          IHM.getInstance().repaint();
        }
      });
      wiimote.addGestureListener(gestureListener);
      wiimote.setTrainButton(Wiimote.BUTTON_HOME);

      spellListener = new ButtonListener() {

        @Override
        public void buttonReleaseReceived(ButtonReleasedEvent event) {
          // bla
        }

        @Override
        public void buttonPressReceived(ButtonPressedEvent event) {
          if (event.getButton() == Wiimote.BUTTON_A) {
            if (IHM.getInstance().setTarget()) {
              IncantationManager.getInstance().listen();
              listen();
              System.out.println("you can start casting...");
              FusionManager.getInstance().startTimer();
            } else
              System.out.println("you didn't click on a target");
          }
        }
      };

      Field f = ProcessingUnit.class.getDeclaredField("gesturelistener");
      f.setAccessible(true);
      theVector = ((Vector<?>) f.get(wiimote.getProcessingUnit()));// .clear();

      Field f2 = Device.class.getDeclaredField("buttonlistener");
      f2.setAccessible(true);
      theVector2 = ((Vector<?>) f2.get(wiimote));

    } catch (IOException e1) {
      e1.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
  }

  public void listen() {
    theVector.clear();

    wiimote.addGestureListener(gestureListener);
  }

  public String getGesture(int id) {
    return gestureMap.get(id);
  }

  public void spellCast() throws IOException {
    wiimote.vibrateForTime(100);
  }

  public void canCastSpell() {
    wiimote.addButtonListener(spellListener);
  }

  public void cannotCastSpell() {
    theVector2.remove(spellListener);

//    Field f;
//    try {
//      f = ProcessingUnit.class.getDeclaredField("gesturelistener");
//      f.setAccessible(true);
//      ((Vector<?>) f.get(wiimote.getProcessingUnit())).clear();
//      System.out.println("size of the vector : " + ((Vector<?>) f.get(wiimote.getProcessingUnit())).size());
//    } catch (SecurityException e) {
//      e.printStackTrace();
//    } catch (NoSuchFieldException e) {
//      e.printStackTrace();
//    } catch (IllegalArgumentException e) {
//      e.printStackTrace();
//    } catch (IllegalAccessException e) {
//      e.printStackTrace();
//    }
  }
}
TOP

Related Classes of jpotter.GestureManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.