Skip to content

Commit

Permalink
Allow for loads other than NSMBW Any% to be retimed
Browse files Browse the repository at this point in the history
  • Loading branch information
LetsPlentendo-CH committed Feb 11, 2021
1 parent c309375 commit 61aff46
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 22 deletions.
2 changes: 1 addition & 1 deletion FinalRetimeWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Label Grid.Row="2" Grid.Column="1" Content="Loads:" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" FontSize="15"/>
<Label x:Name="WLoads" Grid.Row="3" Content="--:--.---" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" FontSize="15"/>
<Label x:Name="Loads" Grid.Row="3" Grid.Column="1" Content="--:--.---" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" FontSize="15"/>
<Label Grid.Row="4" Grid.ColumnSpan="2" Content="Generated with LoadRetimer v1.1.0" HorizontalAlignment="Center" FontSize="10" />
<Label Grid.Row="4" Grid.ColumnSpan="2" Content="Generated with LoadRetimer v1.2" HorizontalAlignment="Center" FontSize="10" />
<Button Grid.Row="5" Grid.ColumnSpan="2" Content="Close" Click="Button_Click" />
</Grid>
</Window>
2 changes: 1 addition & 1 deletion Info.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label Content="Load Retimer v1.1.0" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" FontSize="25"/>
<Label Content="Load Retimer v1.2" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" FontSize="25"/>
<TextBlock Grid.Row="1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Padding="2,0,0,0">
Controls:<LineBreak/>
A/D: go back/forwards 1 frame<LineBreak/>
Expand Down
8 changes: 5 additions & 3 deletions LoadInfo.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label x:Name="LoadName" Content="Load" Grid.ColumnSpan="2" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
<TextBox x:Name="LoadName" Grid.ColumnSpan="2" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
Load
</TextBox>
<Separator VerticalAlignment="Top" Margin="0,49,0,0" Grid.ColumnSpan="2"/>
<Label x:Name="LoadFrameDurationF" Grid.Row="1" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold"/>
<Label x:Name="LoadFrameDurationS" Grid.Row="2" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold"/>
<Label x:Name="LoadFrameBeginS" Grid.Row="1" Grid.Column="1" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label x:Name="LoadFrameEndS" Grid.Row="2" Grid.Column="1" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label x:Name="LoadFrameBeginS" Grid.Row="1" Grid.Column="1" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MouseUp="LoadFrameBeginS_MouseUp" Foreground="#FF0645AD" Cursor="Hand"/>
<Label x:Name="LoadFrameEndS" Grid.Row="2" Grid.Column="1" Content="---" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MouseUp="LoadFrameEndS_MouseUp" Foreground="#FF0645AD" Cursor="Hand"/>

</Grid>
</UserControl>
22 changes: 21 additions & 1 deletion LoadInfo.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public LoadInfo(string name) {
}

public void SetName(string s) {
LoadName.Content = s;
LoadName.Text = s;
}

public void MakeUnchangable() {
LoadName.IsReadOnly = true;
}

public void SetBegin(TimeSpan begin) {
Expand Down Expand Up @@ -61,5 +65,21 @@ public int FrameDuration() {
return 0;
}
}

private void LoadFrameBeginS_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
MainWindow parentWindow = (MainWindow)MainWindow.GetWindow(this);
if (parentWindow == null) return;
TimeSpan tmp = new TimeSpan((long)(frameStart / MainWindow.frameRate * 10_000_000));
parentWindow.Video.Position = Helper.CeilTimeSpanMillis(tmp + parentWindow.Video.PlaybackStartTime.GetValueOrDefault());
}

private void LoadFrameEndS_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
MainWindow parentWindow = (MainWindow)MainWindow.GetWindow(this);
if (parentWindow == null) return;
TimeSpan tmp = new TimeSpan((long)(frameEnd / MainWindow.frameRate * 10_000_000));

// not exactly sure why I can't just round here...
parentWindow.Video.Position = Helper.CeilTimeSpanMillis(tmp + parentWindow.Video.PlaybackStartTime.GetValueOrDefault());
}
}
}
6 changes: 5 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
<Button Grid.Row="1" Grid.Column="1" Content="End Run" Click="EndRun_Click" />
</Grid>

<ListBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" x:Name="LoadBox" HorizontalContentAlignment="Stretch" />
<ListBox Grid.Row="2" Grid.Column="2" x:Name="LoadBox" HorizontalContentAlignment="Stretch" />
<ComboBox x:Name="LoadCategory" Grid.Row="3" Grid.Column="2" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem IsSelected="True">NSMBW Any%</ComboBoxItem>
<ComboBoxItem>Other</ComboBoxItem>
</ComboBox>
<Grid Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
Expand Down
70 changes: 55 additions & 15 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
Expand All @@ -20,7 +17,9 @@ public partial class MainWindow : Window {
private DispatcherTimer timerVideo;
private DispatcherTimer timerAnalyzer;

readonly string[] loadNames = new string[] {
int loadType = 0;

readonly string[] anyPercentLoadNames = new string[] {
"1-1 Start",
"1-1 End",
"1-2 Start",
Expand Down Expand Up @@ -80,6 +79,7 @@ public MainWindow() {
InitializeComponent();
Unosquare.FFME.Library.FFmpegDirectory = @"./ffmpeg";
TotalRunInfo.SetName("Entire Run");
TotalRunInfo.MakeUnchangable();

Video.ScrubbingEnabled = false;

Expand Down Expand Up @@ -127,7 +127,12 @@ private void Info_Click(object sender, RoutedEventArgs e) {

private LoadInfo CreateNewLoadInfo() {
ListBoxItem lbi = new ListBoxItem();
LoadInfo newLoad = new LoadInfo(loadNames[LoadBox.Items.Count]);
LoadInfo newLoad = new LoadInfo(String.Format("Load {0}", LoadBox.Items.Count + 1));
if (loadType == 0) {
if (LoadBox.Items.Count < anyPercentLoadNames.Length) {
newLoad.SetName(anyPercentLoadNames[LoadBox.Items.Count]);
}
}
lbi.Content = newLoad;
LoadBox.Items.Add(lbi);
LoadBox.ScrollIntoView(lbi);
Expand All @@ -140,10 +145,10 @@ private void BeginLoad_Click(object sender, RoutedEventArgs e) {
if (LoadBox.SelectedItem == null) {
CreateNewLoadInfo();
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetBegin(Video.FramePosition);
selLoad.SetBegin(TrueFramePos());
} else {
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetBegin(Video.FramePosition);
selLoad.SetBegin(TrueFramePos());
LoadBox.SelectedItem = null;
}
}
Expand All @@ -152,10 +157,10 @@ private void EndLoad_Click(object sender, RoutedEventArgs e) {
if (LoadBox.SelectedItem == null) {
CreateNewLoadInfo();
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetEnd(Video.FramePosition);
selLoad.SetEnd(TrueFramePos());
} else {
LoadInfo selLoad = (LoadInfo)((ListBoxItem)LoadBox.SelectedItem).Content;
selLoad.SetEnd(Video.FramePosition);
selLoad.SetEnd(TrueFramePos());
LoadBox.SelectedItem = null;
}
}
Expand All @@ -181,10 +186,15 @@ private void OpenFile_Click(object sender, RoutedEventArgs e) {
Filter = "Video files (*.mp4, *.mkv, *.flv, *.wmv, *.avi)|*.mp4;*.mkv;*.flv;*.wmv;*.avi|All files (*.*)|*.*"
};
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
if (Video.IsOpen) {
Video.Close();
}
Video.Open(new Uri(ofd.FileName));
}
}

const byte CURR_VERSION = 2;

private void OpenLoads_Click(object sender, RoutedEventArgs e) {
var ofd = new OpenFileDialog {
Title = "Select loads",
Expand All @@ -193,7 +203,13 @@ private void OpenLoads_Click(object sender, RoutedEventArgs e) {
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var f = ofd.OpenFile();
var binReader = new BinaryReader(f);
bool hasTotalRun = binReader.ReadByte() == 1;
int version = binReader.ReadByte();
bool hasTotalRun;
if (version >= 2) { // the version field didn't exist before
hasTotalRun = binReader.ReadByte() == 1;
} else {
hasTotalRun = version == 1;
}
int loadCount = binReader.ReadByte();
frameRate = binReader.ReadDouble();
FPSLabel.Content = String.Format("FPS: {0:F2}", frameRate);
Expand All @@ -211,7 +227,12 @@ private void OpenLoads_Click(object sender, RoutedEventArgs e) {
LoadInfo newLoad = CreateNewLoadInfo();
newLoad.SetBegin(new TimeSpan((long)(startFrames / frameRate * 10_000_000)));
newLoad.SetEnd(new TimeSpan((long)(endFrames / frameRate * 10_000_000)));
if (version >= 2) {
string loadName = binReader.ReadString();
newLoad.SetName(loadName);
}
}
f.Close();
}
}

Expand All @@ -223,6 +244,7 @@ private void SaveLoads_Click(object sender, RoutedEventArgs e) {
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var f = ofd.OpenFile();
var binWriter = new BinaryWriter(f);
binWriter.Write(CURR_VERSION);
if (TotalRunInfo.FrameDuration() > 0) {
binWriter.Write((byte)1);
} else {
Expand All @@ -238,6 +260,7 @@ private void SaveLoads_Click(object sender, RoutedEventArgs e) {
LoadInfo li = (LoadInfo)((ListBoxItem)LoadBox.Items[i]).Content;
binWriter.Write((UInt32)li.frameStart);
binWriter.Write((UInt32)li.frameEnd);
binWriter.Write(li.LoadName.Text);
}
}
}
Expand Down Expand Up @@ -282,12 +305,12 @@ private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)

private void StartRun_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
TotalRunInfo.SetBegin(Video.FramePosition);
TotalRunInfo.SetBegin(TrueFramePos());
}

private void EndRun_Click(object sender, RoutedEventArgs e) {
if (!Video.IsOpen) return;
TotalRunInfo.SetEnd(Video.FramePosition);
TotalRunInfo.SetEnd(TrueFramePos());
}

private bool userChanging = false;
Expand All @@ -296,7 +319,7 @@ private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<d
if (userChanging) {
if (!Video.IsOpen) return;
var ts = new TimeSpan((long)(Video.NaturalDuration.GetValueOrDefault().TotalSeconds * Slider.Value * 10_000));
Video.Position = ts;
Video.Position = ts + Video.PlaybackStartTime.GetValueOrDefault();
}
}

Expand Down Expand Up @@ -392,10 +415,13 @@ private Point TransformFromNaturalVideo(Point m) {

private void TimerVideo_Tick(object sender, object e) {
if (!Video.IsOpen) return;
Slider.Value = Video.Position.TotalMilliseconds / Video.NaturalDuration.GetValueOrDefault().TotalMilliseconds * 1000;
TimePosition.Content = String.Format("{0:hh\\:mm\\:ss\\.fff}", new TimeSpan((long)(Video.Position.TotalMilliseconds * 10_000)));
var pos = Mouse.GetPosition(Video);
Magnifier.Viewbox = new Rect(pos.X - 10, pos.Y + 10, 20, 20);

var tmp = new TimeSpan((long)(TrueFramePos().TotalMilliseconds * 10_000));
TimePosition.Content = String.Format("{0:hh\\:mm\\:ss\\.fff}", Helper.RoundTimeSpanMillis(tmp));
if (userChanging) return;
Slider.Value = TrueFramePos().TotalMilliseconds / Video.NaturalDuration.GetValueOrDefault().TotalMilliseconds * 1000;
}

private async void Analyzer_Tick(object sender, object e) {
Expand All @@ -418,6 +444,10 @@ private async void Analyzer_Tick(object sender, object e) {
}
}

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
loadType = LoadCategory.SelectedIndex;
}

private System.Drawing.Bitmap tempBmp;

private bool IsBlack(System.Drawing.Bitmap bitmap) {
Expand All @@ -441,6 +471,10 @@ private void Window_SizeChanged(object sender, SizeChangedEventArgs e) {
Rect.Height = p2.Y - p1.Y;
}
}

private TimeSpan TrueFramePos() {
return (TimeSpan)(Video.FramePosition - Video.PlaybackStartTime);
}
}

class Helper {
Expand All @@ -449,5 +483,11 @@ public static TimeSpan RoundTimeSpanMillis(TimeSpan ts) {
var res = new TimeSpan(ticks);
return res;
}

public static TimeSpan CeilTimeSpanMillis(TimeSpan ts) {
long ticks = (long)Math.Ceiling(ts.Ticks / 10_000.0) * 10_000;
var res = new TimeSpan(ticks);
return res;
}
}
}

0 comments on commit 61aff46

Please sign in to comment.