Guest User

Untitled

a guest
Apr 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include "command.h"
  2.  
  3. Command::Command(XPLMCommandRef ref,
  4. std::function<Outcome(Phase)> cb,
  5. bool run_before_sim)
  6. : ref_(ref), before_(run_before_sim ? 1 : 0), callback_(cb) {
  7. XPLMRegisterCommandHandler(
  8. ref_,
  9. [](XPLMCommandRef, XPLMCommandPhase phase, void* vp) {
  10. auto cmd = reinterpret_cast<Command*>(vp);
  11. if (phase == xplm_CommandBegin)
  12. cmd->phase_ = Phase::Begin;
  13. if (phase == xplm_CommandContinue)
  14. cmd->phase_ = Phase::Continue;
  15. if (phase == xplm_CommandEnd)
  16. cmd->phase_ = Phase::End;
  17. auto outcome = cmd->callback_(cmd->phase_);
  18. return outcome == Outcome::Halt ? 0 : 1;
  19. },
  20. before_,
  21. this);
  22. }
  23.  
  24. Command::Command(std::string cmd_to_replace,
  25. std::function<Outcome(Phase)> callback,
  26. bool run_before)
  27. : Command(XPLMFindCommand(cmd_to_replace.c_str()), callback, run_before) {}
  28.  
  29. Command::Command(std::string new_cmd,
  30. std::string description,
  31. std::function<Outcome(Phase)> callback,
  32. bool run_before)
  33. : Command(XPLMCreateCommand(new_cmd.c_str(), description.c_str()),
  34. callback,
  35. run_before) {}
  36.  
  37. Command::~Command() {
  38. XPLMUnregisterCommandHandler(
  39. ref_,
  40. [](XPLMCommandRef, XPLMCommandPhase, void*) { return 0; },
  41. before_,
  42. this);
  43. }
  44.  
  45. Command::Phase Command::phase() const {
  46. return phase_;
  47. }
  48.  
  49. void Command::begin() {
  50. XPLMCommandBegin(ref_);
  51. }
  52.  
  53. void Command::end() {
  54. XPLMCommandEnd(ref_);
  55. }
  56.  
  57. void Command::once() {
  58. XPLMCommandOnce(ref_);
  59. }
Add Comment
Please, Sign In to add comment