Skip to content

Commit

Permalink
feat: 🚧 add triggers API
Browse files Browse the repository at this point in the history
adds a trigger API that allows composing different triggers (base triggers triggered by an event, triggers that combine two triggers) to allow events that use multiple buttons
  • Loading branch information
ion098 authored Sep 23, 2024
1 parent 99897dc commit 9379098
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions include/gamepad/triggers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

namespace Gamepad {

class AbstractTrigger;
class ConstantTrigger;

class Trigger final {
public:
Trigger (bool val): impl(new ConstantTrigger(val)) {}
explicit Trigger(std::unique_ptr<AbstractTrigger> _impl) : impl(std::move(_impl)) {}
bool eval() { return impl.eval(); }
//private:
std::unique_ptr<AbstractTrigger> impl;
}

class AbstractTrigger {
public:
virtual bool eval() = 0;
virtual ~AbstractTrigger() = default;
}

class ConstantTrigger {
public:
explicit ConstantTrigger (bool _val) : _val(val) {}
override bool eval() { return val; }
private:
bool val;
}

class AndTrigger : public AbstractTrigger {
public:
AndTrigger(std::unique_ptr<AbstractTrigger> _left,
std::unique_ptr<AbstractTrigger> _right) : left(_left), right(_right) {}
override bool eval() { return left.eval() & right.eval(); }
private:
std::unique_ptr<AbstractTrigger> left;
std::unique_ptr<AbstractTrigger> right;
}

class OrTrigger : public AbstractTrigger {
public:
OrTrigger(std::unique_ptr<AbstractTrigger> _left,
std::unique_ptr<AbstractTrigger> _right) : left(_left), right(_right) {}
override bool eval() { return left.eval() | right.eval(); }
private:
std::unique_ptr<AbstractTrigger> left;
std::unique_ptr<AbstractTrigger> right;
}

class XorTrigger : public AbstractTrigger {
public:
XorTrigger(std::unique_ptr<AbstractTrigger> _left,
std::unique_ptr<AbstractTrigger> _right) : left(_left), right(_right) {}
override bool eval() { return left.eval() ^ right.eval(); }
private:
std::unique_ptr<AbstractTrigger> left;
std::unique_ptr<AbstractTrigger> right;
}

class NotTrigger : public AbstractTrigger {
public:
NotTrigger(std::unique_ptr<AbstractTrigger> _op) : op(_op) {}
override bool eval() { return !op.eval(); }
private:
std::unique_ptr<AbstractTrigger> op;
}

Trigger operator&(Trigger left, Trigger right) {
return Trigger(new AndTrigger(std::move(left.impl), std::move(right.impl)));
}

Trigger operator|(Trigger left, Trigger right) {
return Trigger(new OrTrigger(std::move(left.impl), std::move(right.impl)));
}

Trigger operator^(Trigger left, Trigger right) {
return Trigger(new XorTrigger(std::move(left.impl), std::move(right.impl)));
}

Trigger operator!(Trigger op) {
return Trigger(new NotTrigger(std::move(op.impl)));
}

}

0 comments on commit 9379098

Please sign in to comment.