Repository navigation
Expand file tree
/
Copy pathFormatBlockEditorForm.cs
More file actions
265 lines (244 loc) · 8.59 KB
/
Copy pathFormatBlockEditorForm.cs
File metadata and controls
265 lines (244 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MuSync.Models;
using MuSync.Utils;
namespace MuSync;
/// <summary>积木编辑器编辑的模板种类:音乐 / 程序 / 组合。</summary>
internal enum TemplateKind
{
Music,
Program,
Combined
}
/// <summary>
/// 模板积木编辑器:自由添加 / 排序 / 删除块,每块可选择类型
/// (歌名、歌手、进度条、程序名、分隔符、自定义文字),实时预览好友可见效果。
/// </summary>
internal sealed class FormatBlockEditorForm : Form
{
private static readonly string[] TypeNames =
[
Loc.L("歌名", "Song"), Loc.L("歌手", "Artist (auto separator)"),
Loc.L("歌手(无连接符)", "Artist (no separator)"),
Loc.L("进度条", "Progress bar"), Loc.L("程序名", "App name"),
Loc.L("分隔符", "Separator"), Loc.L("自定义文字", "Custom text")
];
private static readonly FormatBlockType[] TypeValues =
[
FormatBlockType.Song, FormatBlockType.ArtistPart, FormatBlockType.Artist,
FormatBlockType.Progress, FormatBlockType.App, FormatBlockType.Sep, FormatBlockType.Custom
];
private readonly TemplateKind _kind;
private readonly string _separator;
private readonly List<FormatBlock> _blocks;
private Panel _rowsPanel = null!;
private Label _previewLabel = null!;
public string ResultFormat { get; private set; } = "";
public FormatBlockEditorForm(TemplateKind kind, string initialFormat, string separator)
{
_kind = kind;
_separator = separator;
_blocks = FormatBlockCodec.Parse(initialFormat);
InitializeComponent();
RebuildRows();
UpdatePreview();
}
private void InitializeComponent()
{
Text = _kind switch
{
TemplateKind.Music => Loc.L("编辑「音乐」模板 — 积木编辑", "Edit \"Music\" template — Block editor"),
TemplateKind.Program => Loc.L("编辑「程序」模板 — 积木编辑", "Edit \"App\" template — Block editor"),
_ => Loc.L("编辑「组合」模板 — 积木编辑", "Edit \"Combined\" template — Block editor")
};
Size = new Size(560, 520);
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
Font = new Font("Microsoft YaHei", 9);
_rowsPanel = new Panel
{
Location = new Point(12, 12),
Size = new Size(524, 290),
AutoScroll = true,
BackColor = Color.White,
BorderStyle = BorderStyle.FixedSingle
};
var addButton = new Button
{
Text = Loc.L("+ 添加积木", "+ Add block"),
Location = new Point(12, 310),
Size = new Size(110, 28),
BackColor = Color.White
};
addButton.Click += (_, _) =>
{
_blocks.Add(new FormatBlock { Type = FormatBlockType.Custom });
RebuildRows();
UpdatePreview();
};
_previewLabel = new Label
{
Location = new Point(12, 346),
Size = new Size(524, 40),
ForeColor = Color.FromArgb(0, 102, 51)
};
var okButton = new Button
{
Text = Loc.L("确定", "OK"),
Location = new Point(400, 440),
Size = new Size(64, 28),
BackColor = Color.White,
Anchor = AnchorStyles.Bottom | AnchorStyles.Right
};
okButton.Click += (_, _) =>
{
ResultFormat = FormatBlockCodec.Compile(_blocks);
DialogResult = DialogResult.OK;
Close();
};
var cancelButton = new Button
{
Text = Loc.L("取消", "Cancel"),
Location = new Point(472, 440),
Size = new Size(64, 28),
BackColor = Color.White,
Anchor = AnchorStyles.Bottom | AnchorStyles.Right
};
cancelButton.Click += (_, _) =>
{
DialogResult = DialogResult.Cancel;
Close();
};
Controls.AddRange(_rowsPanel, addButton, _previewLabel, okButton, cancelButton);
AcceptButton = okButton;
CancelButton = cancelButton;
}
private void RebuildRows()
{
_rowsPanel.SuspendLayout();
_rowsPanel.Controls.Clear();
var y = 6;
for (var i = 0; i < _blocks.Count; i++)
{
var index = i;
var block = _blocks[i];
var typeCombo = new ComboBox
{
Location = new Point(8, y),
Width = 135,
DropDownStyle = ComboBoxStyle.DropDownList
};
typeCombo.Items.AddRange(TypeNames);
typeCombo.SelectedIndex = Array.IndexOf(TypeValues, block.Type);
if (typeCombo.SelectedIndex < 0)
{
typeCombo.SelectedIndex = TypeValues.Length - 1;
}
typeCombo.SelectedIndexChanged += (_, _) =>
{
var newType = TypeValues[typeCombo.SelectedIndex];
block.Type = newType;
if (newType != FormatBlockType.Custom)
{
block.Text = "";
}
RebuildRows();
UpdatePreview();
};
var textBox = new TextBox
{
Location = new Point(148, y),
Width = 232,
Text = block.Text,
Enabled = block.Type == FormatBlockType.Custom
};
textBox.TextChanged += (_, _) =>
{
block.Text = textBox.Text;
UpdatePreview();
};
var upButton = new Button
{
Text = "↑",
Location = new Point(386, y - 1),
Size = new Size(30, 26),
Enabled = i > 0
};
upButton.Click += (_, _) => MoveBlock(index, -1);
var downButton = new Button
{
Text = "↓",
Location = new Point(420, y - 1),
Size = new Size(30, 26),
Enabled = i < _blocks.Count - 1
};
downButton.Click += (_, _) => MoveBlock(index, +1);
var delButton = new Button
{
Text = "✕",
Location = new Point(454, y - 1),
Size = new Size(30, 26)
};
delButton.Click += (_, _) =>
{
_blocks.RemoveAt(index);
RebuildRows();
UpdatePreview();
};
_rowsPanel.Controls.AddRange(typeCombo, textBox, upButton, downButton, delButton);
y += 32;
}
_rowsPanel.ResumeLayout();
}
private void MoveBlock(int index, int delta)
{
var target = index + delta;
if (target < 0 || target >= _blocks.Count) return;
(_blocks[index], _blocks[target]) = (_blocks[target], _blocks[index]);
RebuildRows();
UpdatePreview();
}
private void UpdatePreview()
{
if (_previewLabel == null) return;
var compiled = FormatBlockCodec.Compile(_blocks);
var dummySong = new PlayerInfo
{
Identity = "preview",
Title = Loc.L("稻香", "Sunflower"),
Artists = Loc.L("周杰伦", "Post Malone"),
Album = Loc.L("魔杰座", "Spider-Man: Into the Spider-Verse"),
Cover = "",
Schedule = 150,
Duration = 255,
Url = "",
Pause = false
};
var config = new ConfigData
{
CombinedSeparator = _separator,
HideMusicWhenPaused = false
};
string? text;
switch (_kind)
{
case TemplateKind.Music:
config.MusicFormat = compiled;
text = SteamStatusManager.ComposeStatus(dummySong, null, config);
break;
case TemplateKind.Program:
config.ProgramFormat = compiled;
text = SteamStatusManager.ComposeStatus(null, Loc.L("卡拉彼丘", "Strinova"), config);
break;
default:
config.CombinedFormat = compiled;
text = SteamStatusManager.ComposeStatus(dummySong, Loc.L("卡拉彼丘", "Strinova"), config);
break;
}
_previewLabel.Text = Loc.L($"好友看到:{text ?? "(无内容)"}", $"Friends see: {text ?? "(None)"}");
}
}