SHARE
TWEET

Untitled

a guest Feb 7th, 2013 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.wpi.first.wpilibj.templates;
  2. import edu.wpi.first.wpilibj.Joystick;
  3. import edu.wpi.first.wpilibj.buttons.Button;
  4. import edu.wpi.first.wpilibj.command.Command;
  5. import edu.wpi.first.wpilibj.DriverStation;
  6.  
  7. /*
  8.  * @author Arhowk
  9.  */
  10. public class AdvancedButton extends Button{
  11.     protected Joystick m_joystick;
  12.     protected int m_buttonNumber;
  13.     boolean pressed;
  14.     protected boolean m_wasPressed;
  15.     protected Command dummy;
  16.     protected boolean active = false;
  17.     protected Command m_whileHeld;
  18.     protected Command m_whenPressed;
  19.     protected Command m_whenReleased;
  20.     protected Command m_togglePressed;
  21.     protected Command m_toggleReleased;
  22.    
  23.     private void initDummy(){
  24.         dummy = new Command(){
  25.             protected void initialize() {}
  26.             protected void execute() {
  27.                 pressed = isPressed();
  28.                 if(DriverStation.getInstance().isOperatorControl()){
  29.                     if(m_whileHeld != null && pressed){
  30.                         if(!m_whileHeld.isRunning()){
  31.                             m_whileHeld.start();
  32.                         }
  33.                     }else if(m_whileHeld != null && !pressed && m_whileHeld.isRunning()){
  34.                         m_whileHeld.cancel();
  35.                     }
  36.                     if(pressed && !m_wasPressed){
  37.                         if(m_whenPressed != null){
  38.                             m_whenPressed.start();
  39.                         }
  40.                         if(m_togglePressed != null){
  41.                             if(m_togglePressed.isRunning()){
  42.                                 m_togglePressed.cancel();
  43.                             }else{
  44.                                 m_togglePressed.start();
  45.                             }
  46.                         }
  47.                     }else if(!pressed && m_wasPressed){
  48.                         if(m_whenReleased != null){
  49.                             m_whenReleased.start();
  50.                         }
  51.  
  52.                         if(m_toggleReleased != null){
  53.                             if(m_toggleReleased.isRunning()){
  54.                                 m_toggleReleased.cancel();
  55.                             }else{
  56.                                 m_toggleReleased.start();
  57.                             }
  58.                         }
  59.                     }
  60.                     m_wasPressed = pressed;
  61.                 }
  62.             }
  63.  
  64.             protected boolean isFinished() {return false;}
  65.             protected void end() {}
  66.             protected void interrupted() {}
  67.         };
  68.         dummy.start();
  69.         pressed = false;
  70.         m_wasPressed = false;
  71.     }
  72.     protected AdvancedButton(){
  73.         initDummy();
  74.     }
  75.    
  76.     protected boolean isPressed(){
  77.         return m_joystick.getRawButton(m_buttonNumber);
  78.     }
  79.     public AdvancedButton(Joystick joystick, int buttonNumber) {
  80.         m_joystick = joystick;
  81.         m_buttonNumber = buttonNumber;
  82.         initDummy();
  83.     }
  84.     public boolean get(){return false;}
  85.     public void whileHeld(final Command command){
  86.         m_whileHeld = command;
  87.     }
  88.     public void whenPressed(final Command command){
  89.         m_whenPressed = command;
  90.     }
  91.     public void whenReleased(final Command command){
  92.         m_whenReleased = command;
  93.     }
  94.     public void whileActive(final Command command){
  95.         m_whileHeld = command;
  96.     }
  97.     public void whenActive(final Command command){
  98.         m_whenPressed = command;
  99.     }
  100.     public void whenInactive(final Command command){
  101.         m_whenReleased = command;
  102.     }
  103.     public void togglePressed(final Command command){
  104.         m_togglePressed = command;
  105.     }
  106.     public void toggleReleased(final Command command){
  107.         m_toggleReleased = command;
  108.     }
  109.     public void cancelAllCommands(){
  110.         if(m_togglePressed != null){
  111.             m_togglePressed.cancel();
  112.         }
  113.         if(m_toggleReleased != null){
  114.             m_toggleReleased.cancel();
  115.         }
  116.         if(m_whenPressed != null){
  117.             m_whenPressed.cancel();
  118.         }
  119.         if(m_whenReleased != null){
  120.             m_whenReleased.cancel();
  121.         }
  122.         if(m_whileHeld != null){
  123.             m_whileHeld.cancel();
  124.         }
  125.     }
  126. }
RAW Paste Data
Top