ScriptCodeInspector.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 bool Refresh()
  41. {
  42. ScriptCode scriptCode = referencedObject as ScriptCode;
  43. if (scriptCode == null)
  44. return false;
  45. bool anythingModified = false;
  46. if (scriptCode.EditorScript != isEditorField.Value)
  47. {
  48. isEditorField.Value = scriptCode.EditorScript;
  49. anythingModified = true;
  50. }
  51. string newText = scriptCode.Text;
  52. string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
  53. if (newShownText != shownText)
  54. {
  55. textLabel.SetContent(newShownText);
  56. shownText = newShownText;
  57. anythingModified = true;
  58. }
  59. return anythingModified;
  60. }
  61. }
  62. }