mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[commands] Revert to original Trigger implementation (#4673)
Trigger was refactored to use BooleanEvent when it was introduced in #4104. This reverts to the original implementation until edge-based BooleanEvents can be fixed.
This commit is contained in:
@@ -54,7 +54,12 @@ public class BooleanEvent implements BooleanSupplier {
|
||||
* @param action the action to run if this event is active.
|
||||
*/
|
||||
public final void ifHigh(Runnable action) {
|
||||
m_loop.bind(m_signal, action);
|
||||
m_loop.bind(
|
||||
() -> {
|
||||
if (m_signal.getAsBoolean()) {
|
||||
action.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,45 +6,27 @@ package edu.wpi.first.wpilibj.event;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/** The loop polling {@link BooleanEvent} objects and executing the actions bound to them. */
|
||||
public final class EventLoop {
|
||||
private final Collection<Binding> m_bindings = new LinkedHashSet<>();
|
||||
private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
|
||||
|
||||
/**
|
||||
* Bind a new action to run whenever the condition is true.
|
||||
*
|
||||
* @param condition the condition to listen to.
|
||||
* @param action the action to run.
|
||||
*/
|
||||
public void bind(BooleanSupplier condition, Runnable action) {
|
||||
m_bindings.add(new Binding(condition, action));
|
||||
public void bind(Runnable action) {
|
||||
m_bindings.add(action);
|
||||
}
|
||||
|
||||
/** Poll all bindings. */
|
||||
public void poll() {
|
||||
m_bindings.forEach(Binding::poll);
|
||||
m_bindings.forEach(Runnable::run);
|
||||
}
|
||||
|
||||
/** Clear all bindings. */
|
||||
public void clear() {
|
||||
m_bindings.clear();
|
||||
}
|
||||
|
||||
private static class Binding {
|
||||
private final BooleanSupplier m_condition;
|
||||
private final Runnable m_action;
|
||||
|
||||
private Binding(BooleanSupplier condition, Runnable action) {
|
||||
this.m_condition = condition;
|
||||
this.m_action = action;
|
||||
}
|
||||
|
||||
void poll() {
|
||||
if (m_condition.getAsBoolean()) {
|
||||
m_action.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user