Skip to content

Commit

Permalink
feat: add image option to text export
Browse files Browse the repository at this point in the history
DorielRivalet committed Oct 26, 2022

Verified

This commit was signed with the committer’s verified signature.
brooksprumo Brooks
1 parent 14e2d47 commit 96e38c8
Showing 7 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion MHFZ_Overlay/App.config
Original file line number Diff line number Diff line change
@@ -317,7 +317,7 @@
<value>160</value>
</setting>
<setting name="TextFormatExport" serializeAs="String">
<value>Code Block</value>
<value>Image</value>
</setting>
<setting name="GenderExport" serializeAs="String">
<value>Male</value>
11 changes: 6 additions & 5 deletions MHFZ_Overlay/ConfigWindow.xaml
Original file line number Diff line number Diff line change
@@ -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" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='icons/image_FILL0_wght400_GRAD0_opsz48.xaml'}" Click="BtnImageFile_Click" Width="40" Style="{StaticResource ConfigBottomButtons}" Height="40"/>
<Button Background="Transparent" Grid.Row="3" x:Name="BtnLogFile" ToolTip="Export Hunted Monsters Log (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="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"/>
</Grid>


@@ -428,7 +428,7 @@
<TextBlock Text="Text" Margin="4,0,0,0" VerticalAlignment="Center" />
</StackPanel>
</TabItem.Header>
<Grid>
<Grid x:Name="GearTextGrid">
<DockPanel Margin="5,5,5,5">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<TextBlock ToolTip="If the values are incorrect, double check your settings, check your guild card and change gear either from sets or manually" Text="{Binding GetGearStats}" Name="GearStats" TextWrapping="Wrap"/>
@@ -445,8 +445,8 @@
</TabItem.Header>
<Grid>
<DockPanel Margin="5,5,5,5">
<Grid>

<Grid x:Name="HuntedLogGrid">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="1*"/>
@@ -889,6 +889,7 @@
<ComboBox Grid.Column="1" Grid.Row="14" SelectedValuePath="Content" SelectedValue="{Binding Source={StaticResource Settings}, Path=TextFormatExport}">
<ComboBoxItem>Code Block</ComboBoxItem>
<ComboBoxItem>Markdown</ComboBoxItem>
<ComboBoxItem>Image</ComboBoxItem>
<ComboBoxItem>None</ComboBoxItem>
</ComboBox>

72 changes: 70 additions & 2 deletions MHFZ_Overlay/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@
using Window = System.Windows.Window;
using CsvHelper;
using MaterialDesignThemes.Wpf.Converters;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Command;

namespace MHFZ_Overlay
{
@@ -500,6 +503,10 @@ public ConfigWindow(MainWindow mainWindow)
FilterBox.ItemsSource = new string[] { "All", "Large Monster", "Small Monster" };

MyList.Items.Filter = MonsterFilterAll;

//// See: https://stackoverflow.com/questions/22285866/why-relaycommand
//// Or use MVVM Light to obtain RelayCommand.
//this.ScreenShotCommand = new RelayCommand<FrameworkElement>(this.OnScreenShotCommandAsync);
}

private bool MonsterFilterAll(object obj)
@@ -773,6 +780,11 @@ private void BtnSaveFile_Click(object sender, RoutedEventArgs e)
textToSave = string.Format("```text\n{0}\n```", textToSave);
else if (GetTextFormatMode() == "Markdown")
textToSave = MainWindow.DataLoader.model.MarkdownSavedGearStats;
//else if (GetTextFormatMode() == "Image")
//{
// CopyUIElementToClipboard(GearTextGrid);
// return;
//}

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Markdown file (*.md)|*.md|Text file (*.txt)|*.txt";
@@ -802,14 +814,71 @@ private void BtnCopyFile_Click(object sender, RoutedEventArgs e)
textToSave = string.Format("```text\n{0}\n```", textToSave);
else if (GetTextFormatMode() == "Markdown")
textToSave = MainWindow.DataLoader.model.MarkdownSavedGearStats;
else if (GetTextFormatMode() == "Image")
{
CopyUIElementToClipboard(GearTextGrid);
return;
}

//https://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp
Clipboard.SetText(textToSave);
}

private void BtnImageFile_Click(object sender, RoutedEventArgs e)
{
return;
string dir = System.AppDomain.CurrentDomain.BaseDirectory + @"USERDATA\HunterSets\currentSet.png";

CreateBitmapFromVisual(GearTextGrid, dir);
CopyUIElementToClipboard(GearTextGrid);
}

/// <summary>
/// Copies a UI element to the clipboard as an image.
/// </summary>
/// <param name="element">The element to copy.</param>
public static void CopyUIElementToClipboard(FrameworkElement element)
{
double width = element.ActualWidth;
double height = element.ActualHeight;
if (width <= 0 || height <= 0)
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())
{
VisualBrush vb = new VisualBrush(element);
dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
}
bmpCopied.Render(dv);
Clipboard.SetImage(bmpCopied);
}

public static void CreateBitmapFromVisual(Visual target, string fileName)
{
if (target == null || string.IsNullOrEmpty(fileName))
{
return;
}

Rect bounds = VisualTreeHelper.GetDescendantBounds(target);

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

DrawingVisual visual = new DrawingVisual();

using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(target);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}

renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(fileName))
{
bitmapEncoder.Save(stm);
}
}

private void FilterBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -831,7 +900,6 @@ protected void BtnLogFile_Click(object sender, EventArgs e)
}
}


};


1 change: 1 addition & 0 deletions MHFZ_Overlay/MHFZ_Overlay.csproj
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@
<PackageReference Include="FontAwesome.Sharp" Version="6.1.1" />
<PackageReference Include="MaterialDesignThemes" Version="4.5.0" />
<PackageReference Include="Memory.dll.x86" Version="1.2.24" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
2 changes: 1 addition & 1 deletion MHFZ_Overlay/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MHFZ_Overlay/Settings.settings
Original file line number Diff line number Diff line change
@@ -312,7 +312,7 @@
<Value Profile="(Default)">160</Value>
</Setting>
<Setting Name="TextFormatExport" Type="System.String" Scope="User">
<Value Profile="(Default)">Code Block</Value>
<Value Profile="(Default)">Image</Value>
</Setting>
<Setting Name="GenderExport" Type="System.String" Scope="User">
<Value Profile="(Default)">Male</Value>
2 changes: 2 additions & 0 deletions MHFZ_Overlay/addresses/AddressModel.cs
Original file line number Diff line number Diff line change
@@ -9218,6 +9218,8 @@ public string GetGameDamageInfo
// Clipboard.SetText(data);
//}



#endregion

public event PropertyChangedEventHandler? PropertyChanged;

0 comments on commit 96e38c8

Please sign in to comment.