From fc0d69ba1ba4a3bd33e9deb6c801a7699bdfb730 Mon Sep 17 00:00:00 2001 From: ww-rm Date: Wed, 26 Feb 2025 21:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpineViewer/src/SpineListView.Designer.cs | 219 +++++++--------------- SpineViewer/src/SpineListView.cs | 183 ++++++++---------- SpineViewer/src/SpineListView.resx | 53 +----- 3 files changed, 150 insertions(+), 305 deletions(-) diff --git a/SpineViewer/src/SpineListView.Designer.cs b/SpineViewer/src/SpineListView.Designer.cs index ee84afc..61f7eec 100644 --- a/SpineViewer/src/SpineListView.Designer.cs +++ b/SpineViewer/src/SpineListView.Designer.cs @@ -29,153 +29,31 @@ private void InitializeComponent() { components = new System.ComponentModel.Container(); - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SpineListView)); - tableLayoutPanel = new TableLayoutPanel(); - flowLayoutPanel_Buttons = new FlowLayoutPanel(); - button_Add = new Button(); - button_Remove = new Button(); - button_MoveUp = new Button(); - button_MoveDown = new Button(); - button_Insert = new Button(); - button_RemoveAll = new Button(); listView = new ListView(); columnHeader_Name = new ColumnHeader(); columnHeader_Version = new ColumnHeader(); - toolTip = new ToolTip(components); - tableLayoutPanel.SuspendLayout(); - flowLayoutPanel_Buttons.SuspendLayout(); + contextMenuStrip = new ContextMenuStrip(components); + toolStripMenuItem_Add = new ToolStripMenuItem(); + toolStripMenuItem_Insert = new ToolStripMenuItem(); + toolStripMenuItem_Remove = new ToolStripMenuItem(); + toolStripSeparator1 = new ToolStripSeparator(); + toolStripMenuItem_RemoveAll = new ToolStripMenuItem(); + toolStripMenuItem_BatchAdd = new ToolStripMenuItem(); + contextMenuStrip.SuspendLayout(); SuspendLayout(); // - // tableLayoutPanel - // - tableLayoutPanel.ColumnCount = 1; - tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); - tableLayoutPanel.Controls.Add(flowLayoutPanel_Buttons, 0, 0); - tableLayoutPanel.Controls.Add(listView, 0, 1); - tableLayoutPanel.Dock = DockStyle.Fill; - tableLayoutPanel.Location = new Point(0, 0); - tableLayoutPanel.Margin = new Padding(0); - tableLayoutPanel.Name = "tableLayoutPanel"; - tableLayoutPanel.RowCount = 2; - tableLayoutPanel.RowStyles.Add(new RowStyle()); - tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); - tableLayoutPanel.Size = new Size(336, 445); - tableLayoutPanel.TabIndex = 2; - // - // flowLayoutPanel_Buttons - // - flowLayoutPanel_Buttons.AutoSize = true; - flowLayoutPanel_Buttons.Controls.Add(button_Add); - flowLayoutPanel_Buttons.Controls.Add(button_Remove); - flowLayoutPanel_Buttons.Controls.Add(button_MoveUp); - flowLayoutPanel_Buttons.Controls.Add(button_MoveDown); - flowLayoutPanel_Buttons.Controls.Add(button_Insert); - flowLayoutPanel_Buttons.Controls.Add(button_RemoveAll); - flowLayoutPanel_Buttons.Dock = DockStyle.Fill; - flowLayoutPanel_Buttons.Location = new Point(3, 3); - flowLayoutPanel_Buttons.Name = "flowLayoutPanel_Buttons"; - flowLayoutPanel_Buttons.Size = new Size(330, 40); - flowLayoutPanel_Buttons.TabIndex = 4; - // - // button_Add - // - button_Add.Anchor = AnchorStyles.None; - button_Add.AutoSize = true; - button_Add.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_Add.Image = (Image)resources.GetObject("button_Add.Image"); - button_Add.Location = new Point(3, 3); - button_Add.Name = "button_Add"; - button_Add.Size = new Size(34, 34); - button_Add.TabIndex = 0; - toolTip.SetToolTip(button_Add, "在末尾添加项"); - button_Add.UseVisualStyleBackColor = true; - button_Add.Click += button_Add_Click; - // - // button_Remove - // - button_Remove.Anchor = AnchorStyles.None; - button_Remove.AutoSize = true; - button_Remove.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_Remove.Enabled = false; - button_Remove.Image = (Image)resources.GetObject("button_Remove.Image"); - button_Remove.Location = new Point(43, 3); - button_Remove.Name = "button_Remove"; - button_Remove.Size = new Size(34, 34); - button_Remove.TabIndex = 1; - toolTip.SetToolTip(button_Remove, "移除选中项"); - button_Remove.UseVisualStyleBackColor = true; - button_Remove.Click += button_Remove_Click; - // - // button_MoveUp - // - button_MoveUp.Anchor = AnchorStyles.None; - button_MoveUp.AutoSize = true; - button_MoveUp.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_MoveUp.Enabled = false; - button_MoveUp.Image = (Image)resources.GetObject("button_MoveUp.Image"); - button_MoveUp.Location = new Point(83, 3); - button_MoveUp.Name = "button_MoveUp"; - button_MoveUp.Size = new Size(34, 34); - button_MoveUp.TabIndex = 2; - toolTip.SetToolTip(button_MoveUp, "上移选中项"); - button_MoveUp.UseVisualStyleBackColor = true; - button_MoveUp.Click += button_MoveUp_Click; - // - // button_MoveDown - // - button_MoveDown.Anchor = AnchorStyles.None; - button_MoveDown.AutoSize = true; - button_MoveDown.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_MoveDown.Enabled = false; - button_MoveDown.Image = (Image)resources.GetObject("button_MoveDown.Image"); - button_MoveDown.Location = new Point(123, 3); - button_MoveDown.Name = "button_MoveDown"; - button_MoveDown.Size = new Size(34, 34); - button_MoveDown.TabIndex = 3; - toolTip.SetToolTip(button_MoveDown, "下移选中项"); - button_MoveDown.UseVisualStyleBackColor = true; - button_MoveDown.Click += button_MoveDown_Click; - // - // button_Insert - // - button_Insert.Anchor = AnchorStyles.None; - button_Insert.AutoSize = true; - button_Insert.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_Insert.Enabled = false; - button_Insert.Image = (Image)resources.GetObject("button_Insert.Image"); - button_Insert.Location = new Point(163, 3); - button_Insert.Name = "button_Insert"; - button_Insert.Size = new Size(34, 34); - button_Insert.TabIndex = 4; - toolTip.SetToolTip(button_Insert, "在选中项之前插入项"); - button_Insert.UseVisualStyleBackColor = true; - button_Insert.Click += button_Insert_Click; - // - // button_RemoveAll - // - button_RemoveAll.Anchor = AnchorStyles.None; - button_RemoveAll.AutoSize = true; - button_RemoveAll.AutoSizeMode = AutoSizeMode.GrowAndShrink; - button_RemoveAll.Image = (Image)resources.GetObject("button_RemoveAll.Image"); - button_RemoveAll.Location = new Point(203, 3); - button_RemoveAll.Name = "button_RemoveAll"; - button_RemoveAll.Size = new Size(34, 34); - button_RemoveAll.TabIndex = 5; - toolTip.SetToolTip(button_RemoveAll, "移除所有项"); - button_RemoveAll.UseVisualStyleBackColor = true; - button_RemoveAll.Click += button_RemoveAll_Click; - // // listView // listView.AllowDrop = true; listView.Columns.AddRange(new ColumnHeader[] { columnHeader_Name, columnHeader_Version }); + listView.ContextMenuStrip = contextMenuStrip; listView.Dock = DockStyle.Fill; listView.FullRowSelect = true; listView.GridLines = true; - listView.Location = new Point(3, 49); + listView.Location = new Point(0, 0); listView.Name = "listView"; listView.ShowItemToolTips = true; - listView.Size = new Size(330, 393); + listView.Size = new Size(336, 445); listView.TabIndex = 1; listView.UseCompatibleStateImageBehavior = false; listView.View = View.Details; @@ -195,33 +73,78 @@ columnHeader_Version.Text = "版本"; columnHeader_Version.Width = 150; // + // contextMenuStrip + // + contextMenuStrip.ImageScalingSize = new Size(24, 24); + contextMenuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem_Add, toolStripMenuItem_BatchAdd, toolStripMenuItem_Insert, toolStripMenuItem_Remove, toolStripSeparator1, toolStripMenuItem_RemoveAll }); + contextMenuStrip.Name = "contextMenuStrip"; + contextMenuStrip.Size = new Size(241, 193); + contextMenuStrip.Opening += contextMenuStrip_Opening; + // + // toolStripMenuItem_Add + // + toolStripMenuItem_Add.Name = "toolStripMenuItem_Add"; + toolStripMenuItem_Add.Size = new Size(240, 30); + toolStripMenuItem_Add.Text = "添加(&A)..."; + toolStripMenuItem_Add.Click += toolStripMenuItem_Add_Click; + // + // toolStripMenuItem_Insert + // + toolStripMenuItem_Insert.Enabled = false; + toolStripMenuItem_Insert.Name = "toolStripMenuItem_Insert"; + toolStripMenuItem_Insert.Size = new Size(240, 30); + toolStripMenuItem_Insert.Text = "插入(&I)..."; + toolStripMenuItem_Insert.Click += toolStripMenuItem_Insert_Click; + // + // toolStripMenuItem_Remove + // + toolStripMenuItem_Remove.Enabled = false; + toolStripMenuItem_Remove.Name = "toolStripMenuItem_Remove"; + toolStripMenuItem_Remove.Size = new Size(240, 30); + toolStripMenuItem_Remove.Text = "移除(&R)"; + toolStripMenuItem_Remove.Click += toolStripMenuItem_Remove_Click; + // + // toolStripSeparator1 + // + toolStripSeparator1.Name = "toolStripSeparator1"; + toolStripSeparator1.Size = new Size(237, 6); + // + // toolStripMenuItem_RemoveAll + // + toolStripMenuItem_RemoveAll.Enabled = false; + toolStripMenuItem_RemoveAll.Name = "toolStripMenuItem_RemoveAll"; + toolStripMenuItem_RemoveAll.Size = new Size(240, 30); + toolStripMenuItem_RemoveAll.Text = "移除全部(&X)"; + toolStripMenuItem_RemoveAll.Click += toolStripMenuItem_RemoveAll_Click; + // + // toolStripMenuItem_BatchAdd + // + toolStripMenuItem_BatchAdd.Name = "toolStripMenuItem_BatchAdd"; + toolStripMenuItem_BatchAdd.Size = new Size(240, 30); + toolStripMenuItem_BatchAdd.Text = "批量添加(&B)..."; + toolStripMenuItem_BatchAdd.Click += toolStripMenuItem_BatchAdd_Click; + // // SpineListView // AutoScaleDimensions = new SizeF(11F, 24F); AutoScaleMode = AutoScaleMode.Font; - Controls.Add(tableLayoutPanel); + Controls.Add(listView); Name = "SpineListView"; Size = new Size(336, 445); - tableLayoutPanel.ResumeLayout(false); - tableLayoutPanel.PerformLayout(); - flowLayoutPanel_Buttons.ResumeLayout(false); - flowLayoutPanel_Buttons.PerformLayout(); + contextMenuStrip.ResumeLayout(false); ResumeLayout(false); } #endregion - - private TableLayoutPanel tableLayoutPanel; - private FlowLayoutPanel flowLayoutPanel_Buttons; - private Button button_Insert; - private Button button_Remove; - private Button button_MoveUp; - private Button button_MoveDown; private ListView listView; private ColumnHeader columnHeader_Name; private ColumnHeader columnHeader_Version; - private Button button_Add; - private Button button_RemoveAll; - private ToolTip toolTip; + private ContextMenuStrip contextMenuStrip; + private ToolStripMenuItem toolStripMenuItem_Add; + private ToolStripMenuItem toolStripMenuItem_Insert; + private ToolStripMenuItem toolStripMenuItem_Remove; + private ToolStripMenuItem toolStripMenuItem_RemoveAll; + private ToolStripSeparator toolStripSeparator1; + private ToolStripMenuItem toolStripMenuItem_BatchAdd; } } diff --git a/SpineViewer/src/SpineListView.cs b/SpineViewer/src/SpineListView.cs index e44a134..adb4589 100644 --- a/SpineViewer/src/SpineListView.cs +++ b/SpineViewer/src/SpineListView.cs @@ -33,18 +33,22 @@ namespace SpineViewer } /// - /// 弹出添加对话框 + /// 弹出添加对话框在指定位置之前插入一项 /// - public void Add() + private void Insert(int index = -1) { + // 如果索引无效则插在末尾 + if (index < 0 || index > spines.Count) + index = spines.Count; + var dialog = new OpenSpineDialog(); if (dialog.ShowDialog() == DialogResult.OK) { try { var spine = Spine.Spine.New(dialog.Version, dialog.SkelPath, dialog.AtlasPath); - spines.Add(spine); - listView.Items.Add(new ListViewItem([spine.Name, spine.Version.String()], -1) { ToolTipText = spine.SkelPath }); + spines.Insert(index, spine); + listView.Items.Insert(index, new ListViewItem([spine.Name, spine.Version.String()], -1) { ToolTipText = spine.SkelPath }); } catch (Exception ex) { @@ -55,6 +59,14 @@ namespace SpineViewer } } + /// + /// 弹出添加对话框 + /// + public void Add() + { + Insert(); + } + /// /// 弹出批量添加对话框 /// @@ -63,111 +75,13 @@ namespace SpineViewer throw new NotImplementedException(); } - private void button_Add_Click(object sender, EventArgs e) - { - Add(); - } - - private void button_Insert_Click(object sender, EventArgs e) - { - if (listView.SelectedIndices.Count <= 0) - return; - - var index = listView.SelectedIndices[0]; - var dialog = new OpenSpineDialog(); - dialog.ShowDialog(); - try - { - var spine = Spine.Spine.New(dialog.Version, dialog.SkelPath, dialog.AtlasPath); - spines.Insert(index, spine); - listView.Items.Insert(index, new ListViewItem([spine.Name, spine.Version.String()], -1) { ToolTipText = spine.SkelPath }); - } - catch (Exception ex) - { - Program.Logger.Error(ex.ToString()); - Program.Logger.Error($"Failed to load {dialog.SkelPath} {dialog.AtlasPath}"); - MessageBox.Show(ex.ToString(), "骨骼加载失败", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - private void button_Remove_Click(object sender, EventArgs e) - { - if (listView.SelectedIndices.Count <= 0) - return; - - foreach (var i in listView.SelectedIndices.Cast().OrderByDescending(x => x)) - { - spines.RemoveAt(i); - listView.Items.RemoveAt(i); - } - } - - private void button_MoveUp_Click(object sender, EventArgs e) - { - if (listView.SelectedIndices.Count <= 0) - return; - - var index = listView.SelectedIndices[0]; - if (index > 0) - { - (spines[index - 1], spines[index]) = (spines[index], spines[index - 1]); - var item = listView.Items[index]; - listView.Items.RemoveAt(index); - listView.Items.Insert(index - 1, item); - } - } - - private void button_MoveDown_Click(object sender, EventArgs e) - { - if (listView.SelectedIndices.Count <= 0) - return; - - var index = listView.SelectedIndices[0]; - if (index < spines.Count - 1) - { - (spines[index], spines[index + 1]) = (spines[index + 1], spines[index]); - var item = listView.Items[index + 1]; - listView.Items.RemoveAt(index + 1); - listView.Items.Insert(index, item); - } - } - - private void button_RemoveAll_Click(object sender, EventArgs e) - { - if (MessageBox.Show("确认移除所有项吗?", "操作确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) - { - spines.Clear(); - listView.Items.Clear(); - } - } - private void listView_SelectedIndexChanged(object sender, EventArgs e) { - if (listView.SelectedIndices.Count <= 0) + if (PropertyGrid is not null) { - button_Insert.Enabled = false; - button_Remove.Enabled = false; - button_MoveUp.Enabled = false; - button_MoveDown.Enabled = false; - if (PropertyGrid is not null) - PropertyGrid.SelectedObject = null; - } - else if (listView.SelectedIndices.Count <= 1) - { - button_Insert.Enabled = true; - button_Remove.Enabled = true; - button_MoveUp.Enabled = true; - button_MoveDown.Enabled = true; - if (PropertyGrid is not null) + if (listView.SelectedIndices.Count == 1) PropertyGrid.SelectedObject = spines[listView.SelectedIndices[0]]; - } - else - { - button_Insert.Enabled = false; - button_Remove.Enabled = true; - button_MoveUp.Enabled = false; - button_MoveDown.Enabled = false; - if (PropertyGrid is not null) + else PropertyGrid.SelectedObject = null; } } @@ -242,5 +156,64 @@ namespace SpineViewer item.BackColor = listView.BackColor; } } + + private void contextMenuStrip_Opening(object sender, CancelEventArgs e) + { + var selectedCount = listView.SelectedIndices.Count; + var itemsCount = listView.Items.Count; + toolStripMenuItem_Insert.Enabled = selectedCount == 1; + toolStripMenuItem_Remove.Enabled = selectedCount >= 1; + toolStripMenuItem_RemoveAll.Enabled = itemsCount > 0; + } + + private void toolStripMenuItem_Add_Click(object sender, EventArgs e) + { + Insert(); + } + + private void toolStripMenuItem_BatchAdd_Click(object sender, EventArgs e) + { + BatchAdd(); + } + + private void toolStripMenuItem_Insert_Click(object sender, EventArgs e) + { + if (listView.SelectedIndices.Count == 1) + Insert(listView.SelectedIndices[0]); + } + + private void toolStripMenuItem_Remove_Click(object sender, EventArgs e) + { + if (listView.SelectedIndices.Count <= 0) + return; + + if (listView.SelectedIndices.Count > 1) + { + if (MessageBox.Show($"确定移除所选 {listView.SelectedIndices.Count} 项?", "操作确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) + { + return; + } + } + + foreach (var i in listView.SelectedIndices.Cast().OrderByDescending(x => x)) + { + spines.RemoveAt(i); + listView.Items.RemoveAt(i); + } + } + + private void toolStripMenuItem_RemoveAll_Click(object sender, EventArgs e) + { + if (listView.Items.Count <= 0) + return; + + if (MessageBox.Show("确认移除所有项吗?", "操作确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) + { + spines.Clear(); + listView.Items.Clear(); + if (PropertyGrid is not null) + PropertyGrid.SelectedObject = null; + } + } } } diff --git a/SpineViewer/src/SpineListView.resx b/SpineViewer/src/SpineListView.resx index a590340..efb6fba 100644 --- a/SpineViewer/src/SpineListView.resx +++ b/SpineViewer/src/SpineListView.resx @@ -117,58 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAAGdJREFUSEvt - 0TEKgDAQRNG9/6VXItrsBL4JbqopnsWI+RAjM2NVRIxH1v0LGcgb243KQBwkMhAHiQzEQSID+SVYD+ny - tPRFMxl6Hb/S+lPJ7JAVMhAHiQzEQSIDcZDIQI4H7482Y8MFq6iYyveKOvcAAAAASUVORK5CYII= - - - + 17, 17 - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAADJJREFUSEtj - YBgFo2AUjIJRQDH4//8/mKIHhtqFKUFjjCFAWzwQQToKRsEoGAWjgDIAAEamd4nzZfPwAAAAAElFTkSu - QmCC - - - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAAKtJREFUSEvt - jUEOxCAMA/n/p6m6aqV2nGBIpT1xGA5jB7fee1ultXY+nX4GEY57rDoqYgTHKqMiMjhC2M8QEcHPM3gX - IYLwUwfviYgvYzOjIkZjkY/caFSE+2DWZ6MqzOFKxlwGWQ4PFnN2potZj3nUefZsgVR7dzcNMr50L+Y/ - iD5hTth/SZYj+AHziGdXQkdl8HVP4diDDhGOPegQ4diDDhGOvw/+jopjJwdgSnDyI6KwtwAAAABJRU5E - rkJggg== - - - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAAKZJREFUSEvt - jTEOwDAIA/n/p1M1UqXW4BhIxwyXwTE+G2NYFTO7n4F5BhcoHllX6gLFESpcoDhChQsUR6hwgeIXYeW4 - I3x3ywO7/ShYjux0J+wDj9kI/rPe05UFpNN599JF1lH/2PmUUweFP/wPheowm0cyKlwNZDImWwrZUAbc - SQs7UrxHXBCBowy8i3ABA8cR7DNcsAIlVdncwECxI5v3GGToym4u8Ptw8siITo4AAAAASUVORK5CYII= - - - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAALNJREFUSEvt - kUsKgDAMRHP/S1esCjrJNJ9SQXDxupjMB1Raa5JFRPanoR5BCR7XWHVUCR7/oIcSPL45mAlXBu/edMGs - 3xKGJTPeDjtgmJXgnfkur2tAKp67L2xkHu+Onoc5FEjc8G4OesGobo3RwVFBRGNjw0FWFAF7woOVUcwj - SrDAUgbmLJTAwHIE/QwljMCR7FjvQMFjZqznUYhQHetZFFZzPMZ/WcG5pQ+LUcJa3v6kGyUxwZMRgqWr - AAAAAElFTkSuQmCC - - - - - iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAABGdBTUEAALGPC/xhBQAAAPdJREFUSEu1 - klsOwyAMBDlCjtL7X44qFiAyrA1E5GOqdr0PJWrKOaeU0v2R7+9f0Pe3H1+Nsl8Jx0bZa3gHhndhX+2c - Gt7AnsJltwXjj4URIt/GhkEvQI8HcxwzD0NekB5CvxozH4VJgXy9wifHwkGvaMXjjZmfAhFlbZT6bMwy - FBSiVMKcYhA8WC4In6z1UIgQI1tj1kHB4/6HiiGD3ohBUERju6ODoGC5B3OKQcCQfLLu7t48BmF1rPNN - PQ8/hZ2x4r3o87xycGcMuaUMQ6/Guvw0e2ysdExf77GxR2nQNTW8hX210z2cgL2Fb8Yq7H+INJ+i7/8D - i+nyJVO3bVgAAAAASUVORK5CYII= - - \ No newline at end of file