From fc040bcb6270ff0a4bfaab6cfeaad60edef2a11c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 1 Dec 2019 22:17:53 +0100 Subject: [PATCH] Use stack of modes instead of just one --- src/kaka/cakelight/CakeLight.java | 45 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/kaka/cakelight/CakeLight.java b/src/kaka/cakelight/CakeLight.java index 8156833..0c1aa1a 100644 --- a/src/kaka/cakelight/CakeLight.java +++ b/src/kaka/cakelight/CakeLight.java @@ -8,9 +8,12 @@ import org.jnativehook.keyboard.NativeKeyEvent; import org.jnativehook.mouse.NativeMouseEvent; import org.jnativehook.mouse.NativeMouseMotionAdapter; +import java.util.Objects; +import java.util.Stack; + public class CakeLight { private Configuration config; - private Mode mode; + private Stack modes = new Stack<>(); private LedController ledController; public CakeLight(Configuration config, LedController ledController) { @@ -21,16 +24,44 @@ public class CakeLight { public void setMode(Mode mode) { cleanup(); - this.mode = mode; - mode.setFrameListener(ledController::onFrame); - mode.enter(config); + pushMode(mode); } public void cleanup() { - if (this.mode != null) { - this.mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting - this.mode.exit(); + while (popMode()); + } + + public void pushMode(Mode mode) { + Objects.requireNonNull(mode); + if (!modes.isEmpty()) { + stopMode(modes.peek()); + } + modes.push(mode); + startMode(mode); + // TODO: create a composite fading mode of top of stack and new mode + } + + public boolean popMode() { + if (!modes.isEmpty()) { + Mode mode = modes.pop(); + stopMode(mode); + if (!modes.isEmpty()) { + startMode(modes.peek()); + } + return true; } + return false; + // TODO: create a composite fading mode of popped mode and top of stack, unless doing cleanup + } + + private void startMode(Mode mode) { + mode.setFrameListener(ledController::onFrame); + mode.enter(config); + } + + private void stopMode(Mode mode) { + mode.setFrameListener(ledFrame -> {}); // To avoid any frame being sent to the controller while the thread is exiting + mode.exit(); } public void startLoop() { -- 2.11.0