优化结构

This commit is contained in:
Hakoyu 2023-10-20 23:22:48 +08:00
parent 58fe49abc6
commit 355742b219
11 changed files with 166 additions and 125 deletions

View File

@ -40,7 +40,7 @@ public class CalculatorConverter : IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Any(i => i == DependencyProperty.UnsetValue))
return 0;
return 0.0;
if (values.Length == 1)
return values[0];
double result = System.Convert.ToDouble(values[0]);

View File

@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using VPet.ModMaker.Models.ModModel;
@ -159,4 +160,33 @@ public static class Extensions
return bytesRead;
}
}
public static T? FindVisualChild<T>(this DependencyObject obj)
where T : DependencyObject
{
if (obj is null)
return null;
var count = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is T t)
return t;
if (FindVisualChild<T>(child) is T childItem)
return childItem;
}
return null;
}
public static T FindParent<T>(this DependencyObject obj)
where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
}

View File

@ -137,14 +137,25 @@ public class FoodAnimeTypeModel
: this()
{
Name.Value = Path.GetFileName(path);
var infoFile = Path.Combine(path, ModMakerInfo.InfoFile);
if (
Directory.EnumerateFiles(path, ModMakerInfo.InfoFile, SearchOption.AllDirectories).Any()
is false
)
throw new Exception("信息文件\n{0}\n不存在".Translate(infoFile));
if (File.Exists(infoFile))
ParseInfoFile(path, infoFile);
//var infoFile = Path.Combine(path, ModMakerInfo.InfoFile);
//if (
// Directory.EnumerateFiles(path, ModMakerInfo.InfoFile, SearchOption.AllDirectories).Any()
// is false
//)
// throw new Exception("信息文件\n{0}\n不存在".Translate(infoFile));
//if (File.Exists(infoFile))
// ParseInfoFile(path, infoFile);
var infoFiles = Directory.EnumerateFiles(
path,
ModMakerInfo.InfoFile,
SearchOption.AllDirectories
);
if (infoFiles.Any() is false)
throw new Exception("信息文件不存在".Translate());
foreach (var file in infoFiles)
{
ParseInfoFile(Path.GetDirectoryName(file), file);
}
}
public FoodAnimeTypeModel(FoodAnimeTypeModel model)
@ -278,6 +289,8 @@ public class FoodAnimeTypeModel
);
}
}
public void Save(string path) { }
}
public class PNGAnimeInfo

View File

@ -1,4 +1,6 @@
using HKW.HKWViewModels.SimpleObservable;
using LinePutScript;
using LinePutScript.Converter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

View File

@ -238,8 +238,8 @@ public class ModInfoModel : I18nModel<I18nModInfoModel>
}
else if (FoodAnimeTypeModel.FoodAnimeNames.Contains(dirName))
{
//if (FoodAnimeTypeModel.Create(animeDir) is FoodAnimeTypeModel model1)
// petModel.FoodAnimes.Add(model1);
if (FoodAnimeTypeModel.Create(animeDir) is FoodAnimeTypeModel model1)
petModel.FoodAnimes.Add(model1);
}
}
}

View File

@ -56,13 +56,12 @@ public static class Utils
using var sr = new StreamReader(imagePath);
sr.BaseStream.CopyTo(ms);
bitmapImage.StreamSource = ms;
bitmapImage.DecodePixelWidth = DecodePixelWidth;
}
finally
{
bitmapImage.EndInit();
}
if (bitmapImage.PixelWidth > DecodePixelWidth)
bitmapImage.DecodePixelWidth = DecodePixelWidth;
return bitmapImage;
}
}

View File

@ -316,8 +316,7 @@ public class AnimeEditWindowVM
/// </summary>
private void StopCommand_ExecuteEvent()
{
if (_playing is false)
return;
_playing = false;
}
/// <summary>

View File

@ -479,8 +479,7 @@ public class FoodAnimeEditWindowVM
/// </summary>
private void StopCommand_ExecuteEvent()
{
if (_playing is false)
return;
_playing = false;
}
/// <summary>
@ -494,11 +493,16 @@ public class FoodAnimeEditWindowVM
return;
}
_playing = true;
_frontPlayerTask.Start();
_backPlayerTask.Start();
_foodPlayerTask.Start();
await Task.WhenAll(_frontPlayerTask, _backPlayerTask, _foodPlayerTask);
Reset();
do
{
_frontPlayerTask.Start();
_backPlayerTask.Start();
_foodPlayerTask.Start();
await Task.WhenAll(_frontPlayerTask, _backPlayerTask, _foodPlayerTask);
_frontPlayerTask = new(FrontPlay);
_backPlayerTask = new(BackPlay);
_foodPlayerTask = new(FoodPlay);
} while (Loop.Value && _playing);
}
/// <summary>
@ -506,16 +510,13 @@ public class FoodAnimeEditWindowVM
/// </summary>
private void FrontPlay()
{
do
foreach (var model in CurrentAnimeModel.Value.FrontImages)
{
foreach (var model in CurrentAnimeModel.Value.FrontImages)
{
CurrentFrontImageModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
} while (Loop.Value);
CurrentFrontImageModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
}
/// <summary>
@ -523,16 +524,13 @@ public class FoodAnimeEditWindowVM
/// </summary>
private void BackPlay()
{
do
foreach (var model in CurrentAnimeModel.Value.BackImages)
{
foreach (var model in CurrentAnimeModel.Value.BackImages)
{
CurrentBackImageModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
} while (Loop.Value);
CurrentBackImageModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
}
/// <summary>
@ -540,16 +538,13 @@ public class FoodAnimeEditWindowVM
/// </summary>
private void FoodPlay()
{
do
foreach (var model in CurrentAnimeModel.Value.FoodLocations)
{
foreach (var model in CurrentAnimeModel.Value.FoodLocations)
{
CurrentFoodLocationModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
} while (Loop.Value);
CurrentFoodLocationModel.Value = model;
Task.Delay(model.Duration.Value).Wait();
if (_playing is false)
return;
}
}
/// <summary>

View File

@ -12,6 +12,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VPet.ModMaker.Models;
using VPet.ModMaker.Models.ModModel;
using VPet.ModMaker.ViewModels.ModEdit.AnimeEdit;
using VPet_Simulator.Core;
@ -92,7 +93,7 @@ public partial class AnimeEditWindow : Window
HitTestResult result = VisualTreeHelper.HitTest(listBox, pos);
if (result is null)
return;
var listBoxItem = FindVisualParent<ListBoxItem>(result.VisualHit);
var listBoxItem = result.VisualHit.FindParent<ListBoxItem>();
if (listBoxItem == null || listBoxItem.Content != listBox.SelectedItem)
return;
var dataObj = new DataObject(listBoxItem.Content);
@ -119,7 +120,7 @@ public partial class AnimeEditWindow : Window
if (e.Data.GetData(typeof(ImageModel)) is not ImageModel sourcePerson)
return;
//查找目标数据
var listBoxItem = FindVisualParent<ListBoxItem>(result.VisualHit);
var listBoxItem = result.VisualHit.FindParent<ListBoxItem>();
if (listBoxItem == null)
return;
var targetPerson = listBoxItem.Content as ImageModel;
@ -134,35 +135,6 @@ public partial class AnimeEditWindow : Window
list[targetIndex] = temp;
}
public static T? FindVisualChild<T>(DependencyObject obj)
where T : DependencyObject
{
if (obj is null)
return null;
var count = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is T t)
return t;
if (FindVisualChild<T>(child) is T childItem)
return childItem;
}
return null;
}
public static T FindVisualParent<T>(DependencyObject obj)
where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not ListBox listBox)

View File

@ -25,7 +25,7 @@
<DataTemplate x:Key="Expander_AnimeItem" DataType="Expander">
<Expander
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalAlignment="Top"
d:DataContext=""
Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}">
<Expander.ContextMenu>
@ -80,7 +80,16 @@
</Grid.RowDefinitions>
<GroupBox>
<GroupBox.Header>
<Label d:Content="顶层图片" Content="{ll:Str 顶层图片}" />
<StackPanel Orientation="Horizontal">
<Label d:Content="顶层图片" Content="{ll:Str 顶层图片}" />
<TextBlock Margin="10,0,0,0">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}({0})">
<Binding Path="FrontImages.Count" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</GroupBox.Header>
<ListBox
d:ItemsSource="{d:SampleData ItemCount=5}"
@ -88,7 +97,6 @@
Drop="ListBox_Drop"
ItemsSource="{Binding FrontImages, IsAsync=True}"
PreviewMouseMove="ListBox_PreviewMouseMove"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
SelectedItem="{Binding DataContext.CurrentFrontImageModel.Value, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemsPanel>
@ -147,7 +155,16 @@
</GroupBox>
<GroupBox Grid.Row="1">
<GroupBox.Header>
<Label d:Content="底层图片" Content="{ll:Str 底层图片}" />
<StackPanel Orientation="Horizontal">
<Label d:Content="底层图片" Content="{ll:Str 底层图片}" />
<TextBlock Margin="10,0,0,0">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}({0})">
<Binding Path="BackImages.Count" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</GroupBox.Header>
<ListBox
d:ItemsSource="{d:SampleData ItemCount=5}"
@ -155,7 +172,6 @@
Drop="ListBox_Drop"
ItemsSource="{Binding BackImages, IsAsync=True}"
PreviewMouseMove="ListBox_PreviewMouseMove"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
SelectedItem="{Binding DataContext.CurrentBackImageModel.Value, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemsPanel>
@ -214,13 +230,22 @@
</Grid>
<GroupBox Grid.Column="1">
<GroupBox.Header>
<Label d:Content="食物位置" Content="{ll:Str 食物位置}" />
<StackPanel Orientation="Horizontal">
<Label d:Content="食物位置" Content="{ll:Str 食物位置}" />
<TextBlock Margin="10,0,0,0">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}({0})">
<Binding Path="FrontImages.Count" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</GroupBox.Header>
<ListBox
d:ItemsSource="{d:SampleData ItemCount=5}"
ItemsSource="{Binding FoodLocations, IsAsync=True}"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
SelectedItem="{Binding DataContext.CurrentFoodLocationModel.Value, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}">
SelectedItem="{Binding DataContext.CurrentFoodLocationModel.Value, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
@ -371,15 +396,48 @@
<Image
Width="500"
Height="500"
Source="{Binding Source, ElementName=Image_Back}" />
Source="{Binding CurrentBackImageModel.Value.Image.Value}" />
<Image
Height="{Binding Width, RelativeSource={RelativeSource Mode=Self}}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Opacity="{Binding CurrentFoodLocationModel.Value.Opacity.Value}"
RenderTransformOrigin="0,0"
Source="{Binding FoodImage.Value}">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform Angle="{Binding CurrentFoodLocationModel.Value.Rotate.Value}" />
<TranslateTransform />
</TransformGroup>
</Image.RenderTransform>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Width">
<Setter.Value>
<MultiBinding Converter="{StaticResource CalculatorConverter}" ConverterParameter="*">
<Binding Path="CurrentFoodLocationModel.Value.Rect.Width.Value" />
<Binding Source="1" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Margin">
<Setter.Value>
<MultiBinding Converter="{StaticResource RatioMarginConverter}">
<Binding Source="1" />
<Binding Path="CurrentFoodLocationModel.Value.Rect.X.Value" />
<Binding Path="CurrentFoodLocationModel.Value.Rect.Y.Value" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Image.Style>
</Image>
<Image
Width="500"
Height="500"
Source="{Binding Source, ElementName=Image_Center}" />
<Image
Width="500"
Height="500"
Source="{Binding Source, ElementName=Image_Front}" />
Source="{Binding CurrentFrontImageModel.Value.Image.Value}" />
</Grid>
</Image.ToolTip>
</Image>
@ -447,7 +505,7 @@
ItemsSource="{Binding Anime.Value.HappyAnimes, IsAsync=True}"
SelectionChanged="ListBox_Animes_SelectionChanged" />
</TabItem>
<!--<TabItem d:Header="Nomal (0)" Tag="Nomal">
<TabItem d:Header="Nomal (0)" Tag="Nomal">
<TabItem.Header>
<MultiBinding Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}{0} ({1})">
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
@ -488,7 +546,7 @@
ItemContainerStyle="{StaticResource ListBoxItem_Style}"
ItemTemplate="{StaticResource Expander_AnimeItem}"
ItemsSource="{Binding Anime.Value.IllAnimes, IsAsync=True}" />
</TabItem>-->
</TabItem>
</TabControl>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>

View File

@ -54,6 +54,8 @@ public partial class FoodAnimeEditWindow : Window
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ViewModel is null)
return;
if (
sender is not TabControl tabControl
|| tabControl.SelectedItem is not TabItem item
@ -136,35 +138,6 @@ public partial class FoodAnimeEditWindow : Window
//list[targetIndex] = temp;
}
public static T? FindVisualChild<T>(DependencyObject obj)
where T : DependencyObject
{
if (obj is null)
return null;
var count = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is T t)
return t;
if (FindVisualChild<T>(child) is T childItem)
return childItem;
}
return null;
}
public static T FindVisualParent<T>(DependencyObject obj)
where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not ListBox listBox)