//Homebutton
        private void HomeBTN_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!exerciseFinished && exerciseStarted)
            {
                //Custom dialog will be opened when the user wan't to exit the exercise when it's not finishedUIElement rootVisual = this.Content as UIElement;
                UIElement    rootVisual   = this.Content as UIElement;
                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(rootVisual);
                if (rootVisual != null && adornerLayer != null)
                {
                    CustomTools.DarkenAdorner darkenAdorner = new CustomTools.DarkenAdorner(rootVisual);
                    adornerLayer.Add(darkenAdorner);

                    //Dialog will be opened when the user wan't to exit the exercise when it's not finished
                    Modals.YesCancelModal result = new Modals.YesCancelModal(this.synthesizer);
                    result.ShowDialog();
                    adornerLayer.Remove(darkenAdorner);
                }
            }
            else
            {
                if (synthesizer.State == SynthesizerState.Speaking)
                {
                    synthesizer.Pause();
                }
                Application.Current.MainWindow.Content = new HomeScreen();
            }
        }
Exemple #2
0
        private void ShowModal <T>(T modal) where T : Window
        {
            UIElement    rootVisual   = this.Content as UIElement;
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(rootVisual);

            if (rootVisual != null && adornerLayer != null)
            {
                CustomTools.DarkenAdorner darkenAdorner = new CustomTools.DarkenAdorner(rootVisual);
                adornerLayer.Add(darkenAdorner);
                modal.ShowDialog();
                adornerLayer.Remove(darkenAdorner);
            }
        }
        //This will be executed every time a key is pressed
        private void HandleTextComposition(object sender, TextCompositionEventArgs e)
        {
            //Creating a variable for the typed key
            Char keyChar;

            try
            {
                //Try getting the typed key into the char variable
                keyChar = (Char)System.Text.Encoding.ASCII.GetBytes(e.Text)[0];
            }
            catch
            {
                return;
            }

            if (keyChar == 9 || keyChar == 8)
            {
                return;
            }



            //If enter is pressed while the exercise isn't running yet the exercise will start
            if (keyChar == '\r' && !running && !exerciseFinished)
            {
                UIElement    rootVisual   = this.Content as UIElement;
                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(rootVisual);
                if (rootVisual != null && adornerLayer != null)
                {
                    CustomTools.DarkenAdorner darkenAdorner = new CustomTools.DarkenAdorner(rootVisual);
                    adornerLayer.Add(darkenAdorner);

                    //Dialog will be opened when the user wan't to exit the exercise when it's not finished
                    Modals.Countdown countdown = new Modals.Countdown();
                    countdown.ShowDialog();
                    adornerLayer.Remove(darkenAdorner);
                }
                running = true;
                timer.Start();
                exerciseStarted = true;
                startSpeaking();
            }
            else if (exerciseStarted && !exerciseFinished && keyChar != '\r')
            {
                //Catching the backspace key and make it remove the last char from the variable wich holds the
                //written text. All other keys will be added to the same variable.
                string typedTextOld = typedText;

                if (typedText.Length < dbString.Length)
                {
                    typedText += e.Text;
                    typedKeys++;
                }


                //Displayig the typed text on the user's screen (this wil build up the whole sentence from scratch again everytime the text is updated)
                if (typedText.Length <= dbString.Length)
                {
                    ProgressBar.Maximum = dbString.Length;
                    ProgressBar.Value   = 0;
                    //First clearing the textblock
                    inputText.Text = "";
                    //Setting a bool witch turns true when a typo was made by the user
                    bool wrong = false;


                    //Writing the text on the screen of the user char for char
                    for (int i = 0; i < typedText.Length; i++)
                    {
                        //If the text is correct it will be green. If there was a typo all text from
                        //the typo onwards will be red.
                        if ((typedText[i] == dbString[i] || ((dbString[i] == '\'' || dbString[i] == '`' || dbString[i] == '’') && (typedText[i] == '\'' || typedText[i] == '`' || typedText[i] == '’')) || ((dbString[i] == '"' || dbString[i] == '“' || dbString[i] == '”') && (typedText[i] == '"' || typedText[i] == '“' || typedText[i] == '”'))) && wrong == false)
                        {
                            secondOfLastLetter = second;



                            ProgressBar.Foreground = Brushes.DeepSkyBlue;
                            inputText.Inlines.Add(new Run(dbString[i].ToString())
                            {
                                Foreground = Brushes.Green
                            });
                            ProgressBar.Value++;
                        }
                        else if (keyChar != '\r')//Make sure an enter press doesn't count as an error (Enter is pressed to replay the speech)
                        {
                            ProgressBar.Foreground = Brushes.Red;
                            //Make sure a mistake isn't counted multiple times.
                            if (!mistakeIndex.Contains(i) && !wrong)
                            {
                                //Check if mistake was made earlier (!wrong means it only gets the first char where the user goes wrong)
                                if (mistakes.ContainsKey(dbString[i]))
                                {
                                    //If the mistake was already made earlier the mistake will count up to the existing dictionary entry
                                    mistakes[dbString[i]]++;
                                }
                                else if (!mistakes.ContainsKey(dbString[i]))
                                {
                                    //If the mistake wasn't made earlier this wil add the mistake to the list with standard count 1
                                    mistakes.Add(dbString[i], 1);
                                }
                                //Gettin the total amount of mistakes using a LINQ query
                                int mistakesNumber = mistakes.Values.Sum();
                                //Displaying the amount of errors on the screen
                                errorLable.Content = mistakesNumber;
                                //Adding the mistakes index so it wont be counted up when the text updates
                                mistakeIndex.Add(i);
                            }


                            typedText = typedTextOld;

                            //Flipping the wrong variable so all the text after the mistake is also shown in red
                            if (!wrong)
                            {
                                wrong = true;
                            }
                        }
                    }

                    if (!wrong && dbString.Length > typedText.Length && dbString[typedText.Length] == ' ')
                    {
                        startSpeaking();
                    }



                    //If the sentence is completed
                    if (typedText.Length == dbString.Length && !wrong)
                    {
                        //The timer stops
                        timer.Stop();
                        //With a LINQ query the amount of mistakes is calculated
                        int mistakesNumber = mistakes.Values.Sum();
                        //Some basic info will be displayed on the users screen
                        headLabel.Content = $"Done! Total time: {SecondsToTime(second)}\nNumber of mistakes: {mistakesNumber}";
                        //The progressbars color will be set to green
                        ProgressBar.Foreground = Brushes.Green;
                        exerciseFinished       = true;


                        //This will add the results to the resultstable
                        if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["username"]))
                        {
                            //Make a new connectionclass
                            Connection c = new Connection();

                            //Getting the necessary IDs from the database
                            int userID   = c.ID($"SELECT userID FROM Usertable WHERE username='******'");
                            int resultID = 1;
                            resultID = (c.ID("SELECT Max(resultID) FROM Result")) + 1;
                            //The query to insert the result into the result table
                            string CmdString = $"INSERT INTO Result (resultID, mistakes, time, wpm, cpm, userID, exerciseID, speech) VALUES ({resultID}, {mistakesNumber}, {second}, {avgWPM}, {avgCPM}, {userID}, {exerciseID}, 1)";
                            //Executing the query
                            if (c.insertInto(CmdString))
                            {
                                //If the result has been added to the database, the Errors can be saved too
                                //For each keyValuePair in the dictionary the key will be added with the matching value
                                foreach (KeyValuePair <char, int> mistake in mistakes)
                                {
                                    //Getting the id for the new error
                                    int errorID = (c.ID("SELECT Max(errorID) FROM Error")) + 1;
                                    //Setting the query for adding the errors
                                    string insertMistakes = $"INSERT INTO Error (errorID, letter, count, resultID) VALUES ({errorID}, '{mistake.Key}', {mistake.Value}, {resultID})";
                                    //Inserting the error with the query above
                                    c.insertInto(insertMistakes);
                                }
                            }
                        }


                        //Show the results
                        UIElement    rootVisual   = this.Content as UIElement;
                        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(rootVisual);
                        int          wordspm;
                        int          charspm;
                        double       accuracy = ((((double)dbString.Length - (double)mistakesNumber) / (double)dbString.Length) * 100);



                        //If the seconds is higher then 0, divide by seconds.
                        if (second > 0)
                        {
                            wordspm = (typedWords * 60) / second;
                            charspm = (typedKeys * 60) / second;
                        }
                        else
                        {
                            wordspm = (typedWords * 60);
                            charspm = (typedKeys * 60);
                        }
                        Modals.ResultsAfterExercise results = new Modals.ResultsAfterExercise(wordspm, charspm, second, mistakesNumber, accuracy, cpmTimeList, wpmTimeList, mistakes, dbString, exerciseID);
                        if (rootVisual != null && adornerLayer != null)
                        {
                            CustomTools.DarkenAdorner darkenAdorner = new CustomTools.DarkenAdorner(rootVisual, 200);
                            adornerLayer.Add(darkenAdorner);
                            results.ShowDialog();
                        }
                    }
                }
            }
        }