ScriptCodeInspector.cs 4.4 KB

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