Skip to content

Commit e08dcc5

Browse files
committed
UI for scaling textures on export
1 parent 213da48 commit e08dcc5

File tree

4 files changed

+252
-24
lines changed

4 files changed

+252
-24
lines changed

Controls/Metadata.xaml

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,85 @@
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"
89
Height="220" Width="320">
9-
<Grid Background="White">
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.RowDefinitions>
39+
<RowDefinition Height="*" />
40+
<RowDefinition Height="*" />
41+
</Grid.RowDefinitions>
42+
<StackPanel Orientation="Horizontal">
43+
<Label Content="Mode:" VerticalAlignment="Center"/>
44+
<ComboBox ItemsSource="{Binding ExportTextureScaling.ScalingModesHelper}" SelectedItem="{Binding ExportTextureScaling.SelectedScalingModeHelper}" MinWidth="125" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="2,6"/>
45+
</StackPanel>
46+
47+
<DockPanel Grid.Row="1">
48+
49+
<DockPanel.Style>
50+
<Style TargetType="{x:Type DockPanel}">
51+
<Setter Property="IsEnabled" Value="False"/>
52+
<Style.Triggers>
53+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="NearestNeighbor">
54+
<Setter Property="IsEnabled" Value="True"/>
55+
</DataTrigger>
56+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="Bicubic">
57+
<Setter Property="IsEnabled" Value="True"/>
58+
</DataTrigger>
59+
<DataTrigger Binding="{Binding ExportTextureScaling.SelectedScalingModeHelper}" Value="Bilinear">
60+
<Setter Property="IsEnabled" Value="True"/>
61+
</DataTrigger>
62+
</Style.Triggers>
63+
</Style>
64+
</DockPanel.Style>
65+
66+
<StackPanel Orientation="Horizontal">
67+
<Label Content="Change size:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" />
68+
<ComboBox ItemsSource="{Binding ExportTextureScaling.ChangeSizeHelper}" SelectedItem="{Binding ExportTextureScaling.SelectedChangeSizeHelper}" MinWidth="70" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0"/>
69+
</StackPanel>
3470

35-
</Grid>
71+
<StackPanel Orientation="Horizontal" Grid.Column="1" Margin="5,0">
72+
<Label Content="Factor:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" />
73+
<ComboBox ItemsSource="{Binding ExportTextureScaling.ScalingFactorHelper}" SelectedItem="{Binding ExportTextureScaling.SelectedScalingFactor}" MinWidth="50" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="4,0">
74+
<ComboBox.ItemTemplate>
75+
<DataTemplate>
76+
<StackPanel Orientation="Horizontal">
77+
<TextBlock VerticalAlignment="Center" Text="{Binding}" />
78+
<TextBlock VerticalAlignment="Center" Text="x" />
79+
</StackPanel>
80+
</DataTemplate>
81+
</ComboBox.ItemTemplate>
82+
</ComboBox>
83+
</StackPanel>
84+
</DockPanel>
85+
</Grid>
86+
</GroupBox>
3687
</Grid>
3788
</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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ public bool ShouldGetHashFromTextureFilename
132132
}
133133
}
134134

135+
public ExportTextureScalingViewModel ExportTextureScaling { get; } = new ExportTextureScalingViewModel();
136+
135137
#endregion
138+
136139
#region Commands
137140

138141
#region SelectedTexture
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
internal enum Change
19+
{
20+
Always, Reduce_only, Enlarge_only
21+
}
22+
23+
/// <summary>
24+
/// The currently selected export texture scaling mode.
25+
/// </summary>
26+
internal Modes SelectedScalingMode
27+
{
28+
get => _selected_scaling_mode;
29+
set
30+
{
31+
_selected_scaling_mode = value;
32+
SetExportTextureScaling();
33+
OnPropertyChanged(nameof(SelectedScalingMode));
34+
}
35+
}
36+
private Modes _selected_scaling_mode = Modes.None;
37+
38+
internal Change SelectedChangeSize = Change.Always;
39+
40+
/// <summary>
41+
/// Scaling factor that the exported image should have.
42+
/// </summary>
43+
public int SelectedScalingFactor { get; set; } = 4;
44+
45+
/// <summary>
46+
/// Number of pixels that the smallest region should have at least after exporting, when the Dynamic mode is used.
47+
/// </summary>
48+
public int SelectedRegioneSize { get; set; } = 96;
49+
50+
#region UI Helper
51+
52+
public string[] ScalingModesHelper
53+
{
54+
get => Enum.GetNames(typeof(Modes));
55+
}
56+
57+
public string SelectedScalingModeHelper
58+
{
59+
get => SelectedScalingMode.ToString();
60+
set
61+
{
62+
SelectedScalingMode = Enum.Parse<Modes>(value);
63+
OnPropertyChanged(nameof(SelectedScalingModeHelper));
64+
}
65+
}
66+
67+
public int[] ScalingFactorHelper => new int[] { 1, 2, 3, 4, 5, 6, 8, 10 };
68+
69+
public string[] ChangeSizeHelper
70+
{
71+
get
72+
{
73+
if (_change_size_helper == null)
74+
{
75+
_change_size_helper = Enum.GetNames(typeof(Change));
76+
for (int i = 0; i < _change_size_helper.Length; i++)
77+
{
78+
_change_size_helper[i] = _change_size_helper[i].Replace('_', ' ');
79+
}
80+
}
81+
return _change_size_helper;
82+
}
83+
}
84+
private string[] _change_size_helper;
85+
86+
public string SelectedChangeSizeHelper
87+
{
88+
get => SelectedChangeSize.ToString().Replace('_', ' ');
89+
set
90+
{
91+
SelectedChangeSize = Enum.Parse<Change>(value.Replace(' ', '_'));
92+
OnPropertyChanged(nameof(SelectedChangeSizeHelper));
93+
}
94+
}
95+
96+
#endregion UI Helper
97+
98+
/// <summary>
99+
/// set the export scale for each texture.
100+
/// </summary>
101+
private void SetExportTextureScaling()
102+
{
103+
switch (SelectedScalingMode)
104+
{
105+
case Modes.None:
106+
DynamicInputTextureEvents.DynamicInputTextureExportProcessor = null;
107+
break;
108+
109+
case Modes.NearestNeighbor:
110+
case Modes.Bicubic:
111+
case Modes.Bilinear:
112+
DynamicInputTextureEvents.DynamicInputTextureExportProcessor = DefaultScalingProcessor;
113+
break;
114+
115+
default:
116+
break;
117+
}
118+
}
119+
120+
public bool DefaultScalingProcessor(string savepath, DynamicInputTexture dynamicinputtexture)
121+
{
122+
int Scaling = SelectedScalingFactor;
123+
124+
//Should we use scaling?
125+
switch (SelectedChangeSize)
126+
{
127+
case Change.Reduce_only:
128+
if (Scaling >= dynamicinputtexture.ImageWidthScaling) return false;
129+
break;
130+
131+
case Change.Enlarge_only:
132+
if (Scaling <= dynamicinputtexture.ImageWidthScaling) return false;
133+
break;
134+
135+
default:
136+
if (Scaling == dynamicinputtexture.ImageWidthScaling) return false;
137+
break;
138+
}
139+
140+
//The actual scaling.
141+
using (Bitmap newImage = new Bitmap(dynamicinputtexture.HashProperties.ImageWidth * Scaling, dynamicinputtexture.HashProperties.ImageHeight * Scaling))
142+
{
143+
using (Bitmap Image = new Bitmap(dynamicinputtexture.TexturePath))
144+
using (Graphics graphics = Graphics.FromImage(newImage))
145+
{
146+
switch (SelectedScalingMode)
147+
{
148+
case Modes.NearestNeighbor:
149+
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
150+
break;
151+
152+
case Modes.Bicubic:
153+
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
154+
break;
155+
156+
case Modes.Bilinear:
157+
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
158+
break;
159+
160+
default:
161+
break;
162+
}
163+
164+
graphics.DrawImage(Image, 0, 0, newImage.Width, newImage.Height);
165+
}
166+
newImage.Save(savepath, System.Drawing.Imaging.ImageFormat.Png);
167+
}
168+
169+
return true;
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)