Newer
Older
DungeonShooting / DungeonShooting_Godot / editor / src / CodeTextEdit.cs
  1. using Godot;
  2. using File = System.IO.File;
  3.  
  4. namespace DScript.GodotEditor
  5. {
  6. public class CodeTextEdit : TextEdit
  7. {
  8. /// <summary>
  9. /// 获取当前的文本编辑器
  10. /// </summary>
  11. public static CodeTextEdit CurrentTextEdit { get; private set; }
  12. /// <summary>
  13. /// 关键字颜色
  14. /// </summary>
  15. private readonly Color KeyCodeColor = new Color(86 / 255f, 156 / 255f, 214 / 255f);
  16. /// <summary>
  17. /// 注释颜色
  18. /// </summary>
  19. private readonly Color AnnotationColor = new Color(77 / 255f, 144 / 255f, 52 / 255f);
  20. /// <summary>
  21. /// 字符串颜色
  22. /// </summary>
  23. private readonly Color StringColor = new Color(214 / 255f, 157 / 255f, 133 / 255f);
  24. //------- 其他着色是在godot编辑器中设置的
  25.  
  26. /// <summary>
  27. /// 关键字列表
  28. /// </summary>
  29. public static readonly string[] KeyCodes =
  30. {
  31. "var",
  32. "namespace",
  33. "this",
  34. "class",
  35. "extends",
  36. "func",
  37. "get",
  38. "set",
  39. "import",
  40. "static",
  41. "new",
  42. "return",
  43. "for",
  44. "switch",
  45. "case",
  46. "break",
  47. "default",
  48. "while",
  49. "do",
  50. "is",
  51. "repeat",
  52. "null",
  53. "true",
  54. "false",
  55. "readonly",
  56. "enum",
  57. "private",
  58. "super",
  59. "if",
  60. "else",
  61. "continue",
  62. "typeof"
  63. };
  64.  
  65. private int prevTextLength = 0;
  66. private readonly string[] autoCompeleteRight = { "{", "\"", "(", "[" };
  67. private readonly string[] autoCompeleteLeft = { "}", "\"", ")", "]" };
  68.  
  69. //记录光标位置
  70. private int _line;
  71. private int _column;
  72. /// <summary>
  73. /// 字体大小
  74. /// </summary>
  75. public int FontSize { get; private set; } = 15;
  76. /// <summary>
  77. /// 绘制对象
  78. /// </summary>
  79. public TextEditPainter EditPainter { get; private set; }
  80. public override void _Ready()
  81. {
  82. CurrentTextEdit = this;
  83. EditPainter = GetNode<TextEditPainter>("TextEditPainter");
  84. //添加关键字
  85. for (int i = 0; i < KeyCodes.Length; i++)
  86. {
  87. AddKeywordColor(KeyCodes[i], KeyCodeColor);
  88. }
  89.  
  90. AddColorRegion("//", "", AnnotationColor, true);
  91. AddColorRegion("/*", "*/", AnnotationColor);
  92. AddColorRegion("\"", "\"", StringColor);
  93. Text = File.ReadAllText("editor/example.ds");
  94. ClearUndoHistory();
  95. }
  96.  
  97. public override void _Process(float delta)
  98. {
  99. if (Input.IsMouseButtonPressed((int)ButtonList.Right))
  100. {
  101. //绘制报错行
  102. EditPainter.DrawTextEditErrorLine(CursorGetLine());
  103. }
  104. }
  105. /// <summary>
  106. /// 刷新记录的字符串长度
  107. /// </summary>
  108. public void TriggerTextChanged()
  109. {
  110. EmitSignal("text_changed");
  111. }
  112.  
  113. /// <summary>
  114. /// 获取指定范围内的文本
  115. /// </summary>
  116. /// <param name="startLine">起始行</param>
  117. /// <param name="startColumn">起始列</param>
  118. /// <param name="endLine">结束行</param>
  119. /// <param name="endColumn">结束列</param>
  120. public string GetTextInRange(int startLine, int startColumn, int endLine, int endColumn)
  121. {
  122. var line = CursorGetLine();
  123. var column = CursorGetColumn();
  124. Select(startLine, startColumn, endLine, endColumn);
  125. var key = GetSelectionText();
  126. Select(line, column, line, column);
  127. return key;
  128. }
  129.  
  130. /// <summary>
  131. /// 连接信号, 当文本改变时调用
  132. /// </summary>
  133. private void _on_TextEdit_text_changed()
  134. {
  135. var newLength = Text.Length;
  136.  
  137. // //括号补全
  138. // if (newLength > prevTextLength)
  139. // {
  140. // var line = CursorGetLine();
  141. // var column = CursorGetColumn();
  142. // //前一个字符串
  143. // var key = GetTextInRange(line, column - 1, line, column);
  144. //
  145. // for (int i = 0; i < autoCompeleteRight.Length; i++)
  146. // {
  147. // if (key == autoCompeleteRight[i])
  148. // {
  149. // InsertTextAtCursor(autoCompeleteLeft[i]);
  150. // CursorSetColumn(CursorGetColumn() - 1);
  151. // }
  152. // }
  153. // }
  154.  
  155. if (newLength < prevTextLength) //删除内容
  156. {
  157. if (CodeHintManager.IsShowPanel()) //提示面板打开
  158. {
  159. //关闭面板
  160. CodeHintManager.OverInput();
  161. }
  162. }
  163. else if (newLength > prevTextLength) //添加内容
  164. {
  165. if (CodeHintManager.EnterInput) //是否输入过换行
  166. {
  167. Undo();
  168. CodeHintManager.EnterInput = false;
  169. newLength -= 1;
  170. }
  171. else //触发输入
  172. {
  173. //触发输入
  174. CodeHintManager.TriggerInput(this);
  175. }
  176. }
  177.  
  178. prevTextLength = newLength;
  179. }
  180.  
  181. private void _on_TextEdit_cursor_changed()
  182. {
  183. if (CodeHintManager.IsShowPanel() && (Input.IsKeyPressed((int)KeyList.Up) ||
  184. Input.IsKeyPressed((int)KeyList.Down)))
  185. {
  186. CursorSetLine(_line);
  187. CursorSetColumn(_column);
  188. }
  189. else
  190. {
  191. //记录光标位置
  192. _line = CursorGetLine();
  193. _column = CursorGetColumn();
  194. }
  195. }
  196. }
  197. }