private void canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (dragInfo == null) { if (DateTime.Now - canvasMouseDownDate < TimeSpan.FromMilliseconds(150) && e.ChangedButton == MouseButton.Left) { var mousePos = e.GetPosition(canvas); CreateNewOrder(mousePos); } } else { e.Handled = true; canvas.ReleaseMouseCapture(); dragInfo = null; } }
void OrderMouseDown(object sender, MouseButtonEventArgs eventArgs) { if (Keyboard.Modifiers == ModifierKeys.None && eventArgs.RightButton == MouseButtonState.Released) { eventArgs.Handled = true; if (dragInfo == null) { var element = (FrameworkElement) eventArgs.Source; var pos = (Positionable) element.DataContext; var origin = new Point(pos.X, pos.Y); var startPoint = eventArgs.GetPosition(canvas); var origins = new Dictionary<FrameworkElement, Point>(); origins.Add(element, origin); if (canvas.CaptureMouse()) { dragInfo = new DragInfo { Elements = new List<FrameworkElement> { element }, ElementOrigins = origins, MouseOrigin = startPoint }; } } } }
void OrderMouseDown(object sender, MouseButtonEventArgs eventArgs) { if (Keyboard.Modifiers == ModifierKeys.None && eventArgs.RightButton == MouseButtonState.Released) { eventArgs.Handled = true; if (dragInfo == null) { var element = (FrameworkElement)eventArgs.Source; var pos = (Positionable)element.DataContext; var origin = new Point(pos.X, pos.Y); var startPoint = eventArgs.GetPosition(canvas); var origins = new Dictionary <FrameworkElement, Point>(); origins.Add(element, origin); if (canvas.CaptureMouse()) { dragInfo = new DragInfo { Elements = new List <FrameworkElement> { element }, ElementOrigins = origins, MouseOrigin = startPoint }; } } } }
void unitIcon_MouseDown(object sender, MouseButtonEventArgs e) { var unitIcon = (UnitIcon)e.Source; if (Keyboard.Modifiers == ModifierKeys.None && e.RightButton == MouseButtonState.Released) { e.Handled = true; if (dragInfo == null) { var thisElement = (FrameworkElement)unitIcon; var startPoint = e.GetPosition(unitCanvas); var elements = unitIcons.Where(i => i.IsSelected).Select(x => (FrameworkElement)x).ToList(); if (!elements.Contains(thisElement)) { elements = new List <FrameworkElement> { thisElement } } ; var origins = new Dictionary <FrameworkElement, Point>(); foreach (var el in elements) { var pos = (Positionable)el.DataContext; origins.Add(el, new Point(pos.X, pos.Y)); } if (unitCanvas.CaptureMouse()) { dragInfo = new DragInfo { Elements = elements, ElementOrigins = origins, MouseOrigin = startPoint } } ; } } } void unitIcon_MouseUp(object sender, MouseButtonEventArgs e) { var unitIcon = (UnitIcon)e.Source; if (e.ChangedButton == MouseButton.Left) { if (Keyboard.Modifiers == ModifierKeys.Shift || Keyboard.Modifiers == ModifierKeys.Control) { unitIcon.IsSelected = !unitIcon.IsSelected; } //else //{ // foreach (UnitIcon i in unitIcons.Where(i => i.IsSelected)) i.IsSelected = false; // unitIcon.IsSelected = true; //} } } void unitIcon_UnitRequestedDelete(object sender, UnitEventArgs e) { var selectedUnits = unitIcons.Where(i => i.IsSelected).ToList(); if (selectedUnits.Contains((UnitIcon)e.Source)) { foreach (var unitIcon in selectedUnits) { var unit = (UnitStartInfo)unitIcon.DataContext; RemoveUnitIcon(unitIcon); action.Units.Remove(unit); } } else { var unitIcon = (UnitIcon)e.Source; var unit = (UnitStartInfo)unitIcon.DataContext; RemoveUnitIcon(unitIcon); action.Units.Remove(unit); } } void unitIcon_UnitRequestedSetGroups(object sender, UnitEventArgs e) { var source = (UnitIcon)e.Source; var selectedUnits = unitIcons.Where(i => i.IsSelected).ToList(); if (!selectedUnits.Contains(source)) { selectedUnits = new List <UnitIcon> { source } } ; var groupsString = selectedUnits.Count() == 1 ? String.Join(",", e.UnitInfo.Groups) : String.Empty; groupsString = Utils.ShowStringDialog("Insert groups (separate multiple groups with commas).", groupsString); if (groupsString != null) { foreach (var unit in selectedUnits) { var unitStartInfo = (UnitStartInfo)unit.DataContext; unitStartInfo.Groups = new ObservableCollection <string>(groupsString.Split(',')); } } } void unitIcon_UnitRequestedSetOwner(object sender, EventArgs <Player> e) { var source = (UnitIcon)sender; var selectedUnits = unitIcons.Where(i => i.IsSelected).ToList(); if (!selectedUnits.Contains(source)) { selectedUnits = new List <UnitIcon> { source } } ; foreach (var unit in selectedUnits) { unit.Unit.Player = e.Data; } } } }
void unitCanvas_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (dragInfo != null) { e.Handled = true; unitCanvas.ReleaseMouseCapture(); dragInfo = null; } if (isBoxSelecting) { // hide and release the box selectionBox.Visibility = Visibility.Collapsed; unitCanvas.ReleaseMouseCapture(); isBoxSelecting = false; Point boxEndPos = e.GetPosition(unitCanvas); double x1 = boxStartPos.X, y1 = boxStartPos.Y, x2 = boxEndPos.X, y2 = boxEndPos.Y; if (x2 < x1) { double temp = x1; x1 = x2; x2 = temp; } if (y2 < y1) { double temp = y1; y1 = y2; y2 = temp; } // clear existing selection if not Shift if (Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { List <UnitIcon> select = unitIcons.Where(i => i.IsSelected).ToList(); foreach (UnitIcon i in select) { i.IsSelected = false; } select = new List <UnitIcon>(); } // add to selection foreach (UnitIcon icon in unitIcons) { Positionable pos = (Positionable)icon.DataContext; if (pos.X >= x1 && pos.X <= x2 && pos.Y >= y1 && pos.Y <= y2) { icon.IsSelected = true; } } } if (unitDefGrid.Grid.SelectedItem != null && DateTime.Now - mouseDownDate < TimeSpan.FromMilliseconds(150) && e.ChangedButton == MouseButton.Left && Keyboard.Modifiers == ModifierKeys.None) { var unitType = (UnitInfo)unitDefGrid.Grid.SelectedItem; var mousePos = e.GetPosition(unitCanvas); var player = (Player)playerListBox.SelectedItem; var unitStartInfo = new UnitStartInfo(unitType, player, SnapToGridX(mousePos.X), SnapToGridY(mousePos.Y)); ((INotifyPropertyChanged)unitStartInfo).PropertyChanged += (se, ea) => // fixme: leak { if (ea.PropertyName == "Groups") { MainWindow.Instance.Mission.RaisePropertyChanged("AllGroups"); } }; action.Units.Add(unitStartInfo); PlaceUnitIcon(unitStartInfo); } }
void unitIcon_MouseDown(object sender, MouseButtonEventArgs e) { var unitIcon = (UnitIcon)e.Source; if (Keyboard.Modifiers == ModifierKeys.None && e.RightButton == MouseButtonState.Released) { e.Handled = true; if (dragInfo == null) { var thisElement = (FrameworkElement)unitIcon; var startPoint = e.GetPosition(unitCanvas); var elements = unitIcons.Where(i => i.IsSelected).Select(x => (FrameworkElement)x).ToList(); if(!elements.Contains(thisElement)) elements = new List<FrameworkElement> {thisElement}; var origins = new Dictionary<FrameworkElement, Point>(); foreach (var el in elements) { var pos = (Positionable)el.DataContext; origins.Add(el, new Point(pos.X, pos.Y)); } if (unitCanvas.CaptureMouse()) dragInfo = new DragInfo { Elements = elements, ElementOrigins = origins, MouseOrigin = startPoint }; } } }
void unitCanvas_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (dragInfo != null) { e.Handled = true; unitCanvas.ReleaseMouseCapture(); dragInfo = null; } if (isBoxSelecting) { // hide and release the box selectionBox.Visibility = Visibility.Collapsed; unitCanvas.ReleaseMouseCapture(); isBoxSelecting = false; Point boxEndPos = e.GetPosition(unitCanvas); double x1 = boxStartPos.X, y1 = boxStartPos.Y, x2 = boxEndPos.X, y2 = boxEndPos.Y; if (x2 < x1) { double temp = x1; x1 = x2; x2 = temp; } if (y2 < y1) { double temp = y1; y1 = y2; y2 = temp; } // clear existing selection if not Shift if (Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { List<UnitIcon> select = unitIcons.Where(i => i.IsSelected).ToList(); foreach (UnitIcon i in select) i.IsSelected = false; select = new List<UnitIcon>(); } // add to selection foreach (UnitIcon icon in unitIcons) { Positionable pos = (Positionable)icon.DataContext; if (pos.X >= x1 && pos.X <= x2 && pos.Y >= y1 && pos.Y <= y2) { icon.IsSelected = true; } } } if (unitDefGrid.Grid.SelectedItem != null && DateTime.Now - mouseDownDate < TimeSpan.FromMilliseconds(150) && e.ChangedButton == MouseButton.Left && Keyboard.Modifiers == ModifierKeys.None) { var unitType = (UnitInfo)unitDefGrid.Grid.SelectedItem; var mousePos = e.GetPosition(unitCanvas); var player = (Player)playerListBox.SelectedItem; var unitStartInfo = new UnitStartInfo(unitType, player, SnapToGridX(mousePos.X), SnapToGridY(mousePos.Y)); ((INotifyPropertyChanged)unitStartInfo).PropertyChanged += (se, ea) => // fixme: leak { if (ea.PropertyName == "Groups") MainWindow.Instance.Mission.RaisePropertyChanged("AllGroups"); }; action.Units.Add(unitStartInfo); PlaceUnitIcon(unitStartInfo); } }