Skip to content

Commit a0814c1

Browse files
refactor: window class
1 parent a7cea3a commit a0814c1

2 files changed

Lines changed: 279 additions & 275 deletions

File tree

FreeFrame/Timeline.cs

Lines changed: 19 additions & 268 deletions
Original file line numberDiff line numberDiff line change
@@ -18,310 +18,61 @@ public class Timeline
1818
public const int MIN_TIMELINE = 1;
1919
public const int MAX_TIMELINE = 100;
2020

21-
private int _ioTimeline;
22-
private int _ioFps;
23-
private bool _ioIsPlaying;
21+
private int _timelineIndex;
22+
private int _fps;
23+
private bool _isPlaying;
2424

2525
private double _secondsEllapsed;
2626

2727
private SortedDictionary<int, List<Shape>> _timeline;
2828

2929
public SortedDictionary<int, List<Shape>> SortedTimeline { get => _timeline; set => _timeline = value; }
30-
public int IoTimeline
30+
public int TimelineIndex
3131
{
32-
get => _ioTimeline;
32+
get => _timelineIndex;
3333
set
3434
{
3535
if (value > MAX_TIMELINE)
3636
value -= MAX_TIMELINE;
3737

38-
_ioTimeline = Math.Clamp(value, MIN_TIMELINE, MAX_TIMELINE);
38+
_timelineIndex = Math.Clamp(value, MIN_TIMELINE, MAX_TIMELINE);
3939
}
4040
}
41-
public int IoFps { get => _ioFps; private set => _ioFps = value; }
41+
public int Fps { get => _fps; set => _fps = value; }
42+
public bool IsPlaying { get => _isPlaying; set => _isPlaying = value; }
4243

4344
public Timeline()
4445
{
45-
IoTimeline = MIN_TIMELINE;
46-
_ioIsPlaying = false;
47-
IoFps = DEFAULT_FPS;
46+
TimelineIndex = MIN_TIMELINE;
47+
IsPlaying = false;
48+
Fps = DEFAULT_FPS;
4849
_secondsEllapsed = 0;
4950
SortedTimeline = new SortedDictionary<int, List<Shape>>();
5051
}
5152
public void OnRenderFrame(FrameEventArgs e, Window window)
5253
{
53-
if (_ioIsPlaying)
54+
if (IsPlaying)
5455
{
55-
double frameDuration = 1.0 / IoFps;
56+
double frameDuration = 1.0 / Fps;
5657
_secondsEllapsed += e.Time;
5758
if (_secondsEllapsed >= frameDuration)
5859
{
5960
while (_secondsEllapsed >= frameDuration)
6061
{
6162
_secondsEllapsed -= frameDuration;
62-
IoTimeline++;
63+
TimelineIndex++;
6364
}
6465

6566
}
6667
RenderInterpolation(window);
6768
}
6869
}
69-
public void DrawUI(Window window)
70-
{
71-
ImGui.Text("Timeline");
72-
ImGui.Spacing();
73-
if (ImGui.SliderInt("frame", ref _ioTimeline, MIN_TIMELINE, MAX_TIMELINE)) // TODO: please dont hardcode this
74-
RenderInterpolation(window);
75-
76-
ImGui.SameLine();
77-
78-
ImGui.PushItemWidth(80f);
79-
if (ImGui.InputInt("fps", ref _ioFps))
80-
IoFps = Math.Clamp(IoFps, MIN_FPS, MAX_FPS); // TODO: please dont hardcode this
81-
ImGui.PopItemWidth();
82-
83-
if (ImGui.Button(_ioIsPlaying == false ? "Play" : "Pause"))
84-
_ioIsPlaying = !_ioIsPlaying;
85-
86-
ImGui.SameLine();
87-
88-
if (window.SelectedShape != null)
89-
{
90-
if (SortedTimeline.ContainsKey(IoTimeline) && SortedTimeline[IoTimeline] != null && SortedTimeline[IoTimeline].Any(x => x.Id == window.SelectedShape.Id))
91-
{
92-
if (ImGui.Button(String.Format("Remove keyframe {0} for {1}", IoTimeline, window.SelectedShape.GetType().Name)))
93-
{
94-
SortedTimeline[IoTimeline].Remove(SortedTimeline[IoTimeline].Find(x => x.Id == window.SelectedShape.Id)!); // Can't be null
95-
if (SortedTimeline[IoTimeline].Count == 0)
96-
SortedTimeline.Remove(IoTimeline);
97-
// If list _timeline[_ioTimeline] empty then null it
98-
}
99-
}
100-
else
101-
{
102-
if (ImGui.Button(String.Format("Create keyframe for {0}", window.SelectedShape.GetType().Name)))
103-
{
104-
if (SortedTimeline.ContainsKey(IoTimeline) == false || SortedTimeline[IoTimeline] == null)
105-
SortedTimeline[IoTimeline] = new List<Shape>();
10670

107-
foreach (Shape shape in SortedTimeline[IoTimeline])
108-
{
109-
if (shape.Id == window.SelectedShape.Id)
110-
{
111-
Console.WriteLine("Already exist");
112-
SortedTimeline[IoTimeline].Remove(shape);
113-
break;
114-
}
115-
}
116-
SortedTimeline[IoTimeline].Add(window.SelectedShape.ShallowCopy());
117-
}
118-
}
119-
}
120-
}
121-
122-
public void DrawAnimationList(Window window)
123-
{
124-
ImGui.Text("Animation");
125-
ImGui.Spacing();
126-
127-
int numberColumns = SortedTimeline.Count;
128-
if (numberColumns > 0)
129-
{
130-
foreach (Shape shape in window.Shapes)
131-
{
132-
if (ImGui.BeginTable("shape", 2, ImGuiTableFlags.Resizable))
133-
{
134-
ImGui.TableNextRow();
135-
ImGui.TableSetColumnIndex(0);
136-
ImGui.Text(string.Format("{0} - {1}", shape.ShortId, shape.GetType().Name));
137-
ImGui.TableSetColumnIndex(1);
138-
if (ImGui.BeginTable(String.Format("elements##{0}", shape.Id), numberColumns + 1, ImGuiTableFlags.Borders))
139-
{
140-
ImGui.TableSetupColumn("Properties");
141-
foreach (KeyValuePair<int, List<Shape>> shapes in SortedTimeline)
142-
ImGui.TableSetupColumn(shapes.Key.ToString());
143-
ImGui.TableHeadersRow();
144-
145-
146-
int i = 0;
147-
148-
if (shape.IsMoveable)
149-
{
150-
i = 0;
151-
ImGui.TableNextRow();
152-
ImGui.TableSetColumnIndex(i);
153-
ImGui.Text("X");
154-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
155-
{
156-
i++;
157-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
158-
if (sibling != null)
159-
{
160-
ImGui.TableSetColumnIndex(i);
161-
ImGui.Text(sibling.X.ToString());
162-
}
163-
}
164-
165-
i = 0;
166-
ImGui.TableNextRow();
167-
ImGui.TableSetColumnIndex(i);
168-
ImGui.Text("Y");
169-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
170-
{
171-
i++;
172-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
173-
if (sibling != null)
174-
{
175-
ImGui.TableSetColumnIndex(i);
176-
ImGui.Text(sibling.Y.ToString());
177-
}
178-
}
179-
}
180-
181-
if (shape.IsResizeable)
182-
{
183-
i = 0;
184-
ImGui.TableNextRow();
185-
ImGui.TableSetColumnIndex(i);
186-
ImGui.Text("Width");
187-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
188-
{
189-
i++;
190-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
191-
if (sibling != null)
192-
{
193-
ImGui.TableSetColumnIndex(i);
194-
ImGui.Text(sibling.Width.ToString());
195-
}
196-
}
197-
198-
i = 0;
199-
ImGui.TableNextRow();
200-
ImGui.TableSetColumnIndex(i);
201-
ImGui.Text("Height");
202-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
203-
{
204-
i++;
205-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
206-
if (sibling != null)
207-
{
208-
ImGui.TableSetColumnIndex(i);
209-
ImGui.Text(sibling.Height.ToString());
210-
}
211-
}
212-
}
213-
214-
if (shape.IsAngleChangeable)
215-
{
216-
i = 0;
217-
ImGui.TableNextRow();
218-
ImGui.TableSetColumnIndex(i);
219-
ImGui.Text("Angle");
220-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
221-
{
222-
i++;
223-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
224-
if (sibling != null)
225-
{
226-
ImGui.TableSetColumnIndex(i);
227-
ImGui.Text(String.Format("{0}°", sibling.Angle));
228-
}
229-
}
230-
}
231-
232-
if (shape.IsCornerRadiusChangeable)
233-
{
234-
i = 0;
235-
ImGui.TableNextRow();
236-
ImGui.TableSetColumnIndex(i);
237-
ImGui.Text("Corner Radius");
238-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
239-
{
240-
i++;
241-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
242-
if (sibling != null)
243-
{
244-
ImGui.TableSetColumnIndex(i);
245-
ImGui.Text(sibling.CornerRadius.ToString());
246-
}
247-
}
248-
}
249-
250-
i = 0;
251-
ImGui.TableNextRow();
252-
ImGui.TableSetColumnIndex(i);
253-
ImGui.Text("Color");
254-
foreach (KeyValuePair<int, List<Shape>> timeline in SortedTimeline)
255-
{
256-
i++;
257-
Shape? sibling = timeline.Value.Find(x => x.Id == shape.Id);
258-
if (sibling != null)
259-
{
260-
ImGui.TableSetColumnIndex(i);
261-
ImGui.Text(String.Format("RGBA({0}, {1}, {2}, {3})", sibling.Color.R, sibling.Color.G, sibling.Color.B, sibling.Color.A));
262-
}
263-
}
264-
265-
ImGui.EndTable();
266-
}
267-
ImGui.EndTable();
268-
ImGui.Spacing();
269-
}
270-
}
271-
}
272-
}
273-
public void DrawDebugUI(Window window)
274-
{
275-
ImGui.Text("Timeline");
276-
int numberColumns = 0, numberRows = 0;
277-
278-
numberColumns = SortedTimeline.Count;
279-
if (SortedTimeline.Count > 0)
280-
numberRows = SortedTimeline.Max(i => i.Value != null ? i.Value.Count : 0);
281-
282-
if (numberColumns > 0)
283-
{
284-
if (ImGui.BeginTable("elements", numberColumns, ImGuiTableFlags.RowBg | ImGuiTableFlags.Borders))
285-
{
286-
foreach (KeyValuePair<int, List<Shape>> shapes in SortedTimeline)
287-
if (shapes.Value != null)
288-
ImGui.TableSetupColumn(shapes.Key.ToString());
289-
ImGui.TableHeadersRow();
290-
291-
for (int row = 0; row < numberRows; row++)
292-
{
293-
int columnIndex = 0;
294-
ImGui.TableNextRow();
295-
foreach (KeyValuePair<int, List<Shape>> shapes in SortedTimeline)
296-
{
297-
if (shapes.Value != null)
298-
{
299-
if (shapes.Value.Count > row)
300-
{
301-
ImGui.TableSetColumnIndex(columnIndex);
302-
ImGui.Text(string.Format("{0}", shapes.Value[row].GetType().Name));
303-
ImGui.Text(string.Format("{0}", shapes.Value[row].Id));
304-
ImGui.Text(string.Format("{0}", shapes.Value[row].GetHashCode()));
305-
foreach (Renderer vao in shapes.Value[row].Vaos)
306-
{
307-
ImGui.Text(String.Format("VAO: {0}", vao.VertexArrayObjectID));
308-
ImGui.Text(String.Format("VBO: {0}", vao.VertexBufferObjectID));
309-
ImGui.Text(String.Format("IBO: {0}", vao.IndexBufferObjectID));
310-
}
311-
}
312-
columnIndex++;
313-
}
314-
}
315-
}
316-
ImGui.EndTable();
317-
}
318-
}
319-
}
32071
public void UpdateShapeInTimeline(Shape shape)
32172
{
322-
if (SortedTimeline.ContainsKey(IoTimeline) == true && SortedTimeline[IoTimeline] != null)
73+
if (SortedTimeline.ContainsKey(TimelineIndex) == true && SortedTimeline[TimelineIndex] != null)
32374
{
324-
Shape? existingShape = SortedTimeline[IoTimeline].Find(x => x.Id == shape.Id);
75+
Shape? existingShape = SortedTimeline[TimelineIndex].Find(x => x.Id == shape.Id);
32576
if (existingShape != null)
32677
{
32778
Console.WriteLine("There is a shape like meee");
@@ -339,10 +90,10 @@ public void RenderInterpolation(Window window)
33990
{
34091
foreach (Shape shape in window.Shapes)
34192
{
342-
if (SortedTimeline.ContainsKey(IoTimeline) == true && SortedTimeline[IoTimeline] != null && SortedTimeline[IoTimeline].Any(x => x.Id == shape.Id))
93+
if (SortedTimeline.ContainsKey(TimelineIndex) == true && SortedTimeline[TimelineIndex] != null && SortedTimeline[TimelineIndex].Any(x => x.Id == shape.Id))
34394
{
34495
// Draw the current one in this list
345-
Shape sibling = SortedTimeline[IoTimeline].Find(x => x.Id == shape.Id)!;
96+
Shape sibling = SortedTimeline[TimelineIndex].Find(x => x.Id == shape.Id)!;
34697
Console.WriteLine("One already exist");
34798

34899
shape.X = sibling.X;
@@ -366,7 +117,7 @@ public void RenderInterpolation(Window window)
366117
nearest.first = 0;
367118
nearest.second = 0; //Math.Min(keys[keys.Length - 1], _ioTimeline);
368119

369-
int timelineIndex = IoTimeline;
120+
int timelineIndex = TimelineIndex;
370121

371122
if (timelineIndex >= keys[keys.Length - 1])
372123
{

0 commit comments

Comments
 (0)