Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "command.h"
- Command::Command(XPLMCommandRef ref,
- std::function<Outcome(Phase)> cb,
- bool run_before_sim)
- : ref_(ref), before_(run_before_sim ? 1 : 0), callback_(cb) {
- XPLMRegisterCommandHandler(
- ref_,
- [](XPLMCommandRef, XPLMCommandPhase phase, void* vp) {
- auto cmd = reinterpret_cast<Command*>(vp);
- if (phase == xplm_CommandBegin)
- cmd->phase_ = Phase::Begin;
- if (phase == xplm_CommandContinue)
- cmd->phase_ = Phase::Continue;
- if (phase == xplm_CommandEnd)
- cmd->phase_ = Phase::End;
- auto outcome = cmd->callback_(cmd->phase_);
- return outcome == Outcome::Halt ? 0 : 1;
- },
- before_,
- this);
- }
- Command::Command(std::string cmd_to_replace,
- std::function<Outcome(Phase)> callback,
- bool run_before)
- : Command(XPLMFindCommand(cmd_to_replace.c_str()), callback, run_before) {}
- Command::Command(std::string new_cmd,
- std::string description,
- std::function<Outcome(Phase)> callback,
- bool run_before)
- : Command(XPLMCreateCommand(new_cmd.c_str(), description.c_str()),
- callback,
- run_before) {}
- Command::~Command() {
- XPLMUnregisterCommandHandler(
- ref_,
- [](XPLMCommandRef, XPLMCommandPhase, void*) { return 0; },
- before_,
- this);
- }
- Command::Phase Command::phase() const {
- return phase_;
- }
- void Command::begin() {
- XPLMCommandBegin(ref_);
- }
- void Command::end() {
- XPLMCommandEnd(ref_);
- }
- void Command::once() {
- XPLMCommandOnce(ref_);
- }
Add Comment
Please, Sign In to add comment