Newer
Older
DungeonShooting / DungeonShooting_Godot / editor / src / TextEditPainter.cs
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. namespace DScript.GodotEditor
  5. {
  6. /// <summary>
  7. /// 负责代码编辑器内的绘制操作
  8. /// </summary>
  9. public class TextEditPainter : Node2D
  10. {
  11. private Color ErrorLineColor = new Color(1, 0, 0, 0.2f);
  12. //code面板
  13. private CodePanel _codePanel;
  14. private TextEdit _textEdit;
  15. //报错行数
  16. private List<int> _errorLines = new List<int>();
  17.  
  18. /// <summary>
  19. /// 设置文本编辑器
  20. /// </summary>
  21. public void SetTextEdit(CodeTextEdit textEdit)
  22. {
  23. _textEdit = textEdit;
  24. }
  25.  
  26. /// <summary>
  27. /// 设置代码面板
  28. /// </summary>
  29. public void SetIdePanel(CodePanel codePanel)
  30. {
  31. _codePanel = codePanel;
  32. }
  33.  
  34. /// <summary>
  35. /// 绘制 TextEdit 中报错的行数
  36. /// </summary>
  37. /// <param name="line">报错所在行数</param>
  38. public void DrawTextEditErrorLine(int line)
  39. {
  40. if (!_errorLines.Contains(line))
  41. {
  42. _errorLines.Add(line);
  43. _textEdit.SetLineAsSafe(line, true);
  44. }
  45. }
  46. /// <summary>
  47. /// 取消绘制 TextEdit 中报错的行数
  48. /// </summary>
  49. /// <param name="line">报错所在行数</param>
  50. public void UnDrawTextEditErrorLine(int line)
  51. {
  52. _errorLines.Remove(line);
  53. _textEdit.SetLineAsSafe(line, false);
  54. }
  55.  
  56. public Vector2 ToPainterPosition(Vector2 v)
  57. {
  58. if (_codePanel != null)
  59. {
  60. return v * _codePanel.StartScale;
  61. }
  62.  
  63. return v;
  64. }
  65. public override void _Draw()
  66. {
  67. if (_textEdit == null) return;
  68.  
  69. //绘制报错的行
  70. if (_errorLines.Count > 0)
  71. {
  72. var lineHeight = _textEdit.GetLineHeight();
  73. var width = _textEdit.RectSize.x - _textEdit.MinimapWidth;
  74. for (int i = 0; i < _errorLines.Count; i++)
  75. {
  76. var pos = _textEdit.GetPosAtLineColumn(_errorLines[i], 0);
  77. if (pos.x > -1 && pos.y > -1)
  78. {
  79. DrawRect(new Rect2(0, pos.y - lineHeight, width, lineHeight), ErrorLineColor);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }