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
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj2.command;
|
|
|
|
|
|
2022-12-07 21:46:26 -08:00
|
|
|
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
import edu.wpi.first.util.sendable.SendableBuilder;
|
2020-12-29 22:45:16 -08:00
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
/**
|
2023-08-25 12:41:25 -04:00
|
|
|
* A command composition that runs one of a selection of commands using a selector and a key to
|
|
|
|
|
* command mapping.
|
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
|
2023-10-30 14:09:14 -04:00
|
|
|
*
|
|
|
|
|
* @param <K> The type of key used to select the command
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2023-10-30 14:09:14 -04:00
|
|
|
public class SelectCommand<K> extends Command {
|
|
|
|
|
private final Map<K, Command> m_commands;
|
|
|
|
|
private final Supplier<? extends K> m_selector;
|
2019-08-25 17:47:07 -04:00
|
|
|
private Command m_selectedCommand;
|
2022-11-28 02:23:56 +02:00
|
|
|
private boolean m_runsWhenDisabled = true;
|
|
|
|
|
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2023-08-30 16:21:17 -04:00
|
|
|
private final Command m_defaultCommand =
|
|
|
|
|
new PrintCommand("SelectCommand selector value does not correspond to any command!");
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
/**
|
2022-12-26 14:32:13 -05:00
|
|
|
* Creates a new SelectCommand.
|
2019-08-25 17:47:07 -04:00
|
|
|
*
|
|
|
|
|
* @param commands the map of commands to choose from
|
|
|
|
|
* @param selector the selector to determine which command to run
|
|
|
|
|
*/
|
2024-07-13 06:52:30 -07:00
|
|
|
@SuppressWarnings("this-escape")
|
2023-10-30 14:09:14 -04:00
|
|
|
public SelectCommand(Map<K, Command> commands, Supplier<? extends K> selector) {
|
2019-08-25 17:47:07 -04:00
|
|
|
m_commands = requireNonNullParam(commands, "commands", "SelectCommand");
|
|
|
|
|
m_selector = requireNonNullParam(selector, "selector", "SelectCommand");
|
|
|
|
|
|
2023-08-30 16:21:17 -04:00
|
|
|
CommandScheduler.getInstance().registerComposedCommands(m_defaultCommand);
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler.getInstance()
|
|
|
|
|
.registerComposedCommands(commands.values().toArray(new Command[] {}));
|
|
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
for (Command command : m_commands.values()) {
|
2024-07-12 06:01:54 +08:00
|
|
|
addRequirements(command.getRequirements());
|
2022-11-28 02:23:56 +02:00
|
|
|
m_runsWhenDisabled &= command.runsWhenDisabled();
|
|
|
|
|
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
|
|
|
|
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2023-08-30 16:21:17 -04:00
|
|
|
m_selectedCommand = m_commands.getOrDefault(m_selector.get(), m_defaultCommand);
|
2019-08-25 17:47:07 -04:00
|
|
|
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() {
|
2022-11-28 02:23:56 +02:00
|
|
|
return m_runsWhenDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InterruptionBehavior getInterruptionBehavior() {
|
|
|
|
|
return m_interruptBehavior;
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initSendable(SendableBuilder builder) {
|
|
|
|
|
super.initSendable(builder);
|
|
|
|
|
|
|
|
|
|
builder.addStringProperty(
|
|
|
|
|
"selected", () -> m_selectedCommand == null ? "null" : m_selectedCommand.getName(), null);
|
|
|
|
|
}
|
2019-08-25 17:47:07 -04:00
|
|
|
}
|