-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormSlotItemList.cs
447 lines (384 loc) · 13.8 KB
/
FormSlotItemList.cs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KCB;
namespace KCB2
{
public partial class FormSlotItemList : Form
{
public FormSlotItemList(ImageList iconImageList)
{
InitializeComponent();
lvSlotItemList.DoubleBuffer(true);
lvSlotItemList.ListViewItemSorter = Comparer<SlotItemLVItem>.Default;
lvSlotItemList.LoadColumnWithOrder(Properties.Settings.Default.SlotItemListColumnWidth);
if (!Properties.Settings.Default.SlotItemListBounds.IsEmpty)
Bounds = Properties.Settings.Default.SlotItemListBounds;
lvSlotItemList.SmallImageList = iconImageList;
var sm = new SystemMenu(this);
sm.InsertMenuItem(5, "ウィンドウ復帰", 6);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (SystemMenu.GetSysMenuId(m) == 5)
{
WindowState = FormWindowState.Normal;
Bounds = new Rectangle(100, 100, 300, 300);
}
}
/// <summary>
/// 装備数上限
/// </summary>
public int MaxItem { get; set; }
/// <summary>
/// 装備情報の更新
/// </summary>
/// <param name="items"></param>
public void UpdateSlotItemList(IEnumerable<MemberData.Item.Info> items)
{
if (InvokeRequired)
BeginInvoke((MethodInvoker)(() => _updateSlotItemList(items)));
else
_updateSlotItemList(items);
}
void _updateSlotItemList(IEnumerable<MemberData.Item.Info> items)
{
///内部情報の再構成
_slotItemData.Clear();
foreach (var it in items)
{
SlotItemInfo info;
if (_slotItemData.ContainsKey(it.SlotItemType))
{
info = _slotItemData[it.SlotItemType];
info.AddSlotItemOwner(it.Owner);
info.Count++;
}
else
{
info = new SlotItemInfo(it);
_slotItemData[it.SlotItemType] = info;
info.Count = 1;
}
}
///ビューの更新
lvSlotItemList.BeginUpdate();
Dictionary<int, SlotItemLVItem> updateMap = new Dictionary<int, SlotItemLVItem>();
foreach (SlotItemLVItem it in lvSlotItemList.Items)
{
updateMap[it.Info.ID] = it;
}
foreach (var it in _slotItemData)
{
if (updateMap.ContainsKey(it.Key))
{
updateMap[it.Key].Update(it.Value);
updateMap.Remove(it.Key);
}
else
{
lvSlotItemList.Items.Add(new SlotItemLVItem(it.Value));
}
}
foreach (var it in updateMap.Values)
{
lvSlotItemList.Items.Remove(it);
}
updateFormTitle();
lvSlotItemList.EndUpdate();
}
/// <summary>
/// 装備オーナーの更新
/// </summary>
/// <param name="itemOwner"></param>
public void UpdateSlotItemOwner(IDictionary<int, MemberData.Ship.SlotItemOwner> itemOwner,
IDictionary<int, int> itemType)
{
if (InvokeRequired)
BeginInvoke((MethodInvoker)(() => _updateSlotItemOwner(itemOwner, itemType)));
else
_updateSlotItemOwner(itemOwner, itemType);
}
void _updateSlotItemOwner(IDictionary<int, MemberData.Ship.SlotItemOwner> itemOwner,
IDictionary<int, int> itemType)
{
///装備種別一覧ループ
foreach (var it in _slotItemData)
{
//装備所有者情報をクリア
it.Value.ClearSlotItemOwner();
///装備一覧を舐める
foreach (var it2 in itemOwner)
{
///装備種別が一致したら更新
if (itemType.ContainsKey(it2.Key) && itemType[it2.Key] == it.Key)
it.Value.AddSlotItemOwner(it2.Value);
}
}
lvSlotItemList.BeginUpdate();
foreach (SlotItemLVItem it in lvSlotItemList.Items)
{
it.Update(_slotItemData[it.Info.ID]);
}
updateFormTitle();
lvSlotItemList.EndUpdate();
}
void updateFormTitle()
{
int nTotal = 0;
int nUsed = 0;
foreach (var it in _slotItemData)
{
nTotal += it.Value.Count;
nUsed += it.Value.UsingCount;
}
Text = string.Format("装備一覧 使用中:{0} {1}/{2}", nUsed, nTotal, MaxItem);
}
Dictionary<int, SlotItemInfo> _slotItemData = new Dictionary<int, SlotItemInfo>();
/// <summary>
/// 装備情報
/// </summary>
class SlotItemInfo
{
/// <summary>
/// 装備種別ID
/// </summary>
public int ID { get; private set; }
/// <summary>
/// 装備種別名称
/// </summary>
public string Type { get; private set; }
/// <summary>
/// 装備種別num
/// </summary>
public int TypeNum { get; private set; }
/// <summary>
/// 装備名
/// </summary>
public string Name { get; private set; }
/// <summary>
/// 保有数
/// </summary>
public int Count { get; set; }
public class Owner
{
/// <summary>
/// 艦船ID
/// </summary>
public int ID { get; private set; }
/// <summary>
/// 名前
/// </summary>
public string Name { get; private set; }
/// <summary>
/// レベル
/// </summary>
public int Level { get; private set;}
public Owner(MemberData.Ship.SlotItemOwner owner)
{
ID = owner.ID;
Name = owner.Name;
Level = owner.Level;
Count = 1;
}
/// <summary>
/// 装着数
/// </summary>
public int Count { get; set; }
}
/// <summary>
/// 装備所有者一覧
/// </summary>
public List<Owner> ItemOwner { get; private set; }
public SlotItemInfo(MemberData.Item.Info info)
{
ItemOwner = new List<Owner>();
ID = info.SlotItemType;
Name = info.Name;
Type = info.Type;
TypeNum = info.TypeNum;
Count = 1;
AddSlotItemOwner(info.Owner);
}
/// <summary>
/// 装備所有者を追加。nullの場合は黙って無視する
/// </summary>
/// <param name="itemOwner"></param>
public void AddSlotItemOwner(MemberData.Ship.SlotItemOwner itemOwner)
{
if (itemOwner != null)
{
///複数同種の装備をつけてる場合はかぶせる
foreach (var it in ItemOwner)
{
if (it.ID == itemOwner.ID)
{
it.Count++;
return;
}
}
//最初の装備
ItemOwner.Add(new Owner(itemOwner));
}
}
public void ClearSlotItemOwner()
{
ItemOwner.Clear();
}
/// <summary>
/// 使用してる装備数
/// </summary>
public int UsingCount
{
get
{
int count = 0;
foreach (var it in ItemOwner)
count += it.Count;
return count;
}
}
}
/// <summary>
/// リストビュー
/// </summary>
class SlotItemLVItem : ListViewItem, IComparable<SlotItemLVItem>
{
//初期ソートはアイテムID
static private int _column = (int)ColumnIndex.ID;
static private SortOrder _order = SortOrder.Ascending;
/// <summary>
/// ソートする対象カラム
/// </summary>
static public int Column
{
set
{
//カラムに変化がなかった
if (_column == value)
{
if (_order == SortOrder.Ascending)
_order = SortOrder.Descending;
else if (_order == SortOrder.Descending)
_order = SortOrder.Ascending;
}
else//カラムが変わったのでオーダーは保持しない
_order = SortOrder.Ascending;
_column = value;
}
get
{
return _column;
}
}
/// <summary>
/// ソート順
/// </summary>
static public SortOrder Order { get { return _order; } }
public enum ColumnIndex
{
ID = 0,
種別 = 1,
装備名 = 2,
保有数 = 3,
装着数 = 4,
装備艦娘 = 5,
}
public SlotItemInfo Info { get; private set; }
public SlotItemLVItem()
{
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
}
public SlotItemLVItem(SlotItemInfo info)
{
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
SubItems.Add("");
SubItems[(int)ColumnIndex.ID].Text = info.ID.ToString();
SubItems[(int)ColumnIndex.種別].Text = info.Type;
SubItems[(int)ColumnIndex.装備名].Text = info.Name;
Update(info);
}
public void Update(SlotItemInfo info)
{
SubItems[(int)ColumnIndex.保有数].Text = info.Count.ToString();
SubItems[(int)ColumnIndex.装着数].Text = info.UsingCount.ToString();
StringBuilder sb = new StringBuilder();
foreach (var it in info.ItemOwner)
{
if(it.Count > 1)
sb.AppendFormat("{0}(Lv{1})x{2} ", it.Name, it.Level,it.Count);
else
sb.AppendFormat("{0}(Lv{1}) ", it.Name, it.Level);
}
SubItems[(int)ColumnIndex.装備艦娘].Text = sb.ToString();
ImageIndex = info.TypeNum;
Info = info;
}
/// <summary>
/// IComparableインタフェイスの実装
/// </summary>
/// <param name="it"></param>
/// <returns></returns>
public int CompareTo(SlotItemLVItem it)
{
int result = _compare(it.Info);
if (_order == SortOrder.Descending)
result = -result;
else if (_order == SortOrder.None)
result = 0;
return result;
}
int _compare(SlotItemInfo info2)
{
switch ((ColumnIndex)_column)
{
case ColumnIndex.ID:
return Info.ID - info2.ID;
case ColumnIndex.種別:
return Info.TypeNum - info2.TypeNum;
case ColumnIndex.装備名:
return string.Compare(Info.Name, info2.Name);
case ColumnIndex.保有数:
return Info.Count - info2.Count;
case ColumnIndex.装着数:
return Info.UsingCount - info2.UsingCount;
case ColumnIndex.装備艦娘:
default:
return 0;
}
}
}
private void FormSlotItemList_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
Visible = false;
e.Cancel = true;
}
Properties.Settings.Default.SlotItemListColumnWidth = lvSlotItemList.SaveColumnWithOrder();
if (WindowState == FormWindowState.Normal)
Properties.Settings.Default.SlotItemListBounds = Bounds;
else
Properties.Settings.Default.SlotItemListBounds = RestoreBounds;
}
private void lvSlotItemList_ColumnClick(object sender, ColumnClickEventArgs e)
{
SlotItemLVItem.Column = e.Column;
lvSlotItemList.Sort();
lvSlotItemList.SetSortIcon(e.Column, SlotItemLVItem.Order);
}
}
}