rft: WpfGui重构 拆分一键长草-基建任务任务 (#11219)

* rft: WpfGui重构 拆分`一键长草-基建任务`

* perf: 简化
This commit is contained in:
status102
2024-11-22 21:48:21 +08:00
committed by GitHub
parent df464beff1
commit 05e68d2bae
7 changed files with 556 additions and 519 deletions

View File

@@ -607,7 +607,7 @@ namespace MaaWpfGui.Services.RemoteControl
{
// 虽然更改时已经保存过了,不过保险起见还是在点击开始之后再保存一次(任务及基建列表)
Instances.TaskQueueViewModel.TaskItemSelectionChanged();
Instances.SettingsViewModel.InfrastOrderSelectionChanged();
SettingsViewModel.InfrastTask.InfrastOrderSelectionChanged();
InvokeInstanceMethod(Instances.TaskQueueViewModel, "ClearLog");

View File

@@ -16,9 +16,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
@@ -35,9 +33,7 @@ using MaaWpfGui.States;
using MaaWpfGui.Utilities.ValueType;
using MaaWpfGui.ViewModels.UserControl.Settings;
using MaaWpfGui.ViewModels.UserControl.TaskQueue;
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Stylet;
using ComboBox = System.Windows.Controls.ComboBox;
@@ -79,6 +75,11 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public static MallSettingsUserControlModel MallTask { get; } = new();
/// <summary>
/// Gets 基建任务Model
/// </summary>
public static InfrastSettingsUserControlModel InfrastTask { get; } = new();
/// <summary>
/// Gets 肉鸽任务Model
/// </summary>
@@ -189,7 +190,7 @@ namespace MaaWpfGui.ViewModels.UI
private void Init()
{
InitInfrast();
InfrastTask.InitInfrast();
RoguelikeTask.InitRoguelike();
InitConfiguration();
InitUiSettings();
@@ -197,61 +198,6 @@ namespace MaaWpfGui.ViewModels.UI
InitVersionUpdate();
}
private void InitInfrast()
{
var facilityList = new[]
{
"Mfg",
"Trade",
"Control",
"Power",
"Reception",
"Office",
"Dorm",
"Processing",
"Training",
};
var tempOrderList = new List<DragItemViewModel?>(new DragItemViewModel[facilityList.Length]);
var nonOrderList = new List<DragItemViewModel>();
for (int i = 0; i != facilityList.Length; ++i)
{
var facility = facilityList[i];
bool parsed = int.TryParse(ConfigurationHelper.GetFacilityOrder(facility, "-1"), out int order);
DragItemViewModel vm = new DragItemViewModel(
LocalizationHelper.GetString(facility),
facility,
"Infrast.");
if (!parsed || order < 0 || order >= tempOrderList.Count || tempOrderList[order] != null)
{
nonOrderList.Add(vm);
}
else
{
tempOrderList[order] = vm;
}
}
foreach (var newVm in nonOrderList)
{
int i = 0;
while (i < tempOrderList.Count && tempOrderList[i] != null)
{
++i;
}
tempOrderList[i] = newVm;
ConfigurationHelper.SetFacilityOrder(newVm.OriginalName, i.ToString());
}
InfrastItemViewModels = new ObservableCollection<DragItemViewModel>(tempOrderList!);
InfrastItemViewModels.CollectionChanged += InfrastOrderSelectionChanged;
_dormThresholdLabel = LocalizationHelper.GetString("DormThreshold") + ": " + _dormThreshold + "%";
}
private void InitConfiguration()
{
var configurations = new ObservableCollection<CombinedData>();
@@ -471,425 +417,6 @@ namespace MaaWpfGui.ViewModels.UI
#endregion
#region
/// <summary>
/// Gets or sets the infrast item view models.
/// </summary>
public ObservableCollection<DragItemViewModel> InfrastItemViewModels { get; set; }
/// <summary>
/// Gets the list of uses of drones.
/// </summary>
public List<CombinedData> UsesOfDronesList { get; } =
[
new() { Display = LocalizationHelper.GetString("DronesNotUse"), Value = "_NotUse" },
new() { Display = LocalizationHelper.GetString("Money"), Value = "Money" },
new() { Display = LocalizationHelper.GetString("SyntheticJade"), Value = "SyntheticJade" },
new() { Display = LocalizationHelper.GetString("CombatRecord"), Value = "CombatRecord" },
new() { Display = LocalizationHelper.GetString("PureGold"), Value = "PureGold" },
new() { Display = LocalizationHelper.GetString("OriginStone"), Value = "OriginStone" },
new() { Display = LocalizationHelper.GetString("Chip"), Value = "Chip" },
];
/// <summary>
/// Gets the list of uses of default infrast.
/// </summary>
public List<CombinedData> DefaultInfrastList { get; } =
[
new() { Display = LocalizationHelper.GetString("UserDefined"), Value = UserDefined },
new() { Display = LocalizationHelper.GetString("153Time3"), Value = "153_layout_3_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("153Time4"), Value = "153_layout_4_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("243Time3"), Value = "243_layout_3_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("243Time4"), Value = "243_layout_4_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("333Time3"), Value = "333_layout_for_Orundum_3_times_a_day.json" },
];
private int _dormThreshold = Convert.ToInt32(ConfigurationHelper.GetValue(ConfigurationKeys.DormThreshold, "30"));
/// <summary>
/// Gets or sets the threshold to enter dormitory.
/// </summary>
public int DormThreshold
{
get => _dormThreshold;
set
{
SetAndNotify(ref _dormThreshold, value);
DormThresholdLabel = LocalizationHelper.GetString("DormThreshold") + ": " + _dormThreshold + "%";
ConfigurationHelper.SetValue(ConfigurationKeys.DormThreshold, value.ToString());
}
}
private string _dormThresholdLabel;
/// <summary>
/// Gets or sets the label of dormitory threshold.
/// </summary>
public string DormThresholdLabel
{
get => _dormThresholdLabel;
set => SetAndNotify(ref _dormThresholdLabel, value);
}
/// <summary>
/// Gets infrast order list.
/// </summary>
/// <returns>The infrast order list.</returns>
public List<string> GetInfrastOrderList()
{
return (from item in InfrastItemViewModels where item.IsChecked select item.OriginalName).ToList();
}
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void InfrastItemSelectedAll()
{
foreach (var item in InfrastItemViewModels)
{
item.IsChecked = true;
}
}
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void InfrastItemUnselectedAll()
{
foreach (var item in InfrastItemViewModels)
{
item.IsChecked = false;
}
}
/// <summary>
/// 实时更新基建换班顺序
/// </summary>
/// <param name="sender">ignored object</param>
/// <param name="e">ignored NotifyCollectionChangedEventArgs</param>
public void InfrastOrderSelectionChanged(object? sender = null, NotifyCollectionChangedEventArgs? e = null)
{
_ = (sender, e);
int index = 0;
foreach (var item in InfrastItemViewModels)
{
ConfigurationHelper.SetFacilityOrder(item.OriginalName, index.ToString());
++index;
}
}
public static TaskQueueViewModel CustomInfrastPlanDataContext { get => Instances.TaskQueueViewModel; }
private string _usesOfDrones = ConfigurationHelper.GetValue(ConfigurationKeys.UsesOfDrones, "Money");
/// <summary>
/// Gets or sets the uses of drones.
/// </summary>
public string UsesOfDrones
{
get => _usesOfDrones;
set
{
SetAndNotify(ref _usesOfDrones, value);
ConfigurationHelper.SetValue(ConfigurationKeys.UsesOfDrones, value);
}
}
private bool _continueTraining = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.ContinueTraining, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether to continue training after current training completed.
/// </summary>
public bool ContinueTraining
{
get => _continueTraining;
set
{
SetAndNotify(ref _continueTraining, value);
ConfigurationHelper.SetValue(ConfigurationKeys.ContinueTraining, value.ToString());
}
}
private string _defaultInfrast = ConfigurationHelper.GetValue(ConfigurationKeys.DefaultInfrast, UserDefined);
private const string UserDefined = "user_defined";
/// <summary>
/// Gets or sets the uses of drones.
/// </summary>
public string DefaultInfrast
{
get => _defaultInfrast;
set
{
SetAndNotify(ref _defaultInfrast, value);
if (_defaultInfrast != UserDefined)
{
CustomInfrastFile = @"resource\custom_infrast\" + value;
IsCustomInfrastFileReadOnly = true;
}
else
{
IsCustomInfrastFileReadOnly = false;
}
ConfigurationHelper.SetValue(ConfigurationKeys.DefaultInfrast, value);
}
}
private bool _isCustomInfrastFileReadOnly = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.IsCustomInfrastFileReadOnly, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether CustomInfrastFile is read-only
/// </summary>
public bool IsCustomInfrastFileReadOnly
{
get => _isCustomInfrastFileReadOnly;
set
{
SetAndNotify(ref _isCustomInfrastFileReadOnly, value);
ConfigurationHelper.SetValue(ConfigurationKeys.IsCustomInfrastFileReadOnly, value.ToString());
}
}
private bool _dormFilterNotStationedEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.DormFilterNotStationedEnabled, bool.TrueString));
/// <summary>
/// Gets or sets a value indicating whether the not stationed filter in dorm is enabled.
/// </summary>
public bool DormFilterNotStationedEnabled
{
get => _dormFilterNotStationedEnabled;
set
{
SetAndNotify(ref _dormFilterNotStationedEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.DormFilterNotStationedEnabled, value.ToString());
}
}
private bool _dormTrustEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.DormTrustEnabled, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether trust in dorm is enabled.
/// </summary>
public bool DormTrustEnabled
{
get => _dormTrustEnabled;
set
{
SetAndNotify(ref _dormTrustEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.DormTrustEnabled, value.ToString());
}
}
private bool _originiumShardAutoReplenishment = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.OriginiumShardAutoReplenishment, bool.TrueString));
/// <summary>
/// Gets or sets a value indicating whether Originium shard auto replenishment is enabled.
/// </summary>
public bool OriginiumShardAutoReplenishment
{
get => _originiumShardAutoReplenishment;
set
{
SetAndNotify(ref _originiumShardAutoReplenishment, value);
ConfigurationHelper.SetValue(ConfigurationKeys.OriginiumShardAutoReplenishment, value.ToString());
}
}
private bool _customInfrastEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastEnabled, bool.FalseString));
public bool CustomInfrastEnabled
{
get => _customInfrastEnabled;
set
{
SetAndNotify(ref _customInfrastEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastEnabled, value.ToString());
Instances.TaskQueueViewModel.CustomInfrastEnabled = value;
}
}
/// <summary>
/// Selects infrast config file.
/// </summary>
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void SelectCustomInfrastFile()
{
var dialog = new OpenFileDialog
{
Filter = LocalizationHelper.GetString("CustomInfrastFile") + "|*.json",
};
if (dialog.ShowDialog() == true)
{
CustomInfrastFile = dialog.FileName;
}
DefaultInfrast = UserDefined;
}
private string _customInfrastFile = ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastFile, string.Empty);
public string CustomInfrastFile
{
get => _customInfrastFile;
set
{
SetAndNotify(ref _customInfrastFile, value);
ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastFile, value);
Instances.TaskQueueViewModel.RefreshCustomInfrastPlan();
// SetAndNotify 在值没有变化时不会触发 PropertyChanged 事件,所以这里手动触发一下
Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = false;
{
Instances.TaskQueueViewModel.CustomInfrastPlanIndex--;
Instances.TaskQueueViewModel.CustomInfrastPlanIndex++;
}
Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = true;
}
}
#region
private enum NotifyType
{
None,
SelectedIndex,
ScrollOffset,
}
private NotifyType _notifySource = NotifyType.None;
private Timer _resetNotifyTimer;
private void ResetNotifySource()
{
if (_resetNotifyTimer != null)
{
_resetNotifyTimer.Stop();
_resetNotifyTimer.Close();
}
_resetNotifyTimer = new Timer(20);
_resetNotifyTimer.Elapsed += (_, _) =>
{
_notifySource = NotifyType.None;
};
_resetNotifyTimer.AutoReset = false;
_resetNotifyTimer.Enabled = true;
_resetNotifyTimer.Start();
}
/// <summary>
/// Gets or sets the height of scroll viewport.
/// </summary>
public double ScrollViewportHeight { get; set; }
/// <summary>
/// Gets or sets the extent height of scroll.
/// </summary>
public double ScrollExtentHeight { get; set; }
/// <summary>
/// Gets or sets the list of divider vertical offset.
/// </summary>
public List<double> DividerVerticalOffsetList { get; set; } = [];
private int _selectedIndex;
/// <summary>
/// Gets or sets the index selected.
/// </summary>
public int SelectedIndex
{
get => _selectedIndex;
set
{
switch (_notifySource)
{
case NotifyType.None:
_notifySource = NotifyType.SelectedIndex;
SetAndNotify(ref _selectedIndex, value);
if (DividerVerticalOffsetList?.Count > 0 && value < DividerVerticalOffsetList.Count)
{
ScrollOffset = DividerVerticalOffsetList[value];
}
ResetNotifySource();
break;
case NotifyType.ScrollOffset:
SetAndNotify(ref _selectedIndex, value);
break;
case NotifyType.SelectedIndex:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private double _scrollOffset;
/// <summary>
/// Gets or sets the scroll offset.
/// </summary>
public double ScrollOffset
{
get => _scrollOffset;
set
{
switch (_notifySource)
{
case NotifyType.None:
_notifySource = NotifyType.ScrollOffset;
SetAndNotify(ref _scrollOffset, value);
// 设置 ListBox SelectedIndex 为当前 ScrollOffset 索引
if (DividerVerticalOffsetList?.Count > 0)
{
// 滚动条滚动到底部,返回最后一个 Divider 索引
if (value + ScrollViewportHeight >= ScrollExtentHeight)
{
SelectedIndex = DividerVerticalOffsetList.Count - 1;
ResetNotifySource();
break;
}
// 根据出当前 ScrollOffset 选出最后一个在可视范围的 Divider 索引
var dividerSelect = DividerVerticalOffsetList.Select((n, i) => (
dividerAppeared: value >= n,
index: i));
var index = dividerSelect.LastOrDefault(n => n.dividerAppeared).index;
SelectedIndex = index;
}
ResetNotifySource();
break;
case NotifyType.SelectedIndex:
SetAndNotify(ref _scrollOffset, value);
break;
case NotifyType.ScrollOffset:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
#endregion
#endregion
#region
private bool _receiveAward = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.ReceiveAward, bool.TrueString));
@@ -1702,6 +1229,143 @@ namespace MaaWpfGui.ViewModels.UI
#endregion SettingsGuide
#region
private enum NotifyType
{
None,
SelectedIndex,
ScrollOffset,
}
private NotifyType _notifySource = NotifyType.None;
private Timer _resetNotifyTimer;
private void ResetNotifySource()
{
if (_resetNotifyTimer != null)
{
_resetNotifyTimer.Stop();
_resetNotifyTimer.Close();
}
_resetNotifyTimer = new Timer(20);
_resetNotifyTimer.Elapsed += (_, _) =>
{
_notifySource = NotifyType.None;
};
_resetNotifyTimer.AutoReset = false;
_resetNotifyTimer.Enabled = true;
_resetNotifyTimer.Start();
}
/// <summary>
/// Gets or sets the height of scroll viewport.
/// </summary>
public double ScrollViewportHeight { get; set; }
/// <summary>
/// Gets or sets the extent height of scroll.
/// </summary>
public double ScrollExtentHeight { get; set; }
/// <summary>
/// Gets or sets the list of divider vertical offset.
/// </summary>
public List<double> DividerVerticalOffsetList { get; set; } = new();
private int _selectedIndex;
/// <summary>
/// Gets or sets the index selected.
/// </summary>
public int SelectedIndex
{
get => _selectedIndex;
set
{
switch (_notifySource)
{
case NotifyType.None:
_notifySource = NotifyType.SelectedIndex;
SetAndNotify(ref _selectedIndex, value);
if (DividerVerticalOffsetList?.Count > 0 && value < DividerVerticalOffsetList.Count)
{
ScrollOffset = DividerVerticalOffsetList[value];
}
ResetNotifySource();
break;
case NotifyType.ScrollOffset:
SetAndNotify(ref _selectedIndex, value);
break;
case NotifyType.SelectedIndex:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private double _scrollOffset;
/// <summary>
/// Gets or sets the scroll offset.
/// </summary>
public double ScrollOffset
{
get => _scrollOffset;
set
{
switch (_notifySource)
{
case NotifyType.None:
_notifySource = NotifyType.ScrollOffset;
SetAndNotify(ref _scrollOffset, value);
// 设置 ListBox SelectedIndex 为当前 ScrollOffset 索引
if (DividerVerticalOffsetList?.Count > 0)
{
// 滚动条滚动到底部,返回最后一个 Divider 索引
if (value + ScrollViewportHeight >= ScrollExtentHeight)
{
SelectedIndex = DividerVerticalOffsetList.Count - 1;
ResetNotifySource();
break;
}
// 根据出当前 ScrollOffset 选出最后一个在可视范围的 Divider 索引
var dividerSelect = DividerVerticalOffsetList.Select((n, i) => (
dividerAppeared: value >= n,
index: i));
var index = dividerSelect.LastOrDefault(n => n.dividerAppeared).index;
SelectedIndex = index;
}
ResetNotifySource();
break;
case NotifyType.SelectedIndex:
SetAndNotify(ref _scrollOffset, value);
break;
case NotifyType.ScrollOffset:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
#endregion
/// <summary>
/// Requires the user to restart to apply settings.
/// </summary>

View File

@@ -1210,7 +1210,7 @@ namespace MaaWpfGui.ViewModels.UI
// 虽然更改时已经保存过了,不过保险起见在点击开始之后再次保存任务和基建列表
TaskItemSelectionChanged();
Instances.SettingsViewModel.InfrastOrderSelectionChanged();
SettingsViewModel.InfrastTask.InfrastOrderSelectionChanged();
InfrastTaskRunning = true;
@@ -1429,7 +1429,7 @@ namespace MaaWpfGui.ViewModels.UI
// 虽然更改时已经保存过了,不过保险起见在点击开始之后再次保存任务和基建列表
TaskItemSelectionChanged();
Instances.SettingsViewModel.InfrastOrderSelectionChanged();
SettingsViewModel.InfrastTask.InfrastOrderSelectionChanged();
ClearLog();
@@ -1629,39 +1629,39 @@ namespace MaaWpfGui.ViewModels.UI
private void SetInfrastParams()
{
var order = Instances.SettingsViewModel.GetInfrastOrderList();
var order = SettingsViewModel.InfrastTask.GetInfrastOrderList();
Instances.AsstProxy.AsstSetInfrastTaskParams(
order,
Instances.SettingsViewModel.UsesOfDrones,
Instances.SettingsViewModel.ContinueTraining,
Instances.SettingsViewModel.DormThreshold / 100.0,
Instances.SettingsViewModel.DormFilterNotStationedEnabled,
Instances.SettingsViewModel.DormTrustEnabled,
Instances.SettingsViewModel.OriginiumShardAutoReplenishment,
Instances.SettingsViewModel.CustomInfrastEnabled,
Instances.SettingsViewModel.CustomInfrastFile,
SettingsViewModel.InfrastTask.UsesOfDrones,
SettingsViewModel.InfrastTask.ContinueTraining,
SettingsViewModel.InfrastTask.DormThreshold / 100.0,
SettingsViewModel.InfrastTask.DormFilterNotStationedEnabled,
SettingsViewModel.InfrastTask.DormTrustEnabled,
SettingsViewModel.InfrastTask.OriginiumShardAutoReplenishment,
SettingsViewModel.InfrastTask.CustomInfrastEnabled,
SettingsViewModel.InfrastTask.CustomInfrastFile,
CustomInfrastPlanIndex);
}
private bool AppendInfrast()
{
if (Instances.SettingsViewModel.CustomInfrastEnabled && (!File.Exists(Instances.SettingsViewModel.CustomInfrastFile) || CustomInfrastPlanInfoList.Count == 0))
if (SettingsViewModel.InfrastTask.CustomInfrastEnabled && (!File.Exists(SettingsViewModel.InfrastTask.CustomInfrastFile) || CustomInfrastPlanInfoList.Count == 0))
{
AddLog(LocalizationHelper.GetString("CustomizeInfrastSelectionEmpty"), UiLogColor.Error);
return false;
}
var order = Instances.SettingsViewModel.GetInfrastOrderList();
var order = SettingsViewModel.InfrastTask.GetInfrastOrderList();
return Instances.AsstProxy.AsstAppendInfrast(
order,
Instances.SettingsViewModel.UsesOfDrones,
Instances.SettingsViewModel.ContinueTraining,
Instances.SettingsViewModel.DormThreshold / 100.0,
Instances.SettingsViewModel.DormFilterNotStationedEnabled,
Instances.SettingsViewModel.DormTrustEnabled,
Instances.SettingsViewModel.OriginiumShardAutoReplenishment,
Instances.SettingsViewModel.CustomInfrastEnabled,
Instances.SettingsViewModel.CustomInfrastFile,
SettingsViewModel.InfrastTask.UsesOfDrones,
SettingsViewModel.InfrastTask.ContinueTraining,
SettingsViewModel.InfrastTask.DormThreshold / 100.0,
SettingsViewModel.InfrastTask.DormFilterNotStationedEnabled,
SettingsViewModel.InfrastTask.DormTrustEnabled,
SettingsViewModel.InfrastTask.OriginiumShardAutoReplenishment,
SettingsViewModel.InfrastTask.CustomInfrastEnabled,
SettingsViewModel.InfrastTask.CustomInfrastFile,
CustomInfrastPlanIndex);
}
@@ -2839,14 +2839,14 @@ namespace MaaWpfGui.ViewModels.UI
return;
}
if (!File.Exists(Instances.SettingsViewModel.CustomInfrastFile))
if (!File.Exists(SettingsViewModel.InfrastTask.CustomInfrastFile))
{
return;
}
try
{
string jsonStr = File.ReadAllText(Instances.SettingsViewModel.CustomInfrastFile);
string jsonStr = File.ReadAllText(SettingsViewModel.InfrastTask.CustomInfrastFile);
var root = (JObject)JsonConvert.DeserializeObject(jsonStr);
if (root != null && _customInfrastInfoOutput && root.TryGetValue("title", out var title))

View File

@@ -0,0 +1,372 @@
// <copyright file="InfrastSettingsUserControlModel.cs" company="MaaAssistantArknights">
// MaaWpfGui - A part of the MaaCoreArknights project
// Copyright (C) 2021 MistEO and Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License v3.0 only as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using MaaWpfGui.Models;
using MaaWpfGui.Utilities.ValueType;
using MaaWpfGui.ViewModels.UI;
using Microsoft.Win32;
using Stylet;
namespace MaaWpfGui.ViewModels.UserControl.TaskQueue;
/// <summary>
/// 基建任务
/// </summary>
public class InfrastSettingsUserControlModel : PropertyChangedBase
{
/// <summary>
/// Gets the visibility of task setting views.
/// </summary>
public static TaskSettingVisibilityInfo TaskSettingVisibilities => TaskSettingVisibilityInfo.Current;
public void InitInfrast()
{
var facilityList = new[]
{
"Mfg",
"Trade",
"Control",
"Power",
"Reception",
"Office",
"Dorm",
"Processing",
"Training",
};
var tempOrderList = new List<DragItemViewModel?>(new DragItemViewModel[facilityList.Length]);
var nonOrderList = new List<DragItemViewModel>();
for (int i = 0; i != facilityList.Length; ++i)
{
var facility = facilityList[i];
bool parsed = int.TryParse(ConfigurationHelper.GetFacilityOrder(facility, "-1"), out int order);
DragItemViewModel vm = new DragItemViewModel(
LocalizationHelper.GetString(facility),
facility,
"Infrast.");
if (!parsed || order < 0 || order >= tempOrderList.Count || tempOrderList[order] != null)
{
nonOrderList.Add(vm);
}
else
{
tempOrderList[order] = vm;
}
}
foreach (var newVm in nonOrderList)
{
int i = 0;
while (i < tempOrderList.Count && tempOrderList[i] != null)
{
++i;
}
tempOrderList[i] = newVm;
ConfigurationHelper.SetFacilityOrder(newVm.OriginalName, i.ToString());
}
InfrastItemViewModels = new ObservableCollection<DragItemViewModel>(tempOrderList!);
InfrastItemViewModels.CollectionChanged += InfrastOrderSelectionChanged;
_dormThresholdLabel = LocalizationHelper.GetString("DormThreshold") + ": " + _dormThreshold + "%";
}
/// <summary>
/// Gets or sets the infrast item view models.
/// </summary>
public ObservableCollection<DragItemViewModel> InfrastItemViewModels { get; set; } = [];
/// <summary>
/// Gets the list of uses of drones.
/// </summary>
public List<CombinedData> UsesOfDronesList { get; } =
[
new() { Display = LocalizationHelper.GetString("DronesNotUse"), Value = "_NotUse" },
new() { Display = LocalizationHelper.GetString("Money"), Value = "Money" },
new() { Display = LocalizationHelper.GetString("SyntheticJade"), Value = "SyntheticJade" },
new() { Display = LocalizationHelper.GetString("CombatRecord"), Value = "CombatRecord" },
new() { Display = LocalizationHelper.GetString("PureGold"), Value = "PureGold" },
new() { Display = LocalizationHelper.GetString("OriginStone"), Value = "OriginStone" },
new() { Display = LocalizationHelper.GetString("Chip"), Value = "Chip" },
];
/// <summary>
/// Gets the list of uses of default infrast.
/// </summary>
public List<CombinedData> DefaultInfrastList { get; } =
[
new() { Display = LocalizationHelper.GetString("UserDefined"), Value = UserDefined },
new() { Display = LocalizationHelper.GetString("153Time3"), Value = "153_layout_3_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("153Time4"), Value = "153_layout_4_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("243Time3"), Value = "243_layout_3_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("243Time4"), Value = "243_layout_4_times_a_day.json" },
new() { Display = LocalizationHelper.GetString("333Time3"), Value = "333_layout_for_Orundum_3_times_a_day.json" },
];
private int _dormThreshold = Convert.ToInt32(ConfigurationHelper.GetValue(ConfigurationKeys.DormThreshold, "30"));
/// <summary>
/// Gets or sets the threshold to enter dormitory.
/// </summary>
public int DormThreshold
{
get => _dormThreshold;
set
{
SetAndNotify(ref _dormThreshold, value);
DormThresholdLabel = LocalizationHelper.GetString("DormThreshold") + ": " + _dormThreshold + "%";
ConfigurationHelper.SetValue(ConfigurationKeys.DormThreshold, value.ToString());
}
}
private string _dormThresholdLabel;
/// <summary>
/// Gets or sets the label of dormitory threshold.
/// </summary>
public string DormThresholdLabel
{
get => _dormThresholdLabel;
set => SetAndNotify(ref _dormThresholdLabel, value);
}
/// <summary>
/// Gets infrast order list.
/// </summary>
/// <returns>The infrast order list.</returns>
public List<string> GetInfrastOrderList()
{
return (from item in InfrastItemViewModels where item.IsChecked select item.OriginalName).ToList();
}
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void InfrastItemSelectedAll()
{
foreach (var item in InfrastItemViewModels)
{
item.IsChecked = true;
}
}
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void InfrastItemUnselectedAll()
{
foreach (var item in InfrastItemViewModels)
{
item.IsChecked = false;
}
}
/// <summary>
/// 实时更新基建换班顺序
/// </summary>
/// <param name="sender">ignored object</param>
/// <param name="e">ignored NotifyCollectionChangedEventArgs</param>
public void InfrastOrderSelectionChanged(object? sender = null, NotifyCollectionChangedEventArgs? e = null)
{
_ = (sender, e);
int index = 0;
foreach (var item in InfrastItemViewModels)
{
ConfigurationHelper.SetFacilityOrder(item.OriginalName, index.ToString());
++index;
}
}
public static TaskQueueViewModel CustomInfrastPlanDataContext { get => Instances.TaskQueueViewModel; }
private string _usesOfDrones = ConfigurationHelper.GetValue(ConfigurationKeys.UsesOfDrones, "Money");
/// <summary>
/// Gets or sets the uses of drones.
/// </summary>
public string UsesOfDrones
{
get => _usesOfDrones;
set
{
SetAndNotify(ref _usesOfDrones, value);
ConfigurationHelper.SetValue(ConfigurationKeys.UsesOfDrones, value);
}
}
private bool _continueTraining = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.ContinueTraining, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether to continue training after current training completed.
/// </summary>
public bool ContinueTraining
{
get => _continueTraining;
set
{
SetAndNotify(ref _continueTraining, value);
ConfigurationHelper.SetValue(ConfigurationKeys.ContinueTraining, value.ToString());
}
}
private string _defaultInfrast = ConfigurationHelper.GetValue(ConfigurationKeys.DefaultInfrast, UserDefined);
private const string UserDefined = "user_defined";
/// <summary>
/// Gets or sets the uses of drones.
/// </summary>
public string DefaultInfrast
{
get => _defaultInfrast;
set
{
SetAndNotify(ref _defaultInfrast, value);
if (_defaultInfrast != UserDefined)
{
CustomInfrastFile = @"resource\custom_infrast\" + value;
IsCustomInfrastFileReadOnly = true;
}
else
{
IsCustomInfrastFileReadOnly = false;
}
ConfigurationHelper.SetValue(ConfigurationKeys.DefaultInfrast, value);
}
}
private bool _isCustomInfrastFileReadOnly = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.IsCustomInfrastFileReadOnly, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether CustomInfrastFile is read-only
/// </summary>
public bool IsCustomInfrastFileReadOnly
{
get => _isCustomInfrastFileReadOnly;
set
{
SetAndNotify(ref _isCustomInfrastFileReadOnly, value);
ConfigurationHelper.SetValue(ConfigurationKeys.IsCustomInfrastFileReadOnly, value.ToString());
}
}
private bool _dormFilterNotStationedEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.DormFilterNotStationedEnabled, bool.TrueString));
/// <summary>
/// Gets or sets a value indicating whether the not stationed filter in dorm is enabled.
/// </summary>
public bool DormFilterNotStationedEnabled
{
get => _dormFilterNotStationedEnabled;
set
{
SetAndNotify(ref _dormFilterNotStationedEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.DormFilterNotStationedEnabled, value.ToString());
}
}
private bool _dormTrustEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.DormTrustEnabled, bool.FalseString));
/// <summary>
/// Gets or sets a value indicating whether trust in dorm is enabled.
/// </summary>
public bool DormTrustEnabled
{
get => _dormTrustEnabled;
set
{
SetAndNotify(ref _dormTrustEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.DormTrustEnabled, value.ToString());
}
}
private bool _originiumShardAutoReplenishment = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.OriginiumShardAutoReplenishment, bool.TrueString));
/// <summary>
/// Gets or sets a value indicating whether Originium shard auto replenishment is enabled.
/// </summary>
public bool OriginiumShardAutoReplenishment
{
get => _originiumShardAutoReplenishment;
set
{
SetAndNotify(ref _originiumShardAutoReplenishment, value);
ConfigurationHelper.SetValue(ConfigurationKeys.OriginiumShardAutoReplenishment, value.ToString());
}
}
private bool _customInfrastEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastEnabled, bool.FalseString));
public bool CustomInfrastEnabled
{
get => _customInfrastEnabled;
set
{
SetAndNotify(ref _customInfrastEnabled, value);
ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastEnabled, value.ToString());
Instances.TaskQueueViewModel.CustomInfrastEnabled = value;
}
}
/// <summary>
/// Selects infrast config file.
/// </summary>
// UI 绑定的方法
// ReSharper disable once UnusedMember.Global
public void SelectCustomInfrastFile()
{
var dialog = new OpenFileDialog
{
Filter = LocalizationHelper.GetString("CustomInfrastFile") + "|*.json",
};
if (dialog.ShowDialog() == true)
{
CustomInfrastFile = dialog.FileName;
}
DefaultInfrast = UserDefined;
}
private string _customInfrastFile = ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastFile, string.Empty);
public string CustomInfrastFile
{
get => _customInfrastFile;
set
{
SetAndNotify(ref _customInfrastFile, value);
ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastFile, value);
Instances.TaskQueueViewModel.RefreshCustomInfrastPlan();
// SetAndNotify 在值没有变化时不会触发 PropertyChanged 事件,所以这里手动触发一下
Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = false;
{
Instances.TaskQueueViewModel.CustomInfrastPlanIndex--;
Instances.TaskQueueViewModel.CustomInfrastPlanIndex++;
}
Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = true;
}
}
}

View File

@@ -251,11 +251,12 @@
DataContext="{Binding TaskSettingDataContext}"
IsEnabled="{Binding Idle}"
Visibility="{c:Binding TaskSettingVisibilities.WakeUp}" />
<userControl:InfrastSettingsUserControl
s:View.ActionTarget="{Binding DataContext, RelativeSource={RelativeSource Self}}"
DataContext="{Binding TaskSettingDataContext}"
IsEnabled="{Binding Idle}"
Visibility="{c:Binding TaskSettingVisibilities.Base}" />
<taskQueue:InfrastSettingsUserControl
DataContext="{Binding TaskSettingDataContext.InfrastTask}"
IsEnabled="{Binding DataContext.Idle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
Visibility="{c:Binding DataContext.TaskSettingVisibilities.Base,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}}}" />
<taskQueue:MallSettingsUserControl
DataContext="{Binding TaskSettingDataContext.MallTask}"
IsEnabled="{Binding DataContext.Idle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"

View File

@@ -1,5 +1,5 @@
<UserControl
x:Class="MaaWpfGui.Views.UserControl.InfrastSettingsUserControl"
x:Class="MaaWpfGui.Views.UserControl.TaskQueue.InfrastSettingsUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:CalcBinding;assembly=CalcBinding"
@@ -9,19 +9,19 @@
xmlns:dd="urn:gong-wpf-dragdrop"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:helper="clr-namespace:MaaWpfGui.Helper"
xmlns:local="clr-namespace:MaaWpfGui"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:styles="clr-namespace:MaaWpfGui.Styles"
xmlns:setting="clr-namespace:MaaWpfGui.ViewModels.UI"
xmlns:taskQueue="clr-namespace:MaaWpfGui.ViewModels.UserControl.TaskQueue"
xmlns:ui="clr-namespace:MaaWpfGui.ViewModels.UI"
xmlns:viewModels="clr-namespace:MaaWpfGui.ViewModels"
xmlns:vm="clr-namespace:MaaWpfGui"
d:DataContext="{d:DesignInstance {x:Type ui:SettingsViewModel}}"
d:Background="White"
d:DataContext="{d:DesignInstance {x:Type taskQueue:InfrastSettingsUserControlModel}}"
d:DesignWidth="220"
s:View.ActionTarget="{Binding}"
mc:Ignorable="d">
<StackPanel>
<!-- 只放在高级设置的部分 -->
<StackPanel Visibility="{c:Binding TaskSettingVisibilities.EnableAdvancedSettings}">
<StackPanel Visibility="{c:Binding EnableAdvancedSettings, Source={x:Static setting:TaskQueueViewModel.TaskSettingVisibilities}}">
<CheckBox
Margin="0,10"
Content="{DynamicResource DormTrustEnabled}"
@@ -144,7 +144,7 @@
VerticalAlignment="Top"
Command="{s:Action InfrastItemSelectedAll}"
Content="{DynamicResource SelectAll}"
IsEnabled="{Binding Idle}" />
IsEnabled="{Binding Idle, Source={x:Static setting:TaskQueueViewModel.TaskSettingDataContext}}" />
<Button
Grid.Column="1"
Height="30"
@@ -152,7 +152,7 @@
VerticalAlignment="Top"
Command="{s:Action InfrastItemUnselectedAll}"
Content="{DynamicResource Clear}"
IsEnabled="{Binding Idle}" />
IsEnabled="{Binding Idle, Source={x:Static setting:TaskQueueViewModel.TaskSettingDataContext}}" />
</Grid>
</StackPanel>

View File

@@ -11,11 +11,11 @@
// but WITHOUT ANY WARRANTY
// </copyright>
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MaaWpfGui.Views.UserControl
namespace MaaWpfGui.Views.UserControl.TaskQueue
{
/// <summary>
/// InfrastSettingsUserControl.xaml 的交互逻辑