Skip to content

Commit 6ecf779

Browse files
committed
Added text animation
1 parent e8ff93d commit 6ecf779

20 files changed

+325
-101
lines changed

OpenBoardAnim.Utilities/EnumHelper.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24

35
namespace OpenBoardAnim.Utilities
46
{
@@ -18,5 +20,10 @@ public static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attrib
1820
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
1921
return (attributes.Length > 0) ? (T)attributes[0] : null;
2022
}
23+
24+
public static IEnumerable<T> EnumerateEnum<T>()
25+
{
26+
return Enum.GetValues(typeof(T)).Cast<T>();
27+
}
2128
}
2229
}

OpenBoardAnim/Controls/DraggablePath.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
Canvas.Top="100"
4646
Canvas.Left="100"
4747
Template="{StaticResource DesignerItemTemplate}">
48-
<Path Data="{Binding ImgGeometry}" Stretch="Fill" Stroke="Black"/>
48+
<Path Data="{Binding ImgDrawingGroup}" Stretch="Fill" Stroke="Black"/>
4949
</ContentControl>
5050
</UserControl>

OpenBoardAnim/Controls/DraggablePath.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void Path_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
4747
prevX = transform.X;
4848
prevY = transform.Y;
4949
}
50-
GraphicModel model = draggable.DataContext as GraphicModel;
50+
DrawingModel model = draggable.DataContext as DrawingModel;
5151
if (model != null)
5252
{
5353
model.X = prevX;

OpenBoardAnim/Controls/MoveThumb.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
2222

2323
if (designerItem != null)
2424
{
25-
var model = designerItem.DataContext as GraphicModel;
25+
var model = designerItem.DataContext as GraphicModelBase;
2626
if (model != null)
2727
{
2828

OpenBoardAnim/Controls/ResizeThumb.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace OpenBoardAnim.Controls
77
public class ResizeThumb : Thumb
88
{
99
private double originalRatio = -1;
10+
private double originalHeight = -1;
1011
public ResizeThumb()
1112
{
1213
DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
@@ -23,8 +24,9 @@ private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
2324
designerItem.Height = designerItem.ActualHeight;
2425
designerItem.Width = designerItem.ActualWidth;
2526
originalRatio = designerItem.ActualHeight / designerItem.ActualWidth;
27+
originalHeight = designerItem.Height;
2628
}
27-
var model = designerItem.DataContext as GraphicModel;
29+
var model = designerItem.DataContext as GraphicModelBase;
2830
if (model != null)
2931
{
3032
double deltaVertical, deltaHorizontal;
@@ -58,11 +60,13 @@ private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
5860
default:
5961
break;
6062
}
63+
double newRatio = designerItem.Height / designerItem.Width;
64+
if (newRatio > originalRatio) designerItem.Height = originalRatio * designerItem.Width;
65+
else designerItem.Width = designerItem.Height / originalRatio;
66+
model.ResizeRatio = designerItem.Height/ originalHeight;
6167
}
6268
}
63-
double newRatio = designerItem.Height / designerItem.Width;
64-
if(newRatio > originalRatio)designerItem.Height = originalRatio*designerItem.Width;
65-
else designerItem.Width = designerItem.Height/ originalRatio;
69+
6670
e.Handled = true;
6771
}
6872
}

OpenBoardAnim/Models/DrawingModel.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using OpenBoardAnim.Core;
2+
using System.ComponentModel;
3+
using System.Text.Json.Serialization;
4+
using System.Windows.Input;
5+
using System.Windows.Media;
6+
7+
namespace OpenBoardAnim.Models
8+
{
9+
public class DrawingModel : GraphicModelBase
10+
{
11+
public DrawingModel()
12+
{
13+
AddGraphicCommand = new RelayCommand(AddGraphicCommandHandler, canExecute: o => true);
14+
}
15+
[JsonIgnore]
16+
public ICommand AddGraphicCommand { get; set; }
17+
18+
public Action<DrawingModel> AddGraphic;
19+
protected void AddGraphicCommandHandler(object obj)
20+
{
21+
AddGraphic?.Invoke(this);
22+
}
23+
24+
public string SVGPath { get; set; }
25+
[JsonIgnore]
26+
public DrawingGroup ImgDrawingGroup { get; set; }
27+
28+
public override GraphicModelBase Clone()
29+
{
30+
return new DrawingModel
31+
{
32+
Height = Height,
33+
Width = Width,
34+
ImgDrawingGroup = ImgDrawingGroup,
35+
Name = Name,
36+
X = X,
37+
Y = Y,
38+
SVGPath = SVGPath,
39+
Delay = Delay,
40+
Duration = Duration,
41+
};
42+
}
43+
}
44+
}

OpenBoardAnim/Models/GraphicModel.cs OpenBoardAnim/Models/GraphicModelBase.cs

+12-36
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
11
using OpenBoardAnim.Core;
2-
using System.ComponentModel;
32
using System.Text.Json.Serialization;
43
using System.Windows.Input;
54
using System.Windows.Media;
65

76
namespace OpenBoardAnim.Models
87
{
9-
public class GraphicModel : ObservableObject
8+
public abstract class GraphicModelBase : ObservableObject
109
{
1110
public string Name { get; set; }
12-
public string SVGPath { get; set; }
13-
[JsonIgnore]
14-
public DrawingGroup ImgGeometry { get; set; }
15-
[JsonIgnore]
16-
public ICommand AddGraphicCommand { get; set; }
17-
public GraphicModel()
18-
{
19-
20-
AddGraphicCommand = new RelayCommand(AddGraphicCommandHandler,
21-
canExecute: o => true);
22-
}
23-
public Action<GraphicModel> AddGraphic;
24-
25-
public GraphicModel Clone()
26-
{
27-
return new GraphicModel
28-
{
29-
Height = Height,
30-
Width = Width,
31-
ImgGeometry = ImgGeometry,
32-
Name = Name,
33-
X = X,
34-
Y = Y,
35-
SVGPath = SVGPath,
36-
Delay = Delay,
37-
Duration = Duration,
38-
};
39-
}
40-
4111
private double x;
4212
public double X
4313
{
@@ -62,7 +32,7 @@ public double Y
6232

6333
private double _delay = 0;
6434
public double Delay
65-
{
35+
{
6636
get { return _delay; }
6737
set
6838
{
@@ -104,11 +74,17 @@ public double Width
10474
}
10575
}
10676

107-
private void AddGraphicCommandHandler(object obj)
77+
private double _resizeRatio = 1;
78+
public double ResizeRatio
10879
{
109-
AddGraphic?.Invoke(this);
80+
get { return _resizeRatio; }
81+
set
82+
{
83+
_resizeRatio = value;
84+
OnPropertyChanged();
85+
}
11086
}
11187

112-
88+
public abstract GraphicModelBase Clone();
11389
}
114-
}
90+
}

OpenBoardAnim/Models/SceneModel.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SceneModel : ObservableObject
1111
{
1212
public SceneModel()
1313
{
14-
Graphics = new BindingList<GraphicModel>();
14+
Graphics = new BindingList<GraphicModelBase>();
1515
ReplaceSceneCommand = new RelayCommand(ReplaceSceneCommandHandler,
1616
canExecute: o => true);
1717
}
@@ -26,7 +26,7 @@ public SceneModel Clone()
2626
return new SceneModel
2727
{
2828
Name = Name,
29-
Graphics = new BindingList<GraphicModel>(Graphics.Select(x=>x.Clone()).ToList()),
29+
Graphics = new BindingList<GraphicModelBase>(Graphics.Select(x=>x.Clone()).ToList()),
3030
};
3131
}
3232

@@ -41,8 +41,8 @@ public string Name
4141
}
4242
}
4343

44-
private BindingList<GraphicModel> _graphics;
45-
public BindingList<GraphicModel> Graphics
44+
private BindingList<GraphicModelBase> _graphics;
45+
public BindingList<GraphicModelBase> Graphics
4646
{
4747
get { return _graphics; }
4848
set

OpenBoardAnim/Models/TextModel.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Media;
7+
8+
namespace OpenBoardAnim.Models
9+
{
10+
public class TextModel : GraphicModelBase
11+
{
12+
public PathGeometry TextGeometry { get; set; }
13+
14+
public override GraphicModelBase Clone()
15+
{
16+
return new TextModel
17+
{
18+
Height = Height,
19+
Width = Width,
20+
TextGeometry = TextGeometry,
21+
Name = Name,
22+
X = X,
23+
Y = Y,
24+
Delay = Delay,
25+
Duration = Duration,
26+
};
27+
}
28+
}
29+
}

OpenBoardAnim/OpenBoardAnim.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<ItemGroup>
3434
<ProjectReference Include="..\OpenBoardAnim.Library\OpenBoardAnim.Library.csproj" />
35+
<ProjectReference Include="..\OpenBoardAnim.Utilities\OpenBoardAnim.Utilities.csproj" />
3536
</ItemGroup>
3637

3738
<ItemGroup>

OpenBoardAnim/Services/CacheService.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CacheService
1919
private List<SceneEntity> _sceneEntities;
2020
public BindingList<RecentProjectModel> RecentProjects { get; set; }
2121
public ProjectDetails CurrentProject { get; set; }
22-
public BindingList<GraphicModel> LoadedGraphics { get; set; }
22+
public BindingList<DrawingModel> LoadedGraphics { get; set; }
2323
public BindingList<SceneModel> LoadedScenes { get; set; }
2424

2525
public CacheService(GraphicRepository gRepo, SceneRepository sRepo, ProjectRepository pRepo)
@@ -39,7 +39,8 @@ public ProjectDetails LoadProjectFromFile(RecentProjectModel model)
3939
{
4040
foreach (var g in s.Graphics)
4141
{
42-
g.ImgGeometry = SVGHelper.GetPathGeometryFromSVG(g.SVGPath);
42+
if(g is DrawingModel d)
43+
d.ImgDrawingGroup = GeometryHelper.GetPathGeometryFromSVG(d.SVGPath);
4344
}
4445
}
4546
return project;
@@ -82,14 +83,14 @@ private void LoadGraphics()
8283
string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
8384

8485
_graphicEntities = _gRepo.GetAllGraphics();
85-
List<GraphicModel> graphics = _graphicEntities.Select(e =>
86-
new GraphicModel
86+
List<DrawingModel> graphics = _graphicEntities.Select(e =>
87+
new DrawingModel
8788
{
8889
Name = e.Name,
8990
SVGPath = Path.Combine(folder, e.FilePath),
90-
ImgGeometry = SVGHelper.GetPathGeometryFromSVG(Path.Combine(folder, e.FilePath))
91+
ImgDrawingGroup = GeometryHelper.GetPathGeometryFromSVG(Path.Combine(folder, e.FilePath))
9192
}).ToList();
92-
LoadedGraphics = new BindingList<GraphicModel>(graphics);
93+
LoadedGraphics = new BindingList<DrawingModel>(graphics);
9394
}
9495

9596
private void LoadRecentProjects()

OpenBoardAnim/Utils/SVGHelper.cs OpenBoardAnim/Utils/GeometryHelper.cs

+27-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using System.Windows;
89
using System.Windows.Media;
910

1011
namespace OpenBoardAnim.Utils
@@ -22,11 +23,33 @@ public GeometryWithFill(Geometry geometry, Brush brush)
2223
public Brush Brush { get; set; }
2324
}
2425

25-
public class SVGHelper
26+
public class GeometryHelper
2627
{
27-
28+
public static PathGeometry ConvertTextToGeometry(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, double fontSize)
29+
{
30+
Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, FontStretches.Normal);
31+
// Create a formatted text
32+
FormattedText formattedText = new FormattedText(
33+
text,
34+
System.Globalization.CultureInfo.CurrentCulture,
35+
FlowDirection.LeftToRight,
36+
typeface,
37+
fontSize,
38+
Brushes.Black,
39+
VisualTreeHelper.GetDpi(Application.Current.MainWindow).PixelsPerDip);
40+
41+
// Create a geometry from the formatted text
42+
Geometry textGeometry = formattedText.BuildGeometry(new Point(0, 0));
43+
44+
// Convert to PathGeometry
45+
PathGeometry pathGeometry = PathGeometry.CreateFromGeometry(textGeometry);
46+
47+
return pathGeometry;
48+
}
49+
2850
public static DrawingGroup GetPathGeometryFromSVG(string filePath)
2951
{
52+
if(string.IsNullOrEmpty(filePath)) return null;
3053
var svgFileReader = new FileSvgReader(new WpfDrawingSettings());
3154
return svgFileReader.Read(filePath);
3255
}
@@ -44,15 +67,15 @@ public static List<GeometryWithFill> ConvertToGeometry(DrawingGroup drawingGroup
4467
Geometry geometry = geometryDrawing.Geometry;
4568
if (geometry.Transform != null)
4669
{
47-
clone.Children.Add(geometry.Transform);
70+
clone.Children.Insert(0,geometry.Transform);
4871
}
4972
geometry.Transform = clone;
5073
geometrylist.Add(new GeometryWithFill(geometry,geometryDrawing.Brush));
5174
}
5275
else if (drawing is DrawingGroup innerGroup)
5376
{
5477
if (innerGroup.Transform != null)
55-
clone.Children.Add(innerGroup.Transform);
78+
clone.Children.Insert(0, innerGroup.Transform);
5679
geometrylist.AddRange(ConvertToGeometry(innerGroup, clone));
5780
}
5881
}

OpenBoardAnim/Utils/PathAnimationExample.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class PathAnimationExample
1313
{
1414
private Canvas _canvas;
1515
private List<Path> _paths;
16-
private GraphicModel _graphic;
16+
private GraphicModelBase _graphic;
1717
private UIElement _hand;
1818
List<double> _lengths = new List<double>();
1919
private int i = 0;
2020
public TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
21-
public PathAnimationExample(Canvas canvas, List<Path> paths, GraphicModel graphic, UIElement hand)
21+
public PathAnimationExample(Canvas canvas, List<Path> paths, GraphicModelBase graphic, UIElement hand)
2222
{
2323
_canvas = canvas;
2424
_paths = paths;

OpenBoardAnim/ViewModels/EditorActionsViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ public SceneModel CurrentScene
8787
OnPropertyChanged(nameof(SceneGraphics));
8888
}
8989
}
90-
public BindingList<GraphicModel> SceneGraphics => CurrentScene?.Graphics;
90+
public BindingList<GraphicModelBase> SceneGraphics => CurrentScene?.Graphics;
9191
}
9292
}

0 commit comments

Comments
 (0)