Skip to content

Commit

Permalink
Extract common trigger binding logic
Browse files Browse the repository at this point in the history
Also consistently use previous and current
  • Loading branch information
KangarooKoala committed Dec 15, 2024
1 parent 4bca79b commit dea98e0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
* <p>This class is provided by the NewCommands VendorDep
*/
public class Trigger implements BooleanSupplier {
/** Functional interface for the body of a trigger binding. */
@FunctionalInterface
private interface BindingBody {
/**
* Executes the body of the binding.
*
* @param previous The previous state of the condition.
* @param current The current state of the condition.
*/
void run(boolean previous, boolean current);
}

private final BooleanSupplier m_condition;
private final EventLoop m_loop;

Expand All @@ -50,26 +62,38 @@ public Trigger(BooleanSupplier condition) {
}

/**
* Starts the command when the condition changes.
* Adds a binding to the EventLoop.
*
* @param command the command to start
* @return this trigger, so calls can be chained
* @param body The body of the binding to add.
*/
public Trigger onChange(Command command) {
requireNonNullParam(command, "command", "onChange");
private void addBinding(BindingBody body) {
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();
private boolean m_previous = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();
boolean current = m_condition.getAsBoolean();

if (m_pressedLast != pressed) {
command.schedule();
}
body.run(m_previous, current);

m_pressedLast = pressed;
m_previous = current;
}
});
}

/**
* Starts the command when the condition changes.
*
* @param command the command to start
* @return this trigger, so calls can be chained
*/
public Trigger onChange(Command command) {
requireNonNullParam(command, "command", "onChange");
addBinding(
(previous, current) -> {
if (previous != current) {
command.schedule();
}
});
return this;
Expand All @@ -83,19 +107,10 @@ public void run() {
*/
public Trigger onTrue(Command command) {
requireNonNullParam(command, "command", "onTrue");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (!m_pressedLast && pressed) {
command.schedule();
}

m_pressedLast = pressed;
addBinding(
(previous, current) -> {
if (!previous && current) {
command.schedule();
}
});
return this;
Expand All @@ -109,19 +124,10 @@ public void run() {
*/
public Trigger onFalse(Command command) {
requireNonNullParam(command, "command", "onFalse");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (m_pressedLast && !pressed) {
command.schedule();
}

m_pressedLast = pressed;
addBinding(
(previous, current) -> {
if (previous && !current) {
command.schedule();
}
});
return this;
Expand All @@ -139,21 +145,12 @@ public void run() {
*/
public Trigger whileTrue(Command command) {
requireNonNullParam(command, "command", "whileTrue");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (!m_pressedLast && pressed) {
command.schedule();
} else if (m_pressedLast && !pressed) {
command.cancel();
}

m_pressedLast = pressed;
addBinding(
(previous, current) -> {
if (!previous && current) {
command.schedule();
} else if (previous && !current) {
command.cancel();
}
});
return this;
Expand All @@ -171,21 +168,12 @@ public void run() {
*/
public Trigger whileFalse(Command command) {
requireNonNullParam(command, "command", "whileFalse");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (m_pressedLast && !pressed) {
command.schedule();
} else if (!m_pressedLast && pressed) {
command.cancel();
}

m_pressedLast = pressed;
addBinding(
(previous, current) -> {
if (previous && !current) {
command.schedule();
} else if (!previous && current) {
command.cancel();
}
});
return this;
Expand All @@ -199,23 +187,14 @@ public void run() {
*/
public Trigger toggleOnTrue(Command command) {
requireNonNullParam(command, "command", "toggleOnTrue");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (!m_pressedLast && pressed) {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
}
addBinding(
(previous, current) -> {
if (!previous && current) {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
}

m_pressedLast = pressed;
}
});
return this;
Expand All @@ -229,23 +208,14 @@ public void run() {
*/
public Trigger toggleOnFalse(Command command) {
requireNonNullParam(command, "command", "toggleOnFalse");
m_loop.bind(
new Runnable() {
private boolean m_pressedLast = m_condition.getAsBoolean();

@Override
public void run() {
boolean pressed = m_condition.getAsBoolean();

if (m_pressedLast && !pressed) {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
}
addBinding(
(previous, current) -> {
if (previous && !current) {
if (command.isScheduled()) {
command.cancel();
} else {
command.schedule();
}

m_pressedLast = pressed;
}
});
return this;
Expand Down
Loading

0 comments on commit dea98e0

Please sign in to comment.