/// Game Resource browser
//
// Copyright (C) 2018 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using GameRes;
using GARbro.GUI.Properties;
using GARbro.GUI.Strings;
namespace GARbro.GUI
{
///
/// Interaction logic for SettingsWindow.xaml
///
public partial class SettingsWindow : Window
{
public SettingsWindow ()
{
InitializeComponent();
this.DataContext = this.ViewModel = CreateSettingsTree();
this.Closing += (s, e) => {
var section = SectionsPane.SelectedItem as SettingsSectionView;
if (section != null)
LastSelectedSection = section.Label;
};
}
static readonly IEnumerable ViewerSettings = new [] {
MainWindow.DownScaleImage,
};
SettingsViewModel ViewModel;
static string LastSelectedSection = null;
private void OnSectionChanged (object sender, System.Windows.RoutedEventArgs e)
{
this.SettingsPane.Child = null;
var section = SectionsPane.SelectedValue as SettingsSectionView;
if (section != null && section.Panel != null)
this.SettingsPane.Child = section.Panel;
}
private void Button_ClickApply (object sender, System.Windows.RoutedEventArgs e)
{
ApplyChanges();
}
private void Button_ClickOk (object sender, System.Windows.RoutedEventArgs e)
{
ApplyChanges();
DialogResult = true;
}
private void ApplyChanges ()
{
if (!ViewModel.HasChanges)
return;
if (OnApplyChanges != null)
OnApplyChanges (this, EventArgs.Empty);
ViewModel.HasChanges = false;
}
private SettingsViewModel CreateSettingsTree ()
{
SettingsSectionView[] list = {
new SettingsSectionView {
Label = guiStrings.TextViewer,
Panel = CreateSectionPanel (ViewerSettings)
},
new SettingsSectionView {
Label = guiStrings.TextFormats,
Children = EnumerateFormatsSettings(),
},
};
SettingsSectionView selected_section = null;
if (LastSelectedSection != null)
selected_section = EnumerateSections (list).FirstOrDefault (s => s.Label == LastSelectedSection);
if (null == selected_section)
selected_section = list[0];
selected_section.IsSelected = true;
return new SettingsViewModel { Root = list };
}
IEnumerable EnumerateFormatsSettings ()
{
var list = new List();
var formats = FormatCatalog.Instance.Formats.Where (f => f.Settings != null && f.Settings.Any());
foreach (var format in formats.OrderBy (f => f.Tag))
{
var pane = CreateSectionPanel (format.Settings);
if (pane.Children.Count > 0)
{
var section = new SettingsSectionView {
Label = format.Tag,
SectionTitle = guiStrings.TextFormats+" :: "+format.Tag,
Panel = pane
};
list.Add (section);
}
}
return list;
}
Panel CreateSectionPanel (IEnumerable settings)
{
var pane = new WrapPanel();
foreach (var setting in settings)
{
var widget = CreateSettingWidget (setting, setting.Value);
if (widget != null)
pane.Children.Add (widget);
}
return pane;
}
UIElement CreateCheckBoxWidget (IResourceSetting setting)
{
return new CheckBox {
Template = (ControlTemplate)this.Resources["BoundCheckBox"],
DataContext = CreateSettingView (setting),
};
}
UIElement CreateEncodingWidget (IResourceSetting setting)
{
var view = CreateSettingView (setting);
// XXX make a control template in XAML instead
var container = new StackPanel {
Orientation = Orientation.Vertical,
Margin = new Thickness (2.0),
DataContext = view,
};
var caption = new TextBlock {
Text = view.Text,
ToolTip = view.Description,
};
var combo_box = new ComboBox {
ItemsSource = MainWindow.GetEncodingList (true),
Margin = new Thickness (0,4,0,0),
DisplayMemberPath = "EncodingName",
ToolTip = view.Description,
};
var binding = new Binding ("Value") {
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
};
BindingOperations.SetBinding (combo_box, ComboBox.SelectedItemProperty, binding);
container.Children.Add (caption);
container.Children.Add (combo_box);
return container;
}
UIElement CreateGaugeWidget (FixedGaugeSetting setting)
{
return new Slider {
Template = (ControlTemplate)this.Resources["BoundSlider"],
DataContext = CreateSettingView (setting),
Ticks = new DoubleCollection (setting.ValuesSet.Select (x => (double)x)),
};
}
UIElement CreateDropDownWidget (FixedSetSetting setting)
{
return new ComboBox {
Template = (ControlTemplate)this.Resources["BoundDropDownList"],
DataContext = CreateSettingView