public void PopulateGrid(GridButton[,] buttons) { int rows = buttons.GetLength(0); int cols = buttons.GetLength(1); // Add rows for buttons for (int y = 0; y < rows; y++) { RowDefinition newRow = new RowDefinition(); ButtonGrid.RowDefinitions.Add(newRow); } // Add columns for buttons for (int x = 0; x < cols; x++) { ColumnDefinition newCol = new ColumnDefinition(); ButtonGrid.ColumnDefinitions.Add(newCol); } // Add buttons for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { buttons[x, y] = new GridButton(); buttons[x, y].SetCoordinate(x, y); buttons[x, y].Click += new RoutedEventHandler(GridClick); ButtonGrid.Children.Add(buttons[x, y]); } } }
public void SaveMap(GridButton[,] buttons, int mapSelection) { System.IO.Directory.CreateDirectory(@"Maps/"); System.IO.File.WriteAllText(@"Maps/Map - " + mapSelection.ToString() + ".h", CreateString(buttons, "map" + mapSelection.ToString())); }
public ERROR_CODE LoadMap(GridButton[,] buttons, int mapSelection) { try { using (System.IO.StreamReader file = new System.IO.StreamReader(@"Maps/Map - " + mapSelection.ToString() + ".h")) { String line; Char[] delim = { ',' }; Char[] remove = { ' ', '\t', '\n' }; // Ignore the first line line = file.ReadLine(); // Read rows of the file for (int r = 0; r < buttons.GetLength(0); r++) { // If line fails to be read if ((line = file.ReadLine()) == null) return ERROR_CODE.LOAD_ERROR_NOT_ENOUGH_LINES; // Clean up data line = line.Trim(remove); String[] res = line.Split(delim); // If not enough data remains if (res.Length < buttons.GetLength(1)) return ERROR_CODE.LOAD_ERROR_LINES_UNALIGNED; // Set the correct row in output for (int c = 0; c < buttons.GetLength(1); c++) buttons[c, r].isActive = (res[c].Trim(remove) != "0"); } } return ERROR_CODE.ALL_OK; } catch { for (int r = 0; r < buttons.GetLength(0); r++) for (int c = 0; c < buttons.GetLength(1); c++) buttons[c, r].isActive = false; return ERROR_CODE.LOAD_WARNING_FILE_NOT_FOUND; } }
public String CreateString(GridButton[,] buttons, String mapname) { String output = "bool " + mapname + " [" + buttons.Length.ToString() + "] = {"; for (int y = 0; y < buttons.GetLength(0); y++) { output += "\n\t"; for (int x = 0; x < buttons.GetLength(1); x++) { output += Convert.ToInt16(buttons[x, y].isActive).ToString() + ", "; } } output = output.Remove(output.Length - 2); output += "\n};\n"; return output; }
public void GridClick(object sender, RoutedEventArgs e) { GridButton b = (GridButton)sender; b.isActive = !b.isActive; }