} //BaseTone
			internal double Distance(KeyboardElement otherElement) {
				if (rectangle == null || otherElement.rectangle == null) return double.PositiveInfinity;
				double x = Canvas.GetLeft(this.rectangle);
				double xOther = Canvas.GetLeft(otherElement.rectangle);
				double y = Canvas.GetTop(this.rectangle);
				double yOther = Canvas.GetTop(otherElement.rectangle);
				return System.Math.Sqrt((x - xOther) * (x - xOther) + (y - yOther) * (y - yOther));
			} //Distance
		} //SetupVisualization

		void ActivateVisualizedKey(Rectangle rectangle, bool activate) {
			KeyboardElement el = (KeyboardElement)rectangle.Tag;
			var chord = KeyboardLayout.Instance.CurrentChord;
			if (!Keyboard.IsKeyDown(Key.LeftCtrl))
				chord = null;
			byte volume = (byte)this.sliderOnVelocity.Value;
			if (Keyboard.IsKeyDown(Key.LeftShift))
				volume = (byte)this.sliderOnPedalVelocity.Value;
			el.Activate(activate, chord, volume);
		} //ActivateVisualizedKey
Пример #3
0
            }             //FindKeyboardElementInRow

            internal KeyboardElement[] FindChordLayout(KeyboardElement root, ChordNote[] chord)
            {
                KeyboardElementList list = new KeyboardElementList();
                int rootRow   = root.Location.Row;
                int rootShift = root.Location.ShiftInRow;

                foreach (var chordNote in chord)
                {
                    double          distance  = double.PositiveInfinity;
                    KeyboardElement tryToFind = null;
                    for (int rowIndex = 0; rowIndex < Rows.Length; ++rowIndex)
                    {
                        var alternative = FindKeyboardElementInRow((sbyte)(chordNote.PitchShift + root.BaseTone), rowIndex);
                        if (alternative != null)
                        {
                            double newDistance = root.Distance(alternative);
                            if (newDistance < distance)
                            {
                                tryToFind = alternative;
                                distance  = newDistance;
                            }             //if
                        }                 //if
                    }                     //loop
                    if (tryToFind != null)
                    {
                        if (parent.checkBoxHighlightChordsWithLabels.IsChecked == true)
                        {
                            tryToFind.Highlight(chordNote.NoteDescriptor);
                        }
                        list.Add(tryToFind);
                    }             //if
                }                 //loop
                if (list.Count < 1)
                {
                    return(null);
                }
                else
                {
                    return(list.ToArray());
                }
            }             //FindChordLayout
		} //class KeyboardElement

		void SetupVisualization() {
			RadialGradientBrush buttonPressedBrush = new RadialGradientBrush(
				DefinitionSet.pressedButtonStartColor,
				DefinitionSet.pressedButtonEndColor);
			buttonPressedBrush.RadiusX = 1;
			buttonPressedBrush.RadiusY = 1;
			this.ButtonPressedBrush = buttonPressedBrush;
			canvasKeyboard.SnapsToDevicePixels = true;
			//canvasKeyboard.Background = Brushes.Yellow;
			canvasKeyboard.SizeChanged += (sizeSender, sizeEventArgs) => {
				Grid grid = (Grid)canvasKeyboard.Parent;
				canvasKeyboard.Width = grid.ActualWidth;
				canvasKeyboard.Height = grid.ActualHeight;
				double actualWidth = canvasKeyboard.Width;
				double actualHeight = canvasKeyboard.Height;
				canvasKeyboard.Clip = new RectangleGeometry(new Rect(0, 0, actualWidth, actualHeight));
				double sizeByWidth = actualWidth / DefinitionSet.maximimKeyboardWidth;
				double sizeByHeight = actualHeight / DefinitionSet.keyboardHeight;
				double size = Math.Min(sizeByWidth, sizeByHeight);
				double shiftLeft = (actualWidth - size * DefinitionSet.maximimKeyboardWidth) / 2;
				double shiftTop = DefinitionSet.keyboardHeight / 2 + (actualHeight - size * DefinitionSet.keyboardHeight) / 2;
				int keyboardElementCount = 0;
				for (int yIndex = 0; yIndex < KeyboardLayout.Instance.Rows.Length; ++yIndex) {
					bool shortRow = KeyboardLayout.Instance.Rows[yIndex].IsShort;
					double intervalShift = shortRow ? size / 2 : 0;
					for (int xIndex = 0; xIndex < KeyboardLayout.Instance.Rows[yIndex].Elements.Length; ++xIndex) {
						Rectangle rc = new Rectangle();
						TextBlock tb = new TextBlock();
						rc.Fill = Brushes.White; //SA???
						rc.Stroke = Brushes.Black;
						rc.StrokeThickness = 1;
						rc.Width = size;
						rc.Height = size;
						tb.FontSize = (size + 2) / 3; //SA???
						rc.MouseDown += (sender, eventArgs) => {
							ActivateVisualizedKey((Rectangle)sender, true);
						};
						rc.MouseUp += (sender, eventArgs) => {
							ActivateVisualizedKey((Rectangle)sender, false);
						};
						rc.MouseEnter += (sender, eventArgs) => {
							if (eventArgs.LeftButton == MouseButtonState.Pressed)
								ActivateVisualizedKey((Rectangle)sender, true);
						};
						rc.MouseLeave += (sender, eventArgs) => {
							ActivateVisualizedKey((Rectangle)sender, false);
						};
						double xTop = shiftLeft + intervalShift + xIndex * (size - 1);
						double yTop = shiftTop + (DefinitionSet.keyboardHeight - yIndex - 1) * (size - 1);
						Canvas.SetLeft(rc, xTop);
						Canvas.SetTop(rc, yTop);
						Canvas.SetLeft(tb, xTop + 3); //SA???
						Canvas.SetTop(tb, yTop + 2); //SA???
						canvasKeyboard.Children.Add(rc);
						canvasKeyboard.Children.Add(tb);
						KeyboardElement ke = KeyboardLayout.Instance.Keys[keyboardElementCount++];
						ke.Visualize(rc, tb);
						rc.Tag = ke;
					} //loop x
				} //loop y
				KeyboardLayout.Instance.ForEachPhysicalKeyboardElement((note) => { note.ReColor(Brushes.PaleGreen); });
				SetupVisualizationOptions();
				radioShowNotes.IsChecked = false; // for refresh in next line
				radioShowNotes.IsChecked = true;
			}; //canvasKeyboard.SizeChanged
		} //SetupVisualization
Пример #5
0
            }             //class KeyboardRowDescriptor

            internal KeyboardLayout(MainWindow parent)
            {
                this.parent         = parent;
                this.sampleRenderer = new SampleRenderer(parent);
                KeyboardElementList keyList = new KeyboardElementList(0);

                Rows = new KeyboardRowDescriptor[DefinitionSet.keyboardHeight];
                for (int yIndex = 0; yIndex < DefinitionSet.keyboardHeight; ++yIndex)
                {
                    bool shortRow = yIndex % 2 == 0;
                    int  xLength  = DefinitionSet.maximimKeyboardWidth;
                    if (!shortRow)
                    {
                        ++xLength;
                    }
                    KeyboardElement[] rowElements = new KeyboardElement[xLength];
                    for (int xIndex = 0; xIndex < xLength; ++xIndex)
                    {
                        var ke = new KeyboardElement(parent, (byte)yIndex, (byte)xIndex, sampleRenderer.Recording);
                        keyList.Add(ke);
                        rowElements[xIndex] = ke;
                    }             //loop x
                    Rows[yIndex] = new KeyboardRowDescriptor(rowElements, shortRow);
                }                 //loop y
                int physicalRightmost = int.MinValue;

                for (int yIndex = 0; yIndex < Main.PhysicalKeyboardLayout.physicalRows.Length; ++yIndex)
                {
                    int rightMost = Main.PhysicalKeyboardLayout.physicalRows[yIndex].Length * 2 - yIndex;
                    if (physicalRightmost < rightMost)
                    {
                        physicalRightmost = rightMost;
                    }
                }                 //loop
                physicalRightmost += 1;
                int rowShift   = 1 + (DefinitionSet.keyboardHeight - Main.PhysicalKeyboardLayout.physicalRows.Length) / 2;
                int shiftInRow = 1 + (DefinitionSet.maximimKeyboardWidth * 2 - physicalRightmost) / 4;

                this.Keys = keyList.ToArray();
                int maxKey = -1;

                for (int yIndex = 0; yIndex < Main.PhysicalKeyboardLayout.physicalRows.Length; ++yIndex)
                {
                    for (int xIndex = 0; xIndex < Main.PhysicalKeyboardLayout.physicalRows[yIndex].Length; ++xIndex)
                    {
                        int             actualIndexX = shiftInRow;
                        KeyboardElement element      = Rows[yIndex + rowShift].Elements[actualIndexX - yIndex + xIndex];
                        Key             key          = Main.PhysicalKeyboardLayout.physicalRows[yIndex][xIndex];
                        string          sKey         = key.ToString();
                        if (sKey.Length > 1)
                        {
                            if (key < Key.D0 || key > Key.D9)
                            {
                                char replacement;
                                if (DefinitionSet.keyReplacements.TryGetValue(key, out replacement))
                                {
                                    sKey = new string(new char[] { replacement });
                                }
                            }
                            else
                            {
                                sKey = sKey.Substring(1, 1);
                            }
                        }                         //if
                        element.Character = sKey;
                        if ((int)key > maxKey)
                        {
                            maxKey = (int)key;
                        }
                        notes.Add(key, element);
                    }                     // loop x
                    if (Rows[yIndex].IsShort)
                    {
                        ++shiftInRow;
                    }
                }         //loop y
                MaxKey = (Key)maxKey;
            }             //KeyboardLayout
        public MainWindow()
        {
            InitializeComponent();
            Icon = Main.TheApplication.Current.ApplicationIcon;
            SetupHelp(KeyboardLayout.Create(this));
            radioIsWickiHayden.Checked   += (sender, eventArgs) => { KeyboardLayout.Instance.PopulateTones(true); };
            radioIsWickiHayden.Unchecked += (sender, eventArgs) => { KeyboardLayout.Instance.PopulateTones(false); };
            radioIsWickiHayden.IsChecked  = true;
            PopulateInstruments();
            SetupSliders();
            var presetsResetAction = SetupChordPresets(SetupChords());

            buttonResetAll.Click += (sender, evenArgs) => { ResetAll(presetsResetAction); };
            SetupVisualization();
            int maxKey = (int)(int)KeyboardLayout.Instance.MaxKey + 1;

            keyStates     = new BitArray(maxKey, false);
            this.KeyDown += (sender, evenArgs) => {
                KeyboardElement ke = KeyboardLayout.Instance.FindKeyboardElementByKey(evenArgs.Key);
                if (ke != null)
                {
                    if ((int)evenArgs.Key > maxKey)
                    {
                        return;
                    }
                    if (keyStates[(int)evenArgs.Key])
                    {
                        evenArgs.Handled = true;
                        return;
                    }                     //if
                    keyStates[(int)evenArgs.Key] = true;
                    byte velocity;
                    if (Keyboard.IsKeyDown(Key.LeftShift))
                    {
                        velocity = (byte)this.sliderOnPedalVelocity.Value;
                    }
                    else
                    {
                        velocity = (byte)this.sliderOnVelocity.Value;
                    }
                    var chordToUse = KeyboardLayout.Instance.CurrentChord;
                    if (!Keyboard.IsKeyDown(Key.LeftCtrl))
                    {
                        chordToUse = null;
                    }
                    byte volume = (byte)this.sliderOnVelocity.Value;
                    if (Keyboard.IsKeyDown(Key.LeftShift))
                    {
                        volume = (byte)this.sliderOnPedalVelocity.Value;
                    }
                    ke.Activate(true, chordToUse, volume);
                    evenArgs.Handled = true;
                }          //if
            };             //this.KeyDown
            this.KeyUp += (sender, evenArgs) => {
                if ((int)evenArgs.Key > maxKey)
                {
                    return;
                }
                keyStates[(int)evenArgs.Key] = false;
                KeyboardElement ke = KeyboardLayout.Instance.FindKeyboardElementByKey(evenArgs.Key);
                if (ke != null)
                {
                    ke.Activate(false, null, api.maxVelocity);
                    evenArgs.Handled = true;
                }          //if
            };             //this.KeyDown
            this.PreviewKeyDown += (sender, evenArgs) => {
                Key key = evenArgs.SystemKey;
                if (key == Key.None)
                {
                    return;
                }
                string       keyString = key.ToString();
                ToggleButton option;
                if (!chordAlternateKey.TryGetValue(keyString, out option))
                {
                    return;
                }
                var      wasChecked = option.IsChecked;
                CheckBox cb         = option as CheckBox;
                if (cb != null)
                {
                    option.IsChecked = !wasChecked;
                }
                else
                {
                    option.IsChecked = true;
                }
            };    //this.PreviewKeyDown
            Persistence.RestoreState(this);
        }         //MainWindow