OSDN Git Service

艦隊ごとの装備一覧にドラム缶の数を表示する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / EquipPanel.cs
1 // Copyright (C) 2015 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.Windows.Forms;\r
22 \r
23 namespace KancolleSniffer\r
24 {\r
25     public class EquipPanel : Panel\r
26     {\r
27         private const int LineHeight = 14;\r
28         private const int LabelHeight = 12;\r
29         private EquipColumn[] _equipList;\r
30         private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();\r
31         private readonly List<Panel> _panelList = new List<Panel>();\r
32 \r
33         private class EquipColumn\r
34         {\r
35             public string Fleet { get; set; }\r
36             public string Ship { get; set; }\r
37             public int Id { get; set; }\r
38             public string Equip { get; set; }\r
39             public Color Color { get; set; }\r
40 \r
41             public EquipColumn()\r
42             {\r
43                 Fleet = Ship = Equip = "";\r
44                 Color = DefaultBackColor;\r
45             }\r
46         }\r
47 \r
48         public void UpdateEquip(Sniffer sniffer)\r
49         {\r
50             CreateEquipList(sniffer);\r
51             SuspendLayout();\r
52             CreateEquipLabels();\r
53             SetEquipLabels();\r
54             ResumeLayout();\r
55         }\r
56 \r
57         private void CreateEquipList(Sniffer sniffer)\r
58         {\r
59             var list = new List<EquipColumn>();\r
60             var fn = new[] { "第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊" };\r
61             for (var f = 0; f < fn.Length; f++)\r
62             {\r
63                 var drumTotal = 0;\r
64                 var drumShips = 0;\r
65                 var ships = new List<EquipColumn>();\r
66                 foreach (var s in sniffer.GetShipStatuses(f))\r
67                 {\r
68                     var drum = 0;\r
69                     var equips = new List<EquipColumn>();\r
70                     for (var i = 0; i < s.Slot.Length; i++)\r
71                     {\r
72                         var slot = s.Slot[i];\r
73                         var onslot = s.OnSlot[i];\r
74                         var max = s.Spec.MaxEq[i];\r
75                         if (slot == -1)\r
76                             continue;\r
77                         var item = sniffer.Item.ItemDict[slot];\r
78                         if (item.Spec.Name == "ドラム缶(輸送用)")\r
79                             drum++;\r
80                         equips.Add(new EquipColumn\r
81                         {\r
82                             Equip = item.Spec.Name + (item.Level == 0 ? "" : "★" + item.Level) +\r
83                                     (!item.Spec.IsAircraft ? "" : " " + onslot + "/" + max),\r
84                             Color = item.Spec.Color\r
85                         });\r
86                     }\r
87                     if (drum != 0)\r
88                         drumShips++;\r
89                     drumTotal += drum;\r
90                     ships.Add(new EquipColumn { Ship = s.Name, Id = s.Id });\r
91                     ships.AddRange(equips);\r
92                 }\r
93                 list.Add(new EquipColumn\r
94                 {\r
95                     Fleet = fn[f] + (drumTotal == 0 ? "" : " ドラム缶" + drumTotal + "(" + drumShips + "隻)")\r
96                 });\r
97                 list.AddRange(ships);\r
98             }\r
99             _equipList = list.ToArray();\r
100         }\r
101 \r
102         private void CreateEquipLabels()\r
103         {\r
104             for (var i = _labelList.Count; i < _equipList.Length; i++)\r
105                 CreateEquipLabels(i);\r
106         }\r
107 \r
108         private void CreateEquipLabels(int i)\r
109         {\r
110             var y = 1 + LineHeight * i;\r
111             var lbp = new Panel\r
112             {\r
113                 Location = new Point(0, y),\r
114                 Size = new Size(ShipListForm.PanelWidth, LineHeight),\r
115                 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],\r
116                 Visible = false\r
117             };\r
118             lbp.Scale(ShipLabel.ScaleFactor);\r
119             lbp.Tag = lbp.Location.Y;\r
120             var labels = new[]\r
121             {\r
122                 new ShipLabel {Location = new Point(1, 2), AutoSize = true},\r
123                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
124                 new ShipLabel {Location = new Point(40, 2), AutoSize = true},\r
125                 new ShipLabel {Location = new Point(37, 2), Size = new Size(4, LabelHeight - 2)}\r
126             };\r
127             _labelList.Add(labels);\r
128             _panelList.Add(lbp);\r
129             // ReSharper disable once CoVariantArrayConversion\r
130             lbp.Controls.AddRange(labels);\r
131             Controls.Add(lbp);\r
132             foreach (var label in labels)\r
133             {\r
134                 label.Scale();\r
135                 label.PresetColor =\r
136                     label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];\r
137             }\r
138         }\r
139 \r
140         private void SetEquipLabels()\r
141         {\r
142             for (var i = 0; i < _equipList.Length; i++)\r
143                 SetEquip(i);\r
144             for (var i = _equipList.Length; i < _labelList.Count; i++)\r
145                 _panelList[i].Visible = false;\r
146         }\r
147 \r
148         private void SetEquip(int i)\r
149         {\r
150             var lbp = _panelList[i];\r
151             if (!lbp.Visible)\r
152                 lbp.Location = new Point(lbp.Left, (int)lbp.Tag + AutoScrollPosition.Y);\r
153             var e = _equipList[i];\r
154             var labels = _labelList[i];\r
155             labels[0].Text = e.Fleet;\r
156             labels[1].SetName(e.Ship);\r
157             labels[2].Text = e.Equip;\r
158             labels[3].Visible = e.Equip != "";\r
159             labels[3].BackColor = e.Color;\r
160             lbp.Visible = true;\r
161         }\r
162 \r
163         public void ShowShip(int id)\r
164         {\r
165             var i = Array.FindIndex(_equipList, e => e.Id == id);\r
166             if (i == -1)\r
167                 return;\r
168             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
169             AutoScrollPosition = new Point(0, y);\r
170         }\r
171     }\r
172 }