OSDN Git Service

艦娘一覧のウィンドウの位置と大きさを保存する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ShipListForm.cs
1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.Collections.Generic;\r
20 using System.Drawing;\r
21 using System.Linq;\r
22 using System.Windows.Forms;\r
23 \r
24 namespace KancolleSniffer\r
25 {\r
26     public partial class ShipListForm : Form\r
27     {\r
28         private readonly Sniffer _sniffer;\r
29         private readonly Config _config;\r
30         private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();\r
31         private const int HpLabelRight = 126;\r
32 \r
33         public ShipListForm(Sniffer sniffer, Config config)\r
34         {\r
35             InitializeComponent();\r
36             _sniffer = sniffer;\r
37             _config = config;\r
38         }\r
39 \r
40         public void UpdateList()\r
41         {\r
42             if (!Visible)\r
43                 return;\r
44             CreateListLabels();\r
45             SetShipLabels();\r
46         }\r
47 \r
48         private void CreateListLabels()\r
49         {\r
50             panelShipList.SuspendLayout();\r
51             for (var i = _labelList.Count; i < _sniffer.Item.MaxShips; i++)\r
52             {\r
53                 const int height = 12;\r
54                 var y = 3 + 16 * i + panelShipList.AutoScrollPosition.Y;\r
55                 var labels = new[]\r
56                 {\r
57                     new ShipLabel {Location = new Point(1, y), Size = new Size(11, height)},\r
58                     new ShipLabel {Location = new Point(HpLabelRight, y), AutoSize = true},\r
59                     new ShipLabel\r
60                     {\r
61                         Location = new Point(132, y),\r
62                         Size = new Size(23, height),\r
63                         TextAlign = ContentAlignment.MiddleRight\r
64                     },\r
65                     new ShipLabel\r
66                     {\r
67                         Location = new Point(166, y),\r
68                         Size = new Size(23, height),\r
69                         TextAlign = ContentAlignment.MiddleRight\r
70                     },\r
71                     new ShipLabel\r
72                     {\r
73                         Location = new Point(191, y),\r
74                         Size = new Size(41, height),\r
75                         TextAlign = ContentAlignment.MiddleRight\r
76                     },\r
77                     new ShipLabel {Location = new Point(10, y), AutoSize = true}\r
78                 };\r
79                 _labelList.Add(labels);\r
80                 // ReSharper disable once CoVariantArrayConversion\r
81                 panelShipList.Controls.AddRange(labels);\r
82                 labels[1].SizeChanged += labelHP_SizeChanged;\r
83             }\r
84             for (var i = _labelList.Count; i > _sniffer.Item.MaxShips; i--)\r
85             {\r
86                 foreach (var label in _labelList[i - 1])\r
87                 {\r
88                     panelShipList.Controls.Remove(label);\r
89                     label.Dispose();\r
90                 }\r
91                 _labelList.RemoveAt(i - 1);\r
92             }\r
93             panelShipList.ResumeLayout();\r
94         }\r
95 \r
96         private class CompareShipByExp : IComparer<ShipStatus>\r
97         {\r
98             public int Compare(ShipStatus a, ShipStatus b)\r
99             {\r
100                 if (a.Level != b.Level)\r
101                     return b.Level - a.Level;\r
102                 if (a.ExpToNext != b.ExpToNext)\r
103                     return a.ExpToNext - b.ExpToNext;\r
104                 return a.Spec.Id - b.Spec.Id;\r
105             }\r
106         }\r
107 \r
108         private void SetShipLabels()\r
109         {\r
110             var shipList = _sniffer.ShipList;\r
111             var fn = new[] {"", "1", "2", "3", "4"};\r
112             var i = 0;\r
113             foreach (var s in shipList.OrderBy(s => s, new CompareShipByExp()))\r
114             {\r
115                 var labels = _labelList[i++];\r
116                 labels[0].Text = fn[s.Fleet + 1];\r
117                 labels[1].SetHp(s);\r
118                 labels[2].SetCond(s);\r
119                 labels[3].SetLevel(s);\r
120                 labels[4].SetExpToNext(s);\r
121                 labels[5].SetName(s);\r
122             }\r
123         }\r
124 \r
125         private void labelHP_SizeChanged(object sender, EventArgs e)\r
126         {\r
127             var label = (Label)sender;\r
128             label.Location = new Point(HpLabelRight - label.Width, label.Top);\r
129         }\r
130 \r
131         private void ShipListForm_Load(object sender, EventArgs e)\r
132         {\r
133             var config = _config.ShipList;\r
134             if (config.Location.X == int.MinValue)\r
135                 return;\r
136             var bounds = new Rectangle(config.Location, config.Size);\r
137             if (MainForm.IsVisibleOnAnyScreen(bounds))\r
138                 Location = bounds.Location;\r
139             Size = bounds.Size;\r
140         }\r
141 \r
142         private void ShipListForm_FormClosing(object sender, FormClosingEventArgs e)\r
143         {\r
144             var config = _config.ShipList;\r
145             var bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds;\r
146             config.Location = bounds.Location;\r
147             config.Size = bounds.Size;\r
148             Hide();\r
149             e.Cancel = true;\r
150         }\r
151     }\r
152 }