ScriptCodeInspector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /** @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 string shownText = "";
  21. private ScriptCodeImportOptions importOptions;
  22. /// <inheritdoc/>
  23. protected internal override void Initialize()
  24. {
  25. ScriptCode scriptCode = InspectedObject as ScriptCode;
  26. if (scriptCode == null)
  27. return;
  28. importOptions = GetImportOptions();
  29. isEditorField.OnChanged += x =>
  30. {
  31. importOptions.EditorScript = x;
  32. };
  33. GUIPanel textPanel = Layout.AddPanel();
  34. GUILayout textLayoutY = textPanel.AddLayoutY();
  35. textLayoutY.AddSpace(5);
  36. GUILayout textLayoutX = textLayoutY.AddLayoutX();
  37. textLayoutX.AddSpace(5);
  38. textLayoutX.AddElement(textLabel);
  39. textLayoutX.AddSpace(5);
  40. textLayoutY.AddSpace(5);
  41. GUIPanel textBgPanel = textPanel.AddPanel(1);
  42. textBgPanel.AddElement(textBg);
  43. Layout.AddElement(isEditorField);
  44. GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
  45. reimportButton.OnClick += TriggerReimport;
  46. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  47. reimportButtonLayout.AddElement(reimportButton);
  48. reimportButtonLayout.AddFlexibleSpace();
  49. }
  50. /// <inheritdoc/>
  51. protected internal override InspectableState Refresh()
  52. {
  53. ScriptCode scriptCode = InspectedObject as ScriptCode;
  54. if (scriptCode == null)
  55. return InspectableState.NotModified;
  56. isEditorField.Value = importOptions.EditorScript;
  57. string newText = scriptCode.Text;
  58. string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
  59. if (newShownText != shownText)
  60. {
  61. textLabel.SetContent(newShownText);
  62. shownText = newShownText;
  63. }
  64. return InspectableState.NotModified;
  65. }
  66. /// <summary>
  67. /// Retrieves import options for the resource we're currently inspecting.
  68. /// </summary>
  69. /// <returns>Script code import options object.</returns>
  70. private ScriptCodeImportOptions GetImportOptions()
  71. {
  72. ScriptCode scriptCode = InspectedObject as ScriptCode;
  73. ScriptCodeImportOptions output = null;
  74. if (scriptCode != null)
  75. {
  76. LibraryEntry libEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(scriptCode));
  77. if (libEntry != null && libEntry.Type == LibraryEntryType.File)
  78. {
  79. FileEntry fileEntry = (FileEntry)libEntry;
  80. output = fileEntry.Options as ScriptCodeImportOptions;
  81. }
  82. }
  83. if (output == null)
  84. {
  85. if (importOptions == null)
  86. output = new ScriptCodeImportOptions();
  87. else
  88. output = importOptions;
  89. }
  90. return output;
  91. }
  92. /// <summary>
  93. /// Reimports the script code resource according to the currently set import options.
  94. /// </summary>
  95. private void TriggerReimport()
  96. {
  97. ScriptCode scriptCode = (ScriptCode)InspectedObject;
  98. string resourcePath = ProjectLibrary.GetPath(scriptCode);
  99. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  100. }
  101. }
  102. /** @} */
  103. }