ScriptCodeInspector.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /// <inheritdoc/>
  17. protected internal override void Initialize()
  18. {
  19. ScriptCode scriptCode = referencedObject as ScriptCode;
  20. if (scriptCode == null)
  21. return;
  22. isEditorField.OnChanged += x =>
  23. {
  24. scriptCode.EditorScript = x;
  25. EditorApplication.SetDirty(scriptCode);
  26. };
  27. GUIPanel textPanel = layout.AddPanel();
  28. GUILayout textLayoutY = textPanel.AddLayoutY();
  29. textLayoutY.AddSpace(5);
  30. GUILayout textLayoutX = textLayoutY.AddLayoutX();
  31. textLayoutX.AddSpace(5);
  32. textLayoutX.AddElement(textLabel);
  33. textLayoutX.AddSpace(5);
  34. textLayoutY.AddSpace(5);
  35. GUIPanel textBgPanel = textPanel.AddPanel(1);
  36. textBgPanel.AddElement(textBg);
  37. layout.AddElement(isEditorField);
  38. }
  39. /// <inheritdoc/>
  40. protected internal override void Refresh()
  41. {
  42. ScriptCode scriptCode = referencedObject as ScriptCode;
  43. if (scriptCode == null)
  44. return;
  45. isEditorField.Value = scriptCode.EditorScript;
  46. string newText = scriptCode.Text;
  47. string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
  48. if (newShownText != shownText)
  49. {
  50. textLabel.SetContent(newShownText);
  51. shownText = newShownText;
  52. }
  53. }
  54. }
  55. }