1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Collections.Generic;
\r
17 using System.Drawing;
\r
19 using System.Windows.Forms;
\r
20 using KancolleSniffer.View;
\r
21 using KancolleSniffer.View.ShipListPanel;
\r
22 using Clipboard = KancolleSniffer.Util.Clipboard;
\r
24 namespace KancolleSniffer
\r
26 public partial class ListForm : Form
\r
28 private readonly Sniffer _sniffer;
\r
29 private readonly Config _config;
\r
30 private readonly MainForm _main;
\r
31 private readonly MainForm.TimeOutChecker _suppressActivate;
\r
32 private readonly CheckBox[] _shipTypeCheckBoxes;
\r
33 private bool _isMaster;
\r
34 private Settings _settings;
\r
35 public const int PanelWidth = 217;
\r
37 public class Settings
\r
39 public string Mode { get; set; }
\r
40 public ShipCategory ShipCategories { get; set; }
\r
41 public bool ShipType { get; set; }
\r
42 public bool ShowHpInPercent { get; set; }
\r
43 public SortOrder SortOrder { get; set; }
\r
45 public static Settings FromShipListConfig(ShipListConfig config)
\r
49 Mode = config.Mode ?? "全艦",
\r
50 ShipCategories = config.ShipCategories,
\r
51 ShipType = config.ShipType,
\r
52 ShowHpInPercent = config.ShowHpInPercent,
\r
53 SortOrder = config.SortOrder
\r
57 public void SetToShipListConfig(ShipListConfig config)
\r
60 config.ShipCategories = ShipCategories;
\r
61 config.ShipType = ShipType;
\r
62 config.ShowHpInPercent = ShowHpInPercent;
\r
63 config.SortOrder = SortOrder;
\r
67 private object[] PanelNames => new object[] {"全艦", "A", "B", "C", "D", "分類", "修復", "装備", "艦隊", "対空", "戦況", "情報"}
\r
68 .Where(n => IsMaster || (string)n != "分類").ToArray();
\r
70 private char[] PanelKeys => new[] {'Z', 'A', 'B', 'C', 'D', 'G', 'R', 'W', 'X', 'Y', 'S', 'I'}
\r
71 .Where(key => IsMaster || key != 'G').ToArray();
\r
73 public bool IsMaster
\r
79 Text = _isMaster ? "一覧 プライマリ" : "一覧";
\r
80 comboBoxGroup.Items.Clear();
\r
81 comboBoxGroup.Items.AddRange(PanelNames);
\r
85 public enum SortOrder
\r
92 ExpToNextAscend = ExpToNext,
\r
97 public ListForm(MainForm main)
\r
99 InitializeComponent();
\r
102 _sniffer = main.Sniffer;
\r
103 _config = main.Config;
\r
104 _suppressActivate = main.SuppressActivate;
\r
105 _shipTypeCheckBoxes = new[]
\r
107 checkBoxSTypeBattleShip,
\r
108 checkBoxSTypeAircraftCarrier,
\r
109 checkBoxSTypeHeavyCruiser,
\r
110 checkBoxSTypeLightCruiser,
\r
111 checkBoxSTypeDestroyer,
\r
112 checkBoxSTypeEscort,
\r
113 checkBoxSTypeSubmarine,
\r
114 checkBoxSTypeAuxiliary
\r
116 battleResultPanel.HpLabelClick += ToggleHpPercent;
\r
117 shipListPanel.HpLabelClick += ToggleHpPercent;
\r
118 var swipe = new SwipeScrollify();
\r
119 swipe.AddShipListPanel(shipListPanel);
\r
120 swipe.AddTreeView(itemTreeView);
\r
121 swipe.AddPanel(fleetPanel);
\r
124 public void UpdateList()
\r
126 SetHeaderVisibility();
\r
127 SetPanelVisibility();
\r
130 itemTreeView.SetNodes(_sniffer.ItemList);
\r
132 else if (InFleetInfo)
\r
134 fleetPanel.Update(_sniffer);
\r
136 else if (InAntiAir)
\r
138 antiAirPanel.Update(_sniffer);
\r
140 else if (InMiscText)
\r
142 richTextBoxMiscText.Text = _sniffer.MiscText;
\r
144 else if (InShipStatus || InGroupConfig || InRepairList)
\r
146 SetHeaderSortOrder();
\r
147 shipListPanel.Update(_sniffer, comboBoxGroup.Text, _settings);
\r
149 if (shipListPanel.GroupUpdated)
\r
151 StoreShipGroupToConfig();
\r
153 shipListPanel.GroupUpdated = false;
\r
157 private void SetHeaderVisibility()
\r
159 static void Set(Control header, bool visible)
\r
161 header.Visible = visible;
\r
163 header.BringToFront();
\r
166 Set(panelItemHeader, InItemList || InAntiAir || InBattleResult || InMiscText);
\r
167 Set(panelGroupHeader, InGroupConfig);
\r
168 Set(panelRepairHeader, InRepairList);
\r
169 Set(panelFleetHeader, InFleetInfo);
\r
172 private void SetPanelVisibility()
\r
174 static void Set(Control panel, bool visible)
\r
176 // SwipeScrollifyが誤作動するのでEnabledも切り替える
\r
177 panel.Visible = panel.Enabled = visible;
\r
180 Set(shipListPanel, InShipStatus || InGroupConfig || InRepairList);
\r
181 Set(itemTreeView, InItemList);
\r
182 Set(fleetPanel, InFleetInfo);
\r
183 Set(antiAirPanel, InAntiAir);
\r
184 Set(airBattleResultPanel, InBattleResult);
\r
185 Set(battleResultPanel, InBattleResult);
\r
186 Set(richTextBoxMiscText, InMiscText);
\r
189 public void UpdateAirBattleResult()
\r
191 airBattleResultPanel.ShowResultAutomatic = (_config.Spoilers & Spoiler.AirBattleResult) != 0;
\r
192 airBattleResultPanel.SetResult(_sniffer);
\r
195 public void UpdateBattleResult()
\r
197 MoveToBattleResult();
\r
198 battleResultPanel.Spoilers = _config.Spoilers;
\r
199 battleResultPanel.Update(_sniffer);
\r
200 BackFromBattleResult();
\r
203 private int _prevSelectedIndex = -1;
\r
204 private const int BattleResultIndex = 10;
\r
206 private void MoveToBattleResult()
\r
208 if (!_isMaster || !_config.ShipList.AutoBattleResult || comboBoxGroup.SelectedIndex == BattleResultIndex ||
\r
209 _sniffer.InSortie == -1)
\r
211 _prevSelectedIndex = comboBoxGroup.SelectedIndex;
\r
212 comboBoxGroup.SelectedIndex = BattleResultIndex;
\r
215 private void BackFromBattleResult()
\r
217 if (_sniffer.InSortie != -1 || _prevSelectedIndex == -1)
\r
219 comboBoxGroup.SelectedIndex = _prevSelectedIndex;
\r
220 _prevSelectedIndex = -1;
\r
223 public void UpdateCellInfo()
\r
225 battleResultPanel.Spoilers = _config.Spoilers;
\r
226 battleResultPanel.UpdateCellInfo(_sniffer.CellInfo);
\r
229 private void SetHeaderSortOrder()
\r
231 switch (_settings.SortOrder)
\r
233 case SortOrder.None:
\r
234 labelHeaderCond.Text = "cond";
\r
235 labelHeaderExp.Text = "Exp";
\r
237 case SortOrder.CondAscend:
\r
238 labelHeaderCond.Text = "cond▴";
\r
239 labelHeaderExp.Text = "Exp";
\r
241 case SortOrder.CondDescend:
\r
242 labelHeaderCond.Text = "cond▾";
\r
243 labelHeaderExp.Text = "Exp";
\r
245 case SortOrder.ExpToNextAscend:
\r
246 labelHeaderCond.Text = "cond";
\r
247 labelHeaderExp.Text = "Exp▴";
\r
249 case SortOrder.ExpToNextDescend:
\r
250 labelHeaderCond.Text = "cond";
\r
251 labelHeaderExp.Text = "Exp▾";
\r
256 private bool InShipStatus => Array.Exists(new[] {"全艦", "A", "B", "C", "D"}, x => _settings.Mode == x);
\r
258 private bool InGroupConfig => _settings.Mode == "分類";
\r
260 private bool InRepairList => _settings.Mode == "修復";
\r
262 private bool InItemList => _settings.Mode == "装備";
\r
264 private bool InFleetInfo => _settings.Mode == "艦隊";
\r
266 private bool InAntiAir => _settings.Mode == "対空";
\r
268 private bool InBattleResult => _settings.Mode == "戦況";
\r
270 private bool InMiscText => _settings.Mode == "情報";
\r
272 private void ListForm_Load(object sender, EventArgs e)
\r
274 /* DPIではなくズームしたときにパネルは大きくなるがScrollBarはそのままなので隙間ができる。
\r
275 そこでScrollBarの幅に合わせて全体の横幅を設定し直す。*/
\r
276 Width = Scaler.ScaleWidth(PanelWidth + 12 /* PanelとFrameの内側 */) +
\r
277 SystemInformation.VerticalScrollBarWidth + 2 /* 縁の幅 */ + Width - ClientSize.Width;
\r
278 MinimumSize = new Size(Width, 0);
\r
279 MaximumSize = new Size(Width, int.MaxValue);
\r
280 var config = GetConfig();
\r
281 _settings = Settings.FromShipListConfig(config);
\r
282 if (_settings.ShowHpInPercent)
\r
284 shipListPanel.ToggleHpPercent();
\r
285 battleResultPanel.ToggleHpPercent();
\r
287 LoadShipGroupFromConfig();
\r
288 comboBoxGroup.SelectedItem = _settings.Mode;
\r
289 SetCheckBoxSTypeState();
\r
290 if (config.Location.X == int.MinValue)
\r
292 var bounds = new Rectangle(config.Location, config.Size);
\r
293 if (MainForm.IsTitleBarOnAnyScreen(bounds.Location))
\r
294 Location = bounds.Location;
\r
295 Height = bounds.Height;
\r
298 private ShipListConfig GetConfig()
\r
300 if (_isMaster || _config.ListFormGroup.Count == 0)
\r
301 return _config.ShipList;
\r
302 var config = _config.ListFormGroup[0];
\r
303 _config.ListFormGroup.RemoveAt(0);
\r
304 if (config.Mode == "分類")
\r
305 config.Mode = "全艦";
\r
309 private void LoadShipGroupFromConfig()
\r
311 var group = _config.ShipList.ShipGroup;
\r
312 for (var i = 0; i < GroupConfigLabels.GroupCount; i++)
\r
313 shipListPanel.GroupSettings[i] = i < group.Count ? new HashSet<int>(group[i]) : new HashSet<int>();
\r
316 private void SetCheckBoxSTypeState()
\r
318 for (var type = 0; type < _shipTypeCheckBoxes.Length; type++)
\r
319 _shipTypeCheckBoxes[type].Checked = ((int)_settings.ShipCategories & (1 << type)) != 0;
\r
320 checkBoxSTypeAll.Checked = _settings.ShipCategories == ShipCategory.All;
\r
321 checkBoxSTypeDetails.Checked = _settings.ShipType;
\r
324 private void ListForm_FormClosing(object sender, FormClosingEventArgs e)
\r
330 public void SaveConfig()
\r
342 private void SaveMasterState()
\r
344 StoreShipGroupToConfig();
\r
345 var config = _config.ShipList;
\r
346 config.Visible = Visible && WindowState == FormWindowState.Normal;
\r
347 _settings.SetToShipListConfig(config);
\r
350 SaveBounds(config); // 最小化時は以前のサイズを記録する
\r
353 private void SaveSlaveState()
\r
357 if (WindowState != FormWindowState.Normal) // 最小化時は次回復旧しない
\r
359 var config = new ShipListConfig {Visible = true};
\r
360 _settings.SetToShipListConfig(config);
\r
361 _config.ListFormGroup.Add(config);
\r
362 SaveBounds(config);
\r
365 private void SaveBounds(ShipListConfig config)
\r
367 var bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds;
\r
368 config.Location = bounds.Location;
\r
369 config.Size = bounds.Size;
\r
372 public void ChangeWindowState(FormWindowState newState)
\r
376 if (newState == FormWindowState.Minimized)
\r
378 if (WindowState == FormWindowState.Normal)
\r
379 WindowState = FormWindowState.Minimized;
\r
380 if (_config.HideOnMinimized)
\r
381 ShowInTaskbar = false;
\r
385 if (WindowState == FormWindowState.Minimized)
\r
387 System.Windows.Forms.Application.DoEvents();
\r
388 if (_config.HideOnMinimized)
\r
389 ShowInTaskbar = true;
\r
390 WindowState = FormWindowState.Normal;
\r
395 private void ListForm_Activated(object sender, EventArgs e)
\r
399 if (_suppressActivate.Check())
\r
401 if (WindowState == FormWindowState.Minimized)
\r
403 RaiseBothWindows();
\r
406 private void RaiseBothWindows()
\r
408 _main.Owner = null;
\r
414 private void StoreShipGroupToConfig()
\r
416 var all = _sniffer.ShipList.Select(s => s.Id).ToArray();
\r
417 var group = _config.ShipList.ShipGroup;
\r
419 for (var i = 0; i < GroupConfigLabels.GroupCount; i++)
\r
421 if (all.Length > 0)
\r
422 shipListPanel.GroupSettings[i].IntersectWith(all);
\r
423 group.Add(shipListPanel.GroupSettings[i].ToList());
\r
427 public void ShowShip(int id)
\r
431 shipListPanel.ShowShip(id);
\r
433 else if (InFleetInfo)
\r
435 fleetPanel.ShowShip(id);
\r
437 else if (InAntiAir)
\r
439 antiAirPanel.ShowShip(id);
\r
443 private void comboBoxGroup_DropDownClosed(object sender, EventArgs e)
\r
445 SetActiveControl();
\r
448 private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
\r
450 _settings.Mode = comboBoxGroup.Text;
\r
452 SetActiveControl();
\r
453 if (!(InShipStatus || InGroupConfig || InRepairList))
\r
454 SetPanelSTypeState(false);
\r
457 private void ListForm_KeyPress(object sender, KeyPressEventArgs e)
\r
459 var g = Array.FindIndex(PanelKeys, x => x == char.ToUpper(e.KeyChar));
\r
462 comboBoxGroup.SelectedIndex = g;
\r
466 // マウスホイールでスクロールするためにコントロールにフォーカスを合わせる。
\r
467 private void SetActiveControl()
\r
471 ActiveControl = itemTreeView;
\r
473 else if (InFleetInfo)
\r
475 ActiveControl = fleetPanel;
\r
477 else if (InAntiAir)
\r
479 ActiveControl = antiAirPanel;
\r
483 ActiveControl = shipListPanel;
\r
487 private void itemCsvToolStripMenuItem_Click(object sender, EventArgs e)
\r
489 Clipboard.SetText(TextGenerator.GenerateItemList(_sniffer.ItemList));
\r
492 private void kantaiBunsekiToolStripMenuItem_Click(object sender, EventArgs e)
\r
494 Clipboard.SetText(TextGenerator.GenerateKantaiBunsekiItemList(_sniffer.ItemList));
\r
497 private void fleetTextToolStripMenuItem_Click(object sender, EventArgs e)
\r
499 Clipboard.SetText(TextGenerator.GenerateFleetData(_sniffer));
\r
502 private void deckBuilderToolStripMenuItem_Click(object sender, EventArgs e)
\r
504 Clipboard.SetText(TextGenerator.GenerateDeckBuilderData(_sniffer));
\r
507 private void labelHeaderCond_Click(object sender, EventArgs e)
\r
509 switch (_settings.SortOrder)
\r
511 case SortOrder.CondAscend:
\r
512 _settings.SortOrder = SortOrder.CondDescend;
\r
514 case SortOrder.CondDescend:
\r
515 _settings.SortOrder = SortOrder.None;
\r
518 _settings.SortOrder = SortOrder.CondAscend;
\r
524 private void labelHeaderExp_Click(object sender, EventArgs e)
\r
526 switch (_settings.SortOrder)
\r
528 case SortOrder.ExpToNextAscend:
\r
529 _settings.SortOrder = SortOrder.ExpToNextDescend;
\r
531 case SortOrder.ExpToNextDescend:
\r
532 _settings.SortOrder = SortOrder.None;
\r
535 _settings.SortOrder = SortOrder.ExpToNextAscend;
\r
541 private void shipCsvToolStripMenuItem_Click(object sender, EventArgs e)
\r
543 Clipboard.SetText(TextGenerator.GenerateShipList(shipListPanel.CurrentShipList));
\r
546 private void kantaiSarashiToolStripMenuItem_Click(object sender, EventArgs e)
\r
548 Clipboard.SetText(TextGenerator.GenerateKantaiSarashiData(shipListPanel.CurrentShipList));
\r
551 private void labelFleet_Click(object sender, EventArgs e)
\r
553 fleetPanel.ShowFleet(((Label)sender).Text);
\r
556 private void labelHeaderHp_Click(object sender, EventArgs e)
\r
561 private void ToggleHpPercent()
\r
563 _settings.ShowHpInPercent = !_settings.ShowHpInPercent;
\r
564 shipListPanel.ToggleHpPercent();
\r
565 battleResultPanel.ToggleHpPercent();
\r
568 private void labelSTypeButton_Click(object sender, EventArgs e)
\r
570 SetPanelSTypeState(!panelSType.Visible);
\r
573 private void checkBoxSType_Click(object sender, EventArgs e)
\r
575 _settings.ShipCategories = SelectedShipTypes;
\r
577 SetActiveControl();
\r
580 private ShipCategory SelectedShipTypes =>
\r
581 (ShipCategory)_shipTypeCheckBoxes.Select((cb, type) => cb.Checked ? 1 << type : 0).Sum();
\r
583 private void checkBoxSTypeAll_Click(object sender, EventArgs e)
\r
585 foreach (var checkBox in _shipTypeCheckBoxes)
\r
586 checkBox.Checked = checkBoxSTypeAll.Checked;
\r
587 checkBoxSType_Click(sender, e);
\r
590 private void panelSType_Click(object sender, EventArgs e)
\r
592 SetPanelSTypeState(false);
\r
595 private void SetPanelSTypeState(bool visible)
\r
597 panelSType.Visible = visible;
\r
599 panelSType.BringToFront();
\r
600 dropDownButtonSType.BackColor = visible ? CustomColors.ActiveButtonColor : DefaultBackColor;
\r
603 private void checkBoxSTypeDetails_Click(object sender, EventArgs e)
\r
605 _settings.ShipType = checkBoxSTypeDetails.Checked;
\r
607 SetActiveControl();
\r