FontInspector.cs 8.0 KB

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