public Map.MapType StageDataGet(int x, int z) { MapTypeData = Map.MapType.Asphalt; switch (Type) { case StageType.City1: break; case StageType.Forest1: if (x <= 8 && 3 <= z && z <= 11 || x >= 5 && x <= 13 && z >= 14 && z <= 22 || 13 <= x && x <= 21 && z <= 8 || 15 <= x && x <= 23 && 15 <= z && z <= 23 || 10 <= x && x <= 18 && 9 <= z && z <= 17) { MapTypeData = Map.MapType.Forest; } break; case StageType.Wasteland: break; case StageType.Factory1: MapTypeData = Map.MapType.Asphalt; break; case StageType.Mountain1: MapTypeData = Map.MapType.Wasteland; break; default: break; } return(MapTypeData); }
public Controller(Model model, View view) { /* initialize class fields */ this.model = model; this.view = view; this.workers = new List<BackgroundWorker>(); this.cancelled = false; this.running = 0; this.mapType = Map.MapType.Empty; this.nodeEditBackColor = Color.White; this.nodeEditForeColor = Color.White; this.nodeEditBackColor = Color.White; this.nodeEditState = NodeEditState.DISABLED; this.nodeEditFinishAgentId = Agent.AgentID.Agent_0; this.nodeEditStartAgentId = Agent.AgentID.Agent_0; /* initialize the volatile variables */ tempMapSize = model.getSimulation().get(View.ViewNumericUpDown.MapSize); tempPercentWalkable = model.getSimulation().get(View.ViewNumericUpDown.PercentWalkable); tempNumberofAgents = model.getSimulation().get(View.ViewNumericUpDown.NumberOfAgents); /* generate an empty map without agents */ generateMapCommon(null, false); model.getSimulation().setMapID(0); /* mouse event handlers */ view.getPanelMap().MouseDown += new MouseEventHandler(Controller_MouseDown); view.getPanelMap().MouseUp += new MouseEventHandler(Controller_MouseUp); view.getPanelMap().MouseClick += new MouseEventHandler(Controller_MouseClick); this.mouseDownNodes = new List<Node>(); mouseDownRunning = false; mouseDownBackgroundWorker = new BackgroundWorker(); mouseDownBackgroundWorker.DoWork += new DoWorkEventHandler(collectNodes_DoWork); }
public void Button_Click(object sender, View.ViewButton eventHandler) { Debug.WriteLine(eventHandler.ToString() + " Clicked"); switch (eventHandler) { case View.ViewButton.RandomMap: mapType = Map.MapType.Random; break; case View.ViewButton.EmptyMap: mapType = Map.MapType.Empty; break; case View.ViewButton.ResetMap: DrawingControl.SuspendDrawing(view.getPanelMap()); model.Reset(true); DrawingControl.ResumeDrawing(view.getPanelMap()); break; } switch (eventHandler) { case View.ViewButton.RandomMap: case View.ViewButton.EmptyMap: /* save the temp variables */ model.getSimulation().set(View.ViewNumericUpDown.MapSize, tempMapSize); model.getSimulation().setCellSize(view.PANEL_SIZE / tempMapSize); model.getSimulation().set(View.ViewNumericUpDown.PercentWalkable, tempPercentWalkable); model.getSimulation().set(View.ViewNumericUpDown.NumberOfAgents, tempNumberofAgents); /* generate map */ generateMapCommon(null, true); /* initialize the node edit variables */ nodeEditStartAgentId = Agent.AgentID.Agent_0; nodeEditFinishAgentId = Agent.AgentID.Agent_0; nodeEditBackColor = model.getAgent(Agent.AgentID.Agent_0).getColor(Agent.ColorType.BackColor); nodeEditForeColor = model.getAgent(Agent.AgentID.Agent_0).getColor(Agent.ColorType.ForeColor); /* update view */ view.updateAgentItems(); view.getButton(View.ViewButton.Start).Enabled = true; view.getRadioButton(View.ViewRadioButton.EditWalkable).Enabled = true; view.getRadioButton(View.ViewRadioButton.EditNonWalkable).Enabled = true; view.setStatusText("Change Parameters, Edit Map, or Click Start"); view.buttonEditStartUpdate(nodeEditBackColor, nodeEditForeColor, "S", true); view.buttonEditFinishUpdate(nodeEditBackColor, nodeEditForeColor, "F", true); break; case View.ViewButton.Start: cancelled = false; nodeEditState = NodeEditState.DISABLED; model.getMap().setNodes(Node.Cost.Movement, 0.0); model.getMap().setNodes(Node.Cost.Heuristic, 0.0); model.getMap().setNodes(Node.Cost.Total, 0.0); foreach (Agent agent in model.getAgents()) { agent.getMap().setNodes(Node.Cost.Movement, 0.0); agent.getMap().setNodes(Node.Cost.Heuristic, 0.0); agent.getMap().setNodes(Node.Cost.Total, 0.0); agent.getMap().setNodes(Node.Flag.IsWalkable, true); agent.getMap().setNodes(Node.Flag.IsVisible, false); agent.getMap().setNodes(Node.Flag.IsShared, false); } /* update the view */ view.setStatusText(""); view.getButton(View.ViewButton.Cancel).Enabled = true; view.getButton(View.ViewButton.Start).Enabled = false; view.getButton(View.ViewButton.RandomMap).Enabled = false; view.getButton(View.ViewButton.EmptyMap).Enabled = false; view.controlEnable(false); view.getComboBox(View.ViewComboBox.Visualizations).Enabled = false; DrawingControl.SuspendDrawing(view.getPanelMap()); model.Reset(false); DrawingControl.ResumeDrawing(view.getPanelMap()); synchronization = new Synchronization(model.getSimulation().get(View.ViewNumericUpDown.NumberOfAgents)); /* start the agent background threads */ foreach (Agent agent in model.getAgents()) { running++; agent.setNode(Agent.NodeType.Current, agent.getNode(Agent.NodeType.Start)); agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish)); agent.getMap().setNodes(Node.Flag.IsPath, false); agent.getNode(Agent.NodeType.Start).setFlag(Node.Flag.IsWalkable, true); agent.getNode(Agent.NodeType.Finish).setFlag(Node.Flag.IsWalkable, true); BackgroundWorker bgw = new BackgroundWorker(); bgw.WorkerSupportsCancellation = true; bgw.DoWork += new DoWorkEventHandler(Start_DoWork); bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Start_RunWorkerCompleted); bgw.RunWorkerAsync(agent); workers.Add(bgw); } /* init the parameters for un-applied map changes */ view.getNumericUpDown(View.ViewNumericUpDown.MapSize).Value = model.getSimulation().get(View.ViewNumericUpDown.MapSize); view.getNumericUpDown(View.ViewNumericUpDown.PercentWalkable).Value = model.getSimulation().get(View.ViewNumericUpDown.PercentWalkable); view.getNumericUpDown(View.ViewNumericUpDown.NumberOfAgents).Value = model.getSimulation().get(View.ViewNumericUpDown.NumberOfAgents); break; case View.ViewButton.Cancel: cancelled = true; view.getButton(View.ViewButton.Cancel).Enabled = false; view.getButton(View.ViewButton.Start).Enabled = true; view.getButton(View.ViewButton.RandomMap).Enabled = true; view.getButton(View.ViewButton.EmptyMap).Enabled = true; view.controlEnable(true); foreach (BackgroundWorker bgw in workers) { bgw.CancelAsync(); } break; case View.ViewButton.ExportCsv: StringBuilder result = new StringBuilder(); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "*.csv|"; saveFileDialog.OverwritePrompt = true; if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName != null) { ListView listView = view.getSimulationListView(); /* export the column headers */ foreach (ColumnHeader ch in listView.Columns) result.Append(ch.Text + ","); result.AppendLine(); /* export the data rows */ foreach (ListViewItem listItem in listView.Items) { foreach (ListViewItem.ListViewSubItem lvs in listItem.SubItems) result.Append(lvs.Text + ","); result.AppendLine(); } try { File.WriteAllText(saveFileDialog.FileName + ".csv", result.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message, "CSV Export Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } break; case View.ViewButton.ClearResults: model.getResults().clear(); break; } }
/// <summary> /// Opens an open file dialog box and saves the image as a bitmap. /// </summary> /// <param name="type">The map type to load.</param> private void OpenImage(Map.MapType type) { this.OpenFileDialog.Title = $@"Open {type.ToString()} Map..."; DialogResult result = this.OpenFileDialog.ShowDialog(); // If open file dialog was successful, load the bitmap of the image into memory and update the label with the filename if (result == DialogResult.OK) { try { switch (type) { case Map.MapType.Metallic: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.MaskMaps[Map.MapType.Metallic].Bitmap = new Bitmap(bmpTemp); } this.MetallicMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } case Map.MapType.Roughness: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.MaskMaps[Map.MapType.Roughness].Bitmap = new Bitmap(bmpTemp); } this.RoughnessMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } case Map.MapType.AO: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.MaskMaps[Map.MapType.AO].Bitmap = new Bitmap(bmpTemp); } this.AOMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } case Map.MapType.Detail_Mask: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.MaskMaps[Map.MapType.Detail_Mask].Bitmap = new Bitmap(bmpTemp); } this.DetailMaskMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } case Map.MapType.Color: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.OpacityMaps[Map.MapType.Color].Bitmap = new Bitmap(bmpTemp); } this.ColorMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } case Map.MapType.Opacity: { using (Bitmap bmpTemp = new Bitmap(this.OpenFileDialog.FileName)) { this.OpacityMaps[Map.MapType.Opacity].Bitmap = new Bitmap(bmpTemp); } this.OpacityMapLabel.Text = this.OpenFileDialog.SafeFileName; break; } } } // If there was an exception, show an error message catch (Exception) { MessageBox.Show($@"Unable to read image: {this.OpenFileDialog.SafeFileName}!{Environment.NewLine}Please select a valid image format.{Environment.NewLine}For best results use JPG or PNG format."); } } // If file open dialog was unsuccessful, reset file name to default else { this.OpenFileDialog.FileName = ""; } }