LuaEditor.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace Aitex.UI.Charting
  15. {
  16. /// <summary>
  17. /// Interaction logic for LuaEditor.xaml
  18. /// </summary>
  19. public partial class LuaEditor : UserControl
  20. {
  21. public LuaEditor()
  22. {
  23. InitializeComponent();
  24. }
  25. public void SetHotWords(List<string> hotWords)
  26. {
  27. _hotWords = hotWords;
  28. }
  29. List<string> _hotWords = new List<string>();
  30. public string Text
  31. {
  32. get
  33. {
  34. return textBox1.Text;
  35. }
  36. set
  37. {
  38. textBox1.Text = value;
  39. }
  40. }
  41. private void OnKeyUp(object sender, KeyEventArgs e)
  42. {
  43. if (e.Key == Key.OemPeriod)
  44. {
  45. var rect = textBox1.GetRectFromCharacterIndex(textBox1.SelectionStart);
  46. listBox1.Margin = new Thickness(rect.Left + 5, rect.Top + 14, 0, 0);
  47. int index = textBox1.SelectionStart;
  48. while (index > 0 && index <= textBox1.Text.Length)
  49. {
  50. var curChar = textBox1.Text[index - 1];
  51. if (curChar == ' ' ||
  52. curChar == '\r' ||
  53. curChar == '\n' ||
  54. curChar == '\t')
  55. {
  56. break;
  57. }
  58. index--;
  59. }
  60. string keyWord = textBox1.Text.Substring(index, textBox1.SelectionStart - index);
  61. Dictionary<string, string> dics = new Dictionary<string, string>();
  62. foreach (var item in _hotWords)
  63. {
  64. if (item.Contains(keyWord))
  65. {
  66. var leftStr = item.Replace(keyWord, "");
  67. var split = leftStr.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  68. if (split.Length > 1)
  69. {
  70. var ss = "+" + split[0];
  71. if (!dics.ContainsKey(ss))
  72. {
  73. dics.Add(ss, split[0]);
  74. }
  75. }
  76. else if (split.Length == 1)
  77. {
  78. var ss = " " + split[0];
  79. if (!dics.ContainsKey(ss))
  80. {
  81. dics.Add(ss, split[0]);
  82. }
  83. }
  84. }
  85. }
  86. listBox1.Items.Clear();
  87. if (dics.Keys.Count > 0)
  88. {
  89. var keys = dics.Keys.ToList();
  90. keys.Sort();
  91. foreach (var key in keys)
  92. {
  93. TextBlock tb = new TextBlock() { Text = key, Tag = dics[key], FontSize = 12 };
  94. listBox1.Items.Add(tb);
  95. }
  96. listBox1.Visibility = System.Windows.Visibility.Visible;
  97. listBox1.Focus();
  98. }
  99. else
  100. {
  101. listBox1.Visibility = System.Windows.Visibility.Hidden;
  102. textBox1.Focus();
  103. }
  104. }
  105. else
  106. {
  107. listBox1.Visibility = System.Windows.Visibility.Hidden;
  108. }
  109. }
  110. private void listBox1_KeyDown(object sender, KeyEventArgs e)
  111. {
  112. if (e.Key == Key.Enter)
  113. {
  114. var tb = listBox1.SelectedItem as TextBlock;
  115. if (tb != null)
  116. {
  117. listBox1.Visibility = System.Windows.Visibility.Hidden;
  118. int selStart = textBox1.SelectionStart;
  119. string insertStr = (string)tb.Tag;
  120. textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, insertStr);
  121. textBox1.SelectionStart = selStart + insertStr.Length;
  122. textBox1.Focus();
  123. }
  124. }
  125. else if (e.Key == Key.Back)
  126. {
  127. listBox1.Visibility = System.Windows.Visibility.Hidden;
  128. int selStart = textBox1.SelectionStart;
  129. textBox1.Text = textBox1.Text.Remove(selStart - 1, 1);
  130. textBox1.SelectionStart = selStart - 1;
  131. textBox1.Focus();
  132. }
  133. }
  134. }
  135. }