private void RoomCanvas_MouseUp(object sender, MouseButtonEventArgs e) { switch (mChosenButton) { case 0: { //Add wall rectangle System.Windows.Point position = e.GetPosition(RoomCanvas); AddMarker(position); Point leftTop = new Point((position.X-mXDrawOffset)/pixelPerMeter, (position.Y-mYDrawOffset)/pixelPerMeter); RoomCanvas.MouseUp -= RoomCanvas_MouseUp; MouseButtonEventHandler roomCanvasRectDraw = null; roomCanvasRectDraw=(sndr, earg) => { mMarkers.Remove(mMarkers.Last()); RoomCanvas.MouseUp-=roomCanvasRectDraw; RoomCanvas.MouseUp += RoomCanvas_MouseUp; position = earg.GetPosition(RoomCanvas); Point bottomRight = new Point((position.X - mXDrawOffset) / pixelPerMeter, (position.Y - mYDrawOffset) / pixelPerMeter); if (Geometry.Distance(leftTop, bottomRight) < 0.25) { mMarkers.Clear(); DrawRoom(); return; } Point topRight = new Point(bottomRight.X,leftTop.Y); Point bottomLeft = new Point(leftTop.X,bottomRight.Y); mUndoElements.Add(mRoom.AddWall(new Wall(leftTop,topRight,Wall.MaterialPreset.OakWood))); mUndoElements.Add(mRoom.AddWall(new Wall(topRight, bottomRight, Wall.MaterialPreset.OakWood))); mUndoElements.Add(mRoom.AddWall(new Wall(bottomRight, bottomLeft, Wall.MaterialPreset.OakWood))); mUndoElements.Add(mRoom.AddWall(new Wall(leftTop, bottomLeft, Wall.MaterialPreset.OakWood))); UndoMenuItem.IsEnabled = true; DrawRoom(); }; mCanvasMousehandlers.Add(roomCanvasRectDraw); RoomCanvas.MouseUp += roomCanvasRectDraw; break; } case 1: { //Add wall System.Windows.Point position = e.GetPosition(RoomCanvas); AddMarker(position); Point first = new Point((position.X-mXDrawOffset) / pixelPerMeter, (position.Y - mYDrawOffset) / pixelPerMeter); //Magneting foreach (Wall wall in mRoom.Walls) { if (Geometry.Distance(wall.Start, first) < closeDistance) { first = wall.Start; break; } if (Geometry.Distance(wall.End, first) < closeDistance) { first = wall.End; break; } } RoomCanvas.MouseUp -= RoomCanvas_MouseUp; MouseButtonEventHandler roomCanvasLineDraw = null; roomCanvasLineDraw = (sndr, earg) => { mMarkers.Remove(mMarkers.Last()); RoomCanvas.MouseUp -= roomCanvasLineDraw; RoomCanvas.MouseUp+=RoomCanvas_MouseUp; position = earg.GetPosition(RoomCanvas); Point second = new Point((position.X-mXDrawOffset) / pixelPerMeter, (position.Y - mYDrawOffset) / pixelPerMeter); if (Geometry.Distance(first, second) < closeDistance) { mMarkers.Clear(); DrawRoom(); return; } //Magneting foreach (Wall wall in mRoom.Walls) { if (Geometry.Distance(wall.Start, second) < closeDistance) { second = wall.Start; break; } if (Geometry.Distance(wall.End, second) < closeDistance) { second = wall.End; break; } } mUndoElements.Add(mRoom.AddWall(new Wall(first,second,Wall.MaterialPreset.OakWood))); UndoMenuItem.IsEnabled = true; DrawRoom(); }; mCanvasMousehandlers.Add(roomCanvasLineDraw); RoomCanvas.MouseUp += roomCanvasLineDraw; break; } case 2: { //Add source System.Windows.Point position = e.GetPosition(RoomCanvas); SoundPoint point = new SoundPoint((position.X-mXDrawOffset)/pixelPerMeter, (position.Y-mYDrawOffset)/pixelPerMeter); if (mBaseSound != null) { point.Sound = mBaseSound; } mUndoElements.Add(point); mRoom.AddSource(point); break; } case 3: { //Add listener System.Windows.Point position = e.GetPosition(RoomCanvas); ListenerPoint point = new ListenerPoint((position.X-mXDrawOffset) / pixelPerMeter, (position.Y-mYDrawOffset) / pixelPerMeter); mUndoElements.Add(point); mRoom.AddListener(point); break; } case 4: { break; } default: { //Select element System.Windows.Point position = e.GetPosition(RoomCanvas); Point point = new Point((position.X-mXDrawOffset) / pixelPerMeter, (position.Y-mYDrawOffset) / pixelPerMeter); mSelectedRoomObject = SelectObject(point); if (mSelectedRoomObject == null) { ((TextBlock) PropsPanel.Children[0]).Text = "Properties"; PropsPanel.Children.RemoveRange(1,PropsPanel.Children.Count-1); mSelectedRoomObject = null; DrawRoom(); return; } if (mSelectedRoomObject is Wall) { Wall wall = mSelectedRoomObject as Wall; UpdateWallProps(wall); } else if (mSelectedRoomObject is ListenerPoint) { ListenerPoint listener = mSelectedRoomObject as ListenerPoint; UpdateListenerProps(listener); } else if (mSelectedRoomObject is SoundPoint) { SoundPoint source = mSelectedRoomObject as SoundPoint; UpdateSourceProps(source); } break; } } DrawRoom(); }
/// <summary> /// Distance between image of source and listener /// </summary> /// <param name="room">Room where to find listener</param> /// <param name="listener">Listener to use</param> /// <returns></returns> public double GetTotalDistance(Room room, ListenerPoint listener) { if(!room.Listeners.Contains(listener)) throw new ArgumentException("Room elements are non-existent"); return Geometry.Distance(listener, Source); }
private void UpdateListenerProps(ListenerPoint listener) { PropsPanel.Children.Clear(); TextBlock name = new TextBlock { FontSize = 20, Text = "LISTENER", TextAlignment = TextAlignment.Center, Width = rightPanelWidth }; PropsPanel.Children.Add(name); TextBlock location = new TextBlock { FontSize = 14, Text = "Location", Width = rightPanelWidth, TextAlignment = TextAlignment.Center }; PropsPanel.Children.Add(location); StackPanel locationPanel = new StackPanel { Orientation = Orientation.Horizontal, Width = rightPanelWidth }; TextBlock xblock = new TextBlock { FontSize = 10, Text = "X: ", Margin = new Thickness(5, 5, 5, 0) }; locationPanel.Children.Add(xblock); TextBox xbox = new TextBox{ FontSize = 10, Text = listener.X + "", Width = numFieldWidth, TextAlignment = TextAlignment.Left }; locationPanel.Children.Add(xbox); TextBlock yblock = new TextBlock { FontSize = 10, Text = "Y: ", Margin = new Thickness(5, 5, 5, 0) }; locationPanel.Children.Add(yblock); TextBox ybox = new TextBox{ FontSize = 10, Text = listener.Y + "", Width = numFieldWidth, TextAlignment = TextAlignment.Left }; locationPanel.Children.Add(ybox); PropsPanel.Children.Add(locationPanel); StackPanel altitudePanel = new StackPanel {Orientation = Orientation.Horizontal, Width = rightPanelWidth}; TextBlock altitudeBlock = new TextBlock{FontSize = 10, Text = "Altitude: ",Margin = new Thickness(5,5,5,0)}; TextBox altitudeBox = new TextBox { FontSize = 10, Text = listener.Altitude + "", Width = numFieldWidth, TextAlignment = TextAlignment.Left, Margin = new Thickness(5, 5, 5, 0) }; altitudePanel.Children.Add(altitudeBlock); altitudePanel.Children.Add(altitudeBox); PropsPanel.Children.Add(altitudePanel); StackPanel channelPanel = new StackPanel {Orientation = Orientation.Horizontal, Width = rightPanelWidth}; TextBlock channelBlock = new TextBlock{FontSize = 14, Text = "Channel",Margin = new Thickness(5,5,5,0)}; ComboBox channelBox = new ComboBox{Margin = new Thickness(5,5,5,5)}; channelBox.ItemsSource = Enum.GetValues(typeof(Sound.Channel)); channelBox.SelectedItem = listener.Channel; channelPanel.Children.Add(channelBlock); channelPanel.Children.Add(channelBox); PropsPanel.Children.Add(channelPanel); CheckBox directional = new CheckBox{Width = rightPanelWidth, Content = "Directional", IsChecked = listener.Directional}; PropsPanel.Children.Add(directional); TextBox xdirbox = null; TextBox ydirbox = null; if (listener.Directional) { TextBlock direction = new TextBlock { FontSize = 14, Text = "Direction vector", Width = rightPanelWidth, TextAlignment = TextAlignment.Center }; PropsPanel.Children.Add(direction); StackPanel directionPanel = new StackPanel { Orientation = Orientation.Horizontal, Width = rightPanelWidth }; TextBlock xdirblock = new TextBlock { FontSize = 10, Text = "X: ", Margin = new Thickness(5, 5, 5, 0) }; directionPanel.Children.Add(xdirblock); xdirbox = new TextBox { FontSize = 10, Text = listener.DirectionX + "", Width = numFieldWidth, TextAlignment = TextAlignment.Left }; directionPanel.Children.Add(xdirbox); TextBlock ydirblock = new TextBlock { FontSize = 10, Text = "Y: ", Margin = new Thickness(5, 5, 5, 0) }; directionPanel.Children.Add(ydirblock); ydirbox = new TextBox { FontSize = 10, Text = listener.DirectionY + "", Width = numFieldWidth, TextAlignment = TextAlignment.Left }; directionPanel.Children.Add(ydirbox); PropsPanel.Children.Add(directionPanel); } channelBox.SelectionChanged += (sender, args) => { if (listener.Channel != (Sound.Channel)channelBox.SelectedItem && mRoom.Listeners.Any(x => x.Channel == (Sound.Channel)channelBox.SelectedItem)) { ListenerPoint another = mRoom.Listeners.Find(x => x.Channel == (Sound.Channel) channelBox.SelectedItem); another.Channel = listener.Channel; } listener.Channel = (Sound.Channel) channelBox.SelectedItem; }; directional.Checked += delegate { for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == listener) { mRoom.Listeners[i]=new ListenerPoint(listener,new Line(0,0,1,1),ListenerPoint.Cardioid ); mSelectedRoomObject = mRoom.Listeners[i]; DrawRoom(); UpdateListenerProps(mRoom.Listeners[i]); return; } } }; directional.Unchecked += delegate { for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == listener) { mRoom.Listeners[i] = new ListenerPoint(listener); mSelectedRoomObject = mRoom.Listeners[i]; DrawRoom(); UpdateListenerProps(mRoom.Listeners[i]); } } }; xbox.TextChanged += delegate { double newValue; if (!double.TryParse(xbox.Text, out newValue)) return; for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == (ListenerPoint)mSelectedRoomObject) { mRoom.Listeners[i].X = newValue; DrawRoom(); return; } } }; xbox.LostFocus += delegate { var listenerPoint = mSelectedRoomObject as ListenerPoint; if (listenerPoint != null) xbox.Text = listenerPoint.X+""; }; ybox.TextChanged += delegate { double newValue; if (!double.TryParse(ybox.Text, out newValue)) return; for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == (ListenerPoint)mSelectedRoomObject) { mRoom.Listeners[i].Y = newValue; DrawRoom(); return; } } }; ybox.LostFocus += delegate { var listenerPoint = mSelectedRoomObject as ListenerPoint; if (listenerPoint != null) ybox.Text = listenerPoint.Y + ""; }; altitudeBox.TextChanged += delegate { double newValue; if (!double.TryParse(altitudeBox.Text, out newValue)) return; for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == (ListenerPoint)mSelectedRoomObject) { mRoom.Listeners[i].Altitude = newValue; DrawRoom(); return; } } }; altitudeBox.LostFocus += delegate { var listenerPoint = mSelectedRoomObject as ListenerPoint; if (listenerPoint != null) altitudeBox.Text = listenerPoint.Altitude + ""; }; if (xdirbox != null) { xdirbox.TextChanged += delegate { double newValue; if (!double.TryParse(xdirbox.Text, out newValue)) return; for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == (ListenerPoint) mSelectedRoomObject) { mRoom.Listeners[i].DirectionX = newValue; DrawRoom(); return; } } }; xdirbox.LostFocus += delegate { var listenerPoint = mSelectedRoomObject as ListenerPoint; if (listenerPoint != null) xdirbox.Text = listenerPoint.DirectionX + ""; }; } if (ydirbox != null) { ydirbox.TextChanged += delegate { double newValue; if (!double.TryParse(ydirbox.Text, out newValue)) return; for (int i = 0; i < mRoom.Listeners.Count; i++) { if (mRoom.Listeners[i] == (ListenerPoint)mSelectedRoomObject) { mRoom.Listeners[i].DirectionY = newValue; DrawRoom(); return; } } }; ydirbox.LostFocus += delegate { var listenerPoint = mSelectedRoomObject as ListenerPoint; if (listenerPoint != null) ydirbox.Text = listenerPoint.DirectionY + ""; }; } Button deleteButton = new Button { Content = "Delete listener", HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(10, 10, 10, 10) }; PropsPanel.Children.Add(deleteButton); deleteButton.Click += delegate { ((TextBlock)PropsPanel.Children[0]).Text = "Properties"; PropsPanel.Children.RemoveRange(1, PropsPanel.Children.Count - 1); mRoom.RemoveListener(mSelectedRoomObject as ListenerPoint); mSelectedRoomObject = null; DrawRoom(); }; }