FontInspector.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, FontSizeArrayRow> fontSizes;
  13. private GUIArrayField<CharRange, CharRangeArrayRow> charRanges;
  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 = GUIArrayField<int, FontSizeArrayRow>.Create(
  67. new LocEdString("Font sizes"), importOptions.FontSizes, Layout);
  68. fontSizes.OnChanged += x => importOptions.FontSizes = x;
  69. charRanges = GUIArrayField<CharRange, CharRangeArrayRow>.Create(
  70. new LocEdString("Character ranges"), importOptions.CharRanges, Layout);
  71. charRanges.OnChanged += x => importOptions.CharRanges = x;
  72. antialiasingField = new GUIToggleField(new LocEdString("Antialiasing"));
  73. dpiField = new GUIIntField(new LocEdString("DPI"));
  74. reimportButton = new GUIButton(new LocEdString("Reimport"));
  75. reimportButton.OnClick += TriggerReimport;
  76. Layout.AddElement(antialiasingField);
  77. Layout.AddElement(dpiField);
  78. Layout.AddSpace(10);
  79. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  80. reimportButtonLayout.AddFlexibleSpace();
  81. reimportButtonLayout.AddElement(reimportButton);
  82. }
  83. /// <summary>
  84. /// Retrieves import options for the texture we're currently inspecting.
  85. /// </summary>
  86. /// <returns>Font import options object.</returns>
  87. private FontImportOptions GetImportOptions()
  88. {
  89. Font font = InspectedObject as Font;
  90. FontImportOptions output = null;
  91. if (font != null)
  92. {
  93. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(font));
  94. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  95. {
  96. FileEntry texFileEntry = (FileEntry)texEntry;
  97. output = texFileEntry.Options as FontImportOptions;
  98. }
  99. }
  100. if (output == null)
  101. {
  102. if (importOptions == null)
  103. output = new FontImportOptions();
  104. else
  105. output = importOptions;
  106. }
  107. return output;
  108. }
  109. /// <summary>
  110. /// Reimports the texture resource according to the currently set import options.
  111. /// </summary>
  112. private void TriggerReimport()
  113. {
  114. Texture2D texture = (Texture2D)InspectedObject;
  115. string resourcePath = ProjectLibrary.GetPath(texture);
  116. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  117. }
  118. /// <summary>
  119. /// Row element used for displaying GUI for font size array elements.
  120. /// </summary>
  121. public class FontSizeArrayRow : GUIListFieldRow
  122. {
  123. private GUIIntField sizeField;
  124. /// <inheritdoc/>
  125. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  126. {
  127. GUILayoutX titleLayout = layout.AddLayoutX();
  128. sizeField = new GUIIntField(new LocEdString(seqIndex + ". "));
  129. titleLayout.AddElement(sizeField);
  130. sizeField.OnChanged += SetValue;
  131. return titleLayout;
  132. }
  133. /// <inheritdoc/>
  134. internal protected override bool Refresh()
  135. {
  136. sizeField.Value = GetValue<int>();
  137. return false;
  138. }
  139. }
  140. /// <summary>
  141. /// Row element used for displaying GUI for character range array elements.
  142. /// </summary>
  143. public class CharRangeArrayRow : GUIListFieldRow
  144. {
  145. private GUIIntField rangeStartField;
  146. private GUIIntField rangeEndField;
  147. /// <inheritdoc/>
  148. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  149. {
  150. GUILayoutX titleLayout = layout.AddLayoutX();
  151. rangeStartField = new GUIIntField(new LocEdString(seqIndex + ". Start"));
  152. rangeEndField = new GUIIntField(new LocEdString("End"));
  153. titleLayout.AddElement(rangeStartField);
  154. titleLayout.AddElement(rangeEndField);
  155. rangeStartField.OnChanged += x =>
  156. {
  157. CharRange range = GetValue<CharRange>();
  158. range.start = x;
  159. SetValue(range);
  160. };
  161. rangeEndField.OnChanged += x =>
  162. {
  163. CharRange range = GetValue<CharRange>();
  164. range.end = x;
  165. SetValue(range);
  166. };
  167. return titleLayout;
  168. }
  169. /// <inheritdoc/>
  170. internal protected override bool Refresh()
  171. {
  172. CharRange newValue = GetValue<CharRange>();
  173. rangeStartField.Value = newValue.start;
  174. rangeEndField.Value = newValue.end;
  175. return false;
  176. }
  177. }
  178. }
  179. }