Skip to content

Commit 70f36cc

Browse files
[commands] Extract common trigger binding logic (#7550)
This makes the logic clearer in the actual binding methods and will hopefully make it less annoying to make changes such as allowing control over initial edges. Also changes Java to use previous and current to match C++.
1 parent 564c1f2 commit 70f36cc

File tree

3 files changed

+125
-206
lines changed

3 files changed

+125
-206
lines changed

wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/Trigger.java

Lines changed: 69 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
* <p>This class is provided by the NewCommands VendorDep
2525
*/
2626
public class Trigger implements BooleanSupplier {
27+
/** Functional interface for the body of a trigger binding. */
28+
@FunctionalInterface
29+
private interface BindingBody {
30+
/**
31+
* Executes the body of the binding.
32+
*
33+
* @param previous The previous state of the condition.
34+
* @param current The current state of the condition.
35+
*/
36+
void run(boolean previous, boolean current);
37+
}
38+
2739
private final BooleanSupplier m_condition;
2840
private final EventLoop m_loop;
2941

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

5264
/**
53-
* Starts the command when the condition changes.
65+
* Adds a binding to the EventLoop.
5466
*
55-
* @param command the command to start
56-
* @return this trigger, so calls can be chained
67+
* @param body The body of the binding to add.
5768
*/
58-
public Trigger onChange(Command command) {
59-
requireNonNullParam(command, "command", "onChange");
69+
private void addBinding(BindingBody body) {
6070
m_loop.bind(
6171
new Runnable() {
62-
private boolean m_pressedLast = m_condition.getAsBoolean();
72+
private boolean m_previous = m_condition.getAsBoolean();
6373

6474
@Override
6575
public void run() {
66-
boolean pressed = m_condition.getAsBoolean();
76+
boolean current = m_condition.getAsBoolean();
6777

68-
if (m_pressedLast != pressed) {
69-
command.schedule();
70-
}
78+
body.run(m_previous, current);
7179

72-
m_pressedLast = pressed;
80+
m_previous = current;
81+
}
82+
});
83+
}
84+
85+
/**
86+
* Starts the command when the condition changes.
87+
*
88+
* @param command the command to start
89+
* @return this trigger, so calls can be chained
90+
*/
91+
public Trigger onChange(Command command) {
92+
requireNonNullParam(command, "command", "onChange");
93+
addBinding(
94+
(previous, current) -> {
95+
if (previous != current) {
96+
command.schedule();
7397
}
7498
});
7599
return this;
@@ -83,19 +107,10 @@ public void run() {
83107
*/
84108
public Trigger onTrue(Command command) {
85109
requireNonNullParam(command, "command", "onTrue");
86-
m_loop.bind(
87-
new Runnable() {
88-
private boolean m_pressedLast = m_condition.getAsBoolean();
89-
90-
@Override
91-
public void run() {
92-
boolean pressed = m_condition.getAsBoolean();
93-
94-
if (!m_pressedLast && pressed) {
95-
command.schedule();
96-
}
97-
98-
m_pressedLast = pressed;
110+
addBinding(
111+
(previous, current) -> {
112+
if (!previous && current) {
113+
command.schedule();
99114
}
100115
});
101116
return this;
@@ -109,19 +124,10 @@ public void run() {
109124
*/
110125
public Trigger onFalse(Command command) {
111126
requireNonNullParam(command, "command", "onFalse");
112-
m_loop.bind(
113-
new Runnable() {
114-
private boolean m_pressedLast = m_condition.getAsBoolean();
115-
116-
@Override
117-
public void run() {
118-
boolean pressed = m_condition.getAsBoolean();
119-
120-
if (m_pressedLast && !pressed) {
121-
command.schedule();
122-
}
123-
124-
m_pressedLast = pressed;
127+
addBinding(
128+
(previous, current) -> {
129+
if (previous && !current) {
130+
command.schedule();
125131
}
126132
});
127133
return this;
@@ -139,21 +145,12 @@ public void run() {
139145
*/
140146
public Trigger whileTrue(Command command) {
141147
requireNonNullParam(command, "command", "whileTrue");
142-
m_loop.bind(
143-
new Runnable() {
144-
private boolean m_pressedLast = m_condition.getAsBoolean();
145-
146-
@Override
147-
public void run() {
148-
boolean pressed = m_condition.getAsBoolean();
149-
150-
if (!m_pressedLast && pressed) {
151-
command.schedule();
152-
} else if (m_pressedLast && !pressed) {
153-
command.cancel();
154-
}
155-
156-
m_pressedLast = pressed;
148+
addBinding(
149+
(previous, current) -> {
150+
if (!previous && current) {
151+
command.schedule();
152+
} else if (previous && !current) {
153+
command.cancel();
157154
}
158155
});
159156
return this;
@@ -171,21 +168,12 @@ public void run() {
171168
*/
172169
public Trigger whileFalse(Command command) {
173170
requireNonNullParam(command, "command", "whileFalse");
174-
m_loop.bind(
175-
new Runnable() {
176-
private boolean m_pressedLast = m_condition.getAsBoolean();
177-
178-
@Override
179-
public void run() {
180-
boolean pressed = m_condition.getAsBoolean();
181-
182-
if (m_pressedLast && !pressed) {
183-
command.schedule();
184-
} else if (!m_pressedLast && pressed) {
185-
command.cancel();
186-
}
187-
188-
m_pressedLast = pressed;
171+
addBinding(
172+
(previous, current) -> {
173+
if (previous && !current) {
174+
command.schedule();
175+
} else if (!previous && current) {
176+
command.cancel();
189177
}
190178
});
191179
return this;
@@ -199,23 +187,14 @@ public void run() {
199187
*/
200188
public Trigger toggleOnTrue(Command command) {
201189
requireNonNullParam(command, "command", "toggleOnTrue");
202-
m_loop.bind(
203-
new Runnable() {
204-
private boolean m_pressedLast = m_condition.getAsBoolean();
205-
206-
@Override
207-
public void run() {
208-
boolean pressed = m_condition.getAsBoolean();
209-
210-
if (!m_pressedLast && pressed) {
211-
if (command.isScheduled()) {
212-
command.cancel();
213-
} else {
214-
command.schedule();
215-
}
190+
addBinding(
191+
(previous, current) -> {
192+
if (!previous && current) {
193+
if (command.isScheduled()) {
194+
command.cancel();
195+
} else {
196+
command.schedule();
216197
}
217-
218-
m_pressedLast = pressed;
219198
}
220199
});
221200
return this;
@@ -229,23 +208,14 @@ public void run() {
229208
*/
230209
public Trigger toggleOnFalse(Command command) {
231210
requireNonNullParam(command, "command", "toggleOnFalse");
232-
m_loop.bind(
233-
new Runnable() {
234-
private boolean m_pressedLast = m_condition.getAsBoolean();
235-
236-
@Override
237-
public void run() {
238-
boolean pressed = m_condition.getAsBoolean();
239-
240-
if (m_pressedLast && !pressed) {
241-
if (command.isScheduled()) {
242-
command.cancel();
243-
} else {
244-
command.schedule();
245-
}
211+
addBinding(
212+
(previous, current) -> {
213+
if (previous && !current) {
214+
if (command.isScheduled()) {
215+
command.cancel();
216+
} else {
217+
command.schedule();
246218
}
247-
248-
m_pressedLast = pressed;
249219
}
250220
});
251221
return this;

0 commit comments

Comments
 (0)