Class CanvasPainter

java.lang.Object
org.inspirenxe.strata.core.paint.CanvasPainter
All Implemented Interfaces:
org.inspirenxe.strata.api.extension.Painter

public final class CanvasPainter extends Object implements org.inspirenxe.strata.api.extension.Painter
The Painter that components draw with, passing each call to the engine's Canvas. The scoped clip and translation calls pop what they pushed even if the drawing code throws, so the canvas stacks stay balanced. Each host reuses one painter for every frame.

Sized nine-slices and tiled sprites go to the canvas as they are when the engine slices sprites itself. Otherwise the painter draws them from pieces, each one Canvas.spriteRegion(SpriteId, Rect, float, float, float, float, int) call for the part of the image it shows. A row or column holds at most 1024 copies, and a tiled sprite or tiled part at most 16384 in all; past either, the sprite or part is stretched instead.

Drawing a sized nine-slice allocates only the rectangle of each of its nine parts, and drawing tiles only the rectangle of each copy, since the canvas takes rectangles; the slice edges are kept in arrays the painter reuses.

Shapes go to the canvas when the engine draws shapes. Otherwise the painter draws them from fills one device pixel high, gradients at an angle from columns of vertical gradients, shadows from rings of falling alpha, and round clips as rectangular clips, and never calls the canvas's shape methods; smoothed(boolean, Runnable) then only runs its drawing. Either way the painter draws nothing for a color with alpha 0, an empty rectangle, a stroke of width 0, an empty shape or fewer than two points, limits each radius to half the shorter side of its rectangle (a negative radius counts as 0), fills a rounded rectangle of radius 0 with Canvas.fill(Rect, int), draws an arc of a full turn or more as a whole ellipse, and a shadow without blur as a rounded rectangle. pixels(PixelBuffer, Rect, int) always goes to the canvas.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a painter with no canvas and no text metrics.
    CanvasPainter(org.inspirenxe.strata.api.text.TextMetrics metrics)
    Creates a painter with no canvas that measures inline sprites that fit the line with the given metrics.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    backdrop(org.inspirenxe.strata.api.geometry.Rect rect, float radius, float blur, int tint)
     
    begin(org.inspirenxe.strata.spi.Canvas canvas)
    Points the painter at the canvas for this frame, for an engine that stores no slicing with its images.
    begin(org.inspirenxe.strata.spi.Canvas canvas, boolean slicesSprites)
    Points the painter at the canvas for this frame.
    begin(org.inspirenxe.strata.spi.Canvas canvas, org.inspirenxe.strata.spi.Capabilities capabilities, float pixelsPerUnit)
    Points the painter at the canvas for this frame, with what the engine does for itself and the host's density.
    org.inspirenxe.strata.spi.Canvas
    Returns the canvas set by the last begin(Canvas).
    void
    clipped(org.inspirenxe.strata.api.geometry.Rect rect, Runnable draw)
     
    void
    clippedRound(org.inspirenxe.strata.api.geometry.Rect rect, float radius, Runnable draw)
     
    void
    fill(org.inspirenxe.strata.api.geometry.Rect rect, int argb)
     
    void
    fillEllipse(org.inspirenxe.strata.api.geometry.Rect oval, int argb)
     
    void
    fillGradient(org.inspirenxe.strata.api.geometry.Rect rect, int topArgb, int bottomArgb)
     
    void
    fillGradient(org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.extension.Gradient gradient)
     
    void
    fillRoundRect(org.inspirenxe.strata.api.geometry.Rect rect, float radius, int argb)
     
    void
    fillShape(org.inspirenxe.strata.api.extension.Shape shape, int argb)
     
    void
    line(float x1, float y1, float x2, float y2, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    nineSlice(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Insets border, int tint)
     
    void
    nineSlice(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Size size, org.inspirenxe.strata.api.geometry.Insets border, boolean tiled, int tint)
     
    void
    pixels(org.inspirenxe.strata.api.visual.PixelBuffer buffer, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
     
    float
    Returns the density given to begin(Canvas, Capabilities, float), 1 after the other forms of begin, times every scale in progress.
    void
    polyline(float[] points, int count, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    rightToLeftText(org.inspirenxe.strata.api.text.TextSpan logical, org.inspirenxe.strata.api.text.TextSpan reversed, float x, float y)
    Draws a run the bidirectional algorithm put right to left: in logical order with its direction to an engine that shapes right-to-left text, or else the reversed span as plain text.
    void
    sampled(org.inspirenxe.strata.api.visual.Sampling sampling, Runnable draw)
     
    void
    scaled(float scale, Runnable draw)
     
    void
    shadow(org.inspirenxe.strata.api.geometry.Rect rect, float radius, float blur, int argb)
     
    void
    smoothed(boolean smooth, Runnable draw)
     
    void
    sprite(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
     
    void
    strokeArc(org.inspirenxe.strata.api.geometry.Rect oval, float startRadians, float sweepRadians, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    strokeEllipse(org.inspirenxe.strata.api.geometry.Rect oval, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    strokeRect(org.inspirenxe.strata.api.geometry.Rect rect, float radius, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    strokeShape(org.inspirenxe.strata.api.extension.Shape shape, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
     
    void
    text(org.inspirenxe.strata.api.text.TextSpan span, float x, float y)
     
    void
    text(org.inspirenxe.strata.api.text.TextSpan span, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.layout.HorizontalAlignment horizontal, org.inspirenxe.strata.api.layout.VerticalAlignment vertical)
     
    void
    tiled(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Size size, int tint)
     
    void
    translated(float dx, float dy, Runnable draw)
     
    void
    visual(org.inspirenxe.strata.api.visual.Visual visual, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CanvasPainter

      public CanvasPainter()
      Creates a painter with no canvas and no text metrics. Call begin(Canvas) before drawing. It draws inline sprites that fit the line nowhere, since it cannot measure the line.
    • CanvasPainter

      public CanvasPainter(org.inspirenxe.strata.api.text.TextMetrics metrics)
      Creates a painter with no canvas that measures inline sprites that fit the line with the given metrics. Call begin(Canvas) before drawing.
      Parameters:
      metrics - the host's text metrics
  • Method Details

    • begin

      public CanvasPainter begin(org.inspirenxe.strata.spi.Canvas canvas)
      Points the painter at the canvas for this frame, for an engine that stores no slicing with its images.
      Parameters:
      canvas - the frame's canvas
      Returns:
      this painter
    • begin

      public CanvasPainter begin(org.inspirenxe.strata.spi.Canvas canvas, boolean slicesSprites)
      Points the painter at the canvas for this frame.
      Parameters:
      canvas - the frame's canvas
      slicesSprites - true when the engine applies slicing stored with its images, so sized nine-slices and tiled sprites go to the canvas as they are
      Returns:
      this painter
    • begin

      public CanvasPainter begin(org.inspirenxe.strata.spi.Canvas canvas, org.inspirenxe.strata.spi.Capabilities capabilities, float pixelsPerUnit)
      Points the painter at the canvas for this frame, with what the engine does for itself and the host's density.
      Parameters:
      canvas - the frame's canvas
      capabilities - what the engine does for itself; the painter reads whether it slices sprites and draws shapes
      pixelsPerUnit - device pixels per UI unit, greater than 0
      Returns:
      this painter
    • canvas

      public org.inspirenxe.strata.spi.Canvas canvas()
      Returns the canvas set by the last begin(Canvas).
      Returns:
      the canvas, or null before the first frame
    • fill

      public void fill(org.inspirenxe.strata.api.geometry.Rect rect, int argb)
      Specified by:
      fill in interface org.inspirenxe.strata.api.extension.Painter
    • fillGradient

      public void fillGradient(org.inspirenxe.strata.api.geometry.Rect rect, int topArgb, int bottomArgb)
      Specified by:
      fillGradient in interface org.inspirenxe.strata.api.extension.Painter
    • fillGradient

      public void fillGradient(org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.extension.Gradient gradient)
      Specified by:
      fillGradient in interface org.inspirenxe.strata.api.extension.Painter
    • line

      public void line(float x1, float y1, float x2, float y2, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      line in interface org.inspirenxe.strata.api.extension.Painter
    • polyline

      public void polyline(float[] points, int count, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      polyline in interface org.inspirenxe.strata.api.extension.Painter
    • backdrop

      public void backdrop(org.inspirenxe.strata.api.geometry.Rect rect, float radius, float blur, int tint)
      Specified by:
      backdrop in interface org.inspirenxe.strata.api.extension.Painter
    • fillRoundRect

      public void fillRoundRect(org.inspirenxe.strata.api.geometry.Rect rect, float radius, int argb)
      Specified by:
      fillRoundRect in interface org.inspirenxe.strata.api.extension.Painter
    • strokeRect

      public void strokeRect(org.inspirenxe.strata.api.geometry.Rect rect, float radius, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      strokeRect in interface org.inspirenxe.strata.api.extension.Painter
    • fillEllipse

      public void fillEllipse(org.inspirenxe.strata.api.geometry.Rect oval, int argb)
      Specified by:
      fillEllipse in interface org.inspirenxe.strata.api.extension.Painter
    • strokeEllipse

      public void strokeEllipse(org.inspirenxe.strata.api.geometry.Rect oval, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      strokeEllipse in interface org.inspirenxe.strata.api.extension.Painter
    • strokeArc

      public void strokeArc(org.inspirenxe.strata.api.geometry.Rect oval, float startRadians, float sweepRadians, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      strokeArc in interface org.inspirenxe.strata.api.extension.Painter
    • fillShape

      public void fillShape(org.inspirenxe.strata.api.extension.Shape shape, int argb)
      Specified by:
      fillShape in interface org.inspirenxe.strata.api.extension.Painter
    • strokeShape

      public void strokeShape(org.inspirenxe.strata.api.extension.Shape shape, org.inspirenxe.strata.api.extension.Stroke stroke, int argb)
      Specified by:
      strokeShape in interface org.inspirenxe.strata.api.extension.Painter
    • shadow

      public void shadow(org.inspirenxe.strata.api.geometry.Rect rect, float radius, float blur, int argb)
      Specified by:
      shadow in interface org.inspirenxe.strata.api.extension.Painter
    • sampled

      public void sampled(org.inspirenxe.strata.api.visual.Sampling sampling, Runnable draw)
      Specified by:
      sampled in interface org.inspirenxe.strata.api.extension.Painter
    • smoothed

      public void smoothed(boolean smooth, Runnable draw)
      Specified by:
      smoothed in interface org.inspirenxe.strata.api.extension.Painter
    • clippedRound

      public void clippedRound(org.inspirenxe.strata.api.geometry.Rect rect, float radius, Runnable draw)
      Specified by:
      clippedRound in interface org.inspirenxe.strata.api.extension.Painter
    • pixels

      public void pixels(org.inspirenxe.strata.api.visual.PixelBuffer buffer, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
      Specified by:
      pixels in interface org.inspirenxe.strata.api.extension.Painter
    • pixelsPerUnit

      public float pixelsPerUnit()
      Returns the density given to begin(Canvas, Capabilities, float), 1 after the other forms of begin, times every scale in progress.
      Specified by:
      pixelsPerUnit in interface org.inspirenxe.strata.api.extension.Painter
    • sprite

      public void sprite(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
      Specified by:
      sprite in interface org.inspirenxe.strata.api.extension.Painter
    • nineSlice

      public void nineSlice(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Insets border, int tint)
      Specified by:
      nineSlice in interface org.inspirenxe.strata.api.extension.Painter
    • nineSlice

      public void nineSlice(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Size size, org.inspirenxe.strata.api.geometry.Insets border, boolean tiled, int tint)
      Specified by:
      nineSlice in interface org.inspirenxe.strata.api.extension.Painter
    • tiled

      public void tiled(org.inspirenxe.strata.api.theme.SpriteId sprite, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.geometry.Size size, int tint)
      Specified by:
      tiled in interface org.inspirenxe.strata.api.extension.Painter
    • rightToLeftText

      public void rightToLeftText(org.inspirenxe.strata.api.text.TextSpan logical, org.inspirenxe.strata.api.text.TextSpan reversed, float x, float y)
      Draws a run the bidirectional algorithm put right to left: in logical order with its direction to an engine that shapes right-to-left text, or else the reversed span as plain text.
      Parameters:
      logical - the run in logical order
      reversed - the run with its characters reversed and its brackets mirrored
      x - the run's left edge
      y - the top of its line box
    • text

      public void text(org.inspirenxe.strata.api.text.TextSpan span, org.inspirenxe.strata.api.geometry.Rect rect, org.inspirenxe.strata.api.layout.HorizontalAlignment horizontal, org.inspirenxe.strata.api.layout.VerticalAlignment vertical)
      Specified by:
      text in interface org.inspirenxe.strata.api.extension.Painter
    • text

      public void text(org.inspirenxe.strata.api.text.TextSpan span, float x, float y)
      Specified by:
      text in interface org.inspirenxe.strata.api.extension.Painter
    • clipped

      public void clipped(org.inspirenxe.strata.api.geometry.Rect rect, Runnable draw)
      Specified by:
      clipped in interface org.inspirenxe.strata.api.extension.Painter
    • translated

      public void translated(float dx, float dy, Runnable draw)
      Specified by:
      translated in interface org.inspirenxe.strata.api.extension.Painter
    • scaled

      public void scaled(float scale, Runnable draw)
      Specified by:
      scaled in interface org.inspirenxe.strata.api.extension.Painter
    • visual

      public void visual(org.inspirenxe.strata.api.visual.Visual visual, org.inspirenxe.strata.api.geometry.Rect rect, int tint)
      Specified by:
      visual in interface org.inspirenxe.strata.api.extension.Painter