Files @ e7cc8ddcbf58
Branch filter:

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

Jason Maltzen
Update version number to 1.4.0.0
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
f419334a476f
f419334a476f
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
f419334a476f
40eaee10ae56
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
7117d2e703c8
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
f419334a476f
40eaee10ae56
f419334a476f
f419334a476f
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
7117d2e703c8
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
7117d2e703c8
7117d2e703c8
7117d2e703c8
7117d2e703c8
7117d2e703c8
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
40eaee10ae56
7117d2e703c8
40eaee10ae56
40eaee10ae56
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using DesertPaintCodex.Util;
using ReactiveUI;

namespace DesertPaintCodex.ViewModels
{
    public class ReactionTestViewModel : ViewModelBase
    {
        private ReactionTest _reactionTest = Constants.StubReactionTest;
        public ReactionTest ReactionTest
        {
            get => _reactionTest;
            set => this.RaiseAndSetIfChanged(ref _reactionTest, value);
        }

        private readonly List<Reagent> _allPigmentList = new();
        private readonly List<Reagent> _allReagentList = new();
        public ObservableCollection<Reagent> BufferList { get; } = new();


        public ReactionTestViewModel()
        {
            List<string> reagentNames = ReagentService.Names;
            
            foreach (var reagent in reagentNames.Select(ReagentService.GetReagent))
            {
                if (!reagent.IsCatalyst)
                { 
                    _allPigmentList.Add(reagent);
                }
                _allReagentList.Add(reagent);
            }

            this.WhenAnyValue(x => x.ReactionTest)
                .Subscribe(_ => UpdateDerivedState());
            
            ShowScreenSettingsDialog = new Interaction<ScreenSettingsViewModel, Unit>();
            SaveReaction = ReactiveCommand.Create(() => ReactionTest.SaveReaction());
            ClearReaction = ReactiveCommand.Create(() => ReactionTest.ClearReaction());
            FinalizeTestResults = ReactiveCommand.Create(Test);
        }

        private void UpdateDerivedState()
        {
            // There are more "reactive" ways to pull this off, but I don't have time to figure out all those recipes
            // right now.
            SetupTest();
        }
            
        
        private void SetupTest()
        {
            if (ReactionTest.Requires3Way)
            {
                FilterBufferPigments();
            }
        }

        private void FilterBufferPigments()
        {
            // Avalonia doesn't really have proper filtering for listy controls yet.
            PlayerProfile? profile = ProfileManager.CurrentProfile;
            if (profile == null) return;
            ReactionSet reactions = profile.Reactions;
            BufferList.Clear();
            List<Reagent> bufferList = ReactionTest.IsAllCatalysts ? _allPigmentList : _allReagentList;
            foreach (Reagent pigment in bufferList)
            {
                if (pigment == ReactionTest.Reagent1) continue;
                if (pigment == ReactionTest.Reagent2) continue;
                if (reactions.Find(pigment, ReactionTest.Reagent1) == null) continue;
                if (reactions.Find(pigment, ReactionTest.Reagent2) == null) continue;
                
                BufferList.Add(pigment);
            }

            // BufferPigmentList.AddRange(_allPigmentList.Where(pigment =>
            //         pigment != ReactionTest.Reagent1 && pigment != ReactionTest.Reagent2));
        }

        public async void Analyze()
        {
            Debug.WriteLine("Analyze");
            try
            {
                await ReactionTest.StartScan();
                await FinalizeTestResults.Execute();
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Scan canceled.");
            }
        }

        public void MarkInert()
        {
            ReactionTest.MarkInert();
        }

        public void CancelScan()
        {
            ReactionTest.CancelScan();
        }

        public async Task OpenScreenSettings()
        {
            await ShowScreenSettingsDialog.Handle(new ScreenSettingsViewModel());
        }

        public async Task ShowClipInfo()
        {
            await ShowInfoBox("Clipped Reactions", "The Pigment Lab is only capable of displaying color channel values in the 0-255 range. However, sometimes, your reactions will push one or more channels outside of that range. When that happens, the value will be clamped either to 0 or 255. In this case, we say that the reaction was \"Clipped.\"\n\nThis is nothing to panic about, though. We solve this issue by using a third (buffer) reagent to offset the extreme value, so that it falls within measurable range, similar to how we test Catalyst+Catalyst reactions. Desert Paint Codex will automatically do the math for this, but it does move clipped reactions towards the end of your test list, because you'll want any reactions between the buffer reagent and your two test reagents already known, prior to running your buffered test.");
        }

        private void Test()
        {
            Debug.WriteLine("Test complete");
        }
        
        public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }

        public ReactiveCommand<Unit, Unit> ClearReaction { get; }
        public ReactiveCommand<Unit, Unit> SaveReaction { get; }
        public ReactiveCommand<Unit, Unit> FinalizeTestResults { get; }
    }
}