Skip to content

Commit a2b20bc

Browse files
committed
更新了一堆特性(((
1 parent 86e7df2 commit a2b20bc

File tree

6 files changed

+600
-97
lines changed

6 files changed

+600
-97
lines changed

Danmakux/Fadein_1.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,29 @@ public static void Func5(GraphicHelper helper, string str,string alias, string p
205205
}, "cubic-bezier(0,.6,.4,1)");
206206
});
207207
}
208+
209+
//说明:横向向右弹出。
210+
public static void Func6(GraphicHelper helper, string str,string alias, string parent, TextProperty prop,
211+
float delay, float duration)
212+
{
213+
const int charDistance = 20;
214+
const float strokeOffset = 0.002f;
215+
const float charOffset = 0.006f;
216+
helper.AddText(str, alias, parent, prop, (p, noChar, noStroke) =>
217+
{
218+
p.x = 0;
219+
p.alpha = 0;
220+
}, (motion, p, noChar, noStroke) =>
221+
{
222+
var offset = strokeOffset * noStroke + charOffset * noChar ;
223+
motion.Apply(delay);
224+
motion.Apply(0.001f, new TextProperty {alpha = 1});
225+
motion.Apply(duration + offset, new TextProperty()
226+
{
227+
x = charDistance * noChar
228+
},"cubic-bezier(0,.8,.2,1)");
229+
});
230+
}
208231

209232
static void Func3()
210233
{

Danmakux/GraphicHelper.cs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GraphicHelper
1717

1818
private static Regex mediansPattern1 = new Regex(@"(.*),""medians"":\[\[\[.*\]\]\](.*)");
1919

20-
private StringBuilder builder = new StringBuilder();
20+
internal StringBuilder Builder = new StringBuilder();
2121
private List<char> preparedTxt = new List<char>();
2222

2323
private static (float, float) GetBoundingBoxOffset(GraphicInfo info)
@@ -119,34 +119,34 @@ public GraphicHelper(string dataPath)
119119

120120
public void Reset()
121121
{
122-
builder.Clear();
122+
Builder.Clear();
123123
preparedTxt.Clear();
124-
builder.Append(prev_template);
124+
Builder.Append(prev_template);
125125
}
126126

127127
public void DefParent(string alias, string parent, TextProperty prop)
128128
{
129-
builder.Append($"def text {alias} {{");
130-
builder.Append($"content=\" \" ");
131-
builder.Append($"anchorX=.5 ");
132-
builder.Append($"anchorY=.5 ");
133-
builder.Append($"fontSize=10% ");
134-
if (!string.IsNullOrEmpty(parent)) builder.Append($"parent=\"{parent}\" ");
129+
Builder.Append($"def text {alias} {{");
130+
Builder.Append($"content=\" \" ");
131+
Builder.Append($"anchorX=.5 ");
132+
Builder.Append($"anchorY=.5 ");
133+
Builder.Append($"fontSize=10% ");
134+
if (!string.IsNullOrEmpty(parent)) Builder.Append($"parent=\"{parent}\" ");
135135
if (prop != null)
136136
{
137-
if (prop.x != null) builder.Append($",x={prop.x}%");
138-
if (prop.y != null) builder.Append($",y={prop.y}%");
139-
if (prop.rotateX != null) builder.Append($",rotateX={prop.rotateX}");
140-
if (prop.rotateY != null) builder.Append($",rotateY={prop.rotateY}");
141-
if (prop.rotateZ != null) builder.Append($",rotateZ={prop.rotateZ}");
142-
if (prop.scale != null) builder.Append($",scale={prop.scale}");
143-
if (prop.zIndex != null) builder.Append($",zIndex={prop.zIndex}");
144-
if (prop.duration != null) builder.Append($",duration={prop.duration}s");
145-
if (prop.alpha != null) builder.Append($",alpha={prop.alpha}");
146-
if (prop.anchorX != null) builder.Append($",anchorX={prop.anchorX}");
147-
if (prop.anchorY != null) builder.Append($",anchorY={prop.anchorY}");
137+
if (prop.x != null) Builder.Append($",x={prop.x}%");
138+
if (prop.y != null) Builder.Append($",y={prop.y}%");
139+
if (prop.rotateX != null) Builder.Append($",rotateX={prop.rotateX}");
140+
if (prop.rotateY != null) Builder.Append($",rotateY={prop.rotateY}");
141+
if (prop.rotateZ != null) Builder.Append($",rotateZ={prop.rotateZ}");
142+
if (prop.scale != null) Builder.Append($",scale={prop.scale}");
143+
if (prop.zIndex != null) Builder.Append($",zIndex={prop.zIndex}");
144+
if (prop.duration != null) Builder.Append($",duration={prop.duration}s");
145+
if (prop.alpha != null) Builder.Append($",alpha={prop.alpha}");
146+
if (prop.anchorX != null) Builder.Append($",anchorX={prop.anchorX}");
147+
if (prop.anchorY != null) Builder.Append($",anchorY={prop.anchorY}");
148148

149-
builder.Append("}\n");
149+
Builder.Append("}\n");
150150
}
151151
}
152152

@@ -170,7 +170,7 @@ public void AddText(string txt, string alias, string parent, TextProperty prop,
170170
chIndex = preparedTxt.Count;
171171
preparedTxt.Add(ch);
172172
for (int partIndex = 0; partIndex < chGraphic.Strokes.Count; partIndex++)
173-
builder.Append($"let p{chIndex}_{partIndex}=pb{{d=\"{chGraphic.Strokes[partIndex]}\",alpha=0}}\n");
173+
Builder.Append($"let p{chIndex}_{partIndex}=pb{{d=\"{chGraphic.Strokes[partIndex]}\",alpha=0}}\n");
174174
//TODO:对应每一个笔画计算中点,使其支持XY轴旋转。
175175
}
176176
else
@@ -186,39 +186,39 @@ public void AddText(string txt, string alias, string parent, TextProperty prop,
186186
var containerName = $"{alias}_{chSeq}_t{partIndex}";
187187
var subContainerName = $"{alias}_{chSeq}_b{partIndex}";
188188

189-
builder.Append($"let {strokeName}=p{chIndex}_{partIndex}{{parent=\"{subContainerName}\" alpha=1");
189+
Builder.Append($"let {strokeName}=p{chIndex}_{partIndex}{{parent=\"{subContainerName}\" alpha=1");
190190
//builder.Append($"viewBox=\"{-chGraphic.Width / 2:F1} {-chGraphic.Height / 2:F1} {chGraphic.Width:F0} {chGraphic.Height:F0}\"");
191-
if (prop.borderAlpha != null) builder.Append($" borderAlpha={prop.borderAlpha}");
192-
if (prop.borderWidth != null) builder.Append($" borderWidth={prop.borderWidth}");
193-
if (!string.IsNullOrEmpty(prop.borderColor)) builder.Append($" borderColor={prop.borderColor}");
194-
if (!string.IsNullOrEmpty(prop.fillColor)) builder.Append($" fillColor={prop.fillColor}");
195-
if (prop.fillAlpha != null) builder.Append($" borderWidth={prop.fillAlpha}");
191+
if (prop.borderAlpha != null) Builder.Append($" borderAlpha={prop.borderAlpha}");
192+
if (prop.borderWidth != null) Builder.Append($" borderWidth={prop.borderWidth}");
193+
if (!string.IsNullOrEmpty(prop.borderColor)) Builder.Append($" borderColor={prop.borderColor}");
194+
if (!string.IsNullOrEmpty(prop.fillColor)) Builder.Append($" fillColor={prop.fillColor}");
195+
if (prop.fillAlpha != null) Builder.Append($" borderWidth={prop.fillAlpha}");
196196
//if (prop.width != null) builder.Append($" width={prop.width}%");
197197
//if (prop.height != null) builder.Append($" height={prop.height}%");
198-
builder.Append("}");
199-
builder.Append($"let {subContainerName} = cb{{parent=\"{containerName}\"");
198+
Builder.Append("}");
199+
Builder.Append($"let {subContainerName} = cb{{parent=\"{containerName}\"");
200200
//这里的原因是因为scale属性一般是放在第二层,用于缩放本身的内容。如果anchor属性放在第一层可能无法取得预期的效果?
201201
//感觉大概率会导致单个笔画的位置出错,先试试吧
202-
if (prop.anchorX != null) builder.Append($",anchorX={prop.anchorX}");
203-
if (prop.anchorY != null) builder.Append($",anchorY={prop.anchorY}");
204-
builder.Append($"}}");
205-
builder.Append($"let {containerName} = cb{{parent=\"{parent}\"");
202+
if (prop.anchorX != null) Builder.Append($",anchorX={prop.anchorX}");
203+
if (prop.anchorY != null) Builder.Append($",anchorY={prop.anchorY}");
204+
Builder.Append($"}}");
205+
Builder.Append($"let {containerName} = cb{{parent=\"{parent}\"");
206206

207207
//实际上因为anchor 移动的是文字,定位点依然在右上角,所以这里手动加上50%
208-
if (prop.x != null) builder.Append($",x={prop.x + 50}%");
209-
if (prop.y != null) builder.Append($",y={prop.y + 50}%");
210-
if (prop.rotateX != null) builder.Append($",rotateX={prop.rotateX}");
211-
if (prop.rotateY != null) builder.Append($",rotateY={prop.rotateY}");
212-
if (prop.rotateZ != null) builder.Append($",rotateZ={prop.rotateZ}");
213-
if (prop.scale != null) builder.Append($",scale={prop.scale}");
214-
if (prop.zIndex != null) builder.Append($",zIndex={prop.zIndex}");
215-
if (prop.duration != null) builder.Append($",duration={prop.duration}s");
216-
if (prop.alpha != null) builder.Append($",alpha={prop.alpha}");
208+
if (prop.x != null) Builder.Append($",x={prop.x + 50}%");
209+
if (prop.y != null) Builder.Append($",y={prop.y + 50}%");
210+
if (prop.rotateX != null) Builder.Append($",rotateX={prop.rotateX}");
211+
if (prop.rotateY != null) Builder.Append($",rotateY={prop.rotateY}");
212+
if (prop.rotateZ != null) Builder.Append($",rotateZ={prop.rotateZ}");
213+
if (prop.scale != null) Builder.Append($",scale={prop.scale}");
214+
if (prop.zIndex != null) Builder.Append($",zIndex={prop.zIndex}");
215+
if (prop.duration != null) Builder.Append($",duration={prop.duration}s");
216+
if (prop.alpha != null) Builder.Append($",alpha={prop.alpha}");
217217

218-
builder.Append("}\n");
218+
Builder.Append("}\n");
219219
if (onProcessMotion != null)
220220
{
221-
var motion = new MotionHelper(builder, containerName, subContainerName, strokeName);
221+
var motion = new MotionHelper(Builder, containerName, subContainerName, strokeName);
222222
onProcessMotion(motion, prop, chSeq, partIndex);
223223
motion.ProcessBackupLayer();
224224
}
@@ -231,9 +231,16 @@ public void AddText(string txt, string alias, string parent, TextProperty prop,
231231
}
232232
}
233233

234+
public void DefMotion(string dst, Action<MotionHelper> onProcessMotion)
235+
{
236+
var motion = new MotionHelper(Builder, dst, null, null);
237+
onProcessMotion?.Invoke(motion);
238+
motion.ProcessBackupLayer();
239+
}
240+
234241
public string GetResult()
235242
{
236-
return builder.ToString();
243+
return Builder.ToString();
237244
}
238245
}
239246
}

Danmakux/ImageHelper.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Xml;
6+
7+
namespace Danmakux
8+
{
9+
public class ImageHelper
10+
{
11+
12+
private string MoveToCenter(string svgInput)
13+
{
14+
var xOffset = Width / 2;
15+
var yOffset = Height / 2;
16+
17+
StringBuilder strBuilder = new StringBuilder();
18+
ClipHelper.SvgVisitor(svgInput, (cmd, x, y, c1X, c1Y, c2X, c2Y) =>
19+
{
20+
strBuilder.Append(cmd);
21+
x = (x - xOffset) * 1;
22+
c1X = (c1X - xOffset) * 1;
23+
c2X = (c2X - xOffset) * 1;
24+
y = (y - yOffset) * 1;
25+
c1Y = (c1Y - yOffset) * 1;
26+
c2Y = (c2Y - yOffset) * 1;
27+
switch (cmd)
28+
{
29+
case "Z":
30+
break;
31+
case "M":
32+
case "L":
33+
strBuilder.Append($"{x:F2} {y:F2}");
34+
break;
35+
case "Q":
36+
//c1 == c2
37+
strBuilder.Append($" {c1X:F2} {c1Y:F2}");
38+
strBuilder.Append($" {x:F2} {y:F2}");
39+
break;
40+
case "C":
41+
strBuilder.Append($" {c1X:F2} {c1Y:F2}");
42+
strBuilder.Append($" {c2X:F2} {c2Y:F2}");
43+
strBuilder.Append($" {x:F2} {y:F2}");
44+
break;
45+
default:
46+
throw new InvalidDataException();
47+
}
48+
});
49+
return strBuilder.ToString();
50+
}
51+
52+
public ImageHelper(string fileName)
53+
{
54+
XmlDocument doc = new XmlDocument();
55+
doc.Load(fileName);
56+
XmlNode svgInfo = doc.GetElementsByTagName("svg")[0];
57+
Width = float.Parse(svgInfo.Attributes["width"].Value.Replace("pt",""));
58+
Height = float.Parse(svgInfo.Attributes["height"].Value.Replace("pt",""));
59+
foreach (XmlNode node in doc.GetElementsByTagName("path"))
60+
{
61+
var color = node?.Attributes["fill"]?.Value;
62+
var path = node?.Attributes["d"]?.Value;
63+
var opacity = node?.Attributes["opacity"]?.Value;
64+
if (!string.IsNullOrEmpty(color) && !string.IsNullOrEmpty(path))
65+
{
66+
path = MoveToCenter(path);
67+
FillDatas.Add(new FillData()
68+
{
69+
FillColor = color?.Replace("#", "0x"),
70+
Path = path,
71+
Opacity = opacity
72+
});
73+
}
74+
}
75+
}
76+
77+
public void AddImage(GraphicHelper helper, string parent, TextProperty prop,
78+
Action<MotionHelper> onProcessMotion = null)
79+
{
80+
var alias = parent;
81+
var builder = helper.Builder;
82+
var containerName = $"{alias}_i";
83+
var subContainerName = $"{alias}_iX";
84+
builder.Append(
85+
"def text zb {content = \" \" fontSize = 10% anchorX=.5 anchorY=.5 duration=999s x=50% y=50%}");
86+
builder.Append($"let {subContainerName} = zb{{parent=\"{containerName}\"");
87+
//这里的原因是因为scale属性一般是放在第二层,用于缩放本身的内容。如果anchor属性放在第一层可能无法取得预期的效果?
88+
//感觉大概率会导致单个笔画的位置出错,先试试吧
89+
if (prop.anchorX != null) builder.Append($",anchorX={prop.anchorX}");
90+
if (prop.anchorY != null) builder.Append($",anchorY={prop.anchorY}");
91+
builder.Append($"}}");
92+
builder.Append($"let {containerName} = zb{{parent=\"{parent}\"");
93+
94+
//这里直接集成GraphicHelper的zb定义。
95+
if (prop.x != null) builder.Append($",x={prop.x + 50}%");
96+
if (prop.y != null) builder.Append($",y={prop.y + 50}%");
97+
if (prop.rotateX != null) builder.Append($",rotateX={prop.rotateX}");
98+
if (prop.rotateY != null) builder.Append($",rotateY={prop.rotateY}");
99+
if (prop.rotateZ != null) builder.Append($",rotateZ={prop.rotateZ}");
100+
if (prop.scale != null) builder.Append($",scale={prop.scale}");
101+
if (prop.zIndex != null) builder.Append($",zIndex={prop.zIndex}");
102+
if (prop.duration != null) builder.Append($",duration={prop.duration}s");
103+
if (prop.alpha != null) builder.Append($",alpha={prop.alpha}");
104+
105+
builder.Append("}\n");
106+
if (onProcessMotion != null)
107+
{
108+
var motion = new MotionHelper(builder, containerName, subContainerName, "");
109+
onProcessMotion(motion);
110+
motion.ProcessBackupLayer();
111+
}
112+
113+
builder.Append(
114+
$"def path {alias}_b {{width=100% x={-Width / 2 + 50:F1}% y={-Height / 2 + 50:F1}% viewBox=\"{-Width / 2:F1} {-Height / 2:F1} {Width:F1} {Height:F1}\" fillAlpha = 1 duration=999s}}\n");
115+
116+
int seq = 0;
117+
foreach (var fillItem in FillDatas)
118+
{
119+
var strokeName = $"{alias}_{seq}";
120+
builder.Append($"let {strokeName}={alias}_b{{parent=\"{subContainerName}\" alpha={fillItem.Opacity} fillColor={fillItem.FillColor}");
121+
builder.Append($" d=\"{fillItem.Path}\"}}\n");
122+
seq++;
123+
}
124+
125+
}
126+
127+
public class FillData
128+
{
129+
public string FillColor { get; set; }
130+
public string Path { get; set; }
131+
132+
public string Opacity { get; set; }
133+
}
134+
135+
public float Width { get; set; }
136+
public float Height { get; set; }
137+
138+
public List<FillData> FillDatas { get; } = new List<FillData>();
139+
}
140+
}

0 commit comments

Comments
 (0)