2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.command2;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import static org.wpilib.util.ErrorMessages.requireNonNullParam;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.util.sendable.SendableBuilder;
|
2020-12-29 22:45:16 -08:00
|
|
|
import java.util.function.BooleanSupplier;
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A command composition that runs one of two commands, depending on the value of the given
|
|
|
|
|
* condition when this command is initialized.
|
2019-08-25 17:47:07 -04:00
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* <p>The rules for command compositions apply: command instances that are passed to it cannot be
|
|
|
|
|
* added to any other composition or scheduled individually, and the composition requires all
|
|
|
|
|
* subsystems its components require.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* <p>This class is provided by the NewCommands VendorDep
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2023-07-14 01:12:01 -04:00
|
|
|
public class ConditionalCommand extends Command {
|
2019-08-25 17:47:07 -04:00
|
|
|
private final Command m_onTrue;
|
|
|
|
|
private final Command m_onFalse;
|
|
|
|
|
private final BooleanSupplier m_condition;
|
|
|
|
|
private Command m_selectedCommand;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new ConditionalCommand.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param onTrue the command to run if the condition is true
|
|
|
|
|
* @param onFalse the command to run if the condition is false
|
2019-08-25 17:47:07 -04:00
|
|
|
* @param condition the condition to determine which command to run
|
|
|
|
|
*/
|
2024-07-13 06:52:30 -07:00
|
|
|
@SuppressWarnings("this-escape")
|
2019-08-25 17:47:07 -04:00
|
|
|
public ConditionalCommand(Command onTrue, Command onFalse, BooleanSupplier condition) {
|
2022-12-07 07:13:31 +02:00
|
|
|
m_onTrue = requireNonNullParam(onTrue, "onTrue", "ConditionalCommand");
|
|
|
|
|
m_onFalse = requireNonNullParam(onFalse, "onFalse", "ConditionalCommand");
|
|
|
|
|
m_condition = requireNonNullParam(condition, "condition", "ConditionalCommand");
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler.getInstance().registerComposedCommands(onTrue, onFalse);
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2024-07-12 06:01:54 +08:00
|
|
|
addRequirements(m_onTrue.getRequirements());
|
|
|
|
|
addRequirements(m_onFalse.getRequirements());
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
|
|
|
|
if (m_condition.getAsBoolean()) {
|
|
|
|
|
m_selectedCommand = m_onTrue;
|
|
|
|
|
} else {
|
|
|
|
|
m_selectedCommand = m_onFalse;
|
|
|
|
|
}
|
|
|
|
|
m_selectedCommand.initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void execute() {
|
|
|
|
|
m_selectedCommand.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interrupted) {
|
|
|
|
|
m_selectedCommand.end(interrupted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
|
|
|
|
return m_selectedCommand.isFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return m_onTrue.runsWhenDisabled() && m_onFalse.runsWhenDisabled();
|
|
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
2023-06-23 18:21:05 +03:00
|
|
|
@Override
|
|
|
|
|
public InterruptionBehavior getInterruptionBehavior() {
|
|
|
|
|
if (m_onTrue.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf
|
|
|
|
|
|| m_onFalse.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
|
|
|
|
return InterruptionBehavior.kCancelSelf;
|
|
|
|
|
} else {
|
|
|
|
|
return InterruptionBehavior.kCancelIncoming;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
@Override
|
|
|
|
|
public void initSendable(SendableBuilder builder) {
|
|
|
|
|
super.initSendable(builder);
|
|
|
|
|
builder.addStringProperty("onTrue", m_onTrue::getName, null);
|
|
|
|
|
builder.addStringProperty("onFalse", m_onFalse::getName, null);
|
|
|
|
|
builder.addStringProperty(
|
|
|
|
|
"selected",
|
|
|
|
|
() -> {
|
|
|
|
|
if (m_selectedCommand == null) {
|
|
|
|
|
return "null";
|
|
|
|
|
} else {
|
|
|
|
|
return m_selectedCommand.getName();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
null);
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|