PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Layers/TextLayer.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 244 lines | 210 code | 31 blank | 3 comment | 21 complexity | ad0a47c5352143107d4a7916c92ef866 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System.Drawing;
  2. using System.Windows.Media;
  3. using System.Xml.Linq;
  4. using RT.Util.Lingo;
  5. using RT.Util.Serialization;
  6. using WotDataLib;
  7. namespace TankIconMaker.Layers
  8. {
  9. abstract class TextLayer : LayerBase
  10. {
  11. public TextSmoothingStyle FontSmoothing { get; set; }
  12. public static MemberTr FontSmoothingTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontSmoothing); }
  13. public string FontFamily { get; set; }
  14. public static MemberTr FontFamilyTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontFamily); }
  15. public double FontSize { get; set; }
  16. public static MemberTr FontSizeTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontSize); }
  17. public bool FontBold { get; set; }
  18. public static MemberTr FontBoldTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontBold); }
  19. public bool FontItalic { get; set; }
  20. public static MemberTr FontItalicTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontItalic); }
  21. public ColorSelector FontColor { get; set; }
  22. public static MemberTr FontColorTr(Translation tr) { return new MemberTr(tr.Category.Font, tr.TextLayer.FontColor); }
  23. public Anchor Anchor { get; set; }
  24. public static MemberTr AnchorTr(Translation tr) { return new MemberTr(tr.Category.Position, tr.TextLayer.Anchor); }
  25. public int X { get; set; }
  26. public static MemberTr XTr(Translation tr) { return new MemberTr(tr.Category.Position, tr.TextLayer.X); }
  27. public int Y { get; set; }
  28. public static MemberTr YTr(Translation tr) { return new MemberTr(tr.Category.Position, tr.TextLayer.Y); }
  29. public int Width { get; set; }
  30. public static MemberTr WidthTr(Translation tr) { return new MemberTr(tr.Category.Size, tr.TextLayer.Width); }
  31. public int Height { get; set; }
  32. public static MemberTr HeightTr(Translation tr) { return new MemberTr(tr.Category.Size, tr.TextLayer.Height); }
  33. public bool Baseline { get; set; }
  34. public static MemberTr BaselineTr(Translation tr) { return new MemberTr(tr.Category.Position, tr.TextLayer.Baseline); }
  35. public string Format { get; set; }
  36. public static MemberTr FormatTr(Translation tr) { return new MemberTr(tr.Category.TextSource, tr.TextLayer.Format); }
  37. #region Old
  38. // Old stuff, to be deleted eventually...
  39. [ClassifyIgnoreIfDefault]
  40. private int Left, Right, Top, Bottom;
  41. [ClassifyIgnoreIfDefault]
  42. private bool LeftAnchor, RightAnchor, TopAnchor, BottomAnchor;
  43. #endregion
  44. protected abstract string GetText(Tank tank);
  45. public TextLayer()
  46. {
  47. FontFamily = "Arial";
  48. FontSize = 8.5;
  49. X = 3;
  50. Y = 3;
  51. Width = 999;
  52. Height = 999;
  53. Anchor = Anchor.TopLeft;
  54. Baseline = false;
  55. FontColor = new ColorSelector(Colors.White);
  56. }
  57. public override LayerBase Clone()
  58. {
  59. var result = (TextLayer) base.Clone();
  60. result.FontColor = FontColor.Clone();
  61. return result;
  62. }
  63. public override BitmapBase Draw(Tank tank)
  64. {
  65. return Ut.NewBitmapGdi(ParentStyle.IconWidth, ParentStyle.IconHeight, dc =>
  66. {
  67. string text = GetText(tank);
  68. if (!string.IsNullOrEmpty(Format))
  69. {
  70. long numI;
  71. decimal numD;
  72. if (long.TryParse(text, out numI))
  73. try { text = string.Format(Format, numI); }
  74. catch { throw new StyleUserError(App.Translation.TextLayer.FormatStringInvalidNum.Fmt(Format, numI)); }
  75. else if (decimal.TryParse(text, out numD))
  76. try { text = string.Format(Format, numD); }
  77. catch { throw new StyleUserError(App.Translation.TextLayer.FormatStringInvalidNum.Fmt(Format, numD)); }
  78. else
  79. try { text = string.Format(Format, text); }
  80. catch { throw new StyleUserError(App.Translation.TextLayer.FormatStringInvalid.Fmt(Format)); }
  81. }
  82. var style = (FontBold ? FontStyle.Bold : 0) | (FontItalic ? FontStyle.Italic : 0);
  83. dc.TextRenderingHint = FontSmoothing.ToGdi();
  84. dc.DrawString(text, new SolidBrush(FontColor.GetColorGdi(tank)), FontFamily, FontSize, style, X, Y, Anchor,
  85. Width <= 0 ? null : (int?) Width, Height <= 0 ? null : (int?) Height, Baseline);
  86. });
  87. }
  88. protected override void AfterDeserialize(XElement xml)
  89. {
  90. base.AfterDeserialize(xml);
  91. // At one point, a field called "ConvertedFromOld" was introduced instead of increasing Version to 2. The following is a fix for this.
  92. if (xml.Element("ConvertedFromOld") != null && xml.Element("ConvertedFromOld").Value == "True")
  93. SavedByVersion = 2;
  94. // Upgrade to v2
  95. if (SavedByVersion < 2)
  96. {
  97. SavedByVersion = 2;
  98. AnchorRaw anchor;
  99. if (LeftAnchor && RightAnchor)
  100. {
  101. X = (Left + Right) / 2;
  102. anchor = AnchorRaw.Center;
  103. }
  104. else if (LeftAnchor)
  105. {
  106. X = Left;
  107. anchor = AnchorRaw.Left;
  108. }
  109. else if (RightAnchor)
  110. {
  111. X = Right;
  112. anchor = AnchorRaw.Right;
  113. }
  114. else
  115. {
  116. X = 80 / 2; // ok to hard-code 80 because that was the IconWidth of all styles as old as this one
  117. anchor = AnchorRaw.Center;
  118. }
  119. if (TopAnchor && BottomAnchor)
  120. {
  121. Y = (Top + Bottom) / 2;
  122. anchor |= AnchorRaw.Mid;
  123. }
  124. else if (TopAnchor)
  125. {
  126. Y = Top;
  127. anchor |= AnchorRaw.Top;
  128. }
  129. else if (BottomAnchor)
  130. {
  131. Y = Bottom;
  132. anchor |= AnchorRaw.Bottom;
  133. }
  134. else
  135. {
  136. Y = 24 / 2; // ok to hard-code 24 because that was the IconHeight of all styles as old as this one
  137. anchor |= AnchorRaw.Mid;
  138. }
  139. Anchor = (Anchor) anchor;
  140. }
  141. Left = Right = Top = Bottom = 0;
  142. LeftAnchor = RightAnchor = TopAnchor = BottomAnchor = false;
  143. }
  144. }
  145. sealed class PropertyTextLayer : TextLayer
  146. {
  147. public override int Version { get { return 2; } }
  148. public override string TypeName { get { return App.Translation.PropertyTextLayer.LayerName; } }
  149. public override string TypeDescription { get { return App.Translation.PropertyTextLayer.LayerDescription; } }
  150. public ExtraPropertyId Property { get; set; }
  151. public static MemberTr PropertyTr(Translation tr) { return new MemberTr(tr.Category.TextSource, tr.PropertyTextLayer.Property); }
  152. public PropertyTextLayer()
  153. {
  154. Property = new ExtraPropertyId("NameShort", null, "Wargaming");
  155. }
  156. protected override string GetText(Tank tank)
  157. {
  158. return tank[Property];
  159. }
  160. }
  161. sealed class CustomTextLayer : TextLayer
  162. {
  163. public override int Version { get { return 2; } }
  164. public override string TypeName { get { return App.Translation.CustomTextLayer.LayerName; } }
  165. public override string TypeDescription { get { return App.Translation.CustomTextLayer.LayerDescription; } }
  166. public ValueSelector<string> Text { get; set; }
  167. public static MemberTr TextTr(Translation tr) { return new MemberTr(tr.Category.TextSource, tr.CustomTextLayer.Text); }
  168. public CustomTextLayer()
  169. {
  170. Text = new ValueSelector<string>();
  171. Text.Single = "Example";
  172. Text.CountryNone = "";
  173. Text.CountryUSSR = "СССР";
  174. Text.CountryGermany = "Deutschland";
  175. Text.CountryUSA = "USA";
  176. Text.CountryFrance = "France";
  177. Text.CountryChina = "中国";
  178. Text.CountryUK = "UK";
  179. Text.CountryJapan = "日本";
  180. Text.CountryCzech = "Česko";
  181. Text.ClassNone = "";
  182. Text.ClassLight = "LT";
  183. Text.ClassMedium = "MT";
  184. Text.ClassHeavy = "HT";
  185. Text.ClassDestroyer = "D";
  186. Text.ClassArtillery = "A";
  187. Text.CategNormal = "•";
  188. Text.CategPremium = "♦";
  189. Text.CategSpecial = "∞";
  190. Text.TierNone = "";
  191. Text.Tier1 = "I";
  192. Text.Tier2 = "II";
  193. Text.Tier3 = "III";
  194. Text.Tier4 = "IV";
  195. Text.Tier5 = "V";
  196. Text.Tier6 = "VI";
  197. Text.Tier7 = "VII";
  198. Text.Tier8 = "VIII";
  199. Text.Tier9 = "IX";
  200. Text.Tier10 = "X";
  201. }
  202. public override LayerBase Clone()
  203. {
  204. var result = (CustomTextLayer) base.Clone();
  205. result.Text = Text.Clone();
  206. return result;
  207. }
  208. protected override string GetText(Tank tank)
  209. {
  210. return Text.GetValue(tank);
  211. }
  212. }
  213. }