Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wpi.first.wpilibj.templates;
- import edu.wpi.first.wpilibj.Joystick;
- import edu.wpi.first.wpilibj.buttons.Button;
- import edu.wpi.first.wpilibj.command.Command;
- import edu.wpi.first.wpilibj.DriverStation;
- /*
- * @author Arhowk
- */
- public class AdvancedButton extends Button{
- protected Joystick m_joystick;
- protected int m_buttonNumber;
- boolean pressed;
- protected boolean m_wasPressed;
- protected Command dummy;
- protected boolean active = false;
- protected Command m_whileHeld;
- protected Command m_whenPressed;
- protected Command m_whenReleased;
- protected Command m_togglePressed;
- protected Command m_toggleReleased;
- private void initDummy(){
- dummy = new Command(){
- protected void initialize() {}
- protected void execute() {
- pressed = isPressed();
- if(DriverStation.getInstance().isOperatorControl()){
- if(m_whileHeld != null && pressed){
- if(!m_whileHeld.isRunning()){
- m_whileHeld.start();
- }
- }else if(m_whileHeld != null && !pressed && m_whileHeld.isRunning()){
- m_whileHeld.cancel();
- }
- if(pressed && !m_wasPressed){
- if(m_whenPressed != null){
- m_whenPressed.start();
- }
- if(m_togglePressed != null){
- if(m_togglePressed.isRunning()){
- m_togglePressed.cancel();
- }else{
- m_togglePressed.start();
- }
- }
- }else if(!pressed && m_wasPressed){
- if(m_whenReleased != null){
- m_whenReleased.start();
- }
- if(m_toggleReleased != null){
- if(m_toggleReleased.isRunning()){
- m_toggleReleased.cancel();
- }else{
- m_toggleReleased.start();
- }
- }
- }
- m_wasPressed = pressed;
- }
- }
- protected boolean isFinished() {return false;}
- protected void end() {}
- protected void interrupted() {}
- };
- dummy.start();
- pressed = false;
- m_wasPressed = false;
- }
- protected AdvancedButton(){
- initDummy();
- }
- protected boolean isPressed(){
- return m_joystick.getRawButton(m_buttonNumber);
- }
- public AdvancedButton(Joystick joystick, int buttonNumber) {
- m_joystick = joystick;
- m_buttonNumber = buttonNumber;
- initDummy();
- }
- public boolean get(){return false;}
- public void whileHeld(final Command command){
- m_whileHeld = command;
- }
- public void whenPressed(final Command command){
- m_whenPressed = command;
- }
- public void whenReleased(final Command command){
- m_whenReleased = command;
- }
- public void whileActive(final Command command){
- m_whileHeld = command;
- }
- public void whenActive(final Command command){
- m_whenPressed = command;
- }
- public void whenInactive(final Command command){
- m_whenReleased = command;
- }
- public void togglePressed(final Command command){
- m_togglePressed = command;
- }
- public void toggleReleased(final Command command){
- m_toggleReleased = command;
- }
- public void cancelAllCommands(){
- if(m_togglePressed != null){
- m_togglePressed.cancel();
- }
- if(m_toggleReleased != null){
- m_toggleReleased.cancel();
- }
- if(m_whenPressed != null){
- m_whenPressed.cancel();
- }
- if(m_whenReleased != null){
- m_whenReleased.cancel();
- }
- if(m_whileHeld != null){
- m_whileHeld.cancel();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement