Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
Implemented GUI for string mod settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artentus committed May 22, 2019
1 parent 564737f commit a95acd6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
23 changes: 23 additions & 0 deletions ModMyFactory/ModMyFactory/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@
</DataTemplate>

<DataTemplate x:Key="StringModSettingTemplate" DataType="{x:Type modsettings:IModSetting}">
<StackPanel Margin="4" Orientation="Horizontal" FlowDirection="LeftToRight">
<TextBlock Margin="2,0,4,0" VerticalAlignment="Center">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SettingNameConverter}">
<Binding Path="Name"/>
<Binding Path="Owner"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<controls:ExtendedTextBox Text="{Binding Value, Mode=TwoWay}" DefaultText="{Binding DefaultValue}" AllowEmptyText="{Binding AllowEmptyValue}" Width="200" Padding="2"/>
<Button Margin="2,0,0,0" MinWidth="30" MinHeight="0" Height="23" Command="{Binding ResetCommand}" ToolTip="{DynamicResource DefaultValueToolTip}">
<Button.Content>
<TextBlock Style="{StaticResource TextBlockButtonStyle}" Text="{DynamicResource DefaultValueButton}"/>
</Button.Content>
</Button>
</StackPanel>
</DataTemplate>

<DataTemplate x:Key="StringListModSettingTemplate" DataType="{x:Type modsettings:IModSetting}">
Expand Down Expand Up @@ -211,6 +227,13 @@
</Style.Setters>
</Style>

<Style x:Key="{x:Type controls:ExtendedTextBox}" TargetType="{x:Type controls:ExtendedTextBox}">
<Style.Setters>
<Setter Property="Background" Value="{DynamicResource MutedBackgroundBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
</Style.Setters>
</Style>

<Style x:Key="{x:Type PasswordBox}" TargetType="{x:Type PasswordBox}">
<Style.Setters>
<Setter Property="Background" Value="{DynamicResource MutedBackgroundBrush}"/>
Expand Down
66 changes: 66 additions & 0 deletions ModMyFactory/ModMyFactory/Controls/ExtendedTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Windows;
using System.Windows.Controls;

namespace ModMyFactory.Controls
{
class ExtendedTextBox : TextBox
{
const string DefaultDefaultText = "Default";

public static readonly DependencyProperty AllowEmptyTextProperty = DependencyProperty.Register("AllowEmptyText", typeof(bool), typeof(ExtendedTextBox), new PropertyMetadata(true, OnAllowEmptyTextChanged));
public static readonly DependencyProperty DefaultTextProperty = DependencyProperty.Register("DefaultText", typeof(string), typeof(ExtendedTextBox), new PropertyMetadata(DefaultDefaultText, OnDefaultTextChanged, CoerceDefaultText));

private static void OnAllowEmptyTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var etb = (ExtendedTextBox)sender;
if (!(bool)e.NewValue && string.IsNullOrEmpty(etb.Text)) etb.Text = etb.DefaultText;
etb.OnAllowEmptyTextChanged(e);
}

private static void OnDefaultTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var etb = (ExtendedTextBox)sender;
etb.OnDefaultTextChanged(e);
}

private static object CoerceDefaultText(DependencyObject sender, object value)
{
var s = (string)value;
if (string.IsNullOrEmpty(s)) return DefaultDefaultText;
else return s;
}


public event DependencyPropertyChangedEventHandler AllowEmptyTextChanged;
public event DependencyPropertyChangedEventHandler DefaultTextChanged;

public bool AllowEmptyText
{
get => (bool)GetValue(AllowEmptyTextProperty);
set => SetValue(AllowEmptyTextProperty, value);
}

public string DefaultText
{
get => (string)GetValue(DefaultTextProperty);
set => SetValue(DefaultTextProperty, value);
}

protected virtual void OnAllowEmptyTextChanged(DependencyPropertyChangedEventArgs e)
{
AllowEmptyTextChanged?.Invoke(this, e);
}

protected virtual void OnDefaultTextChanged(DependencyPropertyChangedEventArgs e)
{
DefaultTextChanged?.Invoke(this, e);
}

protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);

if (!AllowEmptyText && string.IsNullOrEmpty(Text)) Text = DefaultText;
}
}
}
1 change: 1 addition & 0 deletions ModMyFactory/ModMyFactory/ModMyFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="CommandLine.cs" />
<Compile Include="Controls\ExtendedTextBox.cs" />
<Compile Include="Controls\NumericUpDown.xaml.cs">
<DependentUpon>NumericUpDown.xaml</DependentUpon>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion ModMyFactory/ModMyFactory/Models/ModLocale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private string GetValue(string section, string key)
if (!string.IsNullOrEmpty(section))
completeKey = string.Concat(section, data.SectionKeySeparator, key);

return data.TryGetKey(completeKey, out string result) ? result : null;
return data.TryGetKey(completeKey, out string result) ? result : key;
}

public string GetValue(string key, LocaleType type)
Expand Down

0 comments on commit a95acd6

Please sign in to comment.