Skip to content

Commit cc25e44

Browse files
committed
UI for scaling textures on export
1 parent 738cbe4 commit cc25e44

File tree

4 files changed

+218
-25
lines changed

4 files changed

+218
-25
lines changed

Controls/Metadata.xaml

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,84 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:DolphinDynamicInputTextureCreator.Controls"
7+
xmlns:converters="clr-namespace:DolphinDynamicInputTextureCreator.ValueConverters"
78
mc:Ignorable="d"
8-
Height="220" Width="320">
9-
<Grid Background="White">
9+
Height="200" Width="320">
10+
<UserControl.Resources>
11+
<ResourceDictionary>
12+
13+
</ResourceDictionary>
14+
</UserControl.Resources>
15+
<Grid Grid.Row="0" Background="White">
1016
<Grid.RowDefinitions>
11-
<RowDefinition Height="*" />
12-
<RowDefinition Height="25"/>
17+
<RowDefinition Height="auto" />
18+
<RowDefinition Height="auto" />
19+
<RowDefinition Height="auto" />
20+
<RowDefinition Height="auto" />
21+
<RowDefinition Height="auto" />
1322
</Grid.RowDefinitions>
14-
<Grid Grid.Row="0">
15-
<Grid.RowDefinitions>
16-
<RowDefinition Height="*" />
17-
<RowDefinition Height="*" />
18-
<RowDefinition Height="*" />
19-
<RowDefinition Height="*" />
20-
<RowDefinition Height="*" />
21-
</Grid.RowDefinitions>
22-
<Grid.ColumnDefinitions>
23-
<ColumnDefinition Width="150"/>
24-
<ColumnDefinition Width="*"/>
25-
</Grid.ColumnDefinitions>
26-
<Label Content="Output json name:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
27-
<TextBox Text="{Binding GeneratedJsonName, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0"/>
28-
<Label Content="Generated folder name:" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
29-
<TextBox Text="{Binding GeneratedFolderName, Mode=TwoWay}" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0"/>
30-
<Label Content="Preserve aspect ratio:" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
31-
<CheckBox IsChecked="{Binding PreserveAspectRatio, Mode=TwoWay}" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0"/>
32-
<Label Content="Game ID:" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
33-
<TextBox Text="{Binding GameID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="3" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0"/>
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="150"/>
25+
<ColumnDefinition Width="*"/>
26+
</Grid.ColumnDefinitions>
27+
<Label Content="Output json name:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
28+
<TextBox Text="{Binding GeneratedJsonName, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,6"/>
29+
<Label Content="Generated folder name:" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
30+
<TextBox Text="{Binding GeneratedFolderName, Mode=TwoWay}" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,6"/>
31+
<Label Content="Preserve aspect ratio:" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
32+
<CheckBox IsChecked="{Binding PreserveAspectRatio, Mode=TwoWay}" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,6"/>
33+
<Label Content="Game ID:" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
34+
<TextBox Text="{Binding GameID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="3" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,6"/>
35+
36+
<GroupBox Header="Export Texture Scaling" Grid.Row="4" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="Gray">
37+
<Grid>
38+
<Grid.ColumnDefinitions>
39+
<ColumnDefinition Width="auto"/>
40+
<ColumnDefinition Width="auto"/>
41+
</Grid.ColumnDefinitions>
42+
<Grid.RowDefinitions>
43+
<RowDefinition Height="*" />
44+
<RowDefinition Height="*" />
45+
</Grid.RowDefinitions>
46+
<StackPanel Orientation="Horizontal">
47+
<Label Content="Mode:" VerticalAlignment="Center"/>
48+
<ComboBox ItemsSource="{Binding ExportTextureScaling.ScalingModesHelper}" SelectedItem="{Binding ExportTextureScaling.SelectedScalingModeHelper}" MinWidth="125" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2,6"/>
49+
</StackPanel>
50+
51+
<DockPanel Grid.Column="1">
52+
53+
<DockPanel.Style>
54+
<Style TargetType="{x:Type DockPanel}">
55+
<Setter Property="IsEnabled" Value="False"/>
56+
<Style.Triggers>
57+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="NearestNeighbor">
58+
<Setter Property="IsEnabled" Value="True"/>
59+
</DataTrigger>
60+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="Bicubic">
61+
<Setter Property="IsEnabled" Value="True"/>
62+
</DataTrigger>
63+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="Bilinear">
64+
<Setter Property="IsEnabled" Value="True"/>
65+
</DataTrigger>
66+
</Style.Triggers>
67+
</Style>
68+
</DockPanel.Style>
3469

35-
</Grid>
70+
<StackPanel Orientation="Horizontal" Grid.Column="1" Margin="5,0">
71+
<Label Content="Factor:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" />
72+
<ComboBox ItemsSource="{Binding ExportTextureScaling.ScalingFactorHelper}" SelectedItem="{Binding ExportTextureScaling.SelectedScalingFactor}" MinWidth="50" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0">
73+
<ComboBox.ItemTemplate>
74+
<DataTemplate>
75+
<StackPanel Orientation="Horizontal">
76+
<TextBlock VerticalAlignment="Center" Text="{Binding}" />
77+
<TextBlock VerticalAlignment="Center" Text="x" />
78+
</StackPanel>
79+
</DataTemplate>
80+
</ComboBox.ItemTemplate>
81+
</ComboBox>
82+
</StackPanel>
83+
</DockPanel>
84+
</Grid>
85+
</GroupBox>
3686
</Grid>
3787
</UserControl>

ViewModels/Dialogs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public static bool DialogExportToLocation(in DynamicInputPackViewModel pack)
125125
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
126126
{
127127
pack.ExportToLocation(dialog.SelectedPath);
128+
// Updating the user interface in case the image file has changed.
129+
pack.Textures.Select(pack.Textures.Selected);
128130
return true;
129131
}
130132
return false;

ViewModels/DynamicInputPackViewModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,28 @@ public InputRegion SelectedRegion
118118
}
119119
private InputRegion _selected_region;
120120

121+
/// <summary>
122+
/// Options used to rescale textures during export.
123+
/// </summary>
124+
public ExportTextureScalingViewModel ExportTextureScaling
125+
{
126+
get
127+
{
128+
if (_export_texture_scaling == null)
129+
{
130+
_export_texture_scaling = new ExportTextureScalingViewModel();
131+
if (Textures.ValidSelection)
132+
{
133+
ExportTextureScaling.SelectedScalingFactor = (int)Textures.Selected.ImageWidthScaling;
134+
}
135+
}
136+
return _export_texture_scaling;
137+
}
138+
}
139+
public ExportTextureScalingViewModel _export_texture_scaling;
140+
121141
#endregion
142+
122143
#region Commands
123144

124145
#region SelectedTexture
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using DolphinDynamicInputTexture.Data;
2+
using System;
3+
using System.Drawing;
4+
using System.Drawing.Drawing2D;
5+
6+
namespace DolphinDynamicInputTextureCreator.ViewModels
7+
{
8+
public class ExportTextureScalingViewModel : Other.PropertyChangedBase
9+
{
10+
/// <summary>
11+
/// Possible texture scaling modes.
12+
/// </summary>
13+
internal enum Modes
14+
{
15+
None, NearestNeighbor, Bicubic, Bilinear
16+
}
17+
18+
/// <summary>
19+
/// The currently selected export texture scaling mode.
20+
/// </summary>
21+
internal Modes SelectedScalingMode
22+
{
23+
get => _selected_scaling_mode;
24+
set
25+
{
26+
_selected_scaling_mode = value;
27+
SetExportTextureScaling();
28+
OnPropertyChanged(nameof(SelectedScalingMode));
29+
}
30+
}
31+
private Modes _selected_scaling_mode = Modes.None;
32+
33+
/// <summary>
34+
/// Scaling factor that the exported image should have.
35+
/// </summary>
36+
public int SelectedScalingFactor { get; set; } = 4;
37+
38+
#region UI Helper
39+
40+
public string[] ScalingModesHelper
41+
{
42+
get => Enum.GetNames(typeof(Modes));
43+
}
44+
45+
public string SelectedScalingModeHelper
46+
{
47+
get => SelectedScalingMode.ToString();
48+
set
49+
{
50+
SelectedScalingMode = Enum.Parse<Modes>(value);
51+
OnPropertyChanged(nameof(SelectedScalingModeHelper));
52+
}
53+
}
54+
55+
public int[] ScalingFactorHelper => new int[] { 1, 2, 3, 4, 5, 6, 8, 10 };
56+
57+
#endregion UI Helper
58+
59+
/// <summary>
60+
/// set the export scale for each texture.
61+
/// </summary>
62+
private void SetExportTextureScaling()
63+
{
64+
switch (SelectedScalingMode)
65+
{
66+
case Modes.None:
67+
DynamicInputTextureEvents.DynamicInputTextureExportProcessor = null;
68+
break;
69+
70+
case Modes.NearestNeighbor:
71+
case Modes.Bicubic:
72+
case Modes.Bilinear:
73+
DynamicInputTextureEvents.DynamicInputTextureExportProcessor = DefaultScalingProcessor;
74+
break;
75+
76+
default:
77+
break;
78+
}
79+
}
80+
81+
public bool DefaultScalingProcessor(string savepath, DynamicInputTexture dynamicinputtexture)
82+
{
83+
int Scaling = SelectedScalingFactor;
84+
85+
//Should we use scaling?
86+
if (Scaling == dynamicinputtexture.ImageWidthScaling) return false;
87+
88+
//The actual scaling.
89+
using (Bitmap newImage = new Bitmap(dynamicinputtexture.HashProperties.ImageWidth * Scaling, dynamicinputtexture.HashProperties.ImageHeight * Scaling))
90+
{
91+
using (Bitmap Image = new Bitmap(dynamicinputtexture.TexturePath))
92+
using (Graphics graphics = Graphics.FromImage(newImage))
93+
{
94+
switch (SelectedScalingMode)
95+
{
96+
case Modes.NearestNeighbor:
97+
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
98+
break;
99+
100+
case Modes.Bicubic:
101+
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
102+
break;
103+
104+
case Modes.Bilinear:
105+
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
106+
break;
107+
108+
default:
109+
break;
110+
}
111+
112+
graphics.DrawImage(Image, 0, 0, newImage.Width, newImage.Height);
113+
}
114+
newImage.Save(savepath, System.Drawing.Imaging.ImageFormat.Png);
115+
}
116+
117+
return true;
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)