From 1d700a02e303f42ca886cbb421b84876e23c6be1 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 13 May 2021 22:18:51 +0800 Subject: [PATCH] sh*t --- BallanceTASEditor.csproj | 13 +- Core/TASFile.cs | 17 +- Core/TASStruct.cs | 2 + MainWindow.xaml | 225 +++++----------------- MainWindow.xaml.cs | 1 + DialogUtil.cs => UI/DialogUtil.cs | 2 +- StyleConverter.cs => UI/StyleConverter.cs | 2 +- UI/TASFlow.xaml | 56 ++++++ UI/TASFlow.xaml.cs | 154 +++++++++++++++ TASViewer.cs => UI/TASViewer.cs | 24 +-- 10 files changed, 298 insertions(+), 198 deletions(-) rename DialogUtil.cs => UI/DialogUtil.cs (97%) rename StyleConverter.cs => UI/StyleConverter.cs (98%) create mode 100644 UI/TASFlow.xaml create mode 100644 UI/TASFlow.xaml.cs rename TASViewer.cs => UI/TASViewer.cs (72%) diff --git a/BallanceTASEditor.csproj b/BallanceTASEditor.csproj index 0f7020e..ed15637 100644 --- a/BallanceTASEditor.csproj +++ b/BallanceTASEditor.csproj @@ -56,8 +56,11 @@ MSBuild:Compile Designer - - + + + TASFlow.xaml + + MSBuild:Compile Designer @@ -70,11 +73,15 @@ - + MainWindow.xaml Code + + Designer + MSBuild:Compile + diff --git a/Core/TASFile.cs b/Core/TASFile.cs index da3d260..3138d51 100644 --- a/Core/TASFile.cs +++ b/Core/TASFile.cs @@ -38,15 +38,26 @@ namespace BallanceTASEditor.Core { } } - public int Get(ObservableCollection container, long startIndex, int count) { - if (mPointer == null) return 0; + public void Get(List container, long startIndex, int count) { + // no item. clean container + if (mPointer == null) { + for(int j = 0; j < count; j++) { + container[j].isEnable = false; + } + return; + } + + // fill container var cachePointer = mPointer; int i; for(i = 0; i < count && cachePointer != null; i++, startIndex++) { container[i].Reload(startIndex, cachePointer.Value); + container[i].isEnable = true; cachePointer = cachePointer.Next; } - return i; + for(; i < count; i++) { + container[i].isEnable = false; + } } public void Set(FrameDataField field, long prevRange, long nextRange, bool isSet) { diff --git a/Core/TASStruct.cs b/Core/TASStruct.cs index 5e344a2..5391115 100644 --- a/Core/TASStruct.cs +++ b/Core/TASStruct.cs @@ -8,6 +8,7 @@ using System.Text; namespace BallanceTASEditor.Core.TASStruct { public class FrameDataDisplay : INotifyPropertyChanged { public FrameDataDisplay(long index, FrameData fd) { + isEnable = true; Reload(index, fd); } @@ -26,6 +27,7 @@ namespace BallanceTASEditor.Core.TASStruct { this.keystates = fd.keystates; } + public bool isEnable { get; set; } public long index { get; set; } public float deltaTime { get; set; } public UInt32 keystates { diff --git a/MainWindow.xaml b/MainWindow.xaml index 8c689c4..e783f9f 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -4,14 +4,9 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:BallanceTASEditor" - xmlns:converter="clr-namespace:BallanceTASEditor" + xmlns:controls="clr-namespace:BallanceTASEditor.UI" mc:Ignorable="d" - Title="Ballance TAS Editor" Height="800" Width="500"> - - - - - + Title="Ballance TAS Editor" Height="500" Width="800"> @@ -27,6 +22,9 @@ + + + @@ -39,11 +37,8 @@ + - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index b8a0368..931bf5b 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using BallanceTASEditor.Core; using BallanceTASEditor.Core.TASStruct; +using BallanceTASEditor.UI; using System; using System.Collections.Generic; using System.Linq; diff --git a/DialogUtil.cs b/UI/DialogUtil.cs similarity index 97% rename from DialogUtil.cs rename to UI/DialogUtil.cs index e24f97d..e6d1a13 100644 --- a/DialogUtil.cs +++ b/UI/DialogUtil.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Windows; -namespace BallanceTASEditor { +namespace BallanceTASEditor.UI { public class DialogUtil { public static string OpenFileDialog() { diff --git a/StyleConverter.cs b/UI/StyleConverter.cs similarity index 98% rename from StyleConverter.cs rename to UI/StyleConverter.cs index 5471885..a542e45 100644 --- a/StyleConverter.cs +++ b/UI/StyleConverter.cs @@ -6,7 +6,7 @@ using System.Text; using System.Windows.Data; using System.Windows.Media; -namespace BallanceTASEditor { +namespace BallanceTASEditor.UI { [ValueConversion(typeof(bool), typeof(Color))] public class BackgroundConverter : IValueConverter { diff --git a/UI/TASFlow.xaml b/UI/TASFlow.xaml new file mode 100644 index 0000000..a8f7a5e --- /dev/null +++ b/UI/TASFlow.xaml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/UI/TASFlow.xaml.cs b/UI/TASFlow.xaml.cs new file mode 100644 index 0000000..56e366e --- /dev/null +++ b/UI/TASFlow.xaml.cs @@ -0,0 +1,154 @@ +using BallanceTASEditor.Core.TASStruct; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace BallanceTASEditor.UI { + /// + /// TASFlow.xaml 的交互逻辑 + /// + public partial class TASFlow : UserControl { + public TASFlow() { + InitializeComponent(); + mItemList = new List(); + mItemCount = 0; + SetItemCount(1); + } + + private int mItemCount; + private List mItemList; + public List DataSources { get; set; } + + public void RefreshDataSources() { + if (DataSources == null) return; + + for (int i = 0; i < mItemCount; i++) { + mItemList[i].Reload(DataSources[i]); + } + } + + public void SetItemCount(int newCount) { + var offset = newCount - mItemCount; + var abs = Math.Abs(offset); + if (offset == 0) return; + + // change column defination first + if (offset > 0) { + for(int i = 0; i < abs; i++) { + var item = new ColumnDefinition(); + item.Width = GridLength.Auto; + uiCoreWindow.ColumnDefinitions.Add(item); + } + } else { + uiCoreWindow.ColumnDefinitions.RemoveRange(newCount + 1, abs); // the first col is sheet header, so add 1 additionally + } + + // add / remove item + if (offset > 0) { + for (int i = 0; i < abs; i++) { + var newItem = new TASFlowUIItem(mItemCount + 1 + i); // the first col is sheet header, so add 1 additionally + newItem.Add(uiCoreWindow); + mItemList.Add(newItem); + } + } else { + for(int i = 0; i < abs; i++) { + mItemList[mItemCount].Remove(uiCoreWindow); + } + mItemList.RemoveRange(newCount, abs); + } + + // apply new count + mItemCount = newCount; + } + + } + + public class TASFlowUIItem { + private static readonly Thickness DEFAULT_MARGIN = new Thickness(2); + private static readonly Thickness RECT_MARGIN = new Thickness(1); + private static readonly SolidColorBrush RECT_STROKE = new SolidColorBrush(Colors.Gray); + private static readonly Color COLOR_SET = Color.FromRgb(30, 144, 255); + private static readonly Color COLOR_UNSET = Color.FromArgb(0, 255, 255, 255); + private const int KEY_COUNT = 9; + + public TASFlowUIItem(int column) { + // basic item + frame = new TextBlock(); + deltaTime = new TextBlock(); + + Grid.SetRow(frame, 0); + Grid.SetRow(deltaTime, 1); + Grid.SetColumn(frame, column); + Grid.SetColumn(deltaTime, column); + + frame.Margin = DEFAULT_MARGIN; + deltaTime.Margin = DEFAULT_MARGIN; + + // keystates item + keystates = new Rectangle[KEY_COUNT]; + keystatesFill = new SolidColorBrush[KEY_COUNT]; + for (int i = 0; i < KEY_COUNT; i++) { + keystates[i] = new Rectangle(); + keystatesFill[i] = new SolidColorBrush(COLOR_UNSET); + Grid.SetRow(keystates[i], 2 + i); + Grid.SetColumn(keystates[i], column); + keystates[i].Margin = RECT_MARGIN; + keystates[i].StrokeThickness = 1; + keystates[i].Stroke = RECT_STROKE; + keystates[i].Fill = keystatesFill[i]; + } + } + + public void Add(Grid target) { + target.Children.Add(frame); + target.Children.Add(deltaTime); + for (int i = 0; i < KEY_COUNT; i++) { + target.Children.Add(keystates[i]); + } + } + + public void Remove(Grid target) { + target.Children.Remove(frame); + target.Children.Remove(deltaTime); + for (int i = 0; i < KEY_COUNT; i++) { + target.Children.Remove(keystates[i]); + } + } + + public void Reload(FrameDataDisplay fdd) { + var isEnable = fdd.isEnable; + frame.Text = isEnable ? fdd.index.ToString() : ""; + deltaTime.Text = isEnable ? fdd.deltaTime.ToString() : ""; + keystatesFill[0].Color = isEnable && fdd.key_up ? COLOR_SET : COLOR_UNSET; + keystatesFill[1].Color = isEnable && fdd.key_down ? COLOR_SET : COLOR_UNSET; + keystatesFill[2].Color = isEnable && fdd.key_left ? COLOR_SET : COLOR_UNSET; + keystatesFill[3].Color = isEnable && fdd.key_right ? COLOR_SET : COLOR_UNSET; + keystatesFill[4].Color = isEnable && fdd.key_shift ? COLOR_SET : COLOR_UNSET; + keystatesFill[5].Color = isEnable && fdd.key_space ? COLOR_SET : COLOR_UNSET; + keystatesFill[6].Color = isEnable && fdd.key_q ? COLOR_SET : COLOR_UNSET; + keystatesFill[7].Color = isEnable && fdd.key_esc ? COLOR_SET : COLOR_UNSET; + keystatesFill[8].Color = isEnable && fdd.key_enter ? COLOR_SET : COLOR_UNSET; + + frame.Visibility = isEnable ? Visibility.Visible : Visibility.Collapsed; + deltaTime.Visibility = isEnable ? Visibility.Visible : Visibility.Collapsed; + for (int i = 0; i < KEY_COUNT; i++) { + keystates[i].Visibility = isEnable ? Visibility.Visible : Visibility.Collapsed; + } + } + + public TextBlock frame; + public TextBlock deltaTime; + public Rectangle[] keystates; + private SolidColorBrush[] keystatesFill; + } +} diff --git a/TASViewer.cs b/UI/TASViewer.cs similarity index 72% rename from TASViewer.cs rename to UI/TASViewer.cs index 950be85..5e1c642 100644 --- a/TASViewer.cs +++ b/UI/TASViewer.cs @@ -8,9 +8,9 @@ using System.Collections.ObjectModel; using System.Windows.Controls; using System.Windows; -namespace BallanceTASEditor { +namespace BallanceTASEditor.UI { public class TASViewer : IDisposable { - public TASViewer(TASFile file, Slider slider, DataGrid datagrid) { + public TASViewer(TASFile file, Slider slider, TASFlow datagrid) { mFile = file; mSlider = slider; mDataGrid = datagrid; @@ -21,7 +21,7 @@ namespace BallanceTASEditor { // init data mPosition = 0; - mDataSource = new ObservableCollection(); + mDataSource = new List(); INVALID_FRAME_DATA = new FrameData(-1f, 0); for (int i = 0; i < DATA_LIST_LENGTH; i++) { mDataSource.Add(new FrameDataDisplay(0, INVALID_FRAME_DATA)); @@ -29,32 +29,32 @@ namespace BallanceTASEditor { mFile.Get(mDataSource, 0, DATA_LIST_LENGTH); // bind event and source - mDataGrid.ItemsSource = mDataSource; + mDataGrid.SetItemCount(DATA_LIST_LENGTH); + mDataGrid.DataSources = mDataSource; + mDataGrid.RefreshDataSources(); mSlider.ValueChanged += sliderValueChanged; } public void Dispose() { - mDataGrid.ItemsSource = null; + mDataGrid.DataSources = null; mSlider.ValueChanged -= sliderValueChanged; } - const int DATA_LIST_LENGTH = 30; + const int DATA_LIST_LENGTH = 15; FrameData INVALID_FRAME_DATA; TASFile mFile; Slider mSlider; - DataGrid mDataGrid; + TASFlow mDataGrid; long mPosition; - ObservableCollection mDataSource; + List mDataSource; private void sliderValueChanged(object sender, RoutedPropertyChangedEventArgs e) { long pos = Convert.ToInt64(Math.Floor(e.NewValue)); long offset = pos - mPosition; mFile.Shift(offset); - var gotten = mFile.Get(mDataSource, pos, DATA_LIST_LENGTH); - for(; gotten < DATA_LIST_LENGTH; gotten++) { - mDataSource[gotten].Reload(-1, INVALID_FRAME_DATA); - } + mFile.Get(mDataSource, pos, DATA_LIST_LENGTH); mPosition = pos; + mDataGrid.RefreshDataSources(); } private void updateSliderRange() {