Skip to content

Commit 26fc7dc

Browse files
feat: edit selected element
1 parent 68e455f commit 26fc7dc

11 files changed

Lines changed: 194 additions & 52 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FreeFrame.Components.Shapes
8+
{
9+
10+
public class Hitbox
11+
{
12+
// TODO: Absolute or relative ?
13+
public struct Area
14+
{
15+
public int X;
16+
public int Y;
17+
public int Width;
18+
public int Height;
19+
public Area(int x, int y, int width, int height)
20+
{
21+
X = x;
22+
Y = y;
23+
Width = width;
24+
Height = height;
25+
}
26+
}
27+
List<Area> _areas = new List<Area>();
28+
29+
public List<Area> Areas { get => _areas; set => _areas = value; }
30+
31+
public Hitbox()
32+
{
33+
}
34+
35+
public bool IsThereSomething(float x, float y) // mouse position
36+
{
37+
foreach (Area area in Areas)
38+
if (x >= area.X && x <= area.X+area.Width && y >= area.Y && y <= area.Y+area.Height) // TODO: To check if it's working
39+
return true;
40+
return false;
41+
}
42+
}
43+
}

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ public class SVGCircle : Shape
1313
private int _cy;
1414
private int _r;
1515
#endregion
16-
/*
17-
18-
// https://www.w3.org/TR/SVG2/shapes.html#CircleElement
19-
TODO: take in account the attributes (listed on the doc Basic Shapes)
20-
21-
private Color _fill;
22-
private int _fillOpacity;
23-
private int _opacity;
24-
25-
public SVGCircle(XmlReader reader)
26-
{
27-
for (int i = 0; i < reader.AttributeCount; i++)
28-
{
29-
30-
}
31-
}
32-
*/
3316
public SVGCircle(XmlReader reader) : this(
3417
Convert.ToInt32(reader["r"]),
3518
Convert.ToInt32(reader["cx"]),
@@ -45,12 +28,11 @@ public SVGCircle(int r, int cx, int cy)
4528

4629
public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";
4730

48-
public override float[] GetVertices()
49-
{
50-
throw new NotImplementedException();
51-
}
31+
public override float[] GetVertices() => new float[] { _cx - _r, _cy - _r, _cx + _r, _cy - _r, _cx + _r, _cy + _r, _cx - _r, _cy + _r }; // x, y, x, y, x, y, ... (clockwise)
32+
33+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode
5234

53-
public override uint[] GetVerticesIndexes()
35+
public override Hitbox Hitbox()
5436
{
5537
throw new NotImplementedException();
5638
}

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public override uint[] GetVerticesIndexes()
109109
throw new NotImplementedException();
110110
}
111111

112+
public override Hitbox Hitbox()
113+
{
114+
throw new NotImplementedException();
115+
}
116+
112117
public override string ToString()
113118
{
114119
string output = "d: ";

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public override uint[] GetVerticesIndexes()
3535
throw new NotImplementedException();
3636
}
3737

38+
public override Hitbox Hitbox()
39+
{
40+
throw new NotImplementedException();
41+
}
42+
3843
public override string ToString()
3944
{
4045
string output = "points: ";

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public class SVGRectangle : Shape
2626

2727
private int _rx; // Rounded in the x axes
2828
private int _ry; // Rounded in the y axes
29+
30+
public int X { get => _x; set => _x = value; }
31+
public int Y { get => _y; set => _y = value; }
32+
public int Width { get => _width; set => _width = value; }
33+
public int Height { get => _height; set => _height = value; }
34+
public int Rx { get => _rx; set => _rx = value; }
35+
public int Ry { get => _ry; set => _ry = value; }
2936
#endregion
3037

3138
public SVGRectangle(XmlReader reader) : this(
@@ -42,24 +49,26 @@ public SVGRectangle(int width, int height) : this(width, height, DefaultX, Defau
4249
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, DefaultRX, DefaultRY) { }
4350
public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
4451
{
45-
_x = x;
46-
_y = y;
47-
_width = width;
48-
_height = height;
49-
_rx = rx;
50-
_ry = ry;
51-
}
52-
public override float[] GetVertices()
53-
{
54-
if (_window == null)
55-
throw new Exception("Trying to convert to NDC but no Window is binded");
56-
57-
// x, y, x, y, x, y, ... (clockwise)
58-
return _window.ConvertToNDC(_x, _y, _x + _width, _y, _x + _width, _y + _height, _x, _y + _height);
52+
X = x;
53+
Y = y;
54+
Width = width;
55+
Height = height;
56+
Rx = rx;
57+
Ry = ry;
5958
}
59+
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)
6060
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode
6161

6262

63-
public override string ToString() => $"x: {_x}, y: {_y}, width: {_width}, height: {_height}, rx: {_rx}, ry: {_ry}";
63+
public override string ToString() => $"x: {X}, y: {Y}, width: {Width}, height: {Height}, rx: {Rx}, ry: {Ry}";
64+
65+
public override Hitbox Hitbox()
66+
{
67+
Hitbox hitbox = new Hitbox();
68+
69+
hitbox.Areas.Add(new Hitbox.Area(X, Y, Width, Height));
70+
71+
return hitbox;
72+
}
6473
}
6574
}

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
using OpenTK.Graphics.OpenGL4;
1+
using MathNet.Numerics.LinearAlgebra;
2+
using OpenTK.Graphics.OpenGL4;
3+
using OpenTK.Mathematics;
24
using OpenTK.Windowing.Desktop;
35

46
namespace FreeFrame.Components.Shapes
57
{
68
public abstract class Shape
79
{
8-
protected static Window? _window;
10+
#region Common Geometry Properties
11+
Color4 _color = new Color4(1.0f, 1.0f, 1.0f, 1.0f);
12+
#endregion
13+
private static Window? _window;
914
private Shader _shader;
1015
private int _vertexBufferObject;
1116
private int _vertexArrayObject;
1217
private int _indexBufferObject;
1318

1419
private int _indexCount;
1520

21+
public Color4 Color { get => _color; set => _color = value; }
22+
1623
public Shape() { }
1724
public static void BindWindow(GameWindow window) => _window = (Window)window;
1825
public void GenerateObjects()
@@ -30,8 +37,23 @@ public void GenerateObjects()
3037
/// <param name="index">index array</param>
3138
public void ImplementObjects()
3239
{
40+
//float[] vertices =
41+
//{
42+
// 0.5f, 0.5f, // Top-Right
43+
// 0.5f, 0.0f, // Bottom-Right
44+
// 0.0f, 0.0f, // Bottom-Left
45+
// 0.0f, 0.5f, // Top-Left
46+
//};
47+
48+
//uint[] indexes =
49+
//{
50+
// 0, 1, 2, // First triangle
51+
// 0, 2, 3, // Second triangle
52+
//};
53+
3354
float[] vertices = GetVertices();
3455
uint[] indexes = GetVerticesIndexes();
56+
3557
// VAO
3658
GL.BindVertexArray(_vertexArrayObject);
3759

@@ -64,7 +86,20 @@ public void ImplementObjects()
6486
/// </summary>
6587
public void Draw()
6688
{
89+
if (_window == null)
90+
throw new Exception("Trying to convert to NDC but no Window is binded");
91+
6792
_shader.Use();
93+
94+
// Applied projection matrix
95+
int uModelToNDC = _shader.GetUniformLocation("u_Model_To_NDC"); // TODO: Don't need to apply projection matrix at each frame I think
96+
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter(0, _window.ClientSize.X, _window.ClientSize.Y, 0, -1.0f, 1.0f);
97+
_shader.SetUniformMat4(uModelToNDC, matrix);
98+
99+
// Applied common geometry color
100+
int uColor = _shader.GetUniformLocation("u_Color");
101+
_shader.SetUniformVec4(uColor, (Vector4)Color);
102+
68103
GL.BindVertexArray(_vertexArrayObject);
69104
GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0);
70105
}
@@ -85,6 +120,7 @@ public void DeleteObjects()
85120
/// </summary>
86121
/// <returns>array of indexes</returns>
87122
public abstract uint[] GetVerticesIndexes();
123+
public abstract Hitbox Hitbox();
88124
public abstract override string ToString();
89125
}
90126

FreeFrame/FreeFrame.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="ImGui.NET" Version="1.87.3" />
12+
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
1213
<PackageReference Include="OpenTK" Version="4.7.1" />
1314
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
1415
</ItemGroup>

FreeFrame/Shader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using OpenTK.Mathematics;
78

89
namespace FreeFrame
910
{
@@ -46,6 +47,10 @@ public Shader(string uriVertexShader, string uriFragementShader)
4647
GL.DeleteShader(fragmentShader);
4748
}
4849
public int GetUniformLocation(string uniform) => GL.GetUniformLocation(_program, uniform);
50+
51+
public void SetUniformVec4(int uniform, Vector4 vector) => GL.Uniform4(uniform, vector);
52+
public void SetUniformMat4(int uniform, Matrix4 matrix) => GL.UniformMatrix4(uniform, false, ref matrix);
53+
4954
public void Use() => GL.UseProgram(_program);
5055
public void Delete() => GL.DeleteProgram(_program);
5156
~Shader() => Delete();

FreeFrame/Shaders/shader.frag

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
layout(location = 0) out vec4 color;
44

5-
//uniform vec4 u_Color;
5+
uniform vec4 u_Color;
66

77
void main()
88
{
9-
color = vec4(0.8f, 0.5f, 0.1f, 1.0f);
9+
color = u_Color;
1010
}

FreeFrame/Shaders/shader.vert

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#version 330 core
22

3-
layout(location = 0) in vec3 position;
3+
layout(location = 0) in vec2 position;
4+
5+
uniform mat4 u_Model_To_NDC;
46

57
void main()
68
{
7-
gl_Position = vec4(position, 1.0);
9+
gl_Position = u_Model_To_NDC * vec4(position,1.0, 1.0);
810
}

0 commit comments

Comments
 (0)