+/**
+ * UI for control of mapping.
+ */
+class MappingUI extends OverlayUI {
+
+ private MappingTool mappingTool;
+
+ private final String MAPPING_MODE_ALL = "All On";
+ private final String MAPPING_MODE_SINGLE_CUBE = "Single Cube";
+ private final String[] mappingModes = {
+ MAPPING_MODE_ALL,
+ MAPPING_MODE_SINGLE_CUBE,
+ };
+ private final Method mappingModeStateMethod;
+
+ private final String CUBE_MODE_ALL = "All Strips";
+ private final String CUBE_MODE_SINGLE_STRIP = "Single Strip";
+ private final String CUBE_MODE_STRIP_PATTERN = "Strip Pattern";
+ private final String[] cubeModes = {
+ CUBE_MODE_ALL,
+ CUBE_MODE_SINGLE_STRIP,
+ CUBE_MODE_STRIP_PATTERN
+ };
+ private final Method cubeModeStateMethod;
+
+ private final String CHANNEL_MODE_RED = "Red";
+ private final String CHANNEL_MODE_GREEN = "Green";
+ private final String CHANNEL_MODE_BLUE = "Blue";
+ private final String[] channelModes = {
+ CHANNEL_MODE_RED,
+ CHANNEL_MODE_GREEN,
+ CHANNEL_MODE_BLUE,
+ };
+ private final Method channelModeStateMethod;
+
+ private int firstMappingY;
+ private int firstCubeY;
+ private int firstChannelY;
+ private int cubeFieldY;
+ private int stripFieldY;
+
+ private boolean dragCube;
+ private boolean dragStrip;
+
+ MappingUI(MappingTool mappingTool) {
+ this.mappingTool = mappingTool;
+ try {
+ mappingModeStateMethod = getClass().getMethod("getMappingState", Object.class);
+ channelModeStateMethod = getClass().getMethod("getChannelState", Object.class);
+ cubeModeStateMethod = getClass().getMethod("getCubeState", Object.class);
+ } catch (Exception x) {
+ throw new RuntimeException(x);
+ }
+ }
+
+ public int getMappingState(Object mappingMode) {
+ boolean active = (mappingMode == MAPPING_MODE_SINGLE_CUBE) == mappingTool.mappingModeSingleCube;
+ return active ? STATE_ACTIVE : STATE_DEFAULT;
+ }
+
+ public int getChannelState(Object channelMode) {
+ boolean active = false;
+ if (channelMode == CHANNEL_MODE_RED) {
+ active = mappingTool.channelModeRed;
+ } else if (channelMode == CHANNEL_MODE_GREEN) {
+ active = mappingTool.channelModeGreen;
+ } else if (channelMode == CHANNEL_MODE_BLUE) {
+ active = mappingTool.channelModeBlue;
+ }
+ return active ? STATE_ACTIVE : STATE_DEFAULT;
+ }
+
+ public int getCubeState(Object cubeMode) {
+ boolean active = false;
+ if (cubeMode == CUBE_MODE_ALL) {
+ active = mappingTool.cubeMode == mappingTool.CUBE_MODE_ALL;
+ } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
+ active = mappingTool.cubeMode == mappingTool.CUBE_MODE_SINGLE_STRIP;
+ } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
+ active = mappingTool.cubeMode == mappingTool.CUBE_MODE_STRIP_PATTERN;
+ }
+ return active ? STATE_ACTIVE : STATE_DEFAULT;
+ }
+
+ public void draw() {
+ drawLogoAndBackground();
+ int yPos = 0;
+ firstMappingY = yPos + lineHeight + 6;
+ yPos = drawObjectList(yPos, "MAPPING MODE", mappingModes, mappingModes, mappingModeStateMethod);
+ yPos += sectionSpacing;
+
+ firstCubeY = yPos + lineHeight + 6;
+ yPos = drawObjectList(yPos, "CUBE MODE", cubeModes, cubeModes, cubeModeStateMethod);
+ yPos += sectionSpacing;
+
+ firstChannelY = yPos + lineHeight + 6;
+ yPos = drawObjectList(yPos, "CHANNELS", channelModes, channelModes, channelModeStateMethod);
+ yPos += sectionSpacing;
+
+ cubeFieldY = yPos + lineHeight + 6;
+ yPos = drawValueField(yPos, "CUBE ID", glucose.model.getRawIndexForCube(mappingTool.cubeIndex));
+ yPos += sectionSpacing;
+
+ stripFieldY = yPos + lineHeight + 6;
+ yPos = drawValueField(yPos, "STRIP ID", mappingTool.stripIndex + 1);
+
+ drawToggleTip("Tap 'm' to return");
+ }
+
+ private int drawValueField(int yPos, String label, int value) {
+ yPos += lineHeight;
+ textAlign(LEFT);
+ textFont(titleFont);
+ fill(titleColor);
+ text(label, leftTextPos, yPos);
+ fill(0);
+ yPos += 6;
+ rect(leftTextPos, yPos, w-8, lineHeight);
+ yPos += lineHeight;
+
+ fill(#999999);
+ textAlign(CENTER);
+ textFont(itemFont);
+ text("" + value, leftTextPos + (w-8)/2, yPos - 5);
+
+ return yPos;
+ }
+
+ private int lastY;
+
+ public void mousePressed() {
+ dragCube = dragStrip = false;
+ lastY = mouseY;
+ if (mouseY >= stripFieldY) {
+ if (mouseY < stripFieldY + lineHeight) {
+ dragStrip = true;
+ }
+ } else if (mouseY >= cubeFieldY) {
+ if (mouseY < cubeFieldY + lineHeight) {
+ dragCube = true;
+ }
+ } else if (mouseY >= firstChannelY) {
+ int index = objectClickIndex(firstChannelY);
+ switch (index) {
+ case 0: mappingTool.channelModeRed = !mappingTool.channelModeRed; break;
+ case 1: mappingTool.channelModeGreen = !mappingTool.channelModeGreen; break;
+ case 2: mappingTool.channelModeBlue = !mappingTool.channelModeBlue; break;
+ }
+ } else if (mouseY >= firstCubeY) {
+ int index = objectClickIndex(firstCubeY);
+ switch (index) {
+ case 0: mappingTool.cubeMode = mappingTool.CUBE_MODE_ALL; break;
+ case 1: mappingTool.cubeMode = mappingTool.CUBE_MODE_SINGLE_STRIP; break;
+ case 2: mappingTool.cubeMode = mappingTool.CUBE_MODE_STRIP_PATTERN; break;
+ }
+ } else if (mouseY >= firstMappingY) {
+ int index = objectClickIndex(firstMappingY);
+ if (index < 2) {
+ mappingTool.mappingModeSingleCube = (index > 0);
+ }
+ }
+ }
+
+ public void mouseReleased() {
+ }
+
+ public void mouseDragged() {
+ final int DRAG_THRESHOLD = 5;
+ int dy = lastY - mouseY;
+ if (abs(dy) >= DRAG_THRESHOLD) {
+ lastY = mouseY;
+ if (dragCube) {
+ if (dy < 0) {
+ mappingTool.decCube();
+ } else {
+ mappingTool.incCube();
+ }
+ } else if (dragStrip) {
+ if (dy < 0) {
+ mappingTool.decStrip();
+ } else {
+ mappingTool.incStrip();
+ }
+ }
+ }
+
+ }
+
+
+}
+