Skip to content

Commit 672470b

Browse files
feat: angle supported
1 parent d5a1681 commit 672470b

6 files changed

Lines changed: 67 additions & 31 deletions

File tree

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace FreeFrame.Components.Shapes
88
{
9-
public abstract class Shape
9+
public abstract class Shape : IDrawable
1010
{
1111
bool _isMoveable = true;
1212
bool _isResizeable = true;
@@ -40,9 +40,6 @@ public Shape()
4040
Id = Guid.NewGuid();
4141
}
4242

43-
/// <summary>
44-
/// Trigge draw element through OpenGL context
45-
/// </summary>
4643
public virtual void Draw(Vector2i clientSize, Camera camera)
4744
{
4845
//Console.WriteLine("Draw {0}, {1}, {2}", GetType().Name, Id, GetHashCode());

FreeFrame/IDrawable.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using OpenTK.Mathematics;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace FreeFrame
9+
{
10+
public interface IDrawable
11+
{
12+
List<Renderer> Vaos
13+
{
14+
get;
15+
}
16+
/// <summary>
17+
/// Trigge draw element through OpenGL context
18+
/// </summary>
19+
public void Draw(Vector2i clientSize, Camera camera);
20+
public void DeleteObjects();
21+
}
22+
}

FreeFrame/Renderer.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,15 @@ public void Draw(Vector2i clientSize, Camera camera, Color4 color, Shape shape)
102102
int uProjection = _shader.GetUniformLocation("u_Projection");
103103
_shader.SetUniformMat4(uProjection, camera.GetViewMatrix());
104104

105+
int uRadius = _shader.GetUniformLocation("u_Radius");
106+
_shader.SetUniformFloat(uRadius, shape.CornerRadius);
107+
108+
int uSize = _shader.GetUniformLocation("u_Size");
109+
_shader.SetUniformVec2(uSize, new Vector2(shape.Width, shape.Height));
110+
111+
int uPosition = _shader.GetUniformLocation("u_Position");
112+
_shader.SetUniformVec2(uPosition, new Vector2(shape.X, shape.Y));
105113

106-
Type type = shape.GetType();
107-
if (type == typeof(SVGCircle))
108-
{
109-
int uRadius = _shader.GetUniformLocation("u_Radius");
110-
int uPosition = _shader.GetUniformLocation("u_Position");
111-
112-
_shader.SetUniformFloat(uRadius, shape.Width / 2);
113-
_shader.SetUniformVec2(uPosition, new Vector2(shape.X + shape.Width / 2, shape.Y + shape.Height / 2));
114-
}
115-
else if (type == typeof(SVGRectangle))
116-
{
117-
int uRadius = _shader.GetUniformLocation("u_Radius");
118-
int uSize = _shader.GetUniformLocation("u_Size");
119-
int uPosition = _shader.GetUniformLocation("u_Position");
120-
121-
_shader.SetUniformFloat(uRadius, ((SVGRectangle)shape).CornerRadius);
122-
_shader.SetUniformVec2(uSize, new Vector2(shape.Width, shape.Height));
123-
_shader.SetUniformVec2(uPosition, new Vector2(shape.X, shape.Y)); // Invert y axis
124-
}
125114

126115
GL.BindVertexArray(VertexArrayObjectID);
127116

FreeFrame/Selector.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace FreeFrame
1111
{
12-
public class Selector
12+
public class Selector : IDrawable
1313
{
1414
public struct Area
1515
{
@@ -33,17 +33,17 @@ public enum SelectorType
3333
None
3434
}
3535

36-
private List<(Renderer vao, Area hitbox, SelectorType type)> _vaos;
36+
private List<(Renderer vao, Area hitbox, SelectorType type)> _vaos; // TODO: Just have a List<Renderer> like Shape in order to put it in the interface
37+
38+
public List<Renderer> Vaos { get => null; set => _ = value; } // TODO: Just have a List<Renderer> like Shape in order to put it in the interface
3739

3840
public Selector()
3941
{
4042
_vaos = new List<(Renderer vao, Area hitbox, SelectorType type)>();
4143
}
4244
public void Select(Shape shape)
4345
{
44-
_vaos.ForEach(i => i.vao.DeleteObjects());
45-
_vaos.Clear();
46-
46+
DeleteObjects();
4747

4848
// Edge
4949
Area hitbox = new Area
@@ -98,6 +98,11 @@ public void Draw(Vector2i clientSize, Camera camera)
9898
GL.Disable(EnableCap.LineSmooth);
9999

100100
}
101+
/// <summary>
102+
/// Return true if the given mouse position is in the selector hitbox
103+
/// </summary>
104+
/// <param name="mousePosition">Mouse position</param>
105+
/// <returns>true if touching the selector (and give the selector type), false otherwise (and null) </returns>
101106
public (bool, SelectorType?) HitBox(Vector2i mousePosition)
102107
{
103108
if (_vaos.Count > 0)
@@ -106,5 +111,11 @@ public void Draw(Vector2i clientSize, Camera camera)
106111
return (true, part.type);
107112
return (false, null);
108113
}
114+
115+
public void DeleteObjects()
116+
{
117+
_vaos.ForEach(i => i.vao.DeleteObjects());
118+
_vaos.Clear();
119+
}
109120
}
110121
}

FreeFrame/Shaders/circle.frag

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ layout(location = 0) out vec4 color;
55
uniform vec2 u_Resolution;
66
uniform vec4 u_Color;
77
uniform vec2 u_Position;
8+
uniform vec2 u_Size;
89
uniform float u_Radius;
910

1011
// Draw a circle at a given position with radius and color
@@ -18,7 +19,9 @@ void main() {
1819

1920
vec2 uv = gl_FragCoord.xy;
2021

21-
vec2 position = vec2(u_Position.x, u_Resolution.y - u_Position.y); // Invert y axis
22+
float radius = u_Size.x / 2.0;
2223

23-
color = circle(uv, position, u_Radius, u_Color);
24+
vec2 position = vec2(u_Position.x + u_Size.x / 2.0, u_Resolution.y - (u_Position.y + u_Size.y / 2.0)); // Invert y axis
25+
26+
color = circle(uv, position, radius, u_Color);
2427
}

FreeFrame/Shaders/shader.vert

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@ uniform mat4 u_Transformation;
77
uniform mat4 u_View;
88
uniform mat4 u_Projection;
99

10+
uniform vec2 u_Resolution;
11+
uniform vec2 u_Position;
12+
uniform vec2 u_Size;
13+
1014

1115
void main()
1216
{
13-
gl_Position = u_Transformation * (u_Model_To_NDC * vec4(position,1.0, 1.0)); // * u_View * u_Projection);
17+
vec4 finalPosition = vec4(position,1.0, 1.0);
18+
19+
finalPosition -= vec4(u_Position.x + u_Size.x/2.0, u_Position.y + u_Size.y/2.0, 0.0, 0.0);
20+
21+
finalPosition = finalPosition * u_Transformation;
22+
23+
finalPosition += vec4(u_Position.x + u_Size.x/2.0, u_Position.y + u_Size.y/2.0, 0.0, 0.0);
24+
25+
finalPosition = u_Model_To_NDC * finalPosition;
26+
27+
gl_Position = finalPosition; // * u_View * u_Projection);
1428
}

0 commit comments

Comments
 (0)