OSDN Git Service

連合艦隊を組んだときに連合艦隊表示にならないをの直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / MainWindow / ChargeStatus.cs
1 // Copyright (C) 2020 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System.Drawing;\r
16 using System.Windows.Forms;\r
17 \r
18 namespace KancolleSniffer.View.MainWindow\r
19 {\r
20     public class ChargeStatus : Control, IUpdateContext\r
21     {\r
22         private readonly Color[] _colors = {CUDColors.Yellow, CUDColors.Orange, CUDColors.Red, CUDColors.LightGray};\r
23 \r
24         private float IconWidth => Width * 8 / 17.0f;\r
25 \r
26         private float Head => Height * 8 / 13f;\r
27 \r
28         private float BulletLeft => Width * 9 / 17.0f;\r
29 \r
30         private readonly Brush _defaultBrush = new SolidBrush(DefaultBackColor);\r
31 \r
32         private Graphics _g;\r
33 \r
34         private Model.ChargeStatus _status;\r
35 \r
36         public UpdateContext Context { get; set; }\r
37 \r
38         public override string Text { get; set; } = "";\r
39 \r
40         public new void Update()\r
41         {\r
42             var fleet = (int)Tag;\r
43             _status = Context.Sniffer.Fleets[fleet].ChargeStatus;\r
44             Text = _status.Empty ? "" : $"燃{_status.FuelRatio * 100:f1}% 弾{_status.BullRatio * 100:f1}%";\r
45             Invalidate();\r
46         }\r
47 \r
48         protected override void OnPaint(PaintEventArgs e)\r
49         {\r
50             if (_status == null)\r
51                 return;\r
52             _g = e.Graphics;\r
53             DrawFuelStatus();\r
54             DrawBulletStatus();\r
55         }\r
56 \r
57         private void DrawFuelStatus()\r
58         {\r
59             var charge = _status.Fuel;\r
60             if (charge % 5 == 0)\r
61             {\r
62                 Clear(0);\r
63                 return;\r
64             }\r
65             DrawFuelHead(charge);\r
66             DrawStatus(0, charge % 5 - 1);\r
67         }\r
68 \r
69         private void DrawFuelHead(int charge)\r
70         {\r
71             _g.FillPolygon(\r
72                 new SolidBrush(charge <= 4 ? Color.FromArgb(83, 131, 52) : Color.FromArgb(178, 196, 165)),\r
73                 new[]\r
74                 {\r
75                     new PointF(0, 0), new PointF(IconWidth, 0), new PointF(IconWidth, Height), new Point(0, Height)\r
76                 });\r
77         }\r
78 \r
79         private void DrawBulletStatus()\r
80         {\r
81             var charge = _status.Bull;\r
82             if (charge % 5 == 0)\r
83             {\r
84                 Clear(BulletLeft);\r
85                 return;\r
86             }\r
87             DrawBulletHead(charge);\r
88             DrawStatus(BulletLeft, charge % 5 - 1);\r
89         }\r
90 \r
91         private void DrawBulletHead(int charge)\r
92         {\r
93             var l = BulletLeft;\r
94             var r = l + IconWidth;\r
95             var curve = Height * 6 / 13f;\r
96             var brush = new SolidBrush(charge <= 4 ? Color.FromArgb(153, 101, 0) : Color.FromArgb(205, 185, 144));\r
97             _g.FillClosedCurve(brush,\r
98                 new[] {new PointF(l, curve), new PointF(l + IconWidth / 2.0f, -1), new PointF(r, curve)});\r
99             _g.FillPolygon(brush,\r
100                 new[]\r
101                 {\r
102                     new PointF(l, curve), new PointF(r, curve), new PointF(r, Height), new PointF(l, Height)\r
103                 });\r
104         }\r
105 \r
106         private void DrawStatus(float left, int color)\r
107         {\r
108             var right = left + IconWidth;\r
109             _g.FillPolygon(new SolidBrush(_colors[color]),\r
110                 new[]\r
111                 {\r
112                     new PointF(left, Head), new PointF(right, Head), new PointF(right, Height), new PointF(left, Height)\r
113                 });\r
114         }\r
115 \r
116         private void Clear(float left)\r
117         {\r
118             _g.FillRectangle(_defaultBrush, left, 0, left + IconWidth, Height);\r
119         }\r
120     }\r
121 }