RenderTextureQuad.slice constructor Null safety

RenderTextureQuad.slice(
  1. RenderTextureQuad renderTextureQuad,
  2. Rectangle<int> sourceRectangle,
  3. Rectangle<int> offsetRectangle,
  4. [int rotation = 0]
)

Implementation

factory RenderTextureQuad.slice(RenderTextureQuad renderTextureQuad,
    Rectangle<int> sourceRectangle, Rectangle<int> offsetRectangle,
    [int rotation = 0]) {
  final renderTexture = renderTextureQuad.renderTexture;
  final pixelRatio = renderTextureQuad.pixelRatio;

  final oldRotation = renderTextureQuad.rotation;
  final oldSourceL = renderTextureQuad.sourceRectangle.left;
  final oldSourceT = renderTextureQuad.sourceRectangle.top;
  final oldSourceR = renderTextureQuad.sourceRectangle.right;
  final oldSourceB = renderTextureQuad.sourceRectangle.bottom;
  final oldOffsetL = renderTextureQuad.offsetRectangle.left;
  final oldOffsetT = renderTextureQuad.offsetRectangle.top;

  rotation = (renderTextureQuad.rotation + rotation) % 4;
  var sourceL = sourceRectangle.left;
  var sourceT = sourceRectangle.top;
  var sourceR = sourceRectangle.right;
  var sourceB = sourceRectangle.bottom;
  var offsetL = offsetRectangle.left;
  var offsetT = offsetRectangle.top;
  final offsetW = offsetRectangle.width;
  final offsetH = offsetRectangle.height;

  var tmpSourceL = 0;
  var tmpSourceT = 0;
  var tmpSourceR = 0;
  var tmpSourceB = 0;

  if (oldRotation == 0) {
    tmpSourceL = oldSourceL + oldOffsetL + sourceL;
    tmpSourceT = oldSourceT + oldOffsetT + sourceT;
    tmpSourceR = oldSourceL + oldOffsetL + sourceR;
    tmpSourceB = oldSourceT + oldOffsetT + sourceB;
  } else if (oldRotation == 1) {
    tmpSourceL = oldSourceR - oldOffsetT - sourceB;
    tmpSourceT = oldSourceT + oldOffsetL + sourceL;
    tmpSourceR = oldSourceR - oldOffsetT - sourceT;
    tmpSourceB = oldSourceT + oldOffsetL + sourceR;
  } else if (oldRotation == 2) {
    tmpSourceL = oldSourceR - oldOffsetL - sourceR;
    tmpSourceT = oldSourceB - oldOffsetT - sourceB;
    tmpSourceR = oldSourceR - oldOffsetL - sourceL;
    tmpSourceB = oldSourceB - oldOffsetT - sourceT;
  } else if (oldRotation == 3) {
    tmpSourceL = oldSourceL + oldOffsetT + sourceT;
    tmpSourceT = oldSourceB - oldOffsetL - sourceR;
    tmpSourceR = oldSourceL + oldOffsetT + sourceB;
    tmpSourceB = oldSourceB - oldOffsetL - sourceL;
  }

  sourceL = tmpSourceL.clamp(oldSourceL, oldSourceR);
  sourceT = tmpSourceT.clamp(oldSourceT, oldSourceB);
  sourceR = tmpSourceR.clamp(oldSourceL, oldSourceR);
  sourceB = tmpSourceB.clamp(oldSourceT, oldSourceB);

  if (rotation == 0) {
    offsetL += tmpSourceL - sourceL;
    offsetT += tmpSourceT - sourceT;
  } else if (rotation == 1) {
    offsetL += tmpSourceT - sourceT;
    offsetT += sourceR - tmpSourceR;
  } else if (rotation == 2) {
    offsetL += sourceR - tmpSourceR;
    offsetT += tmpSourceB - sourceB;
  } else if (rotation == 3) {
    offsetL += sourceB - tmpSourceB;
    offsetT += sourceL - tmpSourceL;
  }

  final sourceW = sourceR - sourceL;
  final sourceH = sourceB - sourceT;

  return RenderTextureQuad(
      renderTexture,
      Rectangle<int>(sourceL, sourceT, sourceW, sourceH),
      Rectangle<int>(offsetL, offsetT, offsetW, offsetH),
      rotation,
      pixelRatio);
}