FontInspector.cs 8.9 KB

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