FontInspector.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Renders an inspector for the <see cref="Font"/> resource.
  8. /// </summary>
  9. [CustomInspector(typeof(Font))]
  10. internal class FontInspector : Inspector
  11. {
  12. private bool isInitialized;
  13. private GUIArray fontSizes;
  14. private GUIArray charRanges;
  15. private GUIToggleField antialiasingField;
  16. private GUIIntField dpiField;
  17. private GUIButton reimportButton;
  18. private FontImportOptions importOptions;
  19. /// <summary>
  20. /// Initializes required data the first time <see cref="Refresh"/> is called.
  21. /// </summary>
  22. private void Initialize()
  23. {
  24. if (referencedObject != null)
  25. {
  26. importOptions = GetImportOptions();
  27. RebuildGUI();
  28. }
  29. isInitialized = true;
  30. }
  31. /// <summary>
  32. /// Retrieves import options for the texture we're currently inspecting.
  33. /// </summary>
  34. /// <returns>Font import options object.</returns>
  35. private FontImportOptions GetImportOptions()
  36. {
  37. Font font = referencedObject as Font;
  38. FontImportOptions output = null;
  39. if (font != null)
  40. {
  41. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(font));
  42. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  43. {
  44. FileEntry texFileEntry = (FileEntry)texEntry;
  45. output = texFileEntry.Options as FontImportOptions;
  46. }
  47. }
  48. if (output == null)
  49. {
  50. if (importOptions == null)
  51. output = new FontImportOptions();
  52. else
  53. output = importOptions;
  54. }
  55. return output;
  56. }
  57. /// <summary>
  58. /// Recreates all the GUI elements used by this inspector.
  59. /// </summary>
  60. private void RebuildGUI()
  61. {
  62. layout.Clear();
  63. fontSizes = GUIArray.Create<FontSizeArrayRow, int>(
  64. new LocEdString("Font sizes"), importOptions.FontSizes, layout);
  65. fontSizes.OnChanged += x =>
  66. {
  67. int[] newFontSizes = x as int[];
  68. importOptions.FontSizes = newFontSizes;
  69. RebuildGUI();
  70. };
  71. charRanges = GUIArray.Create<CharRangeArrayRow, CharRange>(
  72. new LocEdString("Character ranges"), importOptions.CharRanges, layout);
  73. charRanges.OnChanged += x =>
  74. {
  75. CharRange[] newRanges = x as CharRange[];
  76. importOptions.CharRanges = newRanges;
  77. RebuildGUI();
  78. };
  79. antialiasingField = new GUIToggleField(new LocEdString("Antialiasing"));
  80. dpiField = new GUIIntField(new LocEdString("DPI"));
  81. reimportButton = new GUIButton(new LocEdString("Reimport"));
  82. reimportButton.OnClick += TriggerReimport;
  83. layout.AddElement(antialiasingField);
  84. layout.AddElement(dpiField);
  85. layout.AddSpace(10);
  86. GUILayout reimportButtonLayout = layout.AddLayoutX();
  87. reimportButtonLayout.AddFlexibleSpace();
  88. reimportButtonLayout.AddElement(reimportButton);
  89. }
  90. /// <summary>
  91. /// Reimports the texture resource according to the currently set import options.
  92. /// </summary>
  93. private void TriggerReimport()
  94. {
  95. Texture2D texture = (Texture2D)referencedObject;
  96. string resourcePath = ProjectLibrary.GetPath(texture);
  97. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  98. }
  99. /// <inheritdoc/>
  100. internal override bool Refresh()
  101. {
  102. if (!isInitialized)
  103. {
  104. Initialize();
  105. isInitialized = true;
  106. }
  107. FontImportOptions newImportOptions = GetImportOptions();
  108. bool rebuildGUI = false;
  109. int[] newFontSizes = newImportOptions.FontSizes;
  110. if (newFontSizes == null)
  111. rebuildGUI |= fontSizes.Array != null;
  112. else
  113. {
  114. if (fontSizes.Array == null)
  115. rebuildGUI = true;
  116. else
  117. rebuildGUI |= newFontSizes.Length != fontSizes.Array.GetLength(0);
  118. }
  119. CharRange[] newCharRanges = newImportOptions.CharRanges;
  120. if (newCharRanges == null)
  121. rebuildGUI |= charRanges.Array != null;
  122. else
  123. {
  124. if (charRanges.Array == null)
  125. rebuildGUI = true;
  126. else
  127. rebuildGUI |= newCharRanges.Length != charRanges.Array.GetLength(0);
  128. }
  129. if (rebuildGUI)
  130. RebuildGUI();
  131. bool anythingModified = fontSizes.Refresh();
  132. anythingModified |= charRanges.Refresh();
  133. if (antialiasingField.Value != newImportOptions.Antialiasing)
  134. {
  135. antialiasingField.Value = newImportOptions.Antialiasing;
  136. anythingModified = true;
  137. }
  138. if (dpiField.Value != newImportOptions.DPI)
  139. {
  140. dpiField.Value = newImportOptions.DPI;
  141. anythingModified = true;
  142. }
  143. if (anythingModified)
  144. importOptions = newImportOptions;
  145. return anythingModified;
  146. }
  147. /// <summary>
  148. /// Row element used for displaying GUI for font size array elements.
  149. /// </summary>
  150. public class FontSizeArrayRow : GUIListRow
  151. {
  152. private GUIIntField sizeField;
  153. /// <inheritdoc/>
  154. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  155. {
  156. GUILayoutX titleLayout = layout.AddLayoutX();
  157. sizeField = new GUIIntField(new LocEdString(seqIndex + ". "));
  158. titleLayout.AddElement(sizeField);
  159. sizeField.OnChanged += SetValue;
  160. return titleLayout;
  161. }
  162. /// <inheritdoc/>
  163. internal protected override bool Refresh()
  164. {
  165. int newValue = GetValue<int>();
  166. if (sizeField.Value != newValue)
  167. {
  168. sizeField.Value = newValue;
  169. return true;
  170. }
  171. return false;
  172. }
  173. }
  174. /// <summary>
  175. /// Row element used for displaying GUI for character range array elements.
  176. /// </summary>
  177. public class CharRangeArrayRow : GUIListRow
  178. {
  179. private GUIIntField rangeStartField;
  180. private GUIIntField rangeEndField;
  181. /// <inheritdoc/>
  182. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  183. {
  184. GUILayoutX titleLayout = layout.AddLayoutX();
  185. rangeStartField = new GUIIntField(new LocEdString(seqIndex + ". Start"));
  186. rangeEndField = new GUIIntField(new LocEdString("End"));
  187. titleLayout.AddElement(rangeStartField);
  188. titleLayout.AddElement(rangeEndField);
  189. rangeStartField.OnChanged += x =>
  190. {
  191. CharRange range = GetValue<CharRange>();
  192. range.start = x;
  193. SetValue(range);
  194. };
  195. rangeEndField.OnChanged += x =>
  196. {
  197. CharRange range = GetValue<CharRange>();
  198. range.end = x;
  199. SetValue(range);
  200. };
  201. return titleLayout;
  202. }
  203. /// <inheritdoc/>
  204. internal protected override bool Refresh()
  205. {
  206. bool anythingModified = false;
  207. CharRange newValue = GetValue<CharRange>();
  208. if (rangeStartField.Value != newValue.start)
  209. {
  210. rangeStartField.Value = newValue.start;
  211. anythingModified = true;
  212. }
  213. if (rangeEndField.Value != newValue.end)
  214. {
  215. rangeEndField.Value = newValue.end;
  216. anythingModified = true;
  217. }
  218. return anythingModified;
  219. }
  220. }
  221. }
  222. }