private void Rectangle_PreviewMouseMove(object sender, MouseEventArgs e) { var element = sender as Rectangle; if (element.Tag == null) { return; } // Initialize the drag & drop operation ChallengerLib.Models.Block data = (ChallengerLib.Models.Block)element.Tag; if (data == null) { return; } if (dragging) { Point mousePos = e.GetPosition(null); Vector diff = draggedPoint - mousePos; if (e.LeftButton == MouseButtonState.Pressed && Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) { // Initialize the drag & drop operation DragDrop.DoDragDrop(element, data, DragDropEffects.Move); } } }
private void OkayButton_Click(object sender, RoutedEventArgs e) { int n; if (NameField.Text == "" || ScoreField.Text == "" || selectedIconPath == null) { MessageBox.Show("At least one of the required parameters for Block Creation is empty."); } else if (NameField.Text.ToUpper() == "DEFAULT") { MessageBox.Show($"{NameField.Text} cannot be the Name of the Block"); } else if (simObjectNames.Except(new List <string> { previousName }).Contains(NameField.Text)) { MessageBox.Show($"{NameField.Text} already exists. Please choose a new name."); } else if (!int.TryParse(ScoreField.Text, out n)) { MessageBox.Show($"{ScoreField.Text} is not a numeric value"); } else if (NewBlock == false && blockToEdit != null) { blockToEdit.Name = NameField.Text; blockToEdit.Score = double.Parse(ScoreField.Text); blockToEdit.Icon = selectedIconPath.Name; blockToEdit.ID = selectedBlockTemplate.ID; blockToEdit.IsEndSimulation = blockToEdit.ID == 2; CreatedBlock = blockToEdit; DialogResult = true; this.Close(); } else { CreatedBlock = new ChallengerLib.Models.Block() { Icon = selectedIconPath.Name, Name = NameField.Text, Score = int.Parse(ScoreField.Text), ID = selectedBlockTemplateID, BlockID = Guid.NewGuid().ToString("N") }; //BlockGUID = Guid.NewGuid().ToString(); CreatedBlock.IsEndSimulation = CreatedBlock.ID == 2; DialogResult = true; this.Close(); } }
public static void SetSelectedIcon(ChallengerLib.Models.Block tmpl, ListView listView) { listView.SelectedItem = tmpl; List <ChallengerIcon> icons = listView.Items.Cast <ChallengerIcon>().Select(item => item).ToList(); int index = 0; foreach (var a in icons) { if (tmpl.Icon == a.Name) { break; } index++; } listView.SelectedIndex = index; }
private void EditAllFromGrid(ChallengerLib.Models.Block newBlock) { for (int i = 0; i < config.Height; i++) { for (int j = 0; j < config.Width; j++) { var toRem = config.Blocks[i, j]; if (toRem == null) { continue; } if (toRem.BlockID == newBlock.BlockID) { config.Blocks[i, j].Score = newBlock.Score; config.Blocks[i, j].Name = newBlock.Name; config.Blocks[i, j].Icon = newBlock.Icon; } } } }
public void SetEdit(ChallengerLib.Models.Block tmpl) { blockToEdit = tmpl; NewEditBoxWindow1.Title = "Edit Simulation Object Window"; NameField.Text = tmpl.Name; ScoreField.Text = tmpl.Score.ToString(); previousName = tmpl.Name; int blockTemplateIndex = 0; foreach (BlockTemplate item in BlockTypeListView.Items) { if (item.ID == tmpl.ID) { break; } blockTemplateIndex++; } BlockTypeListView.SelectedIndex = blockTemplateIndex; SetSelectedIcon(tmpl, BlockIconListView); NewBlock = false; }
private void GridRect_Drop(object sender, DragEventArgs e) { if (!dragging) { return; } bool failed = false; Rectangle rect = (Rectangle)sender; int currentRow = Grid.GetRow(rect); int currentCol = Grid.GetColumn(rect); ChallengerLib.Models.Block block = (ChallengerLib.Models.Block)e.Data.GetData(typeof(ChallengerLib.Models.Block)); ChallengerLib.Models.Block newBlock = new ChallengerLib.Models.Block(); newBlock.ID = block.ID; newBlock.Icon = block.Icon; newBlock.IsEndSimulation = block.IsEndSimulation; newBlock.Name = block.Name; newBlock.Score = block.Score; newBlock.X = block.X; newBlock.Y = block.Y; newBlock.BlockID = block.BlockID; //Robot or the starting location is moved, //should update the config's starting location as well. if (block.ID == 1) //TODO: ID WILL NOT BE USED AS IDENTIFIER LATER { if (draggedFromName != "gridMain") { foreach (var a in config.Blocks) { if (a == null) { continue; } if (a.ID == 1) { //MessageBox.Show("You are not allowed to create more than 1 starting point.", "Action Not Allowed", MessageBoxButton.OK, MessageBoxImage.Error); notifier.ShowError("You are not allowed to create more than 1 starting point."); LoadConfigurationBlocks(); failed = true; dragging = false; break; } } } else { foreach (var a in config.Blocks) { if (a == null) { continue; } if (a.ID == 1 && allowCopy) { //MessageBox.Show("You are not allowed to create more than 1 starting point, please uncheck allow copy option above.", "Action Not Allowed", MessageBoxButton.OK, MessageBoxImage.Error); notifier.ShowError("You are not allowed to create more than 1 starting point, please uncheck allow copy option above."); LoadConfigurationBlocks(); failed = true; dragging = false; break; } } } config.StartingLocation = new Location { X = currentRow, Y = currentCol }; } previousHeight = config.Height; previousWidth = config.Width; if (failed) { return; } newBlock.X = currentRow; newBlock.Y = currentCol; rect.Fill = new ImageBrush { ImageSource = new BitmapImage(new Uri(block.Icon, UriKind.Relative)) }; rect.Tag = newBlock; config.Blocks[currentRow, currentCol] = newBlock; if (!allowCopy) // allowCopy = rectangle inside the grid will replicate each time it is moved. { if (draggedFromName == "gridMain") // Ignore the dragged rectangle from the side panel. { //Get the previous position of the rectangle from the grid and removed it from there //as it was already transferred with the code above. var previousRect = gridMain.Children .Cast <Rectangle>() .Where(i => Grid.GetRow(i) == previousRow && Grid.GetColumn(i) == previousCol) .FirstOrDefault(); previousRect.ClearValue(Rectangle.FillProperty); config.Blocks[previousRow.Value, previousCol.Value] = null; } } SaveConfiguration(); LoadConfigurationBlocks(); previousRow = null; previousCol = null; dragging = false; draggedFromName = ""; }