ConsoleShapePart.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using MinorShift.Emuera;
  6. namespace NTERA.Game.Display
  7. {
  8. abstract class ConsoleShapePart : AConsoleColoredPart
  9. {
  10. public static ConsoleShapePart CreateShape(string shapeType, int[] param, Color color, Color bcolor, bool colorchanged)
  11. {
  12. string type = shapeType.ToLower();
  13. colorchanged = colorchanged || color != Config.ForeColor;
  14. StringBuilder sb = new StringBuilder();
  15. sb.Append("<shape type='");
  16. sb.Append(type);
  17. sb.Append("' param='");
  18. for (int i = 0; i < param.Length;i++ )
  19. {
  20. sb.Append(param[i].ToString());
  21. if (i < param.Length - 1)
  22. sb.Append(", ");
  23. }
  24. sb.Append("'");
  25. if(colorchanged)
  26. {
  27. sb.Append(" color='");
  28. sb.Append(HtmlManager.GetColorToString(color));
  29. sb.Append("'");
  30. }
  31. if(bcolor != Config.FocusColor)
  32. {
  33. sb.Append(" bcolor='");
  34. sb.Append(HtmlManager.GetColorToString(bcolor));
  35. sb.Append("'");
  36. }
  37. sb.Append(">");
  38. ConsoleShapePart ret = null;
  39. int lineHeight = Config.FontSize;
  40. float[] paramPixel = new float[param.Length];
  41. for (int i = 0; i < param.Length; i++)
  42. {
  43. paramPixel[i] = ((float)param[i] * lineHeight) / 100f;
  44. }
  45. RectangleF rectF;
  46. switch (type)
  47. {
  48. case "space":
  49. if (paramPixel.Length == 1 && paramPixel[0] >= 0)
  50. {
  51. rectF = new RectangleF(0, 0, paramPixel[0], lineHeight);
  52. ret = new ConsoleSpacePart(rectF);
  53. }
  54. break;
  55. case "rect":
  56. if (paramPixel.Length == 1 && paramPixel[0] > 0)
  57. {
  58. rectF = new RectangleF(0, 0, paramPixel[0], lineHeight);
  59. ret = new ConsoleRectangleShapePart(rectF);
  60. }
  61. else if (paramPixel.Length == 4)
  62. {
  63. rectF = new RectangleF(paramPixel[0], paramPixel[1], paramPixel[2], paramPixel[3]);
  64. //1820a12 サイズ上限撤廃
  65. if (rectF.X >= 0 && rectF.Width > 0 && rectF.Height > 0)
  66. // rectF.Y >= 0 && (rectF.Y + rectF.Height) <= lineHeight)
  67. {
  68. ret = new ConsoleRectangleShapePart(rectF);
  69. }
  70. }
  71. break;
  72. case "polygon":
  73. break;
  74. }
  75. if (ret == null)
  76. {
  77. ret = new ConsoleErrorShapePart(sb.ToString());
  78. }
  79. ret.AltText = sb.ToString();
  80. ret.Color = color;
  81. ret.ButtonColor = bcolor;
  82. ret.colorChanged = colorchanged;
  83. return ret;
  84. }
  85. public override bool CanDivide => false;
  86. public override string ToString()
  87. {
  88. if (AltText == null)
  89. return "";
  90. return AltText;
  91. }
  92. }
  93. internal sealed class ConsoleRectangleShapePart : ConsoleShapePart
  94. {
  95. public ConsoleRectangleShapePart(RectangleF theRect)
  96. {
  97. Str = "";
  98. originalRectF = theRect;
  99. WidthF = theRect.X + theRect.Width;
  100. rect.Y = (int)theRect.Y;
  101. //if (rect.Y == 0 && theRect.Y >= 0.001f)
  102. // rect.Y = 1;
  103. rect.Height = (int)theRect.Height;
  104. if (rect.Height == 0 && theRect.Height >= 0.001f)
  105. rect.Height = 1;
  106. top = Math.Min(0, rect.Y);
  107. bottom = Math.Max(Config.FontSize, rect.Y + rect.Height);
  108. }
  109. private readonly int top;
  110. private readonly int bottom;
  111. public override int Top => top;
  112. public override int Bottom => bottom;
  113. readonly RectangleF originalRectF;
  114. bool visible;
  115. Rectangle rect;
  116. public override void DrawTo(Graphics graph, int pointY, bool isSelecting, bool isBackLog, TextDrawingMode mode)
  117. {
  118. if (!visible)
  119. return;
  120. Rectangle targetRect = rect;
  121. targetRect.X = targetRect.X + PointX;
  122. targetRect.Y = targetRect.Y + pointY;
  123. Color dcolor = isSelecting ? ButtonColor : Color;
  124. graph.FillRectangle(new SolidBrush(dcolor), targetRect);
  125. }
  126. public override void SetWidth(StringMeasure sm, float subPixel)
  127. {
  128. float widF = (subPixel + WidthF);
  129. Width = (int)(widF);
  130. XsubPixel = widF - Width;
  131. rect.X = (int)(subPixel + originalRectF.X);
  132. rect.Width = Width - rect.X;
  133. rect.X += Config.DrawingParam_ShapePositionShift;
  134. visible = (rect.X >= 0 && rect.Width > 0);// && rect.Y >= 0 && (rect.Y + rect.Height) <= Config.FontSize);
  135. }
  136. }
  137. internal sealed class ConsoleSpacePart : ConsoleShapePart
  138. {
  139. public ConsoleSpacePart(RectangleF theRect)
  140. {
  141. Str = "";
  142. WidthF = theRect.Width;
  143. //Width = width;
  144. }
  145. public override void DrawTo(Graphics graph, int pointY, bool isSelecting, bool isBackLog, TextDrawingMode mode) { }
  146. public override void SetWidth(StringMeasure sm,float subPixel)
  147. {
  148. float widF = (subPixel + WidthF);
  149. Width = (int)(widF);
  150. XsubPixel = widF - Width;
  151. }
  152. }
  153. internal sealed class ConsoleErrorShapePart : ConsoleShapePart
  154. {
  155. public ConsoleErrorShapePart(string errMes)
  156. {
  157. Str = errMes;
  158. AltText = errMes;
  159. }
  160. public override void DrawTo(Graphics graph, int pointY, bool isSelecting, bool isBackLog, TextDrawingMode mode)
  161. {
  162. if (mode == TextDrawingMode.GRAPHICS)
  163. graph.DrawString(Str, Config.Font, new SolidBrush(Config.ForeColor), new Point(PointX, pointY));
  164. else
  165. TextRenderer.DrawText(graph, Str, Config.Font, new Point(PointX, pointY), Config.ForeColor, TextFormatFlags.NoPrefix);
  166. }
  167. public override void SetWidth(StringMeasure sm, float subPixel)
  168. {
  169. if (Error)
  170. {
  171. Width = 0;
  172. return;
  173. }
  174. Width = sm.GetDisplayLength(Str, Config.Font);
  175. XsubPixel = subPixel;
  176. }
  177. }
  178. }