Files
@ a5faa82faf6a
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/SimulatorViewModel.cs
a5faa82faf6a
4.7 KiB
text/x-csharp
Import and Export should be working correctly. Can now import Practical Paint
reactions.txt files. Fixed a crash taht occurred when switching profiles.
reactions.txt files. Fixed a crash taht occurred when switching profiles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Input.Platform;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
namespace DesertPaintCodex.ViewModels
{
public class SimulatorViewModel : ViewModelBase
{
private PaintColor? _paintColor;
public PaintColor? PaintColor
{
get => _paintColor;
set => this.RaiseAndSetIfChanged(ref _paintColor, value);
}
private bool _hasMissingReactions;
public bool HasMissingReactions
{
get => _hasMissingReactions;
set => this.RaiseAndSetIfChanged(ref _hasMissingReactions, value);
}
private bool _isGoodRecipe;
public bool IsGoodRecipe
{
get => _isGoodRecipe;
set => this.RaiseAndSetIfChanged(ref _isGoodRecipe, value);
}
public ObservableCollection<Reagent> Reagents { get; } = new();
public ObservableCollection<Reagent> ActiveReagents { get; } = new();
public ObservableCollection<RecipeItem> RecipeItems { get; } = new();
private readonly PaintRecipe _currentRecipe = new();
public SimulatorViewModel()
{
List<string> reagentNames = ReagentService.Names;
for (int i = 0; i < reagentNames.Count; i++)
{
Reagents.Add(ReagentService.GetReagent(reagentNames[i]));
}
ActiveReagents.CollectionChanged += OnActiveReagentsChanged;
RecipeItems
.ToObservableChangeSet()
.AutoRefresh(item => item.Quantity)
.Subscribe(_ => Refresh());
}
public async void CopyToClipboard()
{
IClipboard clipboard = Application.Current.Clipboard;
await clipboard.SetTextAsync(_currentRecipe.ToString());
}
public void MoveItemUp(RecipeItem item)
{
int pos = RecipeItems.IndexOf(item);
if (pos <= 0) return;
RecipeItems.RemoveAt(pos);
RecipeItems.Insert(pos - 1, item);
Refresh();
}
public void MoveItemDown(RecipeItem item)
{
int pos = RecipeItems.IndexOf(item);
if ((pos < 0) || (pos >= RecipeItems.Count - 1)) return;
RecipeItems.RemoveAt(pos);
RecipeItems.Insert(pos + 1, item);
Refresh();
}
private void UpdateRecipe()
{
_currentRecipe.Clear();
foreach (RecipeItem entry in RecipeItems)
{
if (!entry.Unused)
{
_currentRecipe.AddReagent(entry.Reagent.Name, entry.Quantity);
}
}
PaintColor = null; // TODO: Find a better way to kick the paint swatch when reassigning color from the same ref.
PaintColor = _currentRecipe.ReactedColor;
HasMissingReactions = _currentRecipe.HasMissingReactions();
IsGoodRecipe = !HasMissingReactions && _currentRecipe.IsValidForConcentration(10);
}
private void OnActiveReagentsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
HashSet<Reagent> unmatchedReagents = new(ActiveReagents);
for (int i = RecipeItems.Count - 1; i >= 0; i--)
{
bool found = false;
foreach (Reagent reagent in ActiveReagents)
{
if (reagent == RecipeItems[i].Reagent)
{
unmatchedReagents.Remove(reagent);
found = true;
break;
}
}
if (!found) RecipeItems.RemoveAt(i);
}
foreach (Reagent reagent in unmatchedReagents)
{
RecipeItems.Add(new RecipeItem(reagent, 1));
}
Refresh();
}
private void Refresh()
{
UpdateFlags();
UpdateRecipe();
}
private void UpdateFlags()
{
int activeIngredients = 0;
for (int i = 0; i < RecipeItems.Count; i++)
{
RecipeItem item = RecipeItems[i];
item.First = i == 0;
item.Last = i == RecipeItems.Count - 1;
if (item.Quantity > 0)
{
activeIngredients++;
}
item.Unused = ((activeIngredients > 5) && item.Reagent.IsCatalyst) || (item.Quantity == 0);
}
}
}
}
|