Skip to content

Commit 9ab0eb5

Browse files
committed
代码重构
1 parent 0a0c773 commit 9ab0eb5

File tree

9 files changed

+148
-50
lines changed

9 files changed

+148
-50
lines changed

DeskSpine/DeskSpine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<None Update="res\image\timealarm.ico">
28+
<None Update="res\image\hourlychime.ico">
2929
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3030
</None>
3131
<None Update="res\spine\ArknightsDynamic\skadi2\dyn_illust_char_1012_skadi2.atlas">

DeskSpine/src/AboutForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void linkLabel_RepoUrl_LinkClicked(object sender, LinkLabelLinkClickedEv
119119
else
120120
{
121121
Clipboard.SetText(url);
122-
MessageBox.Show("链接已复制到剪贴板,请前往浏览器进行访问", "复制成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
122+
MessageBox.Show(this, "链接已复制到剪贴板,请前往浏览器进行访问", "复制成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
123123
}
124124
}
125125
}

DeskSpine/src/Config.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SystemConfig
5050
public bool AutuRun { get; set; } = false;
5151
public bool Visible { get; set; } = true;
5252
public string? BalloonIconPath { get; set; }
53-
public bool TimeAlarm = true;
53+
public bool HourlyChime = true;
5454
}
5555

5656
/// <summary>
@@ -60,19 +60,18 @@ public class BasicConfig
6060
{
6161
public bool WallpaperMode { get; set; } = false;
6262
public bool MouseClickThrough { get; set; } = false;
63-
64-
[JsonConverter(typeof(BackgroundColorConverter))] // 结构体不会自动保存字段, 需要用自定义的转换器
65-
public SFML.Graphics.Color BackgroundColor { get; set; } = new(128, 128, 128, 0);
6663
public int PositionX { get; set; } = 0;
6764
public int PositionY { get; set; } = 0;
6865
public uint SizeX { get; set; } = 1000;
6966
public uint SizeY { get; set; } = 1000;
70-
public byte Opacity { get; set; } = 255;
71-
public uint MaxFps { get; set; } = 30;
7267
public float SpinePositionX { get; set; } = 0;
7368
public float SpinePositionY { get; set; } = 0;
74-
public float SpineScale { get; set; } = 1f;
7569
public bool SpineFlip { get; set; } = false;
70+
public float SpineScale { get; set; } = 1f;
71+
public byte Opacity { get; set; } = 255;
72+
public uint MaxFps { get; set; } = 30;
73+
[JsonConverter(typeof(BackgroundColorConverter))] // 结构体不会自动保存字段, 需要用自定义的转换器
74+
public SFML.Graphics.Color BackgroundColor { get; set; } = new(128, 128, 128, 0);
7675
public bool SpineUsePMA { get; set; } = false;
7776

7877
}
@@ -130,7 +129,7 @@ public class Config
130129
/// </summary>
131130
public Config()
132131
{
133-
SystemConfig.BalloonIconPath = Path.Combine(Program.ProgramResourceDirectory, @"image\timealarm.ico");
132+
SystemConfig.BalloonIconPath = Path.Combine(Program.ProgramResourceDirectory, @"image\hourlychime.ico");
134133
SpineConfig.SkelPath0 = Path.Combine(Program.ProgramResourceDirectory, @"spine\AzueLaneSD\guanghui_2\guanghui_2.skel");
135134
}
136135

DeskSpine/src/ConfigForm.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ConfigForm()
3636
shellNotifyIcon = new(notifyIcon);
3737
var resources = new System.ComponentModel.ComponentResourceManager(typeof(ConfigForm));
3838
notifyIcon.Icon = (Icon?)(SystemValue.SystemUseLightTheme ? resources.GetObject("$this.Icon") : resources.GetObject("notifyIcon.Icon"));
39-
hourlyChimeTimer.Elapsed += TimeAlarmTimer_Elapsed;
39+
hourlyChimeTimer.Elapsed += HourlyChimeTimer_Elapsed;
4040
}
4141

4242
/// <summary>
@@ -66,7 +66,7 @@ public string? BalloonIconPath
6666
}
6767
catch (Exception ex)
6868
{
69-
MessageBox.Show($"{value} 加载失败\n\n{ex}", "气泡图标资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
69+
MessageBox.Show(this, $"{value} 加载失败\n\n{ex}", "气泡图标资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
7070
}
7171
}
7272
}
@@ -87,7 +87,7 @@ public Config Value
8787
v.SystemConfig.AutuRun = checkBox_AutoRun.Checked;
8888
v.SystemConfig.Visible = checkBox_Visible.Checked;
8989
v.SystemConfig.BalloonIconPath = string.IsNullOrEmpty(textBox_BalloonIconPath.Text) ? null : textBox_BalloonIconPath.Text;
90-
v.SystemConfig.TimeAlarm = checkBox_HourlyChime.Checked;
90+
v.SystemConfig.HourlyChime = checkBox_HourlyChime.Checked;
9191

9292
// 基础设置
9393
v.BasicConfig.WallpaperMode = checkBox_WallpaperMode.Checked;
@@ -102,11 +102,11 @@ public Config Value
102102
v.BasicConfig.SpineScale = trackBar_SpineScale.Value / 100.0f;
103103
v.BasicConfig.Opacity = (byte)trackBar_Opacity.Value;
104104
v.BasicConfig.MaxFps = (uint)trackBar_MaxFps.Value;
105-
v.BasicConfig.SpineUsePMA = checkBox_SpineUsePMA.Checked;
106105
v.BasicConfig.BackgroundColor = new((byte)numericUpDown_BackgroundColorR.Value,
107106
(byte)numericUpDown_BackgroundColorG.Value,
108107
(byte)numericUpDown_BackgroundColorB.Value,
109108
0);
109+
v.BasicConfig.SpineUsePMA = checkBox_SpineUsePMA.Checked;
110110

111111
// Spine 设置
112112
v.SpineConfig.SpineVersion = (Spine.SpineVersion)comboBox_SpineVersion.SelectedValue;
@@ -133,7 +133,7 @@ public Config Value
133133
checkBox_AutoRun.Checked = value.SystemConfig.AutuRun;
134134
checkBox_Visible.Checked = value.SystemConfig.Visible;
135135
textBox_BalloonIconPath.Text = value.SystemConfig.BalloonIconPath;
136-
checkBox_HourlyChime.Checked = value.SystemConfig.TimeAlarm;
136+
checkBox_HourlyChime.Checked = value.SystemConfig.HourlyChime;
137137

138138
// 基础设置
139139
checkBox_WallpaperMode.Checked = value.BasicConfig.WallpaperMode;
@@ -148,10 +148,10 @@ public Config Value
148148
trackBar_SpineScale.Value = (int)(value.BasicConfig.SpineScale * 100);
149149
trackBar_Opacity.Value = value.BasicConfig.Opacity;
150150
trackBar_MaxFps.Value = (int)value.BasicConfig.MaxFps;
151-
checkBox_SpineUsePMA.Checked = value.BasicConfig.SpineUsePMA;
152151
numericUpDown_BackgroundColorR.Value = value.BasicConfig.BackgroundColor.R;
153152
numericUpDown_BackgroundColorG.Value = value.BasicConfig.BackgroundColor.G;
154153
numericUpDown_BackgroundColorB.Value = value.BasicConfig.BackgroundColor.B;
154+
checkBox_SpineUsePMA.Checked = value.BasicConfig.SpineUsePMA;
155155

156156
// Spine 设置
157157
comboBox_SpineVersion.SelectedValue = value.SpineConfig.SpineVersion;
@@ -250,8 +250,14 @@ private void ConfigForm_FormClosing(object? sender, FormClosingEventArgs e)
250250

251251
private void button_OpenDataFolder_Click(object sender, EventArgs e)
252252
{
253-
try { Process.Start("explorer.exe", Program.ProgramDataDirectory); }
254-
catch (Exception ex) { MessageBox.Show($"无法打开文件夹: {ex.Message}", Program.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Error); }
253+
try
254+
{
255+
Process.Start("explorer.exe", Program.ProgramDataDirectory);
256+
}
257+
catch (Exception ex)
258+
{
259+
MessageBox.Show(this, $"无法打开文件夹: {ex.Message}", Program.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Error);
260+
}
255261
}
256262

257263
private void button_Ok_Click(object sender, EventArgs e)
@@ -266,7 +272,7 @@ private void button_Apply_Click(object sender, EventArgs e)
266272
Value = Program.CurrentConfig; // 刷新页面值
267273
}
268274

269-
private void TimeAlarmTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
275+
private void HourlyChimeTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
270276
{
271277
DateTime now = DateTime.Now;
272278
if (now.Minute == 0)
@@ -380,19 +386,15 @@ private void commandSetFullScreen_Click(object sender, EventArgs e)
380386
var screenBounds = Screen.FromHandle(Program.SpineWindow.Handle).Bounds;
381387
var screenPosition = screenBounds.Location;
382388
var screenSize = screenBounds.Size;
383-
var currentConfig = Program.CurrentConfig;
384389
Program.SpineWindow.Position = new(screenPosition.X, screenPosition.Y);
385390
Program.SpineWindow.Size = new((uint)screenSize.Width, (uint)screenSize.Height);
386-
currentConfig.BasicConfig.PositionX = screenPosition.X;
387-
currentConfig.BasicConfig.PositionY = screenPosition.Y;
388-
currentConfig.BasicConfig.SizeX = (uint)screenSize.Width;
389-
currentConfig.BasicConfig.SizeY = (uint)screenSize.Height;
390-
Program.LocalConfig = currentConfig;
391+
Program.LocalConfig = Program.CurrentConfig;
391392
}
392393

393394
private void commandResetSpine_Click(object sender, EventArgs e)
394395
{
395396
Program.SpineWindow.ResetPositionAndSize();
397+
Program.LocalConfig = Program.CurrentConfig;
396398
}
397399

398400
private void commandSpineTool_Click(object sender, EventArgs e)
@@ -404,7 +406,7 @@ private void commandSpineTool_Click(object sender, EventArgs e)
404406
}
405407
catch (Exception ex)
406408
{
407-
MessageBox.Show($"SpineTool 启动失败\n\n{ex}", Program.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Error);
409+
MessageBox.Show(this, $"SpineTool 启动失败\n\n{ex}", Program.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Error);
408410
}
409411
}
410412

@@ -443,7 +445,7 @@ private void button_SelectBalloonIconPath_Click(object sender, EventArgs e)
443445

444446
private void label_BalloonIcon_Click(object sender, EventArgs e)
445447
{
446-
ShowBalloonTip("这是现在的图标噢", "叮~~~");
448+
ShowBalloonTip("叮——", "这是现在的图标噢");
447449
}
448450

449451
#endregion

DeskSpine/src/Program.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static Config CurrentConfig
107107
config.SystemConfig.AutuRun = AutoRun;
108108
config.SystemConfig.Visible = SpineWindow.Visible;
109109
config.SystemConfig.BalloonIconPath = ConfigForm.BalloonIconPath;
110-
config.SystemConfig.TimeAlarm = ConfigForm.HourlyChime;
110+
config.SystemConfig.HourlyChime = ConfigForm.HourlyChime;
111111

112112
// 基础设置
113113
var position = SpineWindow.Position;
@@ -141,10 +141,12 @@ public static Config CurrentConfig
141141

142142
set
143143
{
144+
// XXX: 不确定是否需要 Lock/Unlock
145+
144146
// 系统设置
145147
AutoRun = value.SystemConfig.AutuRun;
146148
ConfigForm.BalloonIconPath = value.SystemConfig.BalloonIconPath;
147-
ConfigForm.HourlyChime = value.SystemConfig.TimeAlarm;
149+
ConfigForm.HourlyChime = value.SystemConfig.HourlyChime;
148150

149151
// 基础设置
150152
SpineWindow.WallpaperMode = value.BasicConfig.WallpaperMode;
@@ -161,14 +163,32 @@ public static Config CurrentConfig
161163

162164
// Spine 设置
163165
SpineWindow.Spine.AnimatorType = value.SpineConfig.InteractMode;
164-
try { SpineWindow.Spine.Version = value.SpineConfig.SpineVersion; }
165-
catch (Exception ex) { MessageBox.Show($"{value.SpineConfig.SpineVersion} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); }
166+
try
167+
{
168+
SpineWindow.Spine.Version = value.SpineConfig.SpineVersion;
169+
}
170+
catch (Exception ex)
171+
{
172+
MessageBox.Show(
173+
ConfigForm,
174+
$"版本 {value.SpineConfig.SpineVersion} 加载失败\n\n{ex}",
175+
"Spine 资源加载失败",
176+
MessageBoxButtons.OK,
177+
MessageBoxIcon.Error
178+
);
179+
}
166180

167181
for (int i = 0; i < SpineConfig.SlotCount; i++)
168182
{
169183
var skelPath = value.SpineConfig.GetSkelPath(i);
170-
try { SpineWindow.Spine.SetSpine(skelPath, i); }
171-
catch (Exception ex) { MessageBox.Show($"{skelPath} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); }
184+
try
185+
{
186+
SpineWindow.Spine.SetSpine(skelPath, i);
187+
}
188+
catch (Exception ex)
189+
{
190+
MessageBox.Show(ConfigForm, $"{skelPath} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
191+
}
172192
}
173193

174194
// 最后调整窗口可见性
@@ -195,7 +215,7 @@ private static void InitFromConfig(Config value)
195215
{
196216
// 系统配置
197217
ConfigForm.BalloonIconPath = value.SystemConfig.BalloonIconPath;
198-
ConfigForm.HourlyChime = value.SystemConfig.TimeAlarm;
218+
ConfigForm.HourlyChime = value.SystemConfig.HourlyChime;
199219

200220
// 基础配置
201221
SpineWindow.WallpaperMode = value.BasicConfig.WallpaperMode;
@@ -212,14 +232,32 @@ private static void InitFromConfig(Config value)
212232

213233
// Spine 设置
214234
SpineWindow.Spine.AnimatorType = value.SpineConfig.InteractMode;
215-
try { SpineWindow.Spine.Version = value.SpineConfig.SpineVersion; }
216-
catch (Exception ex) { MessageBox.Show($"{value.SpineConfig.SpineVersion} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); }
235+
try
236+
{
237+
SpineWindow.Spine.Version = value.SpineConfig.SpineVersion;
238+
}
239+
catch (Exception ex)
240+
{
241+
MessageBox.Show(
242+
ConfigForm,
243+
$"版本 {value.SpineConfig.SpineVersion} 加载失败\n\n{ex}",
244+
"Spine 资源加载失败",
245+
MessageBoxButtons.OK,
246+
MessageBoxIcon.Error
247+
);
248+
}
217249

218250
for (int i = 0; i < SpineConfig.SlotCount; i++)
219251
{
220252
var skelPath = value.SpineConfig.GetSkelPath(i);
221-
try { SpineWindow.Spine.SetSpine(skelPath, i); }
222-
catch (Exception ex) { MessageBox.Show($"{skelPath} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); }
253+
try
254+
{
255+
SpineWindow.Spine.SetSpine(skelPath, i);
256+
}
257+
catch (Exception ex)
258+
{
259+
MessageBox.Show(ConfigForm, $"{skelPath} 加载失败\n\n{ex}", "Spine 资源加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
260+
}
223261
}
224262

225263
// 最后调整窗口可见性

TinyEngine/src/Animator.cs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ internal class AzurLaneSD : Animator
6161
{
6262
private State state = State.Idle;
6363
private State previousState = State.Idle;
64-
private Random rnd = new();
6564

66-
private float standElapsedTime = 0f;
65+
private Random rnd = new();
66+
private float randomElapsedTime = 0f;
6767
private const float meanWaitingTime = 30f;
68-
private float nextStandTime = 10f;
68+
private float nextRandomTime = 10f;
6969

7070
private const string Animation_Idle = "normal";
7171
private const string Animation_Dragging = "tuozhuai";
@@ -86,12 +86,12 @@ public override void Update(float delta)
8686

8787
if (slots[0] is null) return;
8888

89-
standElapsedTime += delta;
90-
if (standElapsedTime >= nextStandTime)
89+
randomElapsedTime += delta;
90+
if (randomElapsedTime >= nextRandomTime)
9191
{
92-
standElapsedTime = 0;
93-
nextStandTime = (float)(-meanWaitingTime * Math.Log(rnd.NextSingle()));
94-
Debug.WriteLine($"Next time to stand: {nextStandTime}");
92+
randomElapsedTime = 0;
93+
nextRandomTime = (float)(-meanWaitingTime * Math.Log(rnd.NextSingle()));
94+
Debug.WriteLine($"Next time to stand: {nextRandomTime}");
9595
if (state == State.Idle)
9696
{
9797
if (slots[0].CurrentAnimation == Animation_Idle)
@@ -282,4 +282,54 @@ public override void Click(SFML.Window.Mouse.Button button)
282282
}
283283
}
284284
}
285+
286+
/// <summary>
287+
/// 明日方舟动态立绘
288+
/// </summary>
289+
internal class ArknightsDynamic : Animator
290+
{
291+
private Random rnd = new();
292+
private float randomElapsedTime = 0f;
293+
private const float meanWaitingTime = 30f;
294+
private float nextRandomTime = 10f;
295+
296+
private const string Animation_Idle = "Idle";
297+
private const string Animation_MouseLeftClick = "Interact";
298+
private const string Animation_Random = "Special";
299+
300+
public ArknightsDynamic(Spine.Spine[] slots) : base(slots) { }
301+
302+
public override void Reset() { foreach (var sp in slots) if (sp is not null) sp.CurrentAnimation = Animation_Idle; }
303+
304+
public override void Update(float delta)
305+
{
306+
base.Update(delta);
307+
if (slots[0] is null) return;
308+
309+
randomElapsedTime += delta;
310+
if (randomElapsedTime >= nextRandomTime)
311+
{
312+
randomElapsedTime = 0;
313+
nextRandomTime = (float)(-meanWaitingTime * Math.Log(rnd.NextSingle()));
314+
Debug.WriteLine($"Next time to stand: {nextRandomTime}");
315+
if (slots[0].CurrentAnimation == Animation_Idle)
316+
{
317+
slots[0].CurrentAnimation = Animation_Random;
318+
slots[0].AddAnimation(Animation_Idle);
319+
}
320+
}
321+
}
322+
323+
public override void Click(SFML.Window.Mouse.Button button)
324+
{
325+
base.Click(button);
326+
if (slots[0] is null) return;
327+
328+
if (button == SFML.Window.Mouse.Button.Left && slots[0].CurrentAnimation != Animation_MouseLeftClick)
329+
{
330+
slots[0].CurrentAnimation = Animation_MouseLeftClick;
331+
slots[0].AddAnimation(Animation_Idle);
332+
}
333+
}
334+
}
285335
}

0 commit comments

Comments
 (0)