private void render(XML xml) { int depth = getDepth(xml); int[] widths = new int[depth]; int width = getWidths(xml, widths); g = p.createGraphics( (int) ((width + 1) * RECT_WIDTH * 1.5), (int) ((depth + 1) * RECT_HEIGHT * 1.5)); g.beginDraw(); g.background(128); renderTree(xml, widths); g.endDraw(); resetView(); dirtyTree = false; }
private void renderTreeHelper(XML xml, int[] widths, int depth, int[] ltor) { float x = (g.width / widths[depth]) * (0.5f + ltor[depth]++); float y = (g.height / widths.length) * (0.5f + depth); xml.setFloat("x", x); xml.setFloat("y", y); if (xml.getParent() != null) g.line( xml.getParent().getFloat("x"), xml.getParent().getFloat("y") + RECT_HEIGHT / 2, x, y - RECT_HEIGHT / 2); if (xml.hasAttribute("altColor")) { String hexColor = xml.getString("altColor"); g.fill( getColorComponent(hexColor, 0), getColorComponent(hexColor, 1), getColorComponent(hexColor, 2)); } g.rect(x, y, RECT_WIDTH, RECT_HEIGHT, 4); g.fill(0); if (xml.hasAttribute("altName")) g.text(xml.getString("altName"), x, y, RECT_WIDTH, RECT_HEIGHT); else g.text(xml.getName(), x, y, RECT_WIDTH, RECT_HEIGHT); g.fill(255); XML[] children = xml.getChildren(); for (int i = 0; i < children.length; i++) renderTreeHelper(children[i], widths, depth + 1, ltor); }
private void renderTree(XML xml, int[] widths) { g.rectMode(PApplet.CENTER); g.textAlign(PApplet.CENTER, PApplet.CENTER); renderTreeHelper(xml, widths, 0, new int[widths.length]); }