FontInspector.cs 9.1 KB

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