Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: adding angle property
  • Loading branch information
JeremyMeissner committed May 6, 2022
commit 01e6e65619d22d1e7a3c2fda2a3a12fa35c9680b
1 change: 1 addition & 0 deletions FreeFrame/Components/Shapes/SVGCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public SVGCircle(XmlReader reader) : this(
public SVGCircle() : this(0, 0, 0) { }
public SVGCircle(int r, int cx, int cy)
{
IsCornerRadiusChangeable = false;
X = cx - r;
Y = cy - r;
Height = r*2;
Expand Down
2 changes: 2 additions & 0 deletions FreeFrame/Components/Shapes/SVGLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public SVGLine(XmlReader reader) : this(
public SVGLine() : this(DefaultX1, DefaultY1, DefaultX2, DefaultY2) { }
public SVGLine(int x1, int y1, int x2, int y2)
{
IsCornerRadiusChangeable = false;

X = x1;
Y = y1;
Width = x2 - X;
Expand Down
2 changes: 2 additions & 0 deletions FreeFrame/Components/Shapes/SVGPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class SVGPath : Shape
public SVGPath(XmlReader reader) //: this()
{
IsResizeable = false;
IsCornerRadiusChangeable = false;

string d = reader["d"] ?? throw new Exception("d not here"); // TODO: Error handler if d is note here
Match match;
int startIndex = 0;
Expand Down
9 changes: 2 additions & 7 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public class SVGRectangle : Shape
const int DefaultRY = 0;
#endregion

#region Geometry properties
private int _radius; // Rounded radius
public int Radius { get => _radius; set => _radius = value; }
#endregion

public SVGRectangle(XmlReader reader) : this(
Convert.ToInt32(reader["width"]),
Convert.ToInt32(reader["height"]),
Expand All @@ -41,7 +36,7 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
Y = y;
Width = width;
Height = height;
Radius = Math.Max(rx, ry);
CornerRadius = Math.Max(rx, ry);

ImplementObject();
}
Expand All @@ -56,7 +51,7 @@ public override void ImplementObject()
public override float[] GetVertices() => new float[] { X, Y, X + Width, Y, X + Width, Y + Height, X, Y + Height }; // x, y, x, y, x, y, ... (clockwise)
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode

public override string ToString() => $"x: {X}, y: {Y}, width: {Width}, height: {Height}, rx: {Radius}, ry: {Radius}";
public override string ToString() => $"x: {X}, y: {Y}, width: {Width}, height: {Height}, rx: {CornerRadius}, ry: {CornerRadius}";

public override List<Vector2i> GetSelectablePoints()
{
Expand Down
8 changes: 7 additions & 1 deletion FreeFrame/Components/Shapes/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public abstract class Shape
{
bool _isMoveable = true;
bool _isResizeable = true;
bool _isAngleChangeable = true;
bool _isCornerRadiusChangeable = true;
#region Common Geometry Properties
private int _x, _y, _width, _height;
private int _x, _y, _width, _height, _angle, _cornerRadius;
private Color4 _color;
#endregion

Expand All @@ -23,6 +25,10 @@ public abstract class Shape
public Color4 Color { get => _color; set => _color = value; }
public bool IsMoveable { get => _isMoveable; protected set => _isMoveable = value; }
public bool IsResizeable { get => _isResizeable; protected set => _isResizeable = value; }
public int Angle { get => _angle; set => _angle = value; }
public bool IsAngleChangeable { get => _isAngleChangeable; set => _isAngleChangeable = value; }
public bool IsCornerRadiusChangeable { get => _isCornerRadiusChangeable; set => _isCornerRadiusChangeable = value; }
public int CornerRadius { get => _cornerRadius; set => _cornerRadius = value; }

List<Shape>[] _timeline;

Expand Down
8 changes: 3 additions & 5 deletions FreeFrame/Shaders/rectangle.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ uniform vec2 u_Size;
uniform float u_Radius;


float roundexBox(vec2 centerPosition, vec2 size, float rad) {
float roundedBox(vec2 centerPosition, vec2 size, float rad) {
return length(max(abs(centerPosition) - size + rad, 0.0)) - rad;
}
void main() {
// How soft the edges should be (in pixels). Higher values could be used to simulate a drop shadow.
float edgeSoftness = 1.0f;

// The radius of the corners (in pixels).
float radius = clamp(u_Radius, 0.0f, max(u_Size.x, u_Size.y) / 2.0f);
float radius = clamp(u_Radius, 0.0f, min(u_Size.x, u_Size.y) / 2.0f);

vec2 position = vec2(u_Position.x, u_Resolution.y - u_Position.y);

vec2 size = vec2(u_Size.x/2.0f, u_Size.y/2.0f);

// Calculate distance to edge.
float distance = roundexBox(gl_FragCoord.xy - position - size, size, radius);
float distance = roundedBox(gl_FragCoord.xy - position + vec2(-u_Size.x/2.0f, u_Size.y/2.0f), u_Size/2.0f, radius);

// Smooth the result (free antialiasing).
float smoothedAlpha = 1.0f - smoothstep(0.0f, edgeSoftness * 2.0f, distance);
Expand Down
3 changes: 2 additions & 1 deletion FreeFrame/Shaders/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
layout(location = 0) in vec2 position;

uniform mat4 u_Model_To_NDC;
uniform mat4 u_Transformation;

void main()
{
gl_Position = u_Model_To_NDC * vec4(position,1.0, 1.0);
gl_Position = u_Transformation * (u_Model_To_NDC * vec4(position,1.0, 1.0));
}
10 changes: 9 additions & 1 deletion FreeFrame/VertexArrayObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public void Draw(Vector2i clientSize, Color4 color)
int uResolution = _shader.GetUniformLocation("u_Resolution");
_shader.SetUniformVec2(uResolution, (Vector2)clientSize);

int uTransformation = _shader.GetUniformLocation("u_Transformation");
Matrix4 transform = Matrix4.Identity;
_shader.SetUniformMat4(uTransformation, transform);

GL.BindVertexArray(_vertexArrayObject);

GL.DrawElements(_primitiveType, _indexCount, DrawElementsType.UnsignedInt, 0);
Expand All @@ -77,6 +81,10 @@ public void Draw(Vector2i clientSize, Color4 color, Shape shape)
int uResolution = _shader.GetUniformLocation("u_Resolution");
_shader.SetUniformVec2(uResolution, (Vector2)clientSize);

int uTransformation = _shader.GetUniformLocation("u_Transformation");
Matrix4 rotation = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(shape.Angle));
_shader.SetUniformMat4(uTransformation, rotation);


Type type = shape.GetType();
if (type == typeof(SVGCircle))
Expand All @@ -93,7 +101,7 @@ public void Draw(Vector2i clientSize, Color4 color, Shape shape)
int uSize = _shader.GetUniformLocation("u_Size");
int uPosition = _shader.GetUniformLocation("u_Position");

_shader.SetUniformFloat(uRadius, ((SVGRectangle)shape).Radius);
_shader.SetUniformFloat(uRadius, ((SVGRectangle)shape).CornerRadius);
_shader.SetUniformVec2(uSize, new Vector2(shape.Width, shape.Height));
_shader.SetUniformVec2(uPosition, new Vector2(shape.X, shape.Y)); // Invert y axis
}
Expand Down
Loading