2 using System.Collections.Generic;
5 using System.Threading.Tasks;
10 using Rectangle = System.Drawing.Rectangle;
11 using Color = System.Drawing.Color;
15 public class CActDigit : CActivity
17 Rectangle[] digitRect = new Rectangle[12];
18 CTexture[] texDigit = new CTexture[12];
22 Color colorGradBottom;
27 public int MaximumHeight { get; private set; }
29 public CActDigit(Color color, int pt)
35 public CActDigit(Color colorInside, Color colorEdge, int pt)
37 this.colorInside = colorInside;
38 this.colorEdge = colorEdge;
43 public CActDigit(Color colorInside, Color colorEdge, Color gradTop, Color gradBottom, int pt)
45 this.colorInside = colorInside;
46 this.colorEdge = colorEdge;
47 this.colorGradTop = gradTop;
48 this.colorGradBottom = gradBottom;
54 public override void OnManagedリソースの作成()
56 string dg = "0123456789";
60 for (int i = 0; i < texDigit.Length; ++i)
62 TextureFactory.tテクスチャの解放(ref texDigit[i]);
65 using (FontFamily ff = new FontFamily("MS PGothic"))
67 using (CPrivateFont cpf = new CPrivateFont(ff, pt, FontStyle.Bold))
69 for (int i = 0; i < 12; ++i)
74 sub = dg.Substring(i, 1);
84 x = cpf.DrawPrivateFont(sub, colorInside, colorEdge, colorGradTop, colorGradBottom);
88 x = cpf.DrawPrivateFont(sub, colorInside, colorEdge);
92 x = cpf.DrawPrivateFont(sub, colorInside);
95 using (Bitmap bmp = x)
97 texDigit[i] = TextureFactory.tテクスチャの生成(bmp);
98 digitRect[i] = MeasureRectangle(bmp);
99 MaximumHeight = Math.Max(MaximumHeight, digitRect[i].Height);
104 base.OnManagedリソースの作成();
108 public override void OnManagedリソースの解放()
112 for (int i = 0; i < texDigit.Length; ++i)
114 TextureFactory.tテクスチャの解放(ref texDigit[i]);
116 base.OnManagedリソースの解放();
121 /// bmp 内の不透明ピクセルからなる最大の範囲(幅)を得る。
122 /// CPrivateFont で文字描画前の bmp は透明になっているから透明でないピクセルをなめて実際の幅を得る。
123 /// (重いので描画処理中には呼ばないこと)
125 /// <param name="bmp">計算対象ビットマップ。</param>
126 /// <returns>有効なピクセルを囲う最小の矩形情報</returns>
127 private Rectangle MeasureRectangle(Bitmap bmp)
129 int xmin = bmp.Width;
131 int ymin = bmp.Height;
133 for (int x = 0; x < bmp.Width; ++x)
135 for (int y = 0; y < bmp.Height; ++y)
137 if (bmp.GetPixel(x, y).A != 0)
139 xmin = Math.Min(xmin, x);
140 xmax = Math.Max(xmax, x);
141 ymin = Math.Min(ymin, y);
142 ymax = Math.Max(ymax, y);
146 return new Rectangle(xmin, ymin, xmax - xmin, ymax - ymin);
149 public int Measure(int num)
151 return DrawSub((long)num, 0, 0, false);
154 public int Measure(long num)
156 return DrawSub(num, 0, 0, false);
159 private int numlen(int n)
174 public int Measure(double num, int prec)
177 int integer = (int)num;
178 ret += Measure(integer);
180 ret += digitRect[11].Width;
182 int dec = (int)((num - integer) * Math.Pow(10, prec));
183 for (int d = 0; d < prec - numlen(dec); ++d)
185 // 0.007 のような場合で 007 の 00 をカウントする
186 ret += digitRect[0].Width;
193 public void Draw(int num, int x, int y)
195 DrawSub((long)num, x, y);
198 public void Draw(long num, int x, int y)
200 DrawSub(num, x, y, true);
203 public void Draw(double num, int prec, int x, int y)
205 int integer = (int)num;
208 x += DrawSub(integer, x, y);
211 texDigit[11].t2D描画(CDTXMania.Instance.Device, x, y);
212 x += digitRect[11].Width;
215 int dec = (int)((num - integer) * Math.Pow(10, prec));
216 for (int d = 0; d < prec - numlen(dec); ++d)
218 x += DrawSub(0, x, y, true);
220 DrawSub((int)dec, x, y);
223 private int DrawSub(long num, int x = 0, int y = 0, bool draw = true)
227 List<int> d = new List<int>();
239 d.Add((int)(num % 10));
251 if (texDigit[t] != null)
255 texDigit[t].t2D描画(CDTXMania.Instance.Device, x, y, digitRect[t]);
256 x += digitRect[t].Width;
258 ret += digitRect[t].Width;