/// <summary> /// Instanciate and fill a new board /// </summary> /// <param name="w">width in square</param> /// <param name="h">height in square</param> private void CreateBoard(int w, int h) { var uniformGrid = (UniformGrid)this.FindName("Board"); if (uniformGrid == null) return; this.mapElements_ = new MapElements(w, h); uniformGrid.Children.Clear(); uniformGrid.Columns = w; uniformGrid.Rows = h; for (var i = 0; i < h; i++) { for (var j = 0; j < w; j++) { var elt = new SquareLabel(i, j); elt.Entities[KeResources.Index[KeResources.Type.Background]] = KeResources.Proxy[KeResources.Type.Background].Clone(); elt.Content = new AssetImage(KeResources.Type.Background); elt.MouseLeftButtonUp += OnLeftButtonUp; elt.MouseLeftButtonDown += OnLeftButtonDown; elt.MouseRightButtonUp += OnRightButtonUp; elt.MouseRightButtonDown += OnRightButtonDown; elt.MouseEnter += OnBucketAction; uniformGrid.Children.Add(elt); } } }
/// <summary> /// Open a file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonOpen_OnClick(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog(); var mySerializer = new XmlSerializer(typeof(MapElements)); dialog.ShowDialog(); dialog.CheckFileExists = true; if (!string.IsNullOrEmpty(dialog.FileName)) { using (var stream = dialog.OpenFile()) { try { this.mapElements_ = (MapElements) mySerializer.Deserialize(stream); } catch (Exception) { var se = this.FindName("SelectedEntity") as Label; if (se != null) se.Content = "Fail to load file. "; } this.Title = Path.GetFileName(dialog.FileName); var box = this.FindName("TextBoxFilename") as TextBox; if (box != null) { box.Text = Path.GetFileNameWithoutExtension(dialog.FileName); } var uniformGrid = (UniformGrid) this.FindName("Board"); if (uniformGrid == null) return; uniformGrid.Children.Clear(); uniformGrid.Columns = this.mapElements_.SizeX; uniformGrid.Rows = this.mapElements_.SizeY; for (var i = 0; i < this.mapElements_.SizeY; i++) { for (var j = 0; j < this.mapElements_.SizeX; j++) { var elt = new SquareLabel(i, j); foreach (var entity in this.mapElements_.Board[j][i].Entities) { elt.Entities[KeResources.Index[KeResources.TypeLink[entity.TileIdentifier]]] = entity.Clone(); elt.Content = new AssetImage(entity.TileIdentifier); } elt.MouseLeftButtonUp += OnLeftButtonUp; elt.MouseLeftButtonDown += OnLeftButtonDown; elt.MouseRightButtonUp += OnRightButtonUp; elt.MouseRightButtonDown += OnRightButtonDown; elt.MouseEnter += OnBucketAction; uniformGrid.Children.Add(elt); } } var turn = this.FindName("TextBoxTurn") as TextBox; turn.Text = this.mapElements_.Score.Turn.ToString(CultureInfo.InvariantCulture); var score1 = this.FindName("TextBoxScore1") as TextBox; score1.Text = this.mapElements_.Score.Score1.ToString(CultureInfo.InvariantCulture); var score2 = this.FindName("TextBoxScore2") as TextBox; score2.Text = this.mapElements_.Score.Score2.ToString(CultureInfo.InvariantCulture); var score3 = this.FindName("TextBoxScore3") as TextBox; score3.Text = this.mapElements_.Score.Score3.ToString(CultureInfo.InvariantCulture); } } }