FontInspector.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. boldField = new GUIToggleField(new LocEdString("Bold"));
  85. italicField = new GUIToggleField(new LocEdString("Italic"));
  86. dpiField = new GUIIntField(new LocEdString("DPI"));
  87. reimportButton = new GUIButton(new LocEdString("Reimport"));
  88. reimportButton.OnClick += TriggerReimport;
  89. Layout.AddElement(renderModeField);
  90. Layout.AddElement(boldField);
  91. Layout.AddElement(italicField);
  92. Layout.AddElement(dpiField);
  93. Layout.AddSpace(10);
  94. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  95. reimportButtonLayout.AddFlexibleSpace();
  96. reimportButtonLayout.AddElement(reimportButton);
  97. }
  98. /// <summary>
  99. /// Retrieves import options for the texture we're currently inspecting.
  100. /// </summary>
  101. /// <returns>Font import options object.</returns>
  102. private FontImportOptions GetImportOptions()
  103. {
  104. Font font = InspectedObject as Font;
  105. FontImportOptions output = null;
  106. if (font != null)
  107. {
  108. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(font));
  109. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  110. {
  111. FileEntry texFileEntry = (FileEntry)texEntry;
  112. output = texFileEntry.Options as FontImportOptions;
  113. }
  114. }
  115. if (output == null)
  116. {
  117. if (importOptions == null)
  118. output = new FontImportOptions();
  119. else
  120. output = importOptions;
  121. }
  122. return output;
  123. }
  124. /// <summary>
  125. /// Reimports the texture resource according to the currently set import options.
  126. /// </summary>
  127. private void TriggerReimport()
  128. {
  129. Texture2D texture = (Texture2D)InspectedObject;
  130. string resourcePath = ProjectLibrary.GetPath(texture);
  131. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  132. }
  133. /// <summary>
  134. /// Row element used for displaying GUI for font size array elements.
  135. /// </summary>
  136. public class FontSizeArrayRow : GUIListFieldRow
  137. {
  138. private GUIIntField sizeField;
  139. /// <inheritdoc/>
  140. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  141. {
  142. GUILayoutX titleLayout = layout.AddLayoutX();
  143. sizeField = new GUIIntField(new LocEdString(SeqIndex + ". "));
  144. titleLayout.AddElement(sizeField);
  145. sizeField.OnChanged += x => { SetValue(x); MarkAsModified(); };
  146. sizeField.OnFocusLost += ConfirmModify;
  147. sizeField.OnConfirmed += ConfirmModify;
  148. return titleLayout;
  149. }
  150. /// <inheritdoc/>
  151. internal protected override InspectableState Refresh()
  152. {
  153. sizeField.Value = GetValue<int>();
  154. return base.Refresh();
  155. }
  156. }
  157. /// <summary>
  158. /// Row element used for displaying GUI for character range array elements.
  159. /// </summary>
  160. public class CharRangeArrayRow : GUIListFieldRow
  161. {
  162. private GUIIntField rangeStartField;
  163. private GUIIntField rangeEndField;
  164. /// <inheritdoc/>
  165. protected override GUILayoutX CreateGUI(GUILayoutY layout)
  166. {
  167. GUILayoutX titleLayout = layout.AddLayoutX();
  168. rangeStartField = new GUIIntField(new LocEdString(SeqIndex + ". Start"));
  169. rangeEndField = new GUIIntField(new LocEdString("End"));
  170. titleLayout.AddElement(rangeStartField);
  171. titleLayout.AddElement(rangeEndField);
  172. rangeStartField.OnChanged += x =>
  173. {
  174. CharRange range = GetValue<CharRange>();
  175. range.start = x;
  176. SetValue(range);
  177. MarkAsModified();
  178. };
  179. rangeEndField.OnChanged += x =>
  180. {
  181. CharRange range = GetValue<CharRange>();
  182. range.end = x;
  183. SetValue(range);
  184. MarkAsModified();
  185. };
  186. rangeStartField.OnFocusLost += ConfirmModify;
  187. rangeStartField.OnConfirmed += ConfirmModify;
  188. rangeEndField.OnFocusLost += ConfirmModify;
  189. rangeEndField.OnConfirmed += ConfirmModify;
  190. return titleLayout;
  191. }
  192. /// <inheritdoc/>
  193. internal protected override InspectableState Refresh()
  194. {
  195. CharRange newValue = GetValue<CharRange>();
  196. rangeStartField.Value = newValue.start;
  197. rangeEndField.Value = newValue.end;
  198. return base.Refresh();
  199. }
  200. }
  201. }
  202. }