Пример #1
0
        private void scoreScrollViewer_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // Get the current mouse position.
                mousePos = e.GetPosition(null);
                Vector diff = startPoint - mousePos;

                if (!playbackThread.IsBusy)
                {
                    if (e.LeftButton == MouseButtonState.Pressed &&
                        (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                         Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
                    {
                        ScrollViewer sv = (ScrollViewer)sender;
                        ScoreStaffs  ss = LayoutController.FindAncestor <ScoreStaffs>((DependencyObject)e.OriginalSource);
                        if (ss != null)
                        {
                            int        index    = scoreWrapPanel.Children.IndexOf(ss);
                            DataObject dragData = new DataObject("int", index);
                            DragDrop.DoDragDrop(ss, dragData, DragDropEffects.Move);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Пример #2
0
        /// <summary>
        /// Create a ScoreStaffs object for a bar.
        /// </summary>
        /// <param name="genome"></param>
        /// <param name="tempo"></param>
        /// <param name="barPosition"></param>
        /// <returns></returns>
        private ScoreStaffs CreateScoreStaffsBar(Genome genome, string tempo, int barPosition)
        {
            // The staffs for the new bar.
            ScoreStaffs ss = new ScoreStaffs();

            ss.Background = Brushes.White;
            ss.setContent(genome);
            ss.AddTempoInfo(tempo);
            if (addEmptyBar == false)
            {
                ss.AddIndexTag(barCount + 1);
            }
            else
            {
                ss.AddIndexTag(barPosition + 1);
            }
            ss.borderStaff.BorderThickness = new Thickness(0);
            ss.borderStaff.BorderBrush     = Brushes.Blue;

            ss.AllowDrop = true;
            AddScoreStaffsMouseLeftButtonUpEventHandler(ss);
            AddScoreStaffsDragEnterEventHandler(ss);
            AddScoreStaffsDropEventHandler(ss);

            return(ss);
        }
Пример #3
0
        private void CreateIndividualTemplateInField(ref int index, ref byte cellValue, int j, int k, string borderStaffIndex)
        {
            Border g = CreateIndividualGrid(index);

            g.Tag = cellValue;
            borderTags.Add(cellValue);
            fieldGrid.Children.Add(g);

            Grid.SetRow(g, k - 1);
            Grid.SetColumn(g, j + 1);

            cellValue++;

            // The staff for the individual.
            ScoreStaffs obj = new ScoreStaffs();

            obj.Name              = "scoreStaffs" + index;
            obj.borderStaff.Name += borderStaffIndex;
            if (j != 0)
            {
                obj.Margin = new Thickness(1, 0, 0, 0);
            }
            fieldGrid.Children.Add(obj);

            Grid.SetRow(obj, k);
            Grid.SetColumn(obj, j + 1);

            cellValue++;
        }
Пример #4
0
        public void ApplyKeySignature()
        {
            KeySignature temp = Parameters.KeySignature;

            Parameters.KeySignature = LayoutController.StringToEnum <KeySignature>(keySignature + "Maj");
            geneticSong.ApplyKeySignature(keySignature);
            Parameters.KeySignature = temp;
            int defaultNumberElem = ScoreStaffs.defaultNumberElem;

            for (int i = 0; i < scores.Count; i++)
            {
                ScoreStaffs ss   = scores[i];
                Grid        grid = (Grid)ss.borderStaff.Child;
                grid.Children.RemoveRange(defaultNumberElem, grid.Children.Count - defaultNumberElem);
                ss.setContent(geneticSong.songGenomes[i]);
                ss.AddTempoInfo(geneticSong.tempos[i].ToString());
                ss.AddIndexTag(i + 1);
            }

            foreach (WrapPanel wp in keySignatures)
            {
                ((Image)wp.Children[0]).Source = LayoutController.GetKeySignature(keySignature, Clefs[0].ToString());
                ((Image)wp.Children[1]).Source = LayoutController.GetKeySignature(keySignature, Clefs[1].ToString());
            }

            MarkSavedScore(true);
        }
Пример #5
0
        /// <summary>
        /// Updates the template of each field individual with the corresponding updated genome information.
        /// </summary>
        /// <param name="selected"></param>
        /// <param name="genomes"></param>
        private void UpdateFieldContent(List <byte> selected, List <Genome> genomes)
        {
            Border   b                 = new Border();
            Grid     g                 = new Grid();
            CheckBox cb                = new CheckBox();
            byte     popSize           = Parameters.PopSize;
            int      defaultNumberElem = ScoreStaffs.defaultNumberElem;

            for (byte i = 0; i < popSize; i++)
            {
                ScoreStaffs ss   = (ScoreStaffs)fieldGrid.Children[borderTags[i] + 1];
                Grid        grid = (Grid)ss.borderStaff.Child;
                grid.Children.RemoveRange(defaultNumberElem, grid.Children.Count - defaultNumberElem);
                ss.setContent(genomes[i]);

                // The selections from a generation are not persisted to the next one.
                // But the selected genomes which are mutated by the user, remain selected after the mutation.
                if (!isManuallyMutated)
                {
                    if (selected.IndexOf(i) != -1)
                    {
                        b            = (Border)fieldGrid.Children[borderTags[i]];
                        g            = (Grid)b.Child;
                        cb           = (CheckBox)g.Children[2];
                        cb.IsChecked = false;
                        cb.RaiseEvent(new RoutedEventArgs(CheckBox.UncheckedEvent));
                    }
                }
            }
            isManuallyMutated = false;
        }
Пример #6
0
 private void AddScoreStaffsDragEnterEventHandler(ScoreStaffs ss)
 {
     ss.DragEnter += delegate(object senderS, DragEventArgs eS)
     {
         if (senderS == eS.Source)
         {
             eS.Effects = DragDropEffects.None;
         }
     };
 }
Пример #7
0
        private void AddPlaybackThreadRunWorkerCompletedEventHandler(int index)
        {
            playbackThreads[index].RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                if (!args.Cancelled)
                {
                    currPlayingStatus = 0;
                }

                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                ss.staffGrid.Children.RemoveAt(ss.staffGrid.Children.Count - 1);
            };
        }
Пример #8
0
        private void AddBar(Genome genome, string ksa, Octave[] octaves, DurationCFugue durCF, byte[] barIter, string tempo)
        {
            if (barCount == 0)
            {
                keySignature = ksa;
                Clefs[0]     = Scales.GetClef(octaves[0]);
                Clefs[1]     = Scales.GetClef(octaves[1]);
                Tempo        = "";
                geneticSong  = new GeneticSong(keySignature);
            }

            if (keySignature == ksa)
            {
                byte[] oct         = { (byte)octaves[0], (byte)octaves[1] };
                int    barPosition = GetBarPosition();
                geneticSong.AddGenome(tempo, genome, oct, durCF, barIter, barPosition);

                if (pause == true)
                {
                    addBarOnPause = true;
                }

                // If the new bar is the first one in the next row.
                if (barCount % individPerRow == 0)
                {
                    StartNewRow(ksa);
                }

                ScoreStaffs ss = CreateScoreStaffsBar(genome, tempo, barPosition);

                if (addEmptyBar == false)
                {
                    scores.Add(ss);
                    scoreWrapPanel.Children.Add(ss);
                }
                else
                {
                    AddBlankBar(ss, barPosition);
                }

                barCount++;
                addEmptyBar = false;
                Tempo       = tempo;
            }
            else
            {
                MessageBox.Show("Key signatures must match!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Пример #9
0
        private Button LockButtonSetup(int index, CheckBox selectCheckBox)
        {
            Image  theLock;
            Button lockButton = LockButtonLayout(index, out theLock);

            lockButton.Click += delegate
            {
                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                if ((int)lockButton.Tag == 0)  // If it's unlocked.
                {
                    theLock.Source = LayoutController.GetImage("/Resources/App icons/lock.png");
                    if (selectCheckBox.IsChecked == true)
                    {
                        ss.borderStaff.BorderBrush = Brushes.Magenta;
                    }
                    else
                    {
                        ss.borderStaff.BorderBrush = Brushes.Blue;
                    }
                    lockButton.Tag = 1;
                    lockTag        = true;
                }
                else  // If it's locked.
                {
                    theLock.Source = LayoutController.GetImage("/Resources/App icons/un_lock.png");
                    if (selectCheckBox.IsChecked == true)
                    {
                        ss.borderStaff.BorderBrush = Brushes.Red;
                    }
                    else
                    {
                        ss.borderStaff.BorderBrush = Brushes.Black;
                    }
                    lockButton.Tag = 0;
                    lockTag        = false;
                }
            };

            return(lockButton);
        }
Пример #10
0
        private void AddBlankBar(ScoreStaffs ss, int pos)
        {
            if (selectIndex != -1)
            {
                for (int i = selectIndex + 1; i < scores.Count; i++)
                {
                    Label l = (Label)scores[i].staffGrid.Children[scores[i].staffGrid.Children.Count - 1];
                    int   n = Convert.ToInt32(l.Content);
                    l.Content = n + 1;
                }
            }

            scores.Insert(pos, ss);
            int newBarCount = scores.Count;

            int k = (int)Math.Ceiling((double)newBarCount / individPerRow);

            if (newBarCount <= individPerRow)
            {
                k = 1;
            }

            keySignatures.RemoveRange(k, keySignatures.Count - k);

            scoreWrapPanel.Children.Clear();

            k = 0;
            int count = 0;

            while (k < scores.Count)
            {
                scoreWrapPanel.Children.Add(keySignatures[count++]);
                for (int j = 0; j < individPerRow && k < scores.Count; j++)
                {
                    scoreWrapPanel.Children.Add(scores[k]);
                    k++;
                }
            }
        }
Пример #11
0
        private void fieldGrid_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // Get the current mouse position.
                Point  mousePos = e.GetPosition(null);
                Vector diff     = startPoint - mousePos;

                if (e.LeftButton == MouseButtonState.Pressed &&
                    (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                     Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
                {
                    // Get the dragged ListViewItem.
                    Grid        grid = (Grid)sender;
                    ScoreStaffs ss   = LayoutController.FindAncestor <ScoreStaffs>((DependencyObject)e.OriginalSource);

                    // Initialize the drag & drop operation.
                    if (ss != null)
                    {
                        int        index    = Convert.ToInt32(ss.Name.Substring(ss.Name.LastIndexOf("s") + 1));
                        Genome     gen      = individuals[index];
                        DataObject dragData = new DataObject();
                        dragData.SetData("Object", gen);
                        dragData.SetData("String", KeySignatureAccidental + "|" + Tempo);
                        Octave[] octaves = { octave0, octave1 };
                        dragData.SetData("MusicNoteLib.Octave[]", octaves);
                        dragData.SetData("MusicNoteLib.DurationCFugue", Duration);
                        byte[] barIter = { barIteration0, barIteration1 };
                        dragData.SetData("byte[]", barIter);

                        DragDrop.DoDragDrop(ss, dragData, DragDropEffects.Copy);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Пример #12
0
        /// <summary>
        /// Fills the template of each field individual with the corresponding genome information.
        /// </summary>
        private void SetFieldContent()
        {
            Border b       = new Border();
            Grid   g       = new Grid();
            Button button  = new Button();
            byte   popSize = Parameters.PopSize;

            for (byte i = 0; i < popSize; i++)
            {
                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[i] + 1];
                ss.setContent(individuals[i]);

                // If the individual is supposed to be preserved.
                if (preserved.IndexOf(i) != -1)
                {
                    b      = (Border)fieldGrid.Children[borderTags[i]];
                    g      = (Grid)b.Child;
                    button = (Button)g.Children[1];
                    // Trigger a click event on the locking-genome button.
                    button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                }
            }
        }
Пример #13
0
 private void AddScoreStaffsMouseLeftButtonUpEventHandler(ScoreStaffs ss)
 {
     ss.MouseLeftButtonUp += delegate
     {
         if (isSelectionMode1 == false)
         {
             if (ss.clicked == false)
             {
                 ss.clicked = true;
                 ss.borderStaff.Background = Brushes.Beige;
             }
             else
             {
                 ss.clicked = false;
                 ss.borderStaff.Background = Brushes.White;
             }
         }
         else
         {
             int newPrimBar = scores.IndexOf(ss);
             if (selectIndex != newPrimBar)
             {
                 if (selectIndex != -1)   // If there is a mode-1-selected bar.
                 {
                     ScoreStaffs oldPrimBar = (ScoreStaffs)scores[selectIndex];
                     oldPrimBar.borderStaff.BorderThickness = new Thickness(0);
                 }
                 ss.borderStaff.BorderThickness = new Thickness(2);
             }
             else
             {
                 ss.borderStaff.BorderThickness = new Thickness(0);
             }
             RefreshValues(newPrimBar);
         }
     };
 }
Пример #14
0
        private CheckBox SelectCheckBoxSetup(int index)
        {
            CheckBox selectCheckBox = new CheckBox();

            selectCheckBox.Name = "selectCheckBox" + index;
            selectCheckBox.HorizontalAlignment = HorizontalAlignment.Left;
            selectCheckBox.VerticalAlignment   = VerticalAlignment.Center;
            selectCheckBox.Margin = new Thickness(52, 0, 0, 0);

            selectCheckBox.Checked += delegate
            {
                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                if (lockTag == true)
                {
                    ss.borderStaff.BorderBrush = Brushes.Magenta;
                }
                else
                {
                    ss.borderStaff.BorderBrush = Brushes.Red;
                }
            };
            selectCheckBox.Unchecked += delegate
            {
                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                if (lockTag == false)
                {
                    ss.borderStaff.BorderBrush = Brushes.Black;
                }
                else
                {
                    ss.borderStaff.BorderBrush = Brushes.Blue;
                }
            };

            return(selectCheckBox);
        }
Пример #15
0
        private void AddScoreStaffsDropEventHandler(ScoreStaffs ss)
        {
            ss.Drop += delegate(object senderSS, DragEventArgs eSS)
            {
                Genome g = (Genome)eSS.Data.GetData("Object");
                if (g == null)
                {
                    int         indexAlt = Convert.ToInt32(eSS.Data.GetData("int"));
                    ScoreStaffs ssAlt    = (ScoreStaffs)scoreWrapPanel.Children[indexAlt];
                    int         index    = scoreWrapPanel.Children.IndexOf(ss);
                    if (index != indexAlt)
                    {
                        Label lAlt       = (Label)ssAlt.staffGrid.Children[ssAlt.staffGrid.Children.Count - 1];
                        Label l          = (Label)ss.staffGrid.Children[ss.staffGrid.Children.Count - 1];
                        int   indexSc    = Convert.ToInt32(l.Content) - 1;
                        int   indexAltSc = Convert.ToInt32(lAlt.Content) - 1;

                        if (selectIndex != -1)
                        {
                            if (indexSc == startIndex)
                            {
                                ss.borderStaff.BorderThickness    = new Thickness(0);
                                ssAlt.borderStaff.BorderThickness = new Thickness(2);
                            }
                            if (indexAltSc == startIndex)
                            {
                                ss.borderStaff.BorderThickness    = new Thickness(2);
                                ssAlt.borderStaff.BorderThickness = new Thickness(0);
                            }
                        }

                        string aux = lAlt.Content.ToString();
                        lAlt.Content = l.Content;
                        l.Content    = aux;

                        scoreWrapPanel.Children.Remove(ss);
                        scoreWrapPanel.Children.Remove(ssAlt);
                        scores.Remove(ss);
                        scores.Remove(ssAlt);

                        if (index < indexAlt)
                        {
                            scoreWrapPanel.Children.Insert(index, ssAlt);
                            scoreWrapPanel.Children.Insert(indexAlt, ss);
                            scores.Insert(indexSc, ssAlt);
                            scores.Insert(indexAltSc, ss);
                        }
                        else
                        {
                            scoreWrapPanel.Children.Insert(indexAlt, ss);
                            scoreWrapPanel.Children.Insert(index, ssAlt);
                            scores.Insert(indexAltSc, ss);
                            scores.Insert(indexSc, ssAlt);
                        }
                        geneticSong.SwapGenomes(indexSc, indexAltSc);

                        MarkSavedScore(true);
                    }
                }
            };
        }
Пример #16
0
        private void AddPlaybackThreadDoWorkEventHandler(Border border, int index)
        {
            playbackThreads[index].DoWork += delegate(object s, DoWorkEventArgs args)
            {
                double left = barBeat;
                border.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                         new Action(
                                             delegate()
                {
                    // Add metronome.
                    ScoreStaffs ss           = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                    Rectangle rect           = new Rectangle();
                    rect.Width               = 3;
                    rect.Height              = ScoreStaffs.thisHeight;
                    rect.Fill                = Brushes.Blue;
                    rect.Margin              = new Thickness(left, 0, 0, 0);
                    rect.HorizontalAlignment = HorizontalAlignment.Left;
                    Grid.SetRowSpan(rect, 2);
                    ss.staffGrid.Children.Add(rect);
                }
                                             ));

                MusicNoteLib.MusicNoteLib.myPlayAsync(0, individuals[index].AssemblePlayGenome(Tempo));

                int time  = 0;
                int tempo = 670 * 120 / Convert.ToInt32(Tempo);
                tempo -= tempo % 10;

                while (MusicNoteLib.MusicNoteLib.myIsPlaying())
                {
                    if (playbackThreads[index].CancellationPending)
                    {
                        args.Cancel = true;
                        break;
                    }
                    time += 10;
                    Thread.Sleep(10);

                    // Update metronome position in currently playing bar in FIELD window.
                    if (time % tempo == 0)
                    {
                        left += barBeat;
                        if (time / tempo != 4)
                        {
                            border.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                     new Action(
                                                         delegate()
                            {
                                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                                ((Rectangle)ss.staffGrid.Children[ss.staffGrid.Children.Count - 1]).Margin = new Thickness(left, 0, 0, 0);
                            }));
                        }
                    }
                }

                while (time / tempo != 4)
                {
                    if (playbackThreads[index].CancellationPending)
                    {
                        args.Cancel = true;
                        break;
                    }
                    time += 10;
                    Thread.Sleep(10);

                    // Update metronome position in currently playing bar in FIELD window.
                    if (time % tempo == 0)
                    {
                        left += barBeat;
                        if (time / tempo != 4)
                        {
                            border.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                                     new Action(
                                                         delegate()
                            {
                                ScoreStaffs ss = (ScoreStaffs)fieldGrid.Children[borderTags[index] + 1];
                                ((Rectangle)ss.staffGrid.Children[ss.staffGrid.Children.Count - 1]).Margin = new Thickness(left, 0, 0, 0);
                            }));
                        }
                    }
                }

                Thread.Sleep(50);
                MusicNoteLib.MusicNoteLib.myStopPlay();
            };
        }