class DeweyL2 extends SimpleMapItem { DeweyL1 parent; String id; String name; int level; int country; int cIdx; boolean active = true; double oldSize; color c; float hue; float brightness; float textPadding = 15; float boxLeft, boxTop; float boxRight, boxBottom; DeweyL2(DeweyL1 parent, String id, String name, int level, int country, int cIdx) { this.parent = parent; this.id = id; this.name = name; this.level = level; this.country = country; this.cIdx = cIdx; } void updateColors() { if (parent != null) { hue = map(cIdx, 0, 9, 0, 330); // previously depended on "order" } brightness = 30+7*cIdx; // previously depended on modified date colorMode(HSB, 360, 100, 100); if (parent == countries[country].zoomItem) { c = color(hue, 40, 80); } else if (parent != null) { c = color(parent.hue, 40, brightness); } colorMode(RGB, 255); } void calcBox() { boxLeft = countries[country].zoomBounds.spanX(x, 0, rectW*2); boxRight = countries[country].zoomBounds.spanX(x+w, 0, rectW*2); boxTop = countries[country].zoomBounds.spanY(y, 0, rectH*2); boxBottom = countries[country].zoomBounds.spanY(y+h, 0, rectH*2); } void draw() { calcBox(); if(active) { fill(c); rect(boxLeft, boxTop, boxRight, boxBottom); if (textFits()) { drawTitle(); } else if (mouseInside()) { countries[country].rolloverItem = this; } } } void drawTitle() { // need to translate for foreground?! textFont(fontBold); pushMatrix(); translate(0,0,2); float middleX = (boxLeft + boxRight) / 2; float middleY = (boxTop + boxBottom) / 2; if (middleX > 0 && middleX < width && middleY > 0 && middleY < height) { if (boxLeft + textWidth(name) + textPadding*2 > width) { textAlign(RIGHT); fill(255); if(countries[country].rolloverItem == this) text(name, width-5, mouseY); else text(name, width - textPadding, boxBottom - textPadding); } else { textAlign(LEFT); fill(255); if(countries[country].rolloverItem == this) text(name, mouseX, mouseY); else text(name, boxLeft + textPadding, boxBottom - textPadding); } } popMatrix(); } boolean textFits() { float wide = textWidth(name) + textPadding*2; float high = textAscent() + textDescent() + textPadding*2; return (boxRight - boxLeft > wide) && (boxBottom - boxTop > high); } boolean mouseInside() { int fixW = width/2-rectW; int fixH = height/2+offsetH-rectH; return (mouseX-fixW > boxLeft && mouseX-fixW < boxRight && mouseY-fixH > boxTop && mouseY-fixH < boxBottom); } boolean mousePressed() { if (mouseInside()) { if (mouseButton == LEFT) { parent.zoomIn(); return true; } else if (mouseButton == RIGHT) { if (parent == countries[country].zoomItem) { parent.zoomOut(); } else { parent.hideContents(); } return true; } } return false; } }