Newer
Older
DungeonShooting / DungeonShooting_Godot / editor / src / CodeTextEditor.cs
@小李xl 小李xl on 24 Sep 2022 2 KB 代码提示功能开发
  1. using Godot;
  2. using File = System.IO.File;
  3.  
  4. namespace DScript.GodotEditor
  5. {
  6. public class CodeTextEditor : TextEdit
  7. {
  8. /// <summary>
  9. /// 关键字颜色
  10. /// </summary>
  11. private readonly Color KeyCodeColor = new Color(86 / 255f, 156 / 255f, 214 / 255f);
  12. /// <summary>
  13. /// 注释颜色
  14. /// </summary>
  15. private readonly Color AnnotationColor = new Color(77 / 255f, 144 / 255f, 52 / 255f);
  16. /// <summary>
  17. /// 字符串颜色
  18. /// </summary>
  19. private readonly Color StringColor = new Color(214 / 255f, 157 / 255f, 133 / 255f);
  20. //------- 其他着色是在godot编辑器中设置的
  21.  
  22. /// <summary>
  23. /// 关键字列表
  24. /// </summary>
  25. private readonly string[] KeyCodes =
  26. {
  27. "var",
  28. "namespace",
  29. "this",
  30. "class",
  31. "extends",
  32. "func",
  33. "get",
  34. "set",
  35. "import",
  36. "static",
  37. "new",
  38. "return",
  39. "for",
  40. "switch",
  41. "case",
  42. "break",
  43. "default",
  44. "while",
  45. "do",
  46. "is",
  47. "repeat",
  48. "null",
  49. "true",
  50. "false",
  51. "readonly",
  52. "enum",
  53. "private",
  54. "super",
  55. "if",
  56. "else",
  57. "continue",
  58. "typeof"
  59. };
  60.  
  61. private int prevTextLength = 0;
  62. private readonly string[] autoCompeleteRight = { "{", "\"", "(", "[" };
  63. private readonly string[] autoCompeleteLeft = { "}", "\"", ")", "]" };
  64.  
  65. /// <summary>
  66. /// 字体大小
  67. /// </summary>
  68. public int FontSize { get; private set; } = 15;
  69. private TextEditPainter _editPainter;
  70. public override void _Ready()
  71. {
  72. _editPainter = GetNode<TextEditPainter>("TextEditPainter");
  73. //添加关键字
  74. for (int i = 0; i < KeyCodes.Length; i++)
  75. {
  76. AddKeywordColor(KeyCodes[i], KeyCodeColor);
  77. }
  78.  
  79. AddColorRegion("//", "", AnnotationColor, true);
  80. AddColorRegion("/*", "*/", AnnotationColor);
  81. AddColorRegion("\"", "\"", StringColor);
  82. Text = File.ReadAllText("editor/example.ds");
  83. ClearUndoHistory();
  84. }
  85.  
  86. public override void _Process(float delta)
  87. {
  88. if (Input.IsMouseButtonPressed((int)ButtonList.Right))
  89. {
  90. _editPainter.DrawTextEditErrorLine(CursorGetLine());
  91. CodeHintPanel.Instance.ShowPanel(_editPainter.ToPainterPosition(GetPosAtLineColumn(CursorGetLine(), 0)));
  92. CodeHintPanel.Instance.ActiveIndex = 1;
  93. }
  94. }
  95.  
  96. /// <summary>
  97. /// 连接信号, 当文本改变时调用
  98. /// </summary>
  99. private void _on_TextEdit_text_changed()
  100. {
  101. var newLength = Text.Length;
  102. if (newLength != prevTextLength)
  103. {
  104. //括号补全
  105. if (newLength > prevTextLength)
  106. {
  107. var line = CursorGetLine();
  108. var column = CursorGetColumn();
  109. Select(line, column - 1, line, column);
  110. var key = GetSelectionText();
  111. Select(line, column, line, column);
  112. for (int i = 0; i < autoCompeleteRight.Length; i++)
  113. {
  114. if (key == autoCompeleteRight[i])
  115. {
  116. InsertTextAtCursor(autoCompeleteLeft[i]);
  117. CursorSetColumn(CursorGetColumn() - 1);
  118. }
  119. }
  120. }
  121. }
  122.  
  123. prevTextLength = newLength;
  124. }
  125. }
  126. }