using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Aitex.UI.Charting { /// /// Interaction logic for LuaEditor.xaml /// public partial class LuaEditor : UserControl { public LuaEditor() { InitializeComponent(); } public void SetHotWords(List hotWords) { _hotWords = hotWords; } List _hotWords = new List(); public string Text { get { return textBox1.Text; } set { textBox1.Text = value; } } private void OnKeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.OemPeriod) { var rect = textBox1.GetRectFromCharacterIndex(textBox1.SelectionStart); listBox1.Margin = new Thickness(rect.Left + 5, rect.Top + 14, 0, 0); int index = textBox1.SelectionStart; while (index > 0 && index <= textBox1.Text.Length) { var curChar = textBox1.Text[index - 1]; if (curChar == ' ' || curChar == '\r' || curChar == '\n' || curChar == '\t') { break; } index--; } string keyWord = textBox1.Text.Substring(index, textBox1.SelectionStart - index); Dictionary dics = new Dictionary(); foreach (var item in _hotWords) { if (item.Contains(keyWord)) { var leftStr = item.Replace(keyWord, ""); var split = leftStr.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length > 1) { var ss = "+" + split[0]; if (!dics.ContainsKey(ss)) { dics.Add(ss, split[0]); } } else if (split.Length == 1) { var ss = " " + split[0]; if (!dics.ContainsKey(ss)) { dics.Add(ss, split[0]); } } } } listBox1.Items.Clear(); if (dics.Keys.Count > 0) { var keys = dics.Keys.ToList(); keys.Sort(); foreach (var key in keys) { TextBlock tb = new TextBlock() { Text = key, Tag = dics[key], FontSize = 12 }; listBox1.Items.Add(tb); } listBox1.Visibility = System.Windows.Visibility.Visible; listBox1.Focus(); } else { listBox1.Visibility = System.Windows.Visibility.Hidden; textBox1.Focus(); } } else { listBox1.Visibility = System.Windows.Visibility.Hidden; } } private void listBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { var tb = listBox1.SelectedItem as TextBlock; if (tb != null) { listBox1.Visibility = System.Windows.Visibility.Hidden; int selStart = textBox1.SelectionStart; string insertStr = (string)tb.Tag; textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, insertStr); textBox1.SelectionStart = selStart + insertStr.Length; textBox1.Focus(); } } else if (e.Key == Key.Back) { listBox1.Visibility = System.Windows.Visibility.Hidden; int selStart = textBox1.SelectionStart; textBox1.Text = textBox1.Text.Remove(selStart - 1, 1); textBox1.SelectionStart = selStart - 1; textBox1.Focus(); } } } }