Example #1
0
        private void ShowroomButton_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
                {
                    e.Handled = true;
                    CustomShowroomWrapper.StartAsync(Car, SelectedSkin);
                    return;
                }

                if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) ||
                    !CarOpenInShowroomDialog.Run(Car, SelectedSkin?.Id))
                {
                    e.Handled = true;
                    new CarOpenInShowroomDialog(Car, SelectedSkin?.Id).ShowDialog();
                }
            }
            else if (e.ChangedButton == MouseButton.Right)
            {
                e.Handled = true;
                var contextMenu = new ContextMenu();

                var item = new MenuItem {
                    Header = ControlsStrings.Car_OpenInShowroom
                };
                item.Click += (s, args) => CarOpenInShowroomDialog.Run(Car, SelectedSkin?.Id);
                contextMenu.Items.Add(item);

                item = new MenuItem {
                    Header = ControlsStrings.Common_Presets
                };
                foreach (var menuItem in PresetsMenuHelper.GroupPresets(CarOpenInShowroomDialog.PresetableKeyValue, p => {
                    CarOpenInShowroomDialog.RunPreset(p.Filename, Car, SelectedSkin?.Id);
                }))
                {
                    item.Items.Add(menuItem);
                }
                contextMenu.Items.Add(item);

                item = new MenuItem {
                    Header = ControlsStrings.Common_Settings, InputGestureText = UiStrings.KeyShift
                };
                item.Click += (s, args) => new CarOpenInShowroomDialog(Car, SelectedSkin?.Id).ShowDialog();
                contextMenu.Items.Add(item);

                // TODO: Presets!

                item = new MenuItem {
                    Header = ControlsStrings.Car_OpenInCustomShowroom, InputGestureText = UiStrings.KeyAlt
                };
                item.Click += (s, args) => CustomShowroomWrapper.StartAsync(Car, SelectedSkin);
                contextMenu.Items.Add(item);

                contextMenu.IsOpen = true;
            }
        }
Example #2
0
        public RaceGridViewModel(bool ignoreStartingPosition = false, [CanBeNull] string keySaveable = KeySaveable)
        {
            IgnoreStartingPosition = ignoreStartingPosition;

            _saveable = new SaveHelper <SaveableData>(keySaveable, () => {
                var data = new SaveableData {
                    ModeId                = Mode.Id,
                    FilterValue           = FilterValue,
                    ShuffleCandidates     = ShuffleCandidates,
                    AiLevelFixed          = AiLevelFixed,
                    AiLevelArrangeRandom  = AiLevelArrangeRandom,
                    AiLevelArrangeReverse = AiLevelArrangeReverse,
                    AiLevel               = AiLevel,
                    AiLevelMin            = AiLevelMin,
                    OpponentsNumber       = OpponentsNumber,
                    StartingPosition      = StartingPosition,
                };

                if (Mode == BuiltInGridMode.CandidatesManual)
                {
                    var priority = false;
                    data.CarIds  = NonfilteredList.Select(x => {
                        if (x.CandidatePriority != 1)
                        {
                            priority = true;
                        }
                        return(x.Car.Id);
                    }).ToArray();

                    if (priority)
                    {
                        data.CandidatePriorities = NonfilteredList.Select(x => x.CandidatePriority).ToArray();
                    }
                }
                else if (Mode == BuiltInGridMode.Custom)
                {
                    data.CarIds = NonfilteredList.Where(x => !x.SpecialEntry).Select(x => x.Car.Id).ToArray();
                }

                if (data.CarIds != null)
                {
                    var filtered = NonfilteredList.Where(x => !x.SpecialEntry).ToList();

                    if (filtered.Any(x => x.AiLevel.HasValue))
                    {
                        data.AiLevels = filtered.Select(x => x.AiLevel ?? -1).ToArray();
                    }

                    if (filtered.Any(x => x.Name != null))
                    {
                        data.Names = filtered.Select(x => x.Name).ToArray();
                    }

                    if (filtered.Any(x => x.Nationality != null))
                    {
                        data.Nationalities = filtered.Select(x => x.Nationality).ToArray();
                    }

                    if (filtered.Any(x => x.CarSkin != null))
                    {
                        data.SkinIds = filtered.Select(x => x.CarSkin?.Id).ToArray();
                    }
                }

                return(data);
            }, data => {
                ShuffleCandidates    = data.ShuffleCandidates ?? true;
                AiLevelFixed         = data.AiLevelFixed ?? false;
                AiLevelArrangeRandom = data.AiLevelArrangeRandomly.HasValue ? (data.AiLevelArrangeRandomly.Value ? 1d : 0d) :
                                       data.AiLevelArrangeRandom ?? 0.1d;
                AiLevelArrangeReverse = data.AiLevelArrangeReverse ?? false;
                AiLevel    = data.AiLevel ?? 95;
                AiLevelMin = data.AiLevelMin ?? 85;

                FilterValue  = data.FilterValue;
                ErrorMessage = null;

                var mode = Modes.GetByIdOrDefault <IRaceGridMode>(data.ModeId);
                if (mode == null)
                {
                    NonfatalError.NotifyBackground(ToolsStrings.RaceGrid_GridModeIsMissing,
                                                   string.Format(ToolsStrings.RaceGrid_GridModeIsMissing_Commentary, data.ModeId));
                    Mode = BuiltInGridMode.SameCar;
                }
                else
                {
                    Mode = mode;
                }

                if (Mode.CandidatesMode)
                {
                    if (Mode == BuiltInGridMode.CandidatesManual && data.CarIds != null)
                    {
                        // TODO: Async?
                        NonfilteredList.ReplaceEverythingBy(data.CarIds.Select(x => CarsManager.Instance.GetById(x)).Select((x, i) => {
                            if (x == null)
                            {
                                return(null);
                            }

                            var aiLevel   = data.AiLevels?.ElementAtOrDefault(i);
                            var carSkinId = data.SkinIds?.ElementAtOrDefault(i);
                            return(new RaceGridEntry(x)
                            {
                                CandidatePriority = data.CandidatePriorities?.ElementAtOr(i, 1) ?? 1,
                                AiLevel = aiLevel >= 0 ? aiLevel : (int?)null,
                                Name = data.Names?.ElementAtOrDefault(i),
                                Nationality = data.Nationalities?.ElementAtOrDefault(i),
                                CarSkin = carSkinId != null ? x.GetSkinById(carSkinId) : null,
                            });
                        }).NonNull());
                    }
                    else
                    {
                        NonfilteredList.Clear();
                    }

                    SetOpponentsNumberInternal(data.OpponentsNumber ?? 7);
                }
                else
                {
                    NonfilteredList.ReplaceEverythingBy(data.CarIds?.Select(x => CarsManager.Instance.GetById(x)).Select((x, i) => {
                        if (x == null)
                        {
                            return(null);
                        }

                        var aiLevel   = data.AiLevels?.ElementAtOrDefault(i);
                        var carSkinId = data.SkinIds?.ElementAtOrDefault(i);

                        return(new RaceGridEntry(x)
                        {
                            AiLevel = aiLevel >= 0 ? aiLevel : null,
                            Name = data.Names?.ElementAtOrDefault(i),
                            Nationality = data.Nationalities?.ElementAtOrDefault(i),
                            CarSkin = carSkinId != null ? x.GetSkinById(carSkinId) : null,
                        });
                    }).NonNull() ?? new RaceGridEntry[0]);
                }

                StartingPosition = data.StartingPosition ?? 7;
                FinishLoading();
            }, Reset);

            _presetsHelper = new PresetsMenuHelper();

            _randomGroup = new HierarchicalGroup(ToolsStrings.RaceGrid_Random);
            UpdateRandomModes();

            Modes = new HierarchicalGroup {
                BuiltInGridMode.SameCar,
                _randomGroup,
                BuiltInGridMode.Custom,
                _presetsHelper.Create(PresetableKeyValue, p => {
                    ImportFromPresetData(p.ReadData());
                }, ControlsStrings.Common_Presets)
            };

            NonfilteredList.CollectionChanged   += OnCollectionChanged;
            NonfilteredList.ItemPropertyChanged += OnItemPropertyChanged;
            FilteredView = new BetterListCollectionView(NonfilteredList)
            {
                CustomSort = this
            };

            // _saveable.Initialize();
            FilesStorage.Instance.Watcher(ContentCategory.GridTypes).Update += OnGridTypesUpdate;
        }