FontInspector.cs 7.3 KB

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