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 23:55:59 -04:00
|
|
|
|
|
|
|
|
#include "frc2/command/ConditionalCommand.h"
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
using namespace frc2;
|
|
|
|
|
|
|
|
|
|
ConditionalCommand::ConditionalCommand(std::unique_ptr<Command>&& onTrue,
|
|
|
|
|
std::unique_ptr<Command>&& onFalse,
|
|
|
|
|
std::function<bool()> condition)
|
|
|
|
|
: m_condition{std::move(condition)} {
|
2022-12-07 07:13:31 +02:00
|
|
|
CommandScheduler::GetInstance().RequireUngrouped(
|
|
|
|
|
{onTrue.get(), onFalse.get()});
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
m_onTrue = std::move(onTrue);
|
|
|
|
|
m_onFalse = std::move(onFalse);
|
|
|
|
|
|
2022-12-07 07:13:31 +02:00
|
|
|
m_onTrue->SetComposed(true);
|
|
|
|
|
m_onFalse->SetComposed(true);
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
m_runsWhenDisabled &= m_onTrue->RunsWhenDisabled();
|
|
|
|
|
m_runsWhenDisabled &= m_onFalse->RunsWhenDisabled();
|
|
|
|
|
|
|
|
|
|
AddRequirements(m_onTrue->GetRequirements());
|
|
|
|
|
AddRequirements(m_onFalse->GetRequirements());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConditionalCommand::Initialize() {
|
|
|
|
|
if (m_condition()) {
|
|
|
|
|
m_selectedCommand = m_onTrue.get();
|
|
|
|
|
} else {
|
|
|
|
|
m_selectedCommand = m_onFalse.get();
|
|
|
|
|
}
|
|
|
|
|
m_selectedCommand->Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void ConditionalCommand::Execute() {
|
|
|
|
|
m_selectedCommand->Execute();
|
|
|
|
|
}
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
void ConditionalCommand::End(bool interrupted) {
|
|
|
|
|
m_selectedCommand->End(interrupted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConditionalCommand::IsFinished() {
|
|
|
|
|
return m_selectedCommand->IsFinished();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool ConditionalCommand::RunsWhenDisabled() const {
|
|
|
|
|
return m_runsWhenDisabled;
|
|
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
|
|
|
|
void ConditionalCommand::InitSendable(wpi::SendableBuilder& builder) {
|
|
|
|
|
CommandBase::InitSendable(builder);
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"onTrue", [this] { return m_onTrue->GetName(); }, nullptr);
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"onFalse", [this] { return m_onFalse->GetName(); }, nullptr);
|
|
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"selected",
|
|
|
|
|
[this] {
|
|
|
|
|
if (m_selectedCommand) {
|
|
|
|
|
return m_selectedCommand->GetName();
|
|
|
|
|
} else {
|
|
|
|
|
return std::string{"null"};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|