Skip to content

Commit

Permalink
tools/config: disable editing of enforced properties
Browse files Browse the repository at this point in the history
This makes enforced properties read-only inside the config tool, and
adds a banner to the top of the main window to inform the user why some
settings cannot be changed.
  • Loading branch information
lahm86 committed Nov 10, 2024
1 parent 3ca5456 commit c0583e1
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 4 deletions.
29 changes: 26 additions & 3 deletions tools/config/TRX_ConfigToolLib/Controls/PropertyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:TRX_ConfigToolLib.Controls"
xmlns:models="clr-namespace:TRX_ConfigToolLib.Models"
xmlns:utils="clr-namespace:TRX_ConfigToolLib.Utils"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">

<UserControl.Resources>
<ResourceDictionary Source="/TRX_ConfigToolLib;component/Resources/styles.xaml" />
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TRX_ConfigToolLib;component/Resources/styles.xaml" />
<ResourceDictionary>
<utils:BoolToVisibilityConverter
x:Key="BoolToCollapsedConverter"
FalseValue="Collapsed"
TrueValue="Visible"/>
<utils:BoolToVisibilityConverter
x:Key="InverseBoolToCollapsedConverter"
FalseValue="Visible"
TrueValue="Collapsed"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<StackPanel>
<StackPanel IsEnabled="{Binding IsEnabled}">
<TextBlock
Text="{Binding Title}"
Style="{StaticResource PropertyTitleStyle}"/>
Expand All @@ -25,7 +40,8 @@

<ContentControl
Content="{Binding}"
HorizontalAlignment="Left">
HorizontalAlignment="Left"
Visibility="{Binding IsEnabled, Converter={StaticResource BoolToCollapsedConverter}}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type models:BoolProperty}">
<CheckBox
Expand All @@ -52,5 +68,12 @@
</DataTemplate>
</ContentControl.Resources>
</ContentControl>

<TextBlock
Style="{StaticResource PropertyEnforcedStyle}"
Visibility="{Binding IsEnabled, Converter={StaticResource InverseBoolToCollapsedConverter}}">
<Run Text="{Binding DataContext.ViewText[label_enforced], RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
<Run Text="{Binding EnforcedValue}"/>
</TextBlock>
</StackPanel>
</UserControl>
19 changes: 18 additions & 1 deletion tools/config/TRX_ConfigToolLib/Controls/TRXConfigWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,28 @@
<Grid
Margin="7">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Border
Visibility="{Binding HasReadOnlyItems, Converter={StaticResource BoolToCollapsedConverter}}"
BorderThickness="1"
BorderBrush="#666"
Margin="0,0,0,7"
Background="Orange">
<StackPanel>
<TextBlock
HorizontalAlignment="Center"
Margin="5"
FontWeight="Bold"
Text="{Binding ViewText[label_locked_properties]}"/>
</StackPanel>
</Border>

<TabControl
Grid.Row="1"
Margin="0,0,0,7"
Padding="0"
IsEnabled="{Binding IsEditorActive}"
Expand All @@ -165,7 +182,7 @@
</TabControl>

<Grid
Grid.Row="1">
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
Expand Down
15 changes: 15 additions & 0 deletions tools/config/TRX_ConfigToolLib/Models/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void EditorPropertyChanged(object sender, PropertyChangedEventArgs e)
{
IsEditorDirty = _configuration.IsDataDirty();
IsEditorDefault = _configuration.IsDataDefault();
HasReadOnlyItems = _configuration.HasReadOnlyItems();
}

public void Load()
Expand Down Expand Up @@ -80,6 +81,20 @@ public bool IsEditorDefault
}
}

private bool _hasReadOnlyItems;
public bool HasReadOnlyItems
{
get => _hasReadOnlyItems;
set
{
if (_hasReadOnlyItems != value)
{
_hasReadOnlyItems = value;
NotifyPropertyChanged();
}
}
}

private string _selectedFile;
public string SelectedFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public abstract class BaseProperty : BaseNotifyPropertyChanged
{
private static readonly object _nullValue = new();

public string Field { get; set; }

public string Title
Expand All @@ -19,6 +21,23 @@ public string Description
public abstract void SetToDefault();
public abstract bool IsDefault { get; }

private object _enforcedValue = _nullValue;
public object EnforcedValue
{
get => _enforcedValue;
set
{
if (_enforcedValue != value)
{
_enforcedValue = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(IsEnabled));
}
}
}

public bool IsEnabled => _enforcedValue == _nullValue;

public virtual void Initialise(Specification specification)
{
SetToDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace TRX_ConfigToolLib.Models;

public class Configuration
{
private static readonly string _enforcedKey = "enforced";
private static readonly string _specificationPath = "Resources.specification.json";
private static readonly JsonSerializerSettings _serializerSettings = new()
{
Expand Down Expand Up @@ -70,13 +71,22 @@ public bool IsDataDefault()
return Properties.All(p => p.IsDefault);
}

public bool HasReadOnlyItems()
{
return Properties.Any(p => !p.IsEnabled);
}

public void Read(string jsonPath)
{
JObject externalData = File.Exists(jsonPath)
? JObject.Parse(File.ReadAllText(jsonPath))
: new();
JObject activeData = new();

JObject enforcedData = externalData.ContainsKey(_enforcedKey)
? externalData[_enforcedKey].ToObject<JObject>()
: null;

foreach (BaseProperty property in Properties)
{
if (externalData.ContainsKey(property.Field))
Expand All @@ -89,6 +99,10 @@ public void Read(string jsonPath)
property.SetToDefault();
}

if (enforcedData != null && enforcedData.ContainsKey(property.Field))
{
property.EnforcedValue = enforcedData[property.Field].ToString();
}
activeData[property.Field] = JToken.FromObject(property.ExportValue());
}

Expand Down
2 changes: 2 additions & 0 deletions tools/config/TRX_ConfigToolLib/Resources/Lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"menu_help": "_Help",
"command_github": "_GitHub",
"command_about": "_About",
"label_locked_properties": "This configuration set contains some read-only values defined by the game author.",
"label_enforced": "Value enforced to:",
"checkbox_enabled": "Enabled",
"spinner_msg_invalid_number": "The input string is not a valid number",
"spinner_msg_comparison_failed": "The input value must be between {0} and {1}",
Expand Down
2 changes: 2 additions & 0 deletions tools/config/TRX_ConfigToolLib/Resources/Lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"menu_help": "_Ayuda",
"command_github": "_GitHub",
"command_about": "_Acerca de",
"label_locked_properties": "Este conjunto de configuración contiene algunos valores de solo lectura definidos por el autor del juego.",
"label_enforced": "Valor aplicado a:",
"checkbox_enabled": "Habilitado",
"spinner_msg_invalid_number": "La cadena de entrada no es un número válido",
"spinner_msg_comparison_failed": "El valor de entrada debe estar entre {0} y {1}",
Expand Down
2 changes: 2 additions & 0 deletions tools/config/TRX_ConfigToolLib/Resources/Lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"menu_help": "_Aide",
"command_github": "_GitHub",
"command_about": "_A propos",
"label_locked_properties": "Cet ensemble de configuration contient certaines valeurs en lecture seule définies par l'auteur du jeu.",
"label_enforced": "Valeur imposée à:",
"checkbox_enabled": "Activé",
"spinner_msg_invalid_number": "La valeur n'est pas un nombre valide",
"spinner_msg_comparison_failed": "La valeur doit être entre {0} et {1}",
Expand Down
2 changes: 2 additions & 0 deletions tools/config/TRX_ConfigToolLib/Resources/Lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"menu_help": "_Aiuto",
"command_github": "_GitHub",
"command_about": "_Informazioni su",
"label_locked_properties": "Questo set di configurazione contiene alcuni valori di sola lettura definiti dall'autore del gioco.",
"label_enforced": "Valore applicato a:",
"checkbox_enabled": "Abilitato",
"spinner_msg_invalid_number": "Il valore immesso non è un numero valido",
"spinner_msg_comparison_failed": "Il valore immesso deve essere compreso tra {0} e {1}",
Expand Down
39 changes: 39 additions & 0 deletions tools/config/TRX_ConfigToolLib/Resources/styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<Setter
Property="Margin"
Value="0,0,0,2" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
Expand All @@ -77,6 +82,25 @@
<Setter
Property="Margin"
Value="0,0,0,10" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
x:Key="PropertyEnforcedStyle"
TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontWeight" Value="Bold"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
Expand All @@ -85,6 +109,11 @@
<Setter
Property="VerticalContentAlignment"
Value="Center" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
Expand All @@ -93,6 +122,11 @@
<Setter
Property="MinWidth"
Value="150" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
Expand All @@ -101,6 +135,11 @@
<Setter
Property="MinWidth"
Value="100" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.6"/>
</Trigger>
</Style.Triggers>
</Style>

<Style
Expand Down

0 comments on commit c0583e1

Please sign in to comment.