Skip to content

Commit

Permalink
feat: implement save file dialogs to gear export options
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Oct 26, 2022
1 parent 169084e commit 97b5533
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MHFZ_Overlay/ConfigWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@
</Grid.RowDefinitions>
<Button Background="Transparent" Grid.Row="0" x:Name="BtnSaveFile" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/file_download_FILL0_wght400_GRAD0_opsz48.xaml'}" Style="{StaticResource ConfigBottomButtons}" Height="40" Width="40" ToolTip="Save gear to text file" Click="BtnSaveFile_Click"/>
<Button Background="Transparent" Grid.Row="1" x:Name="BtnCopyFile" ToolTip="Copy gear to clipboard" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/content_copy_FILL0_wght400_GRAD0_opsz48.xaml'}" Click="BtnCopyFile_Click" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40"/>
<Button Background="Transparent" Grid.Row="2" x:Name="BtnImageFile" ToolTip="Export image and save screenshot to clipboard (instantly overwrites existing file)" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/image_FILL0_wght400_GRAD0_opsz48.xaml'}" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40" Click="BtnImageFile_Click"/>
<Button Background="Transparent" Grid.Row="3" x:Name="BtnLogFile" ToolTip="Export Hunted Monsters Log (instantly overwrites existing file)" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/description_FILL0_wght400_GRAD0_opsz48.xaml'}" Click="BtnLogFile_Click" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40"/>
<Button Background="Transparent" Grid.Row="2" x:Name="BtnImageFile" ToolTip="Save gear image to disk and copy screenshot to clipboard" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/image_FILL0_wght400_GRAD0_opsz48.xaml'}" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40" Click="BtnImageFile_Click"/>
<Button Background="Transparent" Grid.Row="3" x:Name="BtnLogFile" ToolTip="Export hunted monsters log as Comma Separated Values" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/description_FILL0_wght400_GRAD0_opsz48.xaml'}" Click="BtnLogFile_Click" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40"/>
</Grid>


Expand Down
55 changes: 46 additions & 9 deletions MHFZ_Overlay/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -826,12 +826,22 @@ private void BtnCopyFile_Click(object sender, RoutedEventArgs e)

private void BtnImageFile_Click(object sender, RoutedEventArgs e)
{
System.Windows.MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Overwrite current file?", "Gear Stats", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Asterisk, MessageBoxResult.No); if (messageBoxResult.ToString() == "No") { return; }
//System.Windows.MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Overwrite current file?", "Gear Stats", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Asterisk, MessageBoxResult.No); if (messageBoxResult.ToString() == "No") { return; }

string dir = System.AppDomain.CurrentDomain.BaseDirectory + @"USERDATA\HunterSets\currentSet.png";
SaveFileDialog savefile = new SaveFileDialog();
string dateTime = DateTime.Now.ToString();
dateTime = dateTime.Replace("/", "-");
dateTime = dateTime.Replace(" ", "_");
dateTime = dateTime.Replace(":", "-");
savefile.FileName = "HuntedLog-" + dateTime + ".png";
savefile.Filter = "PNG files (*.png)|*.png";
savefile.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + @"USERDATA\HunterSets\";

CreateBitmapFromVisual(GearTextGrid, dir);
CopyUIElementToClipboard(GearTextGrid);
if (savefile.ShowDialog() == true)
{
CreateBitmapFromVisual(GearTextGrid, savefile.FileName);
CopyUIElementToClipboard(GearTextGrid);
}
}

/// <summary>
Expand All @@ -843,7 +853,11 @@ public static void CopyUIElementToClipboard(FrameworkElement element)
double width = element.ActualWidth;
double height = element.ActualHeight;
if (width <= 0 || height <= 0)
{
System.Windows.MessageBox.Show("Please load the gear stats by visiting the text tab in the configuration window", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
return;
}

RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 96, 96, PixelFormats.Default);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
Expand All @@ -864,6 +878,12 @@ public static void CreateBitmapFromVisual(Visual target, string fileName)

Rect bounds = VisualTreeHelper.GetDescendantBounds(target);

if (bounds.Width <= 0 || bounds.Height <= 0)
{
System.Windows.MessageBox.Show("Please load the gear stats by visiting the text tab in the configuration window", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
return;
}

RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32);

DrawingVisual visual = new DrawingVisual();
Expand Down Expand Up @@ -893,14 +913,31 @@ private void FilterBox_SelectionChanged(object sender, SelectionChangedEventArgs
// on generate csv button click
protected void BtnLogFile_Click(object sender, EventArgs e)
{
System.Windows.MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Overwrite current file?", "Gear Stats", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Asterisk, MessageBoxResult.No); if (messageBoxResult.ToString() == "No") { return; }
//System.Windows.MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Overwrite current file?", "Gear Stats", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Asterisk, MessageBoxResult.No); if (messageBoxResult.ToString() == "No") { return; }

string dir = System.AppDomain.CurrentDomain.BaseDirectory + @"USERDATA\HuntedLogs\log.csv";
SaveFileDialog savefile = new SaveFileDialog();
string dateTime = DateTime.Now.ToString();
dateTime = dateTime.Replace("/", "-");
dateTime = dateTime.Replace(" ", "_");
dateTime = dateTime.Replace(":", "-");
savefile.FileName = "HuntedLog-"+dateTime+".csv";
savefile.Filter = "CSV files (*.csv)|*.csv";
savefile.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + @"USERDATA\HuntedLogs\";

using (var writer = new StreamWriter(dir))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
if (savefile.ShowDialog() == true)
{
csv.WriteRecords(Monsters);
//using (StreamWriter sw = new StreamWriter(savefile.FileName,
// false, System.Text.Encoding.Unicode))
//{
// sw.WriteLine("Test line");
// sw.WriteLine("Test line2");
// sw.WriteLine("Test line3");
//}
using (var writer = new StreamWriter(savefile.FileName))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(Monsters);
}
}
}
};
Expand Down

0 comments on commit 97b5533

Please sign in to comment.