class RhythmGroupBinItem extends RhythmGroupItem implements MapModel { MapLayout algorithm = new SquarifiedLayout();//new PivotBySplitSize(); Mappable[] items; boolean contentsVisible; boolean layoutValid; //inherit a size, order, and id from RhythmGroupItem RhythmGroupBinItem(RhythmGroupBinItem parent, String id, int order, float depth, String[] childrenIDs) { //all rhythm group bins are also "rhythm groups" super(parent, id, order, depth); items = new Mappable[0]; addChildren(childrenIDs); } void addChild(RhythmGroupItem item) { items = (Mappable[])expand(items, items.length + 1); items[items.length - 1] = item; } void addChild(RhythmGroupBinItem item) { items = (Mappable[])expand(items, items.length + 1); items[items.length - 1] = item; } void addChildren(String[] childrenIDs) { //now pull out the children ids, create rhythmgroupitems for them and add them to the bin if (childrenIDs != null) { childrenIDs = sort(childrenIDs); int oldLength = items.length; items = (Mappable[])expand(items, oldLength + childrenIDs.length); //println("Length of the children list: " + childrenIDs.length); //println("Length of the items list: " + items.length); //items = new Mappable[childrenIDs.length]; //loop over all the children, adding them to the bin for (int i = 0; i < childrenIDs.length; i++) { RhythmGroupItem rg = new RhythmGroupItem(this, childrenIDs[i], oldLength + i, depth+1); items[oldLength + i] = rg; } } } void draw() { checkLayout(); calcBox(); if (this != rootItem) super.draw(); if (contentsVisible) { for (int i = 0; i < items.length; i++) { items[i].draw(); } } //else { //} } ArrayList getBinsAtDepth(float d) { ArrayList retList = new ArrayList(); if (depth > d || floatEquals(depth, d)) { retList.add(this); for (int i = 0; i < getItemCount(); i++) { if (items[i] instanceof RhythmGroupItem) { retList.addAll(((RhythmGroupItem)items[i]).getBinsAtDepth(d)); } } } return retList; } double calculateSizes() { //the size of a node is the sum of its children's sizes size = 0; for (int i = 0; i < getItemCount(); i++) { if (items[i] instanceof RhythmGroupBinItem) { size += ((RhythmGroupBinItem)items[i]).calculateSizes(); } else if (items[i] instanceof RhythmGroupItem) { size += ((RhythmGroupItem)items[i]).getSize(); } } return size; } ArrayList getContainedRhythmGroups() { //a bin contains all the rhythm groups below it ArrayList retList = new ArrayList(); for (int i = 0; i < getItemCount(); i++) { if (items[i] instanceof RhythmGroupBinItem) { retList.addAll(((RhythmGroupBinItem)items[i]).getContainedRhythmGroups()); } else if (items[i] instanceof RhythmGroupItem) { retList.addAll(((RhythmGroupItem)items[i]).getContainedRhythmGroups()); } } return retList; } boolean mousePressed(boolean expandTree) { //only expand if the 'shift' key is held down too if (mouseInside()) { if (contentsVisible) { for (int i = 0; i < items.length; i++) { ((RhythmGroupItem)items[i]).mousePressed(expandTree); } } else { if (mouseButton == LEFT) { if (expandTree) { contentsVisible = true; } selectedItem = this; selectedItemRGs = this.getContainedRhythmGroups(); Collections.shuffle(selectedItemRGs); statusTranslateY = 0; } else if (mouseButton == RIGHT && parent != null) { if (expandTree) { parent.hideContents(); } selectedItem = null; selectedItemRGs = null; statusTranslateY = 0; } } } return true; } void propagateColorToChildren(float _hue) { for (int i = 0; i < getItemCount(); i++) { if (items[i] instanceof RhythmGroupBinItem) { ((RhythmGroupBinItem)items[i]).hue = _hue; ((RhythmGroupBinItem)items[i]).sat = random(60, 70); ((RhythmGroupBinItem)items[i]).bri = random(60, 70); ((RhythmGroupBinItem)items[i]).propagateColorToChildren(_hue); } else if (items[i] instanceof RhythmGroupItem) { ((RhythmGroupItem)items[i]).hue = _hue; ((RhythmGroupItem)items[i]).sat = random(80, 85); ((RhythmGroupItem)items[i]).bri = random(80, 85); } } } void checkLayout() { if (!layoutValid) { if (getItemCount() != 0) { algorithm.layout(this, bounds); } layoutValid = true; } } ArrayList getSiblings() { ArrayList retList = new ArrayList(); if (parent != null) { for (int i = 0; i < parent.getItemCount(); i++) { RhythmGroupItem potentialSibling = (RhythmGroupItem)parent.items[i]; if (potentialSibling != this) { retList.add(potentialSibling); println("Found a sibling! ID: " + potentialSibling.id); } } } return retList; } Mappable[] getItems() { return items; } int getItemCount() { return items.length; } void showContents() { contentsVisible = true; } void hideContents() { if (parent != null) { contentsVisible = false; } } /* void updateColors() { if (parent != null) { if (parent == rootItem) { hue = map(order, 0, rootItem.getItemCount(), 0, 360); } else { hue = parent.hue; } } for (int i = 0; i < getItemCount(); i++) { if (items[i] instanceof RhythmGroupBinItem) { ((RhythmGroupBinItem)items[i]).updateColors(); } else if (items[i] instanceof RhythmGroupItem) { ((RhythmGroupItem)items[i]).hue = hue; } } } */ }