Files @ 7117d2e703c8
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/ScreenSettingsViewModel.cs

Malkyne
Updated scanner for Tale 10, and fixed several bugs.

using Avalonia.Platform;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using ReactiveUI;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using DesertPaintCodex.Util;

namespace DesertPaintCodex.ViewModels
{
    public class ScreenSettingsViewModel : ViewModelBase
    {
        public ObservableCollection<ScreenMetrics> Screens { get; } = new();
        
        private int _pixeMultiplier;
        public int PixelMultiplier { get => _pixeMultiplier; private set => this.RaiseAndSetIfChanged(ref _pixeMultiplier, value); }

        private int _screenIndex;
        public int ScreenIndex { get => _screenIndex; private set => this.RaiseAndSetIfChanged(ref _screenIndex, value); }
        
        public ScreenSettingsViewModel()
        {
            if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                IScreenImpl screen = desktop.MainWindow.PlatformImpl.Screen;
                IReadOnlyList<Screen> screens = screen.AllScreens;

                for (int i = 0; i < screens.Count; i++)
                {
                    Screens.Add(new ScreenMetrics(screens[i].Bounds, screens[i].Primary));
                }
                
                SettingsService.Get(SettingKey.PixelMultiplier, out _pixeMultiplier, Constants.DefaultPixelMultiplier);
                SettingsService.Get(SettingKey.ScreenIndex,     out _screenIndex, 0);

                _screenIndex = System.Math.Min(_screenIndex, screens.Count - 1); // In case the user has fewer screens than last time.

                Save = ReactiveCommand.Create(() => {
                    SettingsService.Set(SettingKey.PixelMultiplier, _pixeMultiplier);
                    SettingsService.Set(SettingKey.ScreenIndex,     _screenIndex);
                    SettingsService.Set(SettingKey.ScreenX,         screens[_screenIndex].Bounds.X);
                    SettingsService.Set(SettingKey.ScreenY,         screens[_screenIndex].Bounds.Y);
                    SettingsService.Set(SettingKey.ScreenWidth,     screens[_screenIndex].Bounds.Width);
                    SettingsService.Set(SettingKey.ScreenHeight,    screens[_screenIndex].Bounds.Height);

                    SettingsService.Save();
                
                    ReactionScannerService.Instance.RefreshFromSettings();
                });
            }
            else
            {
                // Placeholder stuff.
                Screens.Add(new ScreenMetrics(new Avalonia.PixelRect(0, 0, 1920, 1080), true));
                Screens.Add(new ScreenMetrics(new Avalonia.PixelRect(1920, 0, 1920, 1080), false));
                
                PixelMultiplier       = 1;
                ScreenIndex           = 0;
                
                Save = ReactiveCommand.Create(() => { });
            }
        }

        public ReactiveCommand<Unit, Unit> Save { get; }
    }
}