2
0

ScriptCodeInspector.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 bool isInitialized;
  16. private string shownText = "";
  17. /// <inheritdoc/>
  18. internal override bool Refresh()
  19. {
  20. ScriptCode scriptCode = referencedObject as ScriptCode;
  21. if (scriptCode == null)
  22. return false;
  23. if (!isInitialized)
  24. {
  25. isEditorField.OnChanged += x =>
  26. {
  27. scriptCode.EditorScript = x;
  28. EditorApplication.SetDirty(scriptCode);
  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. isInitialized = true;
  42. }
  43. bool anythingModified = false;
  44. if (scriptCode.EditorScript != isEditorField.Value)
  45. {
  46. isEditorField.Value = scriptCode.EditorScript;
  47. anythingModified = true;
  48. }
  49. string newText = scriptCode.Text;
  50. string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
  51. if (newShownText != shownText)
  52. {
  53. textLabel.SetContent(newShownText);
  54. shownText = newShownText;
  55. anythingModified = true;
  56. }
  57. return anythingModified;
  58. }
  59. }
  60. }