Skip to content

Commit 029cb93

Browse files
feat: color importation
1 parent 80ad23d commit 029cb93

8 files changed

Lines changed: 161 additions & 20 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,14 @@ static public void ExportToFile(List<Shape> shapes, Vector2i clientSize)
9090
fs.WriteByte(bytes[i]);
9191
}
9292
}
93+
94+
static public Color4 HexadecimalToRGB(string hexadecimal)
95+
{
96+
float r = Convert.ToInt32(hexadecimal.Substring(1, 2), 16) / 255f;
97+
float g = Convert.ToInt32(hexadecimal.Substring(3, 2), 16) / 255f;
98+
float b = Convert.ToInt32(hexadecimal.Substring(5, 2), 16) / 255f;
99+
float a = Convert.ToInt32(hexadecimal.Substring(7, 2), 16) / 255f;
100+
return new Color4(r, g, b, a);
101+
}
93102
}
94103
}

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ public class SVGCircle : Shape
1313
public SVGCircle(XmlReader reader) : this(
1414
Convert.ToInt32(reader["r"]),
1515
Convert.ToInt32(reader["cx"]),
16-
Convert.ToInt32(reader["cy"])) // TODO: Error handler if r, cx or cy are not here
16+
Convert.ToInt32(reader["cy"]),
17+
Convert.ToString(reader["fill"])) // TODO: Error handler if r, cx or cy are not here
1718
{ }
18-
public SVGCircle() : this(0, 0, 0) { }
19-
public SVGCircle(int r, int cx, int cy)
19+
public SVGCircle() : this(0, 0, 0, "#000000FF") { }
20+
public SVGCircle(int r, int cx, int cy, string color)
2021
{
2122
IsCornerRadiusChangeable = false;
2223
X = cx - r;
2324
Y = cy - r;
2425
Height = r*2;
2526
Width = Height;
27+
Color = Importer.HexadecimalToRGB(color);
2628

2729
ImplementObject();
2830
}

FreeFrame/Components/Shapes/SVGLine.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@ internal class SVGLine : Shape
1616
const int DefaultY1 = 0;
1717
const int DefaultX2 = 0;
1818
const int DefaultY2 = 0;
19+
const string DefaultColor = "#000000FF";
1920
#endregion
2021

2122
public SVGLine(XmlReader reader) : this(
2223
Convert.ToInt32(reader["x1"]),
2324
Convert.ToInt32(reader["y1"]),
2425
Convert.ToInt32(reader["x2"]),
25-
Convert.ToInt32(reader["y2"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
26+
Convert.ToInt32(reader["y2"]),
27+
Convert.ToString(reader["fill"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
2628
{
2729
}
28-
public SVGLine() : this(DefaultX1, DefaultY1, DefaultX2, DefaultY2) { }
29-
public SVGLine(int x1, int y1, int x2, int y2)
30+
public SVGLine() : this(DefaultX1, DefaultY1, DefaultX2, DefaultY2, DefaultColor) { }
31+
public SVGLine(int x1, int y1, int x2, int y2, string color)
3032
{
3133
IsCornerRadiusChangeable = false;
3234

3335
X = x1;
3436
Y = y1;
3537
Width = x2 - X;
3638
Height = y2 - Y;
39+
Color = Importer.HexadecimalToRGB(color);
3740

3841
ImplementObject();
3942
}

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public SVGPath(XmlReader reader) //: this()
107107
}
108108
}
109109

110+
string color = reader["fill"] ?? throw new Exception("color not here"); // TODO: Error handler if d is note here
111+
Color = Importer.HexadecimalToRGB(color);
112+
113+
110114
// Update common properties, use the given attributes
111115
List<Vector2i> points = GetSelectablePoints();
112116
X = points.Min(i => i.X);

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class SVGRectangle : Shape
1616
const int DefaultHeight = 0;
1717
const int DefaultRX = 0;
1818
const int DefaultRY = 0;
19+
const string DefaultColor = "#000000FF";
1920
#endregion
2021

2122
public SVGRectangle(XmlReader reader) : this(
@@ -24,19 +25,21 @@ public SVGRectangle(XmlReader reader) : this(
2425
Convert.ToInt32(reader["x"]),
2526
Convert.ToInt32(reader["y"]),
2627
Convert.ToInt32(reader["rx"]),
27-
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
28+
Convert.ToInt32(reader["ry"]),
29+
Convert.ToString(reader["fill"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
2830
{
2931
}
3032
public SVGRectangle() : this(DefaultWidth, DefaultHeight, DefaultX, DefaultY) { }
3133
public SVGRectangle(int width, int height) : this(width, height, DefaultX, DefaultY) { }
32-
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, DefaultRX, DefaultRY) { }
33-
public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
34+
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, DefaultRX, DefaultRY, DefaultColor) { }
35+
public SVGRectangle(int width, int height, int x, int y, int rx, int ry, string color)
3436
{
3537
X = x;
3638
Y = y;
3739
Width = width;
3840
Height = height;
3941
CornerRadius = Math.Max(rx, ry);
42+
Color = Importer.HexadecimalToRGB(color);
4043

4144
ImplementObject();
4245
}

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public abstract class Shape : IDrawable
3232
public int CornerRadius { get => _cornerRadius; set => _cornerRadius = value; }
3333
public Guid Id { get => _id; private set => _id = value; }
3434
public List<Renderer> Vaos { get => vaos; protected set => vaos = value; }
35-
35+
public string ShortId
36+
{
37+
get => Id.ToString().Substring(0, 6);
38+
}
3639
public Shape()
3740
{
3841
Vaos = new List<Renderer>();

FreeFrame/Timeline.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using FreeFrame.Components.Shapes;
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 class Timeline
11+
{
12+
private int _ioTimeline;
13+
private SortedDictionary<int, List<Shape>> _timeline;
14+
public Timeline()
15+
{
16+
_ioTimeline = 1;
17+
_timeline = new SortedDictionary<int, List<Shape>>();
18+
}
19+
20+
//public void RemoveElementInTimeline(Shape shapeToRemove)
21+
//{
22+
// List<int> shapesToDelete = new();
23+
// foreach (KeyValuePair<int, List<Shape>> shapes in _timeline) // Remove element in the timeline
24+
// {
25+
// foreach (Shape shape in shapes.Value)
26+
// {
27+
// if (shape.Id == shapeToRemove.Id)
28+
// {
29+
// shape.DeleteObjects();
30+
// shapes.Value.Remove(shape);
31+
// break; // Because we know that there is no more of this shape in the current list
32+
// }
33+
// }
34+
// if (shapes.Value.Count == 0) // Remove shapes in timeline
35+
// shapesToDelete.Add(shapes.Key);
36+
// }
37+
// foreach (int id in shapesToDelete)
38+
// _timeline.Remove(id);
39+
//}
40+
//public void ResetTimeline()
41+
//{
42+
// foreach (KeyValuePair<int, List<Shape>> shapes in _timeline) // Remove element in the timeline
43+
// {
44+
// foreach (Shape shape in shapes.Value)
45+
// shape.DeleteObjects();
46+
// shapes.Value.Clear();
47+
// }
48+
// _timeline.Clear();
49+
//}
50+
}
51+
}

FreeFrame/Window.cs

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ enum UserMode
3838
Create,
3939
Move
4040
}
41+
enum ImportMode
42+
{
43+
Add,
44+
Override
45+
}
4146
enum CreateMode
4247
{
4348
Line,
@@ -74,6 +79,7 @@ enum CreateMode
7479

7580
UserMode _userMode;
7681
CreateMode _createMode;
82+
ImportMode _importMode;
7783

7884
ImGuiController _ImGuiController;
7985

@@ -462,9 +468,9 @@ public void OnLeftMouseEnter() // TODO: Rename this
462468
case UserMode.Create: // Create mode
463469
_selectedShape = _createMode switch
464470
{
465-
CreateMode.Line => new SVGLine((int)MouseState.X, (int)MouseState.Y, (int)MouseState.X, (int)MouseState.Y),
471+
CreateMode.Line => new SVGLine((int)MouseState.X, (int)MouseState.Y, (int)MouseState.X, (int)MouseState.Y, "#000000FF"),
466472
CreateMode.Rectangle => new SVGRectangle(0, 0, (int)MouseState.X, (int)MouseState.Y),
467-
CreateMode.Circle => new SVGCircle(0, (int)MouseState.X, (int)MouseState.Y),
473+
CreateMode.Circle => new SVGCircle(0, (int)MouseState.X, (int)MouseState.Y, "#000000FF"),
468474
_ => throw new Exception("A create mode need to be selected"),
469475
};
470476
_shapes.Add(_selectedShape);
@@ -823,15 +829,45 @@ public void ShowUI()
823829
ImGui.SetWindowPos(new System.Numerics.Vector2(ClientSize.X - ImGui.GetWindowWidth(), ClientSize.Y / 2));
824830
ImGui.Text("Tree View");
825831
ImGui.Spacing();
826-
foreach (Shape shape in _shapes)
832+
for (int i = _shapes.Count - 1; i >= 0; i--)
827833
{
828-
if (ImGui.Selectable(String.Format("{0}##{1}", shape.GetType().Name, shape.GetHashCode()), _selectedShape == shape))
834+
if (ImGui.Selectable(String.Format("{0} - {1}##{2}", _shapes[i].ShortId, _shapes[i].GetType().Name, _shapes[i].GetHashCode()), _selectedShape == _shapes[i]))
829835
{
830-
_selectedShape = shape;
831-
_selector.Select(shape);
836+
_selectedShape = _shapes[i];
837+
_selector.Select(_shapes[i]);
832838
_userMode = UserMode.Edit;
833839
Console.WriteLine("New shape selected through tree view");
834840
}
841+
if (i - 1 >= 0)
842+
{
843+
if (ImGui.ArrowButton(String.Format("Down##d{0}", i), ImGuiDir.Down))
844+
{
845+
InvertShape(i, i - 1);
846+
SelectShape(_shapes[i - 1]);
847+
}
848+
}
849+
if (i + 1 < _shapes.Count)
850+
{
851+
if (i - 1 >= 0)
852+
ImGui.SameLine();
853+
if (ImGui.ArrowButton(String.Format("Up##u{0}", i), ImGuiDir.Up))
854+
{
855+
InvertShape(i, i + 1);
856+
SelectShape(_shapes[i + 1]);
857+
}
858+
}
859+
ImGui.Separator();
860+
}
861+
foreach (Shape shape in _shapes)
862+
{
863+
//ImGui.Text(String.Format("{0}", shape.GetType().Name));
864+
//if (ImGui.Selectable(String.Format("{0}##{1}", shape.GetType().Name, shape.GetHashCode()), _selectedShape == shape))
865+
//{
866+
// _selectedShape = shape;
867+
// _selector.Select(shape);
868+
// _userMode = UserMode.Edit;
869+
// Console.WriteLine("New shape selected through tree view");
870+
//}
835871
}
836872
ImGui.End();
837873

@@ -1061,9 +1097,15 @@ public void ShowUI()
10611097
if (ImGui.BeginMenu("Open"))
10621098
{
10631099
if (ImGui.MenuItem("New project", "Ctrl+O"))
1100+
{
10641101
_dialogFilePicker = true;
1102+
_importMode = ImportMode.Override;
1103+
}
10651104
if (ImGui.MenuItem("Import a file", "Ctrl+I"))
1066-
{ }
1105+
{
1106+
_dialogFilePicker = true;
1107+
_importMode = ImportMode.Add;
1108+
}
10671109
ImGui.EndMenu();
10681110
}
10691111
if (ImGui.BeginMenu("Save"))
@@ -1121,7 +1163,19 @@ public void ShowUI()
11211163
{
11221164
ResetSelection();
11231165
ResetTimeline();
1124-
(_shapes, bool compatibilityFlag) = Importer.ImportFromFile(picker.SelectedFile);
1166+
bool compatibilityFlag;
1167+
1168+
switch (_importMode)
1169+
{
1170+
case ImportMode.Add:
1171+
(List<Shape> newShapes, compatibilityFlag) = Importer.ImportFromFile(picker.SelectedFile);
1172+
_shapes.AddRange(newShapes);
1173+
break;
1174+
case ImportMode.Override:
1175+
default:
1176+
(_shapes, compatibilityFlag) = Importer.ImportFromFile(picker.SelectedFile);
1177+
break;
1178+
}
11251179
FilePicker.RemoveFilePicker(this);
11261180
if (compatibilityFlag)
11271181
_dialogCompatibility = true;
@@ -1147,6 +1201,18 @@ public void ShowUI()
11471201
}
11481202
ImGui.End();
11491203
}
1204+
public void SelectShape(Shape shape)
1205+
{
1206+
_selectedShape = shape;
1207+
_selector.Select(shape);
1208+
_userMode = UserMode.Edit;
1209+
}
1210+
public void InvertShape(int index1, int index2)
1211+
{
1212+
Shape tmpShape = _shapes[index2].Clone();
1213+
_shapes[index2] = _shapes[index1].Clone();
1214+
_shapes[index1] = tmpShape;
1215+
}
11501216

11511217
static void HelpMarker(string desc)
11521218
{
@@ -1193,7 +1259,7 @@ public void RenderInterpolation()
11931259

11941260
int timelineIndex = _ioTimeline;
11951261

1196-
1262+
11971263

11981264
if (timelineIndex >= keys[keys.Length - 1])
11991265
{
@@ -1248,7 +1314,7 @@ public void RenderInterpolation()
12481314
Shape first = _timeline[nearest.first].Find(x => x.Id == shape.Id)!; // can't be null
12491315
Shape second = _timeline[nearest.second].Find(x => x.Id == shape.Id)!; // can't be null;
12501316
// If reverse and loop invert the two shape every odd
1251-
1317+
12521318

12531319
//if (first != null && second != null)
12541320
{

0 commit comments

Comments
 (0)