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