BaseDataWindow: Implement branches, attributes and cultivations
This commit is contained in:
170
Elwig/Windows/BaseDataWindow.xaml.Mod.cs
Normal file
170
Elwig/Windows/BaseDataWindow.xaml.Mod.cs
Normal file
@ -0,0 +1,170 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public partial class BaseDataWindow {
|
||||
|
||||
private Dictionary<string, string?>? _mods = null;
|
||||
private Dictionary<Modifier, string>? _modIds = null;
|
||||
private ObservableCollection<Modifier>? _modList = null;
|
||||
private bool _modChanged = false;
|
||||
private bool _modUpdate = false;
|
||||
|
||||
private void ModifiersInitEditing() {
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
_modList = new(Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList());
|
||||
_mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId);
|
||||
_modIds = _modList.ToDictionary(m => m, m => m.ModId);
|
||||
ControlUtils.RenewItemsSource(SeasonModifierList, _modList, m => (m as Modifier)?.ModId);
|
||||
}
|
||||
|
||||
private void ModifiersFinishEditing() {
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
ControlUtils.RenewItemsSource(SeasonModifierList, Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList(), m => (m as Modifier)?.ModId);
|
||||
_modList = null;
|
||||
_mods = null;
|
||||
_modIds = null;
|
||||
_modChanged = false;
|
||||
|
||||
SeasonModifierUpButton.IsEnabled = false;
|
||||
SeasonModifierDownButton.IsEnabled = false;
|
||||
SeasonModifierAddButton.IsEnabled = false;
|
||||
SeasonModifierDeleteButton.IsEnabled = false;
|
||||
}
|
||||
|
||||
private async Task ModifiersSave() {
|
||||
if (!_modChanged || _modList == null || _mods == null || _modIds == null)
|
||||
return;
|
||||
|
||||
int i = 0;
|
||||
foreach (var mod in _modList) mod.Ordering = ++i;
|
||||
|
||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||
foreach (var (modid, _) in _mods.Where(m => m.Value == null)) {
|
||||
Context.Remove(Context.Modifiers.Find(new object?[] { year, modid }));
|
||||
}
|
||||
foreach (var (mod, old) in _modIds) {
|
||||
mod.ModId = old;
|
||||
}
|
||||
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
|
||||
Context.Update(Context.Modifiers.Find(new object?[] { year, old }));
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
|
||||
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
|
||||
await Context.Database.ExecuteSqlAsync($"UPDATE modifier SET modid = {modid} WHERE (year, modid) = ({year}, {old})");
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
|
||||
foreach (var mod in _modList.Where(m => !_modIds.ContainsKey(m))) {
|
||||
if (mod.ModId == null) continue;
|
||||
await Context.AddAsync(mod);
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private void SeasonModifierUpButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (_modList == null) return;
|
||||
_modChanged = true;
|
||||
var idx = SeasonModifierList.SelectedIndex;
|
||||
var item = _modList[idx];
|
||||
_modList.RemoveAt(idx);
|
||||
idx--;
|
||||
_modList.Insert(idx, item);
|
||||
SeasonModifierList.SelectedIndex = idx;
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void SeasonModifierDownButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (_modList == null) return;
|
||||
_modChanged = true;
|
||||
var idx = SeasonModifierList.SelectedIndex;
|
||||
var item = _modList[idx];
|
||||
_modList.RemoveAt(idx);
|
||||
idx++;
|
||||
_modList.Insert(idx, item);
|
||||
SeasonModifierList.SelectedIndex = idx;
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void SeasonModifierAddButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (_modList == null || SeasonList.SelectedItem is not Season s) return;
|
||||
_modChanged = true;
|
||||
var idx = (SeasonModifierList.SelectedIndex != -1) ? SeasonModifierList.SelectedIndex + 1 : _modList.Count;
|
||||
var item = Context.CreateProxy<Modifier>();
|
||||
item.Year = s.Year;
|
||||
_modList.Insert(idx, item);
|
||||
SeasonModifierList.SelectedIndex = idx;
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void SeasonModifierDeleteButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (_modList == null || _mods == null) return;
|
||||
_modChanged = true;
|
||||
var idx = SeasonModifierList.SelectedIndex;
|
||||
var item = _modList[idx];
|
||||
_mods[item.ModId] = null;
|
||||
_modList.RemoveAt(idx);
|
||||
SeasonModifierList.SelectedIndex = idx < _modList.Count ? idx : idx - 1;
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void SeasonModifierList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
UpdateButtons();
|
||||
_modUpdate = true;
|
||||
if (SeasonModifierList.SelectedItem is not Modifier mod) {
|
||||
SeasonModifierIdInput.Text = "";
|
||||
SeasonModifierNameInput.Text = "";
|
||||
SeasonModifierRelInput.Text = "";
|
||||
SeasonModifierAbsInput.Text = "";
|
||||
} else {
|
||||
SeasonModifierIdInput.Text = mod.ModId;
|
||||
SeasonModifierNameInput.Text = mod.Name;
|
||||
SeasonModifierRelInput.Text = (mod.Rel * 100)?.ToString();
|
||||
SeasonModifierAbsInput.Text = mod.Abs?.ToString();
|
||||
}
|
||||
_modUpdate = false;
|
||||
}
|
||||
|
||||
private void SeasonModifier_Changed(object? sender, RoutedEventArgs evt) {
|
||||
if (_modUpdate || (!IsEditing && !IsCreating) || SeasonModifierList.SelectedItem is not Modifier mod || SeasonList.SelectedItem is not Season s || _mods == null || _modIds == null) return;
|
||||
_modChanged = _modChanged ||
|
||||
SeasonModifierIdInput.Text != mod.ModId;
|
||||
|
||||
var old = _modIds.GetValueOrDefault(mod);
|
||||
var id = SeasonModifierIdInput.Text ?? "";
|
||||
if (old != null) _mods[old] = id;
|
||||
mod.ModId = id;
|
||||
mod.Rel = decimal.TryParse(SeasonModifierRelInput.Text, out var vRel) ? vRel / 100 : null;
|
||||
if (mod.Rel != null) SeasonModifierAbsInput.Text = "";
|
||||
mod.AbsValue = decimal.TryParse(SeasonModifierAbsInput.Text, out var vAbs) ? Utils.DecToDb(vAbs, s.Precision) : null;
|
||||
if (mod.AbsValue != null) SeasonModifierRelInput.Text = "";
|
||||
|
||||
CollectionViewSource.GetDefaultView(_modList).Refresh();
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void SeasonModifierIdInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
||||
UpperCaseInput_TextChanged(sender, evt);
|
||||
SeasonModifier_Changed(sender, evt);
|
||||
}
|
||||
|
||||
private void SeasonModifierRelInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
||||
// DecimalInput_TextChanged(sender, evt); FIXME '-' is ignored
|
||||
SeasonModifier_Changed(sender, evt);
|
||||
}
|
||||
|
||||
private void SeasonModifierAbsInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
||||
// DecimalInput_TextChanged(sender, evt); FIXME '-' is ignored
|
||||
SeasonModifier_Changed(sender, evt);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user