X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FCakeLight.java;h=cd59362c9c66044ca280d152b7fabd0dbacbb4bc;hb=c9edf58db4c00b7b95bb7f521063e5ecd79db262;hp=c2f896d1d5d207af4cb8da720553ddaa100a190e;hpb=8ff7ee5a10d5524162186006c9efb91935153f2f;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/CakeLight.java b/src/kaka/cakelight/CakeLight.java index c2f896d..cd59362 100644 --- a/src/kaka/cakelight/CakeLight.java +++ b/src/kaka/cakelight/CakeLight.java @@ -1,5 +1,6 @@ package kaka.cakelight; +import kaka.cakelight.mode.Mode; import org.jnativehook.GlobalScreen; import org.jnativehook.NativeHookException; import org.jnativehook.keyboard.NativeKeyAdapter; @@ -7,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) { @@ -20,21 +24,58 @@ 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() != null); + } + + public void pushMode(Mode mode) { + Objects.requireNonNull(mode); + if (!modes.isEmpty()) { + pauseMode(modes.peek()); } + modes.push(mode); + startMode(mode); + // TODO: create a composite fading mode of top of stack and new mode + } + + public Mode popMode() { + if (!modes.isEmpty()) { + Mode mode = modes.pop(); + stopMode(mode); + if (!modes.isEmpty()) { + resumeMode(modes.peek()); + } + return mode; + } + return null; + // 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 pauseMode(Mode mode) { + mode.pause(); + } + + private void resumeMode(Mode mode) { + mode.resume(); + } + + 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() { - Console.start(this, config); - initNativeHook(); + Console console = Console.start(this, config); + PipeController.start(console); +// initNativeHook(); // TODO // FrameGrabber grabber = FrameGrabber.from(config); // grabber.prepare();