private bool TryLoadGame(bool showSuccess) { string oldFileName = ViewModel.FileName, oldFileTitle = ViewModel.FileTitle; try { if (!GetFileNameToViewModel(false, false)) { return(false); } ModelException.TryThrow <GameController.T>(Controller.TryLoadGame(ViewModel.FileName)); if (showSuccess) { MessageBox.Show(this, "Hra byla načtena.", "Informace", MessageBoxButton.OK, MessageBoxImage.Information); } return(true); } catch (Exception exc) { ViewModel.SetFileName(oldFileName, oldFileTitle); MessageBox.Show(this, ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } }
private void Redo_Executed(object sender, ExecutedRoutedEventArgs e) { if (ViewModel.Position < ViewModel.NumberOfMoves) { try { ModelException.TryThrow <ReplayController.T>(Controller.tryIncPosition()); } catch (Exception exc) { ViewModel.Info = string.Format("Chyba: {0}", ViewModelCommon.ConvertExceptionToShortString(exc)); } } }
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { e.Handled = true; try { if (!ignoreChange) { ModelException.TryThrow <ReplayController.T>(Controller.tryGoToPosition((int)e.NewValue)); } } catch (Exception exc) { ViewModel.Info = string.Format("Chyba: {0}", ViewModelCommon.ConvertExceptionToShortString(exc)); } }
private void Redo_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; try { if (!ViewModel.IsGameCreated && ViewModel.IsRedoStackNotEmpty) { ModelException.TryThrow <GameController.T>(Controller.TryRedo()); CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Tah nelze opakovat." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private bool TrySaveGame(bool showSuccess, bool isSaveAsDialog) { try { if (!GetFileNameToViewModel(true, isSaveAsDialog)) { return(false); } ModelException.TryThrow <GameController.T>(Controller.TrySaveGame(ViewModel.FileName)); if (showSuccess) { MessageBox.Show(this, "Hra byla uložena.", "Informace", MessageBoxButton.OK, MessageBoxImage.Information); } return(true); } catch (Exception exc) { MessageBox.Show(this, ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } }
private void Resume_Executed(object sender, ExecutedRoutedEventArgs e) { try { if (ViewModel.IsPaused || ViewModel.IsCreated) { ModelException.TryThrow <ReplayController.T>(Controller.tryResume()); CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Nelze spustit replay kvůli chybě." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Replay_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; try { if (ViewModel.IsGameFinished) { ReplayModel.T replayModel = ModelException.TryThrow <ReplayModel.T>(ReplayModel.tryCreate(ViewModel.Model.Board, ViewModel.Model.PlayerSettings)); ReplayController.T controller = ModelException.TryThrow <ReplayController.T>(ReplayController.tryCreate(replayModel)); ReplayWindowViewModel vm = new ReplayWindowViewModel(replayModel); ReplayWindow win = new ReplayWindow(controller, vm); win.Owner = this; win.ShowDialog(); } } catch (Exception exc) { MessageBox.Show(this, "Okno přehrávače nelze otevřít." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void History_SelectionChanged(object sender, SelectionChangedEventArgs e) { e.Handled = true; try { if (!ignoreChange) { MoveHistoryItem item = e.AddedItems.OfType <MoveHistoryItem>().FirstOrDefault(); if (item != null) { ModelException.TryThrow <GameController.T>(Controller.TryGoToMove(item.ID)); } } } catch (Exception exc) { MessageBox.Show(this, "Nelze přejít na tah z historie." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void board_BoardSquareClicked(object sender, Controls.BoardSquareClickedEventArgs e) { try { if (ViewModel.IsGameWaitingForHumanPlayerMove) { if (ViewModel.Source != null) { if (e.Square.Coord.Equals(ViewModel.Source) && e.Square.IsSelected) { // clear selection if same source was clicked ViewModel.ClearSelection(); return; } // source was selected, try create move from target Result <Move.T, ErrorDefinitions.Error> result = Controller.TryGetValidMove(ViewModel.Source, e.Square.Coord); if (result.IsSuccess) { // user selected valid move if (MainWindowCommands.CancelSuggestMove.CanExecute(null, this)) { MainWindowCommands.CancelSuggestMove.Execute(null, this); } Move.T move = ((Result <Move.T, ErrorDefinitions.Error> .Success)result).Item; ModelException.TryThrow <Move.T>(Controller.TrySetSelectedMove(move)); } else { // unable to create move with specified target ErrorDefinitions.Error err = ((Result <Move.T, ErrorDefinitions.Error> .Error)result).Item; if (!err.IsNoValidMoveExists) { throw new ModelException(err); } else { // try to use it as new source bool isValidMoveSource = ModelException.TryThrow <bool>(Controller.TryMoveExistsForCoord(e.Square.Coord)); if (!isValidMoveSource) { e.BlinkRed = true; return; } else { ViewModel.ClearSelection(); } } } } if (ViewModel.Source == null) { // no source exists, select this coord as new source bool isValidMoveSource = ModelException.TryThrow <bool>(Controller.TryMoveExistsForCoord(e.Square.Coord)); e.BlinkRed = !isValidMoveSource; if (isValidMoveSource) { ViewModel.SetSource(e.Square.Coord); Controller.GetPossibleTargetCoords(ViewModel.Source).ForEach(ViewModel.SetIsSelected); } } CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Nebyl vybrán platný tah." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void SuggestMove_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; try { if (ViewModel.IsGameWaitingForHumanPlayerMove && !ViewModel.IsMoveSuggestionComputing) { ViewModel.ClearBoardIndications(); ModelException.TryThrow <GameController.T>(Controller.TrySuggestMove()); CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Nepodařilo se napovědět tah." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void CancelSuggestMove_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; try { if (ViewModel.IsMoveSuggestionComputing) { ModelException.TryThrow <GameController.T>(Controller.TryCancelSuggestMove()); CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Nepodařilo se zrušit nápovědu tahu." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Resume_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; try { if (ViewModel.IsGamePaused) { ModelException.TryThrow <GameController.T>(Controller.TryResume()); FocusBoard(); CommandManager.InvalidateRequerySuggested(); } } catch (Exception exc) { MessageBox.Show(this, "Nelze pokračovat ve hře kvůli chybě." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Model_HistoryChanged(object sender, HistoryChangedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { try { ViewModel.Board.History.ClearAndAddHistoryItems(e.Items); try { ignoreChange = true; Result <int, ErrorDefinitions.Error> result = Controller.tryPopMoveNumberFromUndoStack(); if (result.IsSuccess) { int id = ((Result <int, ErrorDefinitions.Error> .Success)result).Item; MoveHistoryItem item = ViewModel.Board.History.FirstOrDefault(i => i.ID == id); history.SelectedItem = item; } else { history.SelectedItem = null; } } finally { ignoreChange = false; } } catch (Exception exc) { MessageBox.Show(this, string.Format("Nepodařilo se aktualizovat historii tahů: {0}", ViewModelCommon.ConvertExceptionToShortString(exc)), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }), System.Windows.Threading.DispatcherPriority.Background); }
private void New_Executed(object sender, ExecutedRoutedEventArgs e) { e.Handled = true; PauseGame(); if (ViewModel.IsGameRunning) { return; } try { bool cancel = false; if (!ViewModel.IsGameCreated) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Chcete před spuštěním nové hry uložit stávající hru ?"); switch (MessageBox.Show(this, sb.ToString(), "Uložit hru ?", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning)) { case MessageBoxResult.Cancel: cancel = true; break; case MessageBoxResult.Yes: cancel = !TrySaveGame(false, false); break; case MessageBoxResult.No: break; } } if (!cancel) { NewGame(); } } catch (Exception exc) { MessageBox.Show(this, "Novou hru se nepodařilo vytvořit." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error); } }