euglena/src/ProjectorApplet.java

135 lines
3.3 KiB
Java
Raw Permalink Normal View History

2016-07-09 17:16:15 -07:00
import processing.core.PApplet;
2016-07-10 17:51:01 -07:00
import java.util.ArrayList;
2016-07-09 17:16:15 -07:00
class ProjectorApplet extends PApplet {
/*
Secondary applet for controlling what gets displayed on
the projector.
This window runs as a separate thread, so in order to draw
things, you should create a ProjectorCommand and queue it
up with projectorApplet.commandQueue.add(myCommand).
*/
// Which screen should we display on?
2016-07-09 17:16:15 -07:00
final int projectorScreenNumber = 2;
// Center coordinates of the screen
2016-07-09 17:16:15 -07:00
final static int centerX = 860;
final static int centerY = 540;
// Background color
2016-07-10 09:03:16 -07:00
int bgColor = color(0, 0, 0);
// Pointer to the main applet
2016-07-10 00:21:47 -07:00
EuglenaApplet parent;
// Calibration component
2016-07-10 00:21:47 -07:00
Calibrator calibrator;
// Queue of commands to execute
2016-07-10 17:51:01 -07:00
ArrayList<ProjectorCommand> commandQueue;
2016-07-10 06:24:02 -07:00
2016-07-10 00:21:47 -07:00
ProjectorApplet(EuglenaApplet parent) {
this.parent = parent;
calibrator = new Calibrator(this);
2016-07-10 17:51:01 -07:00
commandQueue = new ArrayList<>();
2016-07-10 00:21:47 -07:00
}
2016-07-09 17:16:15 -07:00
public void settings() {
fullScreen(P2D, this.projectorScreenNumber);
}
@Override
public void setup() {
clear();
// smooth(); // Smooth might create artifacts when animating?
2016-07-09 17:16:15 -07:00
}
@Override
public void draw() {
/*
Main draw loop for the projector window
*/
/*
Exectue commands from the commandQueue.
If a command finished, mark it for removal.
If we get a ProjectorCommandException, don't execute
any further commands.
*/
2016-07-10 17:51:01 -07:00
ArrayList<Integer> entriesToRemove = new ArrayList<>();
try {
for (int i = 0; i < commandQueue.size(); i++) {
boolean done = commandQueue.get(i).run(this);
if (done) {
entriesToRemove.add(i);
}
2016-07-10 06:24:02 -07:00
}
2016-07-10 17:51:01 -07:00
} catch (ProjectorCommandException e) {
// Do nothing
2016-07-10 06:24:02 -07:00
}
2016-07-10 17:51:01 -07:00
// Remove finished commands (have to do it in reverse order to preserve indices)
2016-07-10 17:51:01 -07:00
for (int i = entriesToRemove.size() - 1; i >= 0; i--) {
commandQueue.remove(i);
}
2016-07-10 18:33:27 -07:00
// Let the calibration module draw anything it needs
2016-07-10 18:33:27 -07:00
calibrator.draw(parent);
2016-07-10 17:51:01 -07:00
}
public void reset() {
/*
Clear the commandQueue and the screen
*/
2016-07-10 17:51:01 -07:00
commandQueue.clear();
this.clear();
2016-07-10 00:21:47 -07:00
}
2016-07-10 09:03:16 -07:00
public void clear() {
/*
Clear the screen
*/
2016-07-10 09:03:16 -07:00
fill(bgColor);
noStroke();
rectMode(CORNER);
rect(0, 0, width * 4, height * 4);
}
public void setBgColor(int c) {
bgColor = c;
}
public void setBgColor(int r, int b, int g) {
bgColor = color(r, g, b);
}
public int getBgColor() {
return bgColor;
}
/*
Functions for converting coordinates and distances from
main-window to projector-window coordinates
*/
2016-07-10 00:21:47 -07:00
public float convertXCoord(float x) {
return (x / calibrator.magx + width * calibrator.offsetx);
}
public float convertYCoord(float y) {
return (y / calibrator.magy + height * calibrator.offsety);
}
public float convertXDistance(float dx) {
return (dx / calibrator.magx);
}
public float convertYDistance(float dy) {
return (dy / calibrator.magy);
2016-07-09 17:16:15 -07:00
}
}