public static IList <KeyLayout> LoadFromDirectory(string layoutsDirectory) { var keyLayouts = new List <KeyLayout>(); Directory.CreateDirectory(layoutsDirectory); string[] keyLayoutFiles = Directory.GetFiles(layoutsDirectory, "*.json"); if (!keyLayoutFiles.Any()) { string message = "경로 " + (string)Settings.Default[MainWindow.KeyLayoutDataDirStr] + "에서 자판 데이터 파일을 찾을 수 없습니다. 해당 경로에 자판 데이터를 생성하고 다시 시도하세요."; throw new KeyLayoutLoadFail(message); } foreach (string keyLayoutFile in keyLayoutFiles) { KeyLayout keyLayout = Load(keyLayoutFile); KeyLayout duplicate = keyLayouts.Find(kl => kl.Name == keyLayout.Name); if (duplicate != null) { string message = "자판 이름 \"" + keyLayout.Name + "\" 이 중복되게 존재합니다.\n" + keyLayout.Location + "\n" + duplicate.Location; throw new KeyLayoutLoadFail(message); } keyLayouts.Add(keyLayout); } return(keyLayouts); }
public static KeyLayout Load(string dataFileLocation) { string keyLayoutLines = File.ReadAllText(dataFileLocation, Encoding.UTF8); try { KeyLayout keyLayout = Parse(keyLayoutLines); keyLayout.Location = dataFileLocation; return(keyLayout); } catch (InvalidKeyLayoutDataException ex) { throw new InvalidKeyLayoutDataException(dataFileLocation + " : " + ex.Message, ex); } }
public static KeyLayout Parse(string data) { KeyLayout keyLayout = JsonConvert.DeserializeObject <KeyLayout>(data); if (string.IsNullOrEmpty(keyLayout.Name)) { const string message = "자판 데이터의 이름(Name 필드)이 주어지지 않았습니다."; throw new InvalidKeyLayoutDataException(message); } if (keyLayout.KeyLayoutData is null) { const string message = "자판 데이터(KeyLayoutData 필드)가 주어지지 않았습니다."; throw new InvalidKeyLayoutDataException(message); } if (string.IsNullOrEmpty(keyLayout.Character)) { const string message = "자판 데이터의 문자 종류(Character 필드)가 주어지지 않았습니다."; throw new InvalidKeyLayoutDataException(message); } if (keyLayout.DefaultKeys is null) { keyLayout.DefaultKeys = new List <KeyPos>(); } var rowNumberData = new List <Tuple <string, int> > { Tuple.Create("숫자열", 13), Tuple.Create("첫째 열", 13), Tuple.Create("둘째 열", 11), Tuple.Create("셋째 열", 10) }; for (int i = 0; i < keyLayout.KeyLayoutData.Count; i++) { if (keyLayout.KeyLayoutData[i].Count != rowNumberData[i].Item2) { string message = rowNumberData[i].Item1 + "의 키 개수는 " + rowNumberData[i].Item2 + " 이어야 하는데 " + keyLayout.KeyLayoutData[i].Count + "개가 주어졌습니다."; throw new InvalidKeyLayoutDataException(message); } } return(keyLayout); }
public static void SaveKeyLayout(KeyLayout keyLayout) { File.WriteAllText(keyLayout.Location, JsonConvert.SerializeObject(keyLayout, Formatting.Indented)); }
public MainWindow() { string exeDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); if (exeDirectory is null) { MessageBox.Show("응용 프로그램 경로를 찾는 도중 에러가 발생했습니다.", "열린타자", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(-1); } if (string.IsNullOrEmpty((string)Settings.Default[KeyLayoutDataDirStr])) { string layoutsDirectory = Path.Combine(exeDirectory, "layouts"); Settings.Default[KeyLayoutDataDirStr] = layoutsDirectory; } try { var keyLayouts = new List <KeyLayout>(KeyLayout.LoadFromDirectory((string)Settings.Default[KeyLayoutDataDirStr])); var layoutName = (string)Settings.Default[KeyLayoutStr]; KeyLayout currentKeylayout = keyLayouts.FirstOrDefault(keyLayout => keyLayout.Name == layoutName); if (currentKeylayout == null) { KeyLayout dubeolsikLayout = keyLayouts.Find(keyLayout => keyLayout.Name == "두벌식 표준"); if (dubeolsikLayout != null) { Settings.Default[KeyLayoutStr] = dubeolsikLayout.Name; CurrentKeyLayout = dubeolsikLayout; } else { Settings.Default[KeyLayoutStr] = keyLayouts[0].Name; CurrentKeyLayout = keyLayouts[0]; } } else { CurrentKeyLayout = currentKeylayout; } } catch (Exception ex) { if (ex is KeyLayoutLoadFail || ex is InvalidKeyLayoutDataException) { MessageBox.Show(ex.Message, "열린타자", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(-1); } } if (string.IsNullOrEmpty((string)Settings.Default[PracticeDataDirStr])) { string dataDirectory = Path.Combine(exeDirectory, "data"); Settings.Default[PracticeDataDirStr] = dataDirectory; } InitializeComponent(); this.Loaded += MainWindow_Loaded; this.Closed += MainWindow_Closed; }
private void SettingsButton_Click(object sender, RoutedEventArgs e) { KeyLayout.SaveKeyLayout(CurrentKeyLayout); var settingsWindow = new SettingsWindow(); settingsWindow.ShowDialog(); if (settingsWindow.KeyLayoutUpdated || settingsWindow.KeyLayoutDataDirUpdated) { CurrentKeyLayout = settingsWindow.SelectedKeyLayout; KeyPracticeMenu.KeyLayoutBox.LoadKeyLayout(); var currentKeyLayoutNameBinding = new Binding { Path = new PropertyPath("Name"), Source = CurrentKeyLayout, }; HomeMenu.CurrentKeyLayoutName.SetBinding(TextBlock.TextProperty, currentKeyLayoutNameBinding); var currentKeyLayoutCharBinding = new Binding { Path = new PropertyPath("Character"), Source = CurrentKeyLayout, }; HomeMenu.CurrentKeyLayoutChar.SetBinding(TextBlock.TextProperty, currentKeyLayoutCharBinding); var mostIncorrectBinding = new Binding { Path = new PropertyPath("Stats.MostIncorrect.Key"), Source = CurrentKeyLayout, Converter = new KeyPosToKeyConverter() }; HomeMenu.MostIncorrectKey.SetBinding(KeyBox.KeyProperty, mostIncorrectBinding); var averageSpeedBinding = new Binding { Path = new PropertyPath("Stats.AverageTypingSpeed"), Source = CurrentKeyLayout, }; HomeMenu.AverageTypingSpeed.SetBinding(TextBlock.TextProperty, averageSpeedBinding); var averageAccuracyBinding = new Binding { Path = new PropertyPath("Stats.AverageAccuracy"), Source = CurrentKeyLayout, StringFormat = "{0}%" }; HomeMenu.AverageAccuracy.SetBinding(TextBlock.TextProperty, averageAccuracyBinding); var sentencePracticeCountBinding = new Binding { Path = new PropertyPath("Stats.SentencePracticeCount"), Source = CurrentKeyLayout, }; HomeMenu.SentencePracticeCount.SetBinding(TextBlock.TextProperty, sentencePracticeCountBinding); CheckSyllablePractice(); } SentencePracticeMenu.LoadData(); ArticlePracticeMenu.LoadData(); }
private static void MainWindow_Closed(object sender, EventArgs e) { KeyLayout.SaveKeyLayout(CurrentKeyLayout); Settings.Default.Save(); }