/** 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; }
/** Starts a drawing session into this glyph's canvas. Call {@link #end} when drawing is done. * Valid after {@link #prepare}. */ public Canvas begin () { return _layer.begin().clear(); }
/** Completes a drawing sesion into this glyph's canvas and uploads the image data to GPU */ public void end () { _layer.end(); }
/** 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(); }
@Override public void init() { CanvasLayer clayer = new CanvasLayer(game.graphics, game.graphics.viewSize); game.rootLayer.add(clayer); conns.add(game.update.connect(clock -> { Canvas canvas = clayer.begin(); canvas.clear(); canvas.setStrokeWidth(3); canvas.setStrokeColor(0x88ff0000); noSegs += direction; if (noSegs > 50) direction = -1; if (noSegs < 20) direction = 1; float r = 100; for (int ii = 0; ii < noSegs; ii++) { float angle = 2*FloatMath.PI * ii / noSegs; IDimension viewSize = game.plat.graphics().viewSize; float x = (r * FloatMath.cos(angle)) + viewSize.width() / 2; float y = (r * FloatMath.sin(angle)) + viewSize.height() /2; canvas.strokeCircle(x, y, 100); } clayer.end(); // reupload the image data })); } }
protected void addAction(CanvasLayer actions, String action) { _actions.add(0, action); if (_actions.size() > 10) _actions.subList(10, _actions.size()).clear(); Canvas canvas = actions.begin(); canvas.clear(); StringBuilder buf = new StringBuilder(); for (String a : _actions) { if (buf.length() > 0) buf.append("\n"); buf.append(a); } canvas.setFillColor(0xFF000000); float y = 0; for (TextLayout layout : game.graphics.layoutText( buf.toString(), game.ui.TEXT_FMT, new TextWrap(300))) { canvas.fillText(layout, 0, y); y += layout.ascent() + layout.descent() + layout.leading(); } actions.end(); }
logger.layer.setTranslation(x, y); game.rootLayer.add(logger.layer); y += logger.layer.height() + 5; motionLabel.layer.setTranslation(x, y); game.rootLayer.add(motionLabel.layer);
public Label(float wid, float hei, TextFormat format) { layer = new CanvasLayer(game.graphics, wid, hei); this.format = format; }
/** * 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); }
/** * Prepares the canvas and renders the supplied text at {@code x, y} using the given config. */ public void renderText (Graphics gfx, StyledText.Plain text, int x, int y) { prepare(gfx, text.width(), text.height()); Canvas canvas = begin(); text.render(canvas, x, y); end(); _layer.setTranslation(text.style.effect.offsetX(), text.style.effect.offsetY()); }
/** Disposes the layer and image, removing them from the containing widget. */ @Override public void close () { if (_layer != null) { _layer.close(); _layer = null; } _preparedWidth = 0; _preparedHeight = 0; }
/** Informs this layer that a drawing operation has just completed. The backing canvas image data * is uploaded to the GPU. */ public void end () { Texture tex = (Texture)tile(); Image image = canvas.image; // if our texture is already the right size, just update it if (tex != null && tex.pixelWidth == image.pixelWidth() && tex.pixelHeight == image.pixelHeight()) tex.update(image); // otherwise we need to create a new texture (setTexture will unreference the old texture which // will cause it to be destroyed) else super.setTile(canvas.image.createTexture(Texture.Config.DEFAULT)); }
addTestLayer("ImageLayer repeat x/y", 100, 100, new ImageLayer(reptex).setSize(100, 100)); time = new CanvasLayer(game.graphics, 100, 100); addTestLayer("updated canvas", 100, 100, time); int curSecs = clock.tick/1000; if (curSecs != lastSecs) { Canvas tcanvas = time.begin(); tcanvas.clear(); tcanvas.setStrokeColor(0xFF000000).strokeRect(0, 0, 99, 99); tcanvas.drawText(""+curSecs, 40, 55); lastSecs = curSecs; time.end();
public void paint() { if (!dirty) { return; } Canvas canvas = layer.begin(); canvas.clear(); canvas.setFillColor(0xFF202020); layout = game.graphics.layoutText(text, format, TextWrap.MANUAL); float yy = 0; for (int line = 0; line < layout.length; line++) { canvas.fillText(layout[line], 0, yy); yy += layout[line].size.height(); } // if (yy > layer.height()) { // game.log.error("Clipped"); // } layer.end(); dirty = false; } }
public Hud (SceneGame game) { _game = game; layer = new CanvasLayer(game.plat.graphics(), 1, 1); }
@Override public void init() { CanvasLayer actions = new CanvasLayer(game.graphics, 300, 300); float x = 50, y = 50; Sound fanfare = loadSound("sounds/fanfare"); float ffx = addButton("Play Fanfare", x, y, () -> { fanfare.play(); addAction(actions, "Played Fanfare."); }); Sound lfanfare = loadSound("sounds/fanfare"); lfanfare.setLooping(true); addLoopButtons(actions, "Fanfare", lfanfare, ffx, y); y += 50; Sound bling = loadSound("sounds/bling"); bling.setLooping(true); addLoopButtons(actions, "Bling", bling, x, y); y += 50; Sound music = loadMusic("sounds/music"); music.setLooping(true); addLoopButtons(actions, "Music", music, x, y); y += 50; game.rootLayer.addAt(actions, x, y); }