shader_type canvas_item;

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Colors.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Transform.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/SDF.gdshaderinc"

uniform vec2 size;
uniform float sharpness = 5;
uniform float borderRadius = 5;
uniform float strokeSize = 5;
uniform float offset;

uniform vec4 fillColor:source_color;
uniform sampler2D fill;
uniform vec4 fillUVTransform;
uniform sampler2D screenFillMultiply;
uniform vec4 screenfillMultiplyUVTransform;
uniform vec2 screenfillMultiplyUVMovement;


uniform vec4 strokeColor:source_color;
uniform sampler2D stroke;
uniform vec4 strokeUVTransform;
uniform sampler2D screenStrokeMultiply;
uniform vec4 screenStrokeMultiplyUVTransform;
uniform vec2 screenStrokeMultiplyUVMovment;

uniform float opacity:hint_range(0,1) = 1;

void vertex()
{
	// Called for every vertex the material is visible on.
}

void fragment()
{
  float maxRadius = min( size.x, size.y ) / 2.0 - offset;

  vec2 fillMinP;
  vec2 fillMaxP;
  float fillRadius;

  computeRectangleBounds( offset, borderRadius, maxRadius, size, fillMinP, fillMaxP, fillRadius );
  float fillMask = roundedRectangle( fillMinP, fillMaxP, UV * size, fillRadius, sharpness );
  vec2 fillUV = tilingOffsetRepeat( UV, fillUVTransform );
  vec2 fillMoved = screenfillMultiplyUVMovement * TIME;
  vec4 screenFillMovement = vec4( 0,0, fillMoved.x, fillMoved.y );
  vec2 screenFillMultiplyUV = tilingOffsetRepeat( SCREEN_UV, screenfillMultiplyUVTransform + screenFillMovement );
  vec4 combinedfillColor = fillColor * texture( fill, fillUV ) * texture( screenFillMultiply, screenFillMultiplyUV );


  vec2 strokeMinP;
  vec2 strokeMaxP;
  float strokeRadius;

  computeRectangleBounds( offset - strokeSize, borderRadius + strokeSize, maxRadius, size,strokeMinP, strokeMaxP, strokeRadius );
  float outerStrokeMask = roundedRectangle( strokeMinP, strokeMaxP, UV * size, strokeRadius, sharpness );
  float strokeMask = outerStrokeMask - fillMask;
  vec2 strokeUV = tilingOffsetRepeat( UV, strokeUVTransform );
  vec2 strokeMoved = screenStrokeMultiplyUVMovment * TIME;
  vec4 screenstrokeMovement = vec4( 0,0, strokeMoved.x, strokeMoved.y );
  vec2 screenStrokeMultiplyUV = tilingOffsetRepeat( SCREEN_UV, screenStrokeMultiplyUVTransform + screenstrokeMovement );
  vec4 combinedStrokeColor = strokeColor * texture( stroke, strokeUV ) * texture( screenStrokeMultiply, screenStrokeMultiplyUV );

  vec2 fillMaskMinP;
  vec2 fillMaskMaxP;
  float fillMaskStrokeRadius;

  computeRectangleBounds( offset - strokeSize * 0.5, borderRadius + strokeSize, maxRadius, size, fillMaskMinP, fillMaskMaxP, fillMaskStrokeRadius );
  float fillAlphaMask = roundedRectangle( fillMaskMinP, fillMaskMaxP, UV * size, fillMaskStrokeRadius, sharpness );


  vec4 strokeLayer = vec4( combinedStrokeColor.rgb, combinedStrokeColor.a );
  vec4 fillLayer = vec4( combinedfillColor.rgb, combinedfillColor.a * fillAlphaMask );

  vec4 strokePM = straightToPremultipliedColor( strokeLayer );
  vec4 fillPM   = straightToPremultipliedColor( fillLayer );

  vec4 outputColor = premultipliedToStraightColor( mix( fillPM, strokePM, strokeMask ) );

  float mask = max ( fillMask , strokeMask );
  mask = fillMask + strokeMask;

  COLOR = vec4( outputColor.rgb, outputColor.a * mask * opacity );

	// Called for every pixel the material is visible on.
}

//void light() {
	// Called for every pixel for every light affecting the CanvasItem.
	// Uncomment to replace the default light processing function with this one.
//}