ScriptCodeInspector.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="ScriptCode"/> resource.
  12. /// </summary>
  13. [CustomInspector(typeof (ScriptCode))]
  14. internal class ScriptCodeInspector : Inspector
  15. {
  16. private const int MAX_SHOWN_CHARACTERS = 3000;
  17. private GUILabel textLabel = new GUILabel("", EditorStyles.MultiLineLabel, GUIOption.FixedHeight(500));
  18. private GUITexture textBg = new GUITexture(null, EditorStylesInternal.ScrollAreaBg);
  19. private GUIToggleField isEditorField = new GUIToggleField(new LocEdString("Is editor script"));
  20. private GUIReimportButton reimportButton;
  21. private string shownText = "";
  22. private ScriptCodeImportOptions importOptions;
  23. /// <inheritdoc/>
  24. protected internal override void Initialize()
  25. {
  26. LoadResource();
  27. ScriptCode scriptCode = InspectedObject as ScriptCode;
  28. if (scriptCode == null)
  29. return;
  30. importOptions = GetImportOptions();
  31. isEditorField.OnChanged += x =>
  32. {
  33. importOptions.EditorScript = x;
  34. };
  35. GUIPanel textPanel = Layout.AddPanel();
  36. GUILayout textLayoutY = textPanel.AddLayoutY();
  37. textLayoutY.AddSpace(5);
  38. GUILayout textLayoutX = textLayoutY.AddLayoutX();
  39. textLayoutX.AddSpace(5);
  40. textLayoutX.AddElement(textLabel);
  41. textLayoutX.AddSpace(5);
  42. textLayoutY.AddSpace(5);
  43. GUIPanel textBgPanel = textPanel.AddPanel(1);
  44. textBgPanel.AddElement(textBg);
  45. Layout.AddElement(isEditorField);
  46. Layout.AddSpace(10);
  47. reimportButton = new GUIReimportButton(InspectedResourcePath, Layout, () =>
  48. {
  49. ProjectLibrary.Reimport(InspectedResourcePath, importOptions, true);
  50. });
  51. UpdateGUIValues();
  52. }
  53. /// <inheritdoc/>
  54. protected internal override InspectableState Refresh(bool force = false)
  55. {
  56. reimportButton.Update();
  57. return InspectableState.NotModified;
  58. }
  59. /// <summary>
  60. /// Updates the GUI element values from the current import options object.
  61. /// </summary>
  62. private void UpdateGUIValues()
  63. {
  64. isEditorField.Value = importOptions.EditorScript;
  65. ScriptCode scriptCode = InspectedObject as ScriptCode;
  66. if (scriptCode == null)
  67. return;
  68. string newText = scriptCode.Text;
  69. string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
  70. if (newShownText != shownText)
  71. {
  72. textLabel.SetContent(newShownText);
  73. shownText = newShownText;
  74. }
  75. }
  76. /// <summary>
  77. /// Retrieves import options for the resource we're currently inspecting.
  78. /// </summary>
  79. /// <returns>Script code import options object.</returns>
  80. private ScriptCodeImportOptions GetImportOptions()
  81. {
  82. ScriptCode scriptCode = InspectedObject as ScriptCode;
  83. ScriptCodeImportOptions output = null;
  84. if (scriptCode != null)
  85. {
  86. LibraryEntry libEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(scriptCode));
  87. if (libEntry != null && libEntry.Type == LibraryEntryType.File)
  88. {
  89. FileEntry fileEntry = (FileEntry)libEntry;
  90. output = fileEntry.Options as ScriptCodeImportOptions;
  91. }
  92. }
  93. if (output == null)
  94. {
  95. if (importOptions == null)
  96. output = new ScriptCodeImportOptions();
  97. else
  98. output = importOptions;
  99. }
  100. return output;
  101. }
  102. }
  103. /** @} */
  104. }