Class UIPainter2D

java.lang.Object
net.risingworld.api.ui.UIElement
net.risingworld.api.ui.UIPainter2D

public class UIPainter2D extends UIElement
A simplified version of the UIMesh element. It enables you to create custom shapes and elements, but instead of providing individual vertices, this class uses a Vector API to creates lines and shapes more easily.

All methods record drawing commands which are executed in the same order on the client. Style settings (colors, line width etc.) apply to all subsequent fill(FillRule) and stroke() calls. Positions are in pixels and relative to the upper left corner of the element (x pointing right, y pointing down). The commands are only sent to the clients once you call update() (or when the element is added to a player), every update transmits all commands.

Please note that most style properties have no impact on the element!

Another note: Unity internally has a hard-coded mesh vertex limit of 65535 for UI elements. If the amount of vertices exceed this value, the UI element can no longer be rendered. Unfortunately there is no workaround available yet, so if your painter element becomes too complex, it could be necessary to split it into multiple instances (or use a UIMesh, which is not limited).
See Also:
  • Constructor Details

    • UIPainter2D

      public UIPainter2D()
  • Method Details

    • arc

      public void arc(Vector2f center, float radius, int startAngle, int endAngle, UIPainter2D.ArcDirection direction)
      Adds an arc to the current sub-path.
      Parameters:
      center - the center of the arc (in pixels).
      radius - the radius of the arc (in pixels).
      startAngle - the start angle of the arc (in degrees).
      endAngle - the end angle of the arc (in degrees).
      direction - the direction in which the arc is drawn.
    • arc

      public void arc(Vector2f center, float radius, float startAngle, float endAngle, UIPainter2D.ArcDirection direction)
      Adds an arc to the current sub-path.
      Parameters:
      center - the center of the arc (in pixels).
      radius - the radius of the arc (in pixels).
      startAngle - the start angle of the arc (in degrees).
      endAngle - the end angle of the arc (in degrees).
      direction - the direction in which the arc is drawn.
    • arcTo

      public void arcTo(Vector2f p1, Vector2f p2, float radius)
      Adds a circular arc to the current sub-path, using the current position, two control points and a radius (connected to the current position by a straight line if necessary).
      Parameters:
      p1 - the first control point (in pixels).
      p2 - the second control point (in pixels).
      radius - the radius of the arc (in pixels).
    • bezierCurveTo

      public void bezierCurveTo(Vector2f p1, Vector2f p2, Vector2f p3)
      Adds a cubic bezier curve from the current position to the end point.
      Parameters:
      p1 - the first control point (in pixels).
      p2 - the second control point (in pixels).
      p3 - the end point of the curve (in pixels).
    • beginPath

      public void beginPath()
      Begins a new path, i.e. discards all previous sub-paths (already filled or stroked shapes remain visible).
    • closePath

      public void closePath()
      Closes the current sub-path with a straight line to its start point.
    • fill

      public void fill(UIPainter2D.FillRule fillRule)
      Fills the current path with the current fill color (see setFillColor(float, float, float, float)).
      Parameters:
      fillRule - the rule which determines whether a point is inside the shape (relevant for overlapping or self-intersecting paths).
    • lineTo

      public void lineTo(Vector2f pos)
      Adds a straight line from the current position to the provided position.
      Parameters:
      pos - the end point of the line (in pixels).
    • moveTo

      public void moveTo(Vector2f pos)
      Begins a new sub-path at the provided position (without drawing a line).
      Parameters:
      pos - the new position (in pixels).
    • quadraticCurveTo

      public void quadraticCurveTo(Vector2f p1, Vector2f p2)
      Adds a quadratic bezier curve from the current position to the end point.
      Parameters:
      p1 - the control point (in pixels).
      p2 - the end point of the curve (in pixels).
    • stroke

      public void stroke()
      Draws the outline of the current path with the current stroke settings (color, line width, line cap and line join).
    • setFillColor

      public void setFillColor(float r, float g, float b, float a)
      Sets the color for subsequent fill(FillRule) calls.
      Parameters:
      r - the red component (0-1).
      g - the green component (0-1).
      b - the blue component (0-1).
      a - the alpha component (0-1).
    • setLineCap

      public void setLineCap(UIPainter2D.LineCap lineCap)
      Sets the shape of the line ends for subsequent stroke() calls.
      Parameters:
      lineCap - the line cap.
    • setLineJoin

      public void setLineJoin(UIPainter2D.LineJoin lineJoin)
      Sets the shape of the corners between connected line segments for subsequent stroke() calls.
      Parameters:
      lineJoin - the line join.
    • setLineWidth

      public void setLineWidth(float lineWidth)
      Sets the line width for subsequent stroke() calls.
      Parameters:
      lineWidth - the line width (in pixels).
    • setMiterLimit

      public void setMiterLimit(float miterLimit)
      Sets the miter limit for subsequent stroke() calls. If the miter length of a corner exceeds this limit (relative to the line width), the corner is beveled instead (only relevant for UIPainter2D.LineJoin.Miter).
      Parameters:
      miterLimit - the miter limit.
    • setStrokeColor

      public void setStrokeColor(float r, float g, float b, float a)
      Sets the color for subsequent stroke() calls.
      Parameters:
      r - the red component (0-1).
      g - the green component (0-1).
      b - the blue component (0-1).
      a - the alpha component (0-1).
    • clear

      public void clear()
      Removes all recorded commands. Call update() afterwards to apply the changes.
    • update

      public void update()
      Sends all recorded commands to all players who have this element. Call this method once after you have finished drawing.