Changed helpful hints to use scheduler service

This commit is contained in:
Brendan Robert 2015-08-12 00:09:15 -05:00
parent 7faca953b6
commit fb24c875fb

View File

@ -41,6 +41,10 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -364,30 +368,11 @@ public class Apple2e extends Computer {
}
private List<RAMListener> hints = new ArrayList<>();
Thread twinkleEffect;
private void enableHints() {
if (hints.isEmpty()) {
hints.add(new RAMListener(RAMEvent.TYPE.EXECUTE, RAMEvent.SCOPE.ADDRESS, RAMEvent.VALUE.ANY) {
@Override
protected void doConfig() {
setScopeStart(0x0FB63);
}
@Override
protected void doEvent(RAMEvent e) {
if (getCpu().getProgramCounter() != getScopeStart()) {
return;
}
if (twinkleEffect == null || !twinkleEffect.isAlive()) {
twinkleEffect = new Thread(() -> {
try {
// Give the floppy drive time to start
Thread.sleep(1000);
ScheduledExecutorService animationTimer = new ScheduledThreadPoolExecutor(1);
Runnable drawHints = () -> {
if (getCpu().getProgramCounter() >> 8 != 0x0c6) {
return;
}
int row = 2;
for (String s : new String[]{
" Welcome to",
@ -416,27 +401,45 @@ public class Apple2e extends Computer {
getMemory().write(addr++, (byte) (c | 0x080), false, true);
}
}
while (getCpu().getProgramCounter() >> 8 == 0x0c6) {
int x = (int) (Math.random() * 26.0) + 7;
int y = (int) (Math.random() * 4.0) + 3;
int addr = 0x0400 + VideoDHGR.calculateTextOffset(y) + x;
byte old = getMemory().readRaw(addr);
for (char c : "+xX*+".toCharArray()) {
if (getCpu().getProgramCounter() >> 8 != 0x0c6) {
break;
};
int animAddr, animCycleNumber;
byte animOldValue;
final String animation = "+xX*+-";
ScheduledFuture animationSchedule;
Runnable doAnimation = () -> {
if (animAddr == 0 || animCycleNumber >= animation.length()) {
if (animAddr > 0) {
getMemory().write(animAddr, animOldValue, true, true);
}
getMemory().write(addr, (byte) (c | 0x080), true, true);
Thread.sleep(100);
int animX = (int) (Math.random() * 24.0) + 7;
int animY = (int) (Math.random() * 3.0) + 3;
animAddr = 0x0400 + VideoDHGR.calculateTextOffset(animY) + animX;
animOldValue = getMemory().readRaw(animAddr);
animCycleNumber = 0;
}
getMemory().write(addr, old, true, true);
if (getCpu().getProgramCounter() >> 8 == 0x0c6) {
getMemory().write(animAddr, (byte) (animation.charAt(animCycleNumber) | 0x080), true, true);
animCycleNumber++;
} else {
getMemory().write(animAddr, animOldValue, true, true);
animationSchedule.cancel(false);
animAddr = 0;
}
} catch (InterruptedException ex) {
Logger.getLogger(Apple2e.class.getName()).log(Level.SEVERE, null, ex);
}
});
twinkleEffect.setName("Startup Animation");
twinkleEffect.start();
};
private void enableHints() {
if (hints.isEmpty()) {
hints.add(new RAMListener(RAMEvent.TYPE.EXECUTE, RAMEvent.SCOPE.ADDRESS, RAMEvent.VALUE.ANY) {
@Override
protected void doConfig() {
setScopeStart(0x0FB63);
}
@Override
protected void doEvent(RAMEvent e) {
animationTimer.schedule(drawHints, 1, TimeUnit.SECONDS);
animationSchedule =
animationTimer.scheduleAtFixedRate(doAnimation, 1250, 100, TimeUnit.MILLISECONDS);
}
});
// Latch to the PRODOS SYNTAX CHECK parser