Exemple #1
0
 /// <summary>
 /// Starts playing the song
 /// </summary>
 private void StartPlayingSong()
 {
     stepgrid.Children.Add(highlights[0], 0, 0);
     Grid.SetRowSpan(highlights[0], highlight2StartRow);
     stepgrid.Children.Add(highlights[1], 0, highlight2StartRow);
     Grid.SetRowSpan(highlights[1], NumRows - highlight2StartRow);
     player.BeginPlaying(song);
     //We call this after BeginPlaying because we know no more notes will try to play.
     //If we did it before, the user could theoretically start a note playing in between StopPlayingNote and BeginPlaying and it
     //wouldn't be stopped
     SingleNotePlayer.StopPlayingNote();
     playStopButton.Image = stopImageName;
 }
        public MainPage()
        {
            InitializeComponent();

            // Initializing the song player and noteArray
            noteArray = new SongPlayer.Note[NumColumns, NumRows];       //stored this way because C# is row-major and we want to access a column at a time
            player    = new SongPlayer(noteArray);

            // Initializing the colorMap
            colorMap = new Dictionary <Color, SongPlayer.Instrument>();

            colorMap[Red]    = player.LoadInstrument("Snare");
            colorMap[Blue]   = player.LoadInstrument("YRM1x Atmosphere");
            colorMap[Green]  = player.LoadInstrument("Bass Drum");
            colorMap[Yellow] = player.LoadInstrument("Hi-Hat");

            // Initialize color of highlighted buttons
            highLightedGrey   = HighLightedVersion(Grey);
            highLightedRed    = HighLightedVersion(Red);
            highLightedBlue   = HighLightedVersion(Blue);
            highLightedGreen  = HighLightedVersion(Green);
            highLightedYellow = HighLightedVersion(Yellow);

            // Initaialize the array of buttons
            buttonArray = new Button[NumColumns, NumRows];  //stored this way because C# is row-major and we want to access a column at a time

            BackgroundColor = Color.FromHex("#000000");     // Make background color black

            Style greyButton = new Style(typeof(Button))    // Button style for testing grid
            {
                Setters =
                {
                    new Setter {
                        Property = Button.BackgroundColorProperty, Value = Grey
                    },
                    new Setter {
                        Property = Button.BorderRadiusProperty, Value = 0
                    },
                }
            };

            //Set up a master grid with 2 columns to eventually place stepgrid and sidebar in.
            mastergrid = new Grid {
                ColumnSpacing = 2
            };
            mastergrid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(8, GridUnitType.Star)
            });
            mastergrid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });


            //Set up grid of note squares
            stepgrid = new Grid {
                ColumnSpacing = 2, RowSpacing = 2
            };

            //Initialize the number of rows and columns
            for (int i = 0; i < NumRows; i++)
            {
                stepgrid.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                });
            }
            for (int i = 0; i < NumColumns; i++)
            {
                stepgrid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
            }

            //Add the note buttons to the grid
            for (int i = 0; i < NumRows; i++)
            {
                for (int j = 0; j < NumColumns; j++)
                {
                    Button button = new Button {
                        Style = greyButton
                    };                                                  // Make a new button
                    stepgrid.Children.Add(button, j, i);                // Add it to the grid
                    button.Clicked   += OnButtonClicked;                // Add it to stepsquare event handler
                    noteArray[j, i]   = SongPlayer.Note.None;           // Add a placeholder to songArray
                    buttonArray[j, i] = button;                         // Add button to buttonArray
                }
            }


            // Make the sidebar
            sidebar = new Grid {
                ColumnSpacing = 1, RowSpacing = 1
            };

            for (int i = 0; i < NumInstruments; i++)
            {
                sidebar.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                });
            }
            sidebar.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(1, GridUnitType.Star)
            });

            // Fill sidebar it with buttons
            Color[] colors = new Color[] { Red, Blue, Green, Yellow };                  // Array of colors

            for (int i = 0; i < colors.Length; i++)
            {
                Button button = new Button {
                    BackgroundColor = colors[i], BorderColor = Color.Black, BorderWidth = 3
                };                                                                                  // Make a new button
                sidebar.Children.Add(button, 0, i);                                                 // Add it to the sidebar
                button.Clicked += OnSidebarClicked;                                                 // Add to sidebar event handler
            }


            // Set up scroll view and put grid inside it
            scroller = new ScrollView {
                Orientation = ScrollOrientation.Vertical                  //Both vertical and horizontal orientation
            };
            scroller.Content = stepgrid;

            // Add the scroller (which contains stepgrid) and sidebar to mastergrid
            mastergrid.Children.Add(scroller, 0, 0); // Add scroller to first column of mastergrid
            mastergrid.Children.Add(sidebar, 1, 0);  // Add sidebar to final column of mastergrid
                                                     //Grid.SetRowSpan(sidebar, NumRows);                  // Make sure that it spans the whole column

            Content             = mastergrid;
            player.BeatStarted += HighlightColumns;
            player.BeginPlaying(240);
        }