/** * Creates a canvas layer with a backing canvas of size {@code width x height} (in display * units). This layer will display nothing until a {@link #begin}/{@link #end} pair is used to * render something to its backing canvas. */ public CanvasLayer (Graphics gfx, float width, float height) { this.gfx = gfx; resize(width, height); }
/** Ensures that the canvas image is at least the specified dimensions and cleared to all * transparent pixels. Also creates and adds the image layer to the parent layer if needed. */ public void prepare (Graphics gfx, float width, float height) { CanvasLayer layer = _layer; if (layer == null) { layer = new CanvasLayer(gfx, width, height); if (_depth != null) layer.setDepth(_depth); _parent.add(layer); _layer = layer; } else if (layer.width() < width || layer.height() < height) { // TODO: should we ever shrink it? layer.resize(width, height); } _preparedWidth = width; _preparedHeight = height; }
/** Updates the HUDs rendered image. Call this after all of its values have been updated * (usually once per second). */ public void update () { // update all of our rows and compute layout metrics float width = 0, height = 0, labelWidth = 0; for (Row row : _rows) { row.update(); width = Math.max(row.size().width(), width); labelWidth = Math.max(row.labelWidth(), labelWidth); height += row.size().height(); } // add in borders width += 5*GAP; height += GAP*_rows.size()+GAP; // create a new image if necessary if (layer.width() < width || layer.height() < height) layer.resize(width, height); // clear our image and render our rows Canvas canvas = layer.begin(); canvas.clear(); canvas.setFillColor(_bgColor).fillRect(0, 0, width, height); canvas.setStrokeColor(_textColor).setFillColor(_textColor); float x = GAP, y = GAP, valueX = labelWidth+2*GAP; for (Row row : _rows) { row.render(canvas, x, y, valueX); y += row.size().height()+GAP; } layer.end(); }