private void _ConstructCardChoiceModel(List<DeckPlace> sourceDecks, List<string> resultDeckNames, List<int> resultDeckMaximums, AdditionalCardChoiceOptions options, ICardChoiceVerifier verifier, int timeOutSeconds, CardChoiceRearrangeCallback callback) { bool isSingleResult = (resultDeckMaximums.Sum() == 1); var choiceModel = new CardChoiceViewModel(); int numLines = sourceDecks.Count; foreach (var deck in sourceDecks) { if (Game.CurrentGame.Decks[deck].Count == 0) { continue; } CardChoiceLineViewModel line = new CardChoiceLineViewModel(); line.DeckName = deck.DeckType.Name; line.IsResultDeck = false; int i = 0; int numCards = Game.CurrentGame.Decks[deck].Count; int maxColumns = Math.Max(numCards / 2 + 1, 5); bool firstRow = true; foreach (var card in Game.CurrentGame.Decks[deck]) { if (numLines == 1 && isSingleResult && i >= maxColumns && firstRow) { Trace.Assert(choiceModel.CardStacks.Count == 0); choiceModel.CardStacks.Add(line); line = new CardChoiceLineViewModel(); line.DeckName = deck.DeckType.Name; line.IsResultDeck = false; firstRow = false; } CardViewModel model = new CardViewModel() { Card = card, IsSelectionMode = isSingleResult, IsEnabled = true }; line.Cards.Add(model); i++; } choiceModel.CardStacks.Add(line); } if (!isSingleResult) { int k = 0; foreach (var deckName in resultDeckNames) { CardChoiceLineViewModel line = new CardChoiceLineViewModel(); line.DeckName = deckName; if (options != null) { if (options.Rearrangeable != null) { line.Rearrangable = options.Rearrangeable[k]; } if (options.DefaultResult != null) { foreach (var card in options.DefaultResult[k]) { line.Cards.Add(new CardViewModel() { Card = card }); } } } line.Capacity = resultDeckMaximums[k++]; line.IsResultDeck = true; choiceModel.CardStacks.Add(line); } } if (options != null && options.Options != null) { for (int i = 0; i < options.Options.Count; i++) { MultiChoiceCommand command = new MultiChoiceCommand(ExecuteCardChoiceCommand) { CanExecuteStatus = false, ChoiceKey = options.Options[i], ChoiceIndex = i }; choiceModel.MultiChoiceCommands.Add(command); } } else { MultiChoiceCommand command = new MultiChoiceCommand(ExecuteCardChoiceCommand) { CanExecuteStatus = false, ChoiceKey = new OptionPrompt("Confirm") }; choiceModel.MultiChoiceCommands.Add(command); } if (options != null && options.IsCancellable) { MultiChoiceCommand command = new MultiChoiceCommand(CancelCardChoiceCommand) { IsCancel = true, ChoiceKey = new OptionPrompt("Cancel") }; choiceModel.MultiChoiceCommands.Add(command); } choiceModel.Verifier = verifier; choiceModel.TimeOutSeconds = timeOutSeconds; CardChoiceModel = choiceModel; }
public void AskForMultipleChoice(Prompt prompt, List<OptionPrompt> choices, int timeOutSeconds) { Application.Current.Dispatcher.Invoke((ThreadStart)delegate() { GameModel.CurrentActivePlayer = this; CurrentPrompt = prompt; CurrentPromptString = PromptFormatter.Format(prompt); _currentMultiChoices = choices; if (!IsPlayable) { Trace.Assert(currentUsageVerifier == null); TimeOutSeconds = timeOutSeconds; MultipleChoiceAnsweredEvent(0); return; } if (prompt.ResourceKey == Prompt.MultipleChoicePromptPrefix + Prompt.SkillUseYewNoPrompt) { Trace.Assert(prompt.Values.Count != 0); var targetSkill = prompt.Values[0] as TriggerSkill; Trace.Assert(targetSkill != null); foreach (var skill in SkillCommands) { if (skill.Skill == targetSkill) { skill.IsHighlighted = true; if (skill.IsSelected) { int answer = choices.IndexOf(Prompt.YesChoice); Trace.Assert(answer != -1); MultipleChoiceAnsweredEvent(answer); return; } else { break; } } } } for (int i = 0; i < choices.Count; i++) { MultiChoiceCommand command = new MultiChoiceCommand(ExecuteMultiChoiceCommand) { CanExecuteStatus = true, ChoiceKey = choices[i], ChoiceIndex = i }; if (choices[i] == Prompt.YesChoice) { SubmitAnswerCommand = command; } else if (choices[i] == Prompt.NoChoice) { CancelAnswerCommand = command; } else { MultiChoiceCommands.Add(command); } } lock (verifierLock) { IsMultiChoiceQuestionShown = true; TimeOutSeconds = timeOutSeconds; } }); }
private void UpdateModel() { CardChoiceViewModel model = DataContext as CardChoiceViewModel; if (model == null) { return; } _cardStacks.Children.Clear(); _canvas.Children.Clear(); if (model.CardStacks.Count == 0) { return; } int maxCount = (from line in model.CardStacks select line.Cards.Count).Max(); _cardStacks.Width = Math.Min(maxCount * Settings.CardChoiceBox.CardXSpacing, Settings.CardChoiceBox.MaxWindowWidth); _cardStacks.Height = model.CardStacks.Count * Settings.CardChoiceBox.CardYSpacing; ObservableCollection <string> deckNames = new ObservableCollection <string>(); // First, create layout. foreach (var line in model.CardStacks) { deckNames.Add(line.DeckName); CardStack stack = new SingleRowCardStack() { ParentCanvas = _canvas }; stack.MaxCardSpacing = Settings.CardChoiceBox.CardXSpacing; stack.MaxCardSpacingOnHighlighted = Settings.CardChoiceBox.CardXSpacing + 15; stack.CardAlignment = HorizontalAlignment.Center; stack.Height = 130d; stack.Margin = new Thickness(1, 10, 1, 10); stack.AddCards(line.Cards); foreach (var card in stack.Cards) { card.Cursor = Cursors.Hand; card.MouseEnter += card_MouseEnter; card.MouseLeave += card_MouseLeave; } stack.HorizontalAlignment = HorizontalAlignment.Left; stack.Width = _cardStacks.Width; _cardStacks.Children.Add(stack); } deckIcons.ItemsSource = deckNames; if (model == null || model.DisplayOnly) { return; } Trace.Assert(model != null); foreach (var s in model.CardStacks) { foreach (var card in s.Cards) { if (model.Verifier.Verify(new List <List <Card> >() { new List <Card>() { card.Card } }) != Core.UI.VerifierResult.Success) { card.IsEnabled = false; } card.OnSelectedChanged += (o, e) => { var c = o as CardViewModel; model.Answer.Clear(); model.Answer.Add(new List <Card>()); if (c.IsSelected) { model.Answer[0].Add(c.Card); foreach (var otherCardStack in model.CardStacks) { foreach (var otherCard in otherCardStack.Cards) { if (otherCard != o) { otherCard.IsSelected = false; } } } if (model.MultiChoiceCommands.Count == 1) { model.MultiChoiceCommands.First().Execute(null); } else { foreach (var command in model.MultiChoiceCommands) { MultiChoiceCommand mc = command as MultiChoiceCommand; mc.CanExecuteStatus = true; } } } else { foreach (var command in model.MultiChoiceCommands) { MultiChoiceCommand mc = command as MultiChoiceCommand; mc.CanExecuteStatus = false; } } }; } } if (model.MultiChoiceCommands.Count > 1) { gridChoices.Visibility = Visibility.Visible; } else { Trace.Assert(model.MultiChoiceCommands.Count == 1); gridChoices.Visibility = Visibility.Hidden; } if (model.TimeOutSeconds > 0) { Duration duration = new Duration(TimeSpan.FromSeconds(model.TimeOutSeconds)); DoubleAnimation doubleanimation = new DoubleAnimation(100d, 0d, duration); progressBar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation); } }