added image conversion function.

This commit is contained in:
morkt 2014-08-22 10:16:11 +04:00
parent 46a4ca6b14
commit a72c4e407b
12 changed files with 357 additions and 3 deletions

View File

@ -76,6 +76,9 @@
<setting name="winStatusBarVisibility" serializeAs="String">
<value>Visible</value>
</setting>
<setting name="appLastImageFormat" serializeAs="String">
<value>PNG</value>
</setting>
</GARbro.GUI.Properties.Settings>
</userSettings>
</configuration>

28
ConvertImages.xaml Normal file
View File

@ -0,0 +1,28 @@
<Window x:Class="GARbro.GUI.ConvertImages"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GARbro.GUI.Strings"
xmlns:p="clr-namespace:GARbro.GUI.Properties"
xmlns:g="clr-namespace:GameRes;assembly=GameRes"
Title="{x:Static s:guiStrings.TextConvertImages}" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
ResizeMode="NoResize" SizeToContent="WidthAndHeight"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Margin="20,10,20,10">
<Label Content="{x:Static s:guiStrings.LabelDestinationFormat}" Target="{Binding ElementName=ImageConversionFormat}" HorizontalAlignment="Left" Padding="0,0,10,0" VerticalAlignment="Center"/>
<ComboBox Name="ImageConversionFormat" DisplayMemberPath="Tag" Width="60" HorizontalAlignment="Left"
ItemsSource="{Binding Source={x:Static g:FormatCatalog.Instance}, Path=ImageFormats, Mode=OneWay}"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=appLastImageFormat, Mode=TwoWay}" SelectedValuePath="Tag"/>
</StackPanel>
<Separator Grid.Row="1"/>
<StackPanel Orientation="Horizontal" Margin="10,0,10,10" HorizontalAlignment="Right" Grid.Row="2">
<Button Content="{x:Static s:guiStrings.ButtonOK}" Click="ConvertButton_Click" Margin="10" Width="75" IsDefault="True" Height="25"/>
<Button Content="{x:Static s:guiStrings.ButtonCancel}" Margin="10" IsCancel="True" Width="75" Height="25"/>
</StackPanel>
</Grid>
</Window>

41
ConvertImages.xaml.cs Normal file
View File

@ -0,0 +1,41 @@
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using GameRes;
using GARbro.GUI.Properties;
namespace GARbro.GUI
{
/// <summary>
/// Interaction logic for ConvertImages.xaml
/// </summary>
public partial class ConvertImages : Window
{
public ConvertImages ()
{
InitializeComponent ();
InitImageFormats (this.ImageConversionFormat);
}
void InitImageFormats (ComboBox image_format)
{
/*
var formats = FormatCatalog.Instance.ImageFormats;
var models = formats.Select (f => new ImageFormatModel (f)).ToList();
var selected_format = Settings.Default.appLastImageFormat;
var selected = models.FirstOrDefault (f => f.Tag.Equals (selected_format));
image_format.ItemsSource = models;
if (null != selected)
image_format.SelectedItem = selected;
else if (models.Any())
image_format.SelectedIndex = 0;
*/
}
private void ConvertButton_Click (object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
}

View File

@ -120,6 +120,9 @@
<Compile Include="ArcParameters.xaml.cs">
<DependentUpon>ArcParameters.xaml</DependentUpon>
</Compile>
<Compile Include="ConvertImages.xaml.cs">
<DependentUpon>ConvertImages.xaml</DependentUpon>
</Compile>
<Compile Include="CreateArchive.xaml.cs">
<DependentUpon>CreateArchive.xaml</DependentUpon>
</Compile>
@ -131,6 +134,7 @@
<Compile Include="ExtractFile.xaml.cs">
<DependentUpon>ExtractFile.xaml</DependentUpon>
</Compile>
<Compile Include="GarConvert.cs" />
<Compile Include="GarCreate.cs" />
<Compile Include="GarExtract.cs" />
<Compile Include="HistoryStack.cs" />
@ -154,6 +158,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="ConvertImages.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="CreateArchive.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

158
GarConvert.cs Normal file
View File

@ -0,0 +1,158 @@
//! \file GarConvert.cs
//! \date Fri Aug 22 08:22:47 2014
//! \brief Game resources conversion methods.
//
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using GameRes;
using GARbro.GUI.Strings;
using GARbro.GUI.Properties;
using Ookii.Dialogs.Wpf;
namespace GARbro.GUI
{
public partial class MainWindow : Window
{
/// <summary>
/// Convert selected images to another format.
/// </summary>
void ConvertImageExec (object sender, ExecutedRoutedEventArgs e)
{
if (ViewModel.IsArchive)
return;
var source = CurrentDirectory.SelectedItems.Cast<EntryViewModel>()
.Where (f => f.Type == "image").Select (f => f.Name);
var convert_dialog = new ConvertImages();
convert_dialog.Owner = this;
var result = convert_dialog.ShowDialog() ?? false;
if (!result)
return;
var selected = convert_dialog.ImageConversionFormat.SelectedValue as string;
var format = FormatCatalog.Instance.ImageFormats.FirstOrDefault (f => f.Tag == selected);
if (null == format)
{
Trace.WriteLine ("Format is not selected", "ConvertImageExec");
return;
}
try
{
Directory.SetCurrentDirectory (ViewModel.Path);
var converter = new GarConvertImages (this);
converter.Convert (source, format);
}
catch (Exception X)
{
PopupError (X.Message, guiStrings.TextImageConvertError);
}
}
}
internal class GarConvertImages
{
private MainWindow m_main;
private ProgressDialog m_progress_dialog;
private IEnumerable<string> m_source;
private ImageFormat m_image_format;
private Exception m_pending_error;
public bool IgnoreErrors { get; set; }
public GarConvertImages (MainWindow parent)
{
m_main = parent;
}
public void Convert (IEnumerable<string> images, ImageFormat format)
{
m_main.StopWatchDirectoryChanges();
m_source = images;
m_image_format = format;
m_progress_dialog = new ProgressDialog ()
{
WindowTitle = guiStrings.TextTitle,
Text = "Converting image",
Description = "",
MinimizeBox = true,
};
m_progress_dialog.DoWork += ConvertWorker;
m_progress_dialog.RunWorkerCompleted += OnConvertComplete;
m_progress_dialog.ShowDialog (m_main);
}
void ConvertWorker (object sender, DoWorkEventArgs e)
{
m_pending_error = null;
try
{
string target_ext = m_image_format.Extensions.First();
int total = m_source.Count();
int i = 0;
foreach (var filename in m_source)
{
if (m_progress_dialog.CancellationPending)
throw new OperationCanceledException();
int progress = i++*100/total;
string target_name = Path.ChangeExtension (filename, target_ext);
if (filename == target_name)
continue;
string source_ext = Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant();
if (m_image_format.Extensions.Any (ext => ext == source_ext))
continue;
try
{
using (var file = File.OpenRead (filename))
{
m_progress_dialog.ReportProgress (progress, string.Format (guiStrings.MsgConvertingImage,
filename), target_name);
var image = ImageFormat.Read (file);
if (null == image)
continue;
try
{
using (var output = File.Create (target_name))
m_image_format.Write (output, image);
}
catch // delete destination file on conversion failure
{
File.Delete (target_name);
throw;
}
}
}
catch (Exception X)
{
if (!IgnoreErrors)
throw;
m_pending_error = X;
}
}
}
catch (Exception X)
{
m_pending_error = X;
}
}
void OnConvertComplete (object sender, RunWorkerCompletedEventArgs e)
{
m_main.ResumeWatchDirectoryChanges();
m_progress_dialog.Dispose();
if (null != m_pending_error)
{
if (m_pending_error is OperationCanceledException)
m_main.SetStatusText (m_pending_error.Message);
else
m_main.PopupError (m_pending_error.Message, guiStrings.TextImageConvertError);
}
m_main.Activate();
m_main.RefreshView();
}
}
}

View File

@ -90,6 +90,9 @@
-->
<MenuItem Header="{x:Static s:guiStrings.CtxMenuRefresh}" InputGestureText="F5"
Command="{x:Static local:Commands.Refresh}"/>
<MenuItem Header="{x:Static s:guiStrings.CtxMenuConvert}" InputGestureText="F6"
Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource booleanToCollapsedVisibilityConverter}}"
Command="{x:Static local:Commands.ConvertImage}" />
</ContextMenu>
</Window.Resources>
@ -356,6 +359,7 @@
<CommandBinding Command="{x:Static local:Commands.DeleteItem}" Executed="DeleteItemExec" CanExecute="CanExecuteOnPhysicalFile" />
<CommandBinding Command="{x:Static local:Commands.RenameItem}" Executed="RenameItemExec" CanExecute="CanExecuteInDirectory" />
<CommandBinding Command="{x:Static local:Commands.ExploreItem}" Executed="ExploreItemExec" CanExecute="CanExecuteInDirectory" />
<CommandBinding Command="{x:Static local:Commands.ConvertImage}" Executed="ConvertImageExec" CanExecute="CanExecuteOnImage" />
<CommandBinding Command="{x:Static local:Commands.SortBy}" Executed="SortByExec" CanExecute="CanExecuteAlways"/>
<CommandBinding Command="{x:Static local:Commands.GoBack}" Executed="GoBackExec" CanExecute="CanExecuteGoBack"/>
<CommandBinding Command="{x:Static local:Commands.GoForward}" Executed="GoForwardExec" CanExecute="CanExecuteGoForward"/>

View File

@ -289,12 +289,12 @@ namespace GARbro.GUI
m_watcher.EnableRaisingEvents = true;
}
void StopWatchDirectoryChanges()
public void StopWatchDirectoryChanges()
{
m_watcher.EnableRaisingEvents = false;
}
void ResumeWatchDirectoryChanges ()
public void ResumeWatchDirectoryChanges ()
{
m_watcher.EnableRaisingEvents = true;
}
@ -794,7 +794,7 @@ namespace GARbro.GUI
RefreshView();
}
private void RefreshView ()
public void RefreshView ()
{
m_app.ResetCache();
var pos = GetCurrentPosition();
@ -962,6 +962,12 @@ namespace GARbro.GUI
e.CanExecute = CurrentDirectory.SelectedIndex != -1;
}
private void CanExecuteOnImage (object sender, CanExecuteRoutedEventArgs e)
{
var entry = CurrentDirectory.SelectedItem as EntryViewModel;
e.CanExecute = !ViewModel.IsArchive && entry != null && entry.Type == "image";
}
private void CanExecuteInArchive (object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = ViewModel.IsArchive && CurrentDirectory.SelectedIndex != -1;
@ -1171,6 +1177,7 @@ namespace GARbro.GUI
public static readonly RoutedCommand DeleteItem = new RoutedCommand();
public static readonly RoutedCommand RenameItem = new RoutedCommand();
public static readonly RoutedCommand ExploreItem = new RoutedCommand();
public static readonly RoutedCommand ConvertImage = new RoutedCommand();
public static readonly RoutedCommand Refresh = new RoutedCommand();
public static readonly RoutedCommand Browse = new RoutedCommand();
public static readonly RoutedCommand FitWindow = new RoutedCommand();

View File

@ -297,5 +297,17 @@ namespace GARbro.GUI.Properties {
this["winStatusBarVisibility"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("PNG")]
public string appLastImageFormat {
get {
return ((string)(this["appLastImageFormat"]));
}
set {
this["appLastImageFormat"] = value;
}
}
}
}

View File

@ -71,5 +71,8 @@
<Setting Name="winStatusBarVisibility" Type="System.Windows.Visibility" Scope="User">
<Value Profile="(Default)">Visible</Value>
</Setting>
<Setting Name="appLastImageFormat" Type="System.String" Scope="User">
<Value Profile="(Default)">PNG</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -69,6 +69,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Convert.
/// </summary>
public static string ButtonConvert {
get {
return ResourceManager.GetString("ButtonConvert", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Extract.
/// </summary>
@ -96,6 +105,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Convert images....
/// </summary>
public static string CtxMenuConvert {
get {
return ResourceManager.GetString("CtxMenuConvert", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy.
/// </summary>
@ -285,6 +303,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Choose destination format.
/// </summary>
public static string LabelDestinationFormat {
get {
return ResourceManager.GetString("LabelDestinationFormat", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Extract files from {0} to.
/// </summary>
@ -420,6 +447,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Converting image {0}.
/// </summary>
public static string MsgConvertingImage {
get {
return ResourceManager.GetString("MsgConvertingImage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Creating archive {0}.
/// </summary>
@ -711,6 +747,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Convert images.
/// </summary>
public static string TextConvertImages {
get {
return ResourceManager.GetString("TextConvertImages", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Create archive.
/// </summary>
@ -774,6 +819,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Image conversion error.
/// </summary>
public static string TextImageConvertError {
get {
return ResourceManager.GetString("TextImageConvertError", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Archive parameters.
/// </summary>

View File

@ -378,4 +378,22 @@ Overwrite?</value>
<data name="MenuView" xml:space="preserve">
<value>_View</value>
</data>
<data name="CtxMenuConvert" xml:space="preserve">
<value>Convert images...</value>
</data>
<data name="ButtonConvert" xml:space="preserve">
<value>Convert</value>
</data>
<data name="LabelDestinationFormat" xml:space="preserve">
<value>Choose destination format</value>
</data>
<data name="TextConvertImages" xml:space="preserve">
<value>Convert images</value>
</data>
<data name="MsgConvertingImage" xml:space="preserve">
<value>Converting image {0}</value>
</data>
<data name="TextImageConvertError" xml:space="preserve">
<value>Image conversion error</value>
</data>
</root>

View File

@ -393,4 +393,22 @@
<data name="MenuView" xml:space="preserve">
<value>Просмотр</value>
</data>
<data name="CtxMenuConvert" xml:space="preserve">
<value>Конверсия изображений...</value>
</data>
<data name="ButtonConvert" xml:space="preserve">
<value>Преобразовать</value>
</data>
<data name="LabelDestinationFormat" xml:space="preserve">
<value>Формат преобразования</value>
</data>
<data name="TextConvertImages" xml:space="preserve">
<value>Преобразовать изображения</value>
</data>
<data name="MsgConvertingImage" xml:space="preserve">
<value>Преобразование файла {0}</value>
</data>
<data name="TextImageConvertError" xml:space="preserve">
<value>Ошибка конверсии изображения</value>
</data>
</root>