ScriptCodeInspector.cs 4.2 KB

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