Files
@ 6a6817b17a06
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/SimulatorViewModel.cs
6a6817b17a06
9.4 KiB
text/x-csharp
Simulator view updates: new warning when the recipe is below minimum concentration. Add the missing reactions to the warning about missing reactions. Show the current saved recipe for a color, and allow replacing/saving the current simulated recipe as the recipe for that color.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reactive.Linq;
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 RecipeLibraryViewModel? _recipeLibraryVM;
private ViewModelBase? _recipeLibraryActivity;
public ViewModelBase? RecipeLibraryActivity { get => _recipeLibraryActivity; private set => this.RaiseAndSetIfChanged(ref _recipeLibraryActivity, value); }
private PaintColor? _paintColor;
public PaintColor? PaintColor
{
get => _paintColor;
set => this.RaiseAndSetIfChanged(ref _paintColor, value);
}
// Stealing the recipe view from the paint generator
private GeneratorRecipe? _existingRecipe;
public GeneratorRecipe? ExistingRecipe { get => _existingRecipe; private set => this.RaiseAndSetIfChanged(ref _existingRecipe, 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);
}
private bool _isValidConcentration;
public bool IsValidConcentration
{
get => _isValidConcentration;
set => this.RaiseAndSetIfChanged(ref _isValidConcentration, value);
}
private string _missingReactionList = string.Empty;
public string MissingReactionList
{
get => _missingReactionList;
set => this.RaiseAndSetIfChanged(ref _missingReactionList, value);
}
public ObservableCollection<Reagent> Reagents { get; } = new();
public ObservableCollection<Reagent> ActiveReagents { get; } = new();
public ObservableCollection<RecipeItem> RecipeItems { get; } = new();
private readonly PaintRecipe _currentRecipe = new();
private PaintColor _tempColor = new PaintColor(0, 0,0);
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 ShowRecipeLibrary()
{
_recipeLibraryVM = new RecipeLibraryViewModel();
Observable.Merge(_recipeLibraryVM.Ok, _recipeLibraryVM.Cancel).Subscribe(_ => LoadRecipe());
RecipeLibraryActivity = _recipeLibraryVM;
}
// Show the list of recipes and allow the user to pick one to load
public void LoadRecipe()
{
// TODO: show the list of recipes
}
public void SelectRecipeToLoad(PaintRecipe recipe)
{
// 1: Clear the current recipe
_currentRecipe.Clear();
RecipeItems.Clear();
// 2: Set the recipe items from the loaded recipe
foreach (PaintRecipe.ReagentQuantity reagentQuantity in recipe.Reagents)
{
Reagent reagent = ReagentService.GetReagent(reagentQuantity.Name);
if (reagent != null)
{
RecipeItems.Add(new RecipeItem(reagent, reagentQuantity.Quantity));
}
}
UpdateRecipe();
}
// Replace the recipe for a color with the current recipe
public void ReplaceRecipe()
{
if (IsGoodRecipe && IsValidConcentration)
{
if (ProfileManager.CurrentProfile == null) return;
string colorName = PaletteService.FindNearest(_currentRecipe.ReactedColor);
_tempColor.Set(colorName, _currentRecipe.ReactedColor);
ProfileManager.CurrentProfile.Recipes[colorName].CopyFrom(_currentRecipe);
UpdateExistingRecipe();
}
}
private void UpdateExistingRecipe()
{
if (IsGoodRecipe)
{
string colorName = PaletteService.FindNearest(_currentRecipe.ReactedColor);
if ((ProfileManager.CurrentProfile != null) && (ProfileManager.CurrentProfile.Recipes.TryGetValue(colorName, out PaintRecipe? paintRecipe)))
{
System.Diagnostics.Debug.WriteLine($"Setting existing recipe for {colorName}.");
GeneratorRecipe? existingRecipe = ExistingRecipe;
if (existingRecipe == null)
{
_tempColor.Set(colorName, _currentRecipe.ReactedColor);
existingRecipe = new GeneratorRecipe(_tempColor);
}
else
{
existingRecipe.Color.Set(colorName, _currentRecipe.ReactedColor);
}
existingRecipe.DraftRecipe(paintRecipe);
ExistingRecipe = null;
ExistingRecipe = existingRecipe;
}
else
{
ExistingRecipe = null;
}
}
else
{
ExistingRecipe = null;
}
}
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();
if (HasMissingReactions)
{
List<(string, string)> missingReactions = _currentRecipe.MissingReactionPairs();
List<string> missingReactionsStrings = new List<string>(missingReactions.Count);
foreach ((string reagent1, string reagent2) in missingReactions)
{
missingReactionsStrings.Add($"{reagent1}+{reagent2}");
}
MissingReactionList = string.Join(", ", missingReactionsStrings);
}
IsGoodRecipe = !HasMissingReactions && _currentRecipe.IsValidForConcentration(10);
IsValidConcentration = _currentRecipe.IsValidForConcentration(10);
UpdateExistingRecipe();
}
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);
}
}
}
}
|