Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Changes without Reloading (and other fixes) #1

Merged
merged 13 commits into from
Aug 18, 2022
Prev Previous commit
Next Next commit
Fixed being able to name two properties the same
  • Loading branch information
LukasLichten committed Aug 17, 2022
commit 66d03d5572951a53a627eb447609ac82c91fadf8
2 changes: 1 addition & 1 deletion Property.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<styles:SHExpander.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBox x:Name="txtPropertyName" Text="{Binding PropertyName}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Property}}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0" Width="Auto" MinWidth="100"/>
<TextBox x:Name="txtPropertyName" Text="{Binding PropertyName}" BorderBrush="{Binding BorderBrush}" DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Property}}}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0" Width="Auto" MinWidth="100"/>
<styles:SHButtonSecondary x:Name="btnDeleteProperty" Command="{x:Static local:SettingsControl.DeletePropertyCommand}" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Property}}}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0">Delete</styles:SHButtonSecondary>
</Grid>
</DataTemplate>
Expand Down
8 changes: 8 additions & 0 deletions SettingsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ private void btnExport_Click(object sender, System.Windows.RoutedEventArgs e)

private void btnApply_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (Plugin.CheckForCollisions(false))
{
//Collisions, so we don't apply the update
System.Windows.MessageBox.Show("There are properties with the same name, please rename one!","Name Collision", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
return;
}


Plugin.PluginManager.ClearActions(Plugin.GetType());
Plugin.PluginManager.ClearProperties(Plugin.GetType());

Expand Down
76 changes: 74 additions & 2 deletions SwitchablePropertiesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Linq;
using System.Windows.Media;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SwitchableProperties
{
Expand All @@ -32,6 +34,8 @@ public void DataUpdate(PluginManager pluginManager, ref GameData data)

public void End(PluginManager pluginManager)
{
CheckForCollisions(true);

// Save settings
File.WriteAllText(PluginManager.GetCommonStoragePath(this.GetType().Name + ".GeneralSettings.json"),
JsonConvert.SerializeObject(Settings, new JsonSerializerSettings
Expand Down Expand Up @@ -158,6 +162,44 @@ internal void GenerateBinds()
}
}
}

internal bool CheckForCollisions(bool fixDublicate)
{
if (Settings.Properties.Count == 0)
return false;

bool existDublicate = false;

HashSet<string> names = new HashSet<string>();

foreach (SwitchableProperty property in Settings.Properties)
{
if (names.Contains(property.PropertyName))
{
existDublicate = true;

if (fixDublicate)
{
int i;
for (i = 0; names.Contains($"{names.Contains(property.PropertyName)}_{i}") && i < Int32.MaxValue; i++) ;

if (i != Int32.MaxValue)
property.PropertyName = $"{property.PropertyName}_{i}";
}
else
{
property.BorderBrush = Brushes.Red;
}
}
else
{
names.Add(property.PropertyName);
property.BorderBrush = Brushes.Black;
}
}

return existDublicate;
}
}

internal class SwitchablePropertyContainer
Expand Down Expand Up @@ -217,10 +259,40 @@ private int GetIndexOfBind()
}
}

public class SwitchableProperty
public class SwitchableProperty : INotifyPropertyChanged
{
public string PropertyName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;

private string _propertyName;

public string PropertyName
{
get => _propertyName;
set
{
_propertyName = value;
OnPropertyChanged();
}
}

[JsonIgnore]
public Brush BorderBrush
{
get => _borderBrush;
set
{
_borderBrush = value;
OnPropertyChanged();
}
}

private Brush _borderBrush = Brushes.Black;
public ObservableCollection<SwitchableBind> Binds { get; set; }

protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public abstract class SwitchableBind
Expand Down