Skip to content

Commit

Permalink
Update Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Jan 3, 2024
1 parent 7ec933a commit 1c0f8f5
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/HUDMerger/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace HUDMerger.Models;

public class Settings
{
public required string TeamFortress2Folder { get; init; }
public required string Language { get; init; }
public required string TeamFortress2Folder { get; set; }
public required string Language { get; set; }
}
17 changes: 2 additions & 15 deletions src/HUDMerger/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HUDMerger"
xmlns:Properties="clr-namespace:HUDMerger.Properties"
xmlns:views="clr-namespace:HUDMerger.Views"
mc:Ignorable="d"
Title="Settings"
Width="600"
Expand All @@ -14,19 +14,6 @@
Icon="Resources\favicon.ico"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label FontSize="22" Grid.Row="0" Margin="10,0,0,0">Settings</Label>
<StackPanel Name="SettingsContainer" Grid.Row="1" Margin="10,0,10,10">
<Label Content="Team Fortress 2 Folder" FontSize="15" ToolTip="Path to Team Fortress 2 Installation. Used for extracting required HUD files." />
<TextBox Style="{StaticResource TextBoxStyle1}" HorizontalAlignment="Left" MinWidth="480" Text="{Binding TeamFortress2Folder, Source={x:Static Properties:Settings.Default}, Mode=TwoWay}" />
</StackPanel>
<WrapPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Row="2" Margin="10,5,10,10">
<Button Style="{StaticResource EnabledButton}" FontSize="14" Padding="20,8" Click="CancelButton_Click" Margin="0,0,10,0" IsCancel="True">Cancel</Button>
<Button Style="{StaticResource AccentButton}" FontSize="14" Padding="20,8" Click="ApplyButton_Click">Apply</Button>
</WrapPanel>
<views:SettingsWindowView />
</Grid>
</Window>
15 changes: 0 additions & 15 deletions src/HUDMerger/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace HUDMerger;

Expand All @@ -15,16 +12,4 @@ public SettingsWindow()
{
InitializeComponent();
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Reload();
Close();
}

private void ApplyButton_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Save();
Close();
}
}
23 changes: 22 additions & 1 deletion src/HUDMerger/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public MainWindowViewModel()
{
LoadSourceHUDCommand = new RelayCommand(LoadSourceHUD);
LoadTargetHUDCommand = new RelayCommand(LoadTargetHUD);
ShowSettingsWindowCommand = new RelayCommand(() => ShowWindow(new SettingsWindow()));
ShowSettingsWindowCommand = new RelayCommand(ShowSettingsWindow);
QuitCommand = new RelayCommand(Application.Current.Shutdown);

ShowAboutWindowCommand = new RelayCommand(() => ShowWindow(new AboutWindow()));
Expand All @@ -96,6 +96,27 @@ public MainWindowViewModel()
MergeCommand = new MergeCommand(this);
}

private void ShowSettingsWindow()
{
using SettingsWindowViewModel settingsWindowViewModel = new();

SettingsWindow settingsWindow = new()
{
DataContext = settingsWindowViewModel,
Owner = Application.Current.MainWindow
};

void OnClose(object? sender, EventArgs args)
{
settingsWindowViewModel.Close -= OnClose;
settingsWindow.Close();
}

settingsWindowViewModel.Close += OnClose;

settingsWindow.Show();
}

private static void ShowWindow(Window window)
{
window.Owner ??= Application.Current.MainWindow;
Expand Down
47 changes: 47 additions & 0 deletions src/HUDMerger/ViewModels/SettingsWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Toolkit.Mvvm.Input;

namespace HUDMerger.ViewModels;

public class SettingsWindowViewModel : ViewModelBase
{
private string _teamFortress2Folder;
public string TeamFortress2Folder
{
get => _teamFortress2Folder;
set
{
_teamFortress2Folder = value;
OnPropertyChanged();
}
}

public ICommand CancelCommand { get; }
public ICommand ApplyCommand { get; }

public event EventHandler? Close;

public SettingsWindowViewModel()
{
_teamFortress2Folder = ((App)Application.Current).Settings.Value.TeamFortress2Folder;
CancelCommand = new RelayCommand(Cancel);
ApplyCommand = new RelayCommand(Apply);
}

private void Cancel()
{
Close?.Invoke(this, new EventArgs());
}

private void Apply()
{
((App)Application.Current).Settings.Value.TeamFortress2Folder = TeamFortress2Folder;

Properties.Settings.Default.TeamFortress2Folder = TeamFortress2Folder;
Properties.Settings.Default.Save();

Close?.Invoke(this, new EventArgs());
}
}
26 changes: 26 additions & 0 deletions src/HUDMerger/Views/SettingsWindowView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<UserControl x:Class="HUDMerger.Views.SettingsWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HUDMerger.Views"
xmlns:viewmodels="clr-namespace:HUDMerger.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance Type={x:Type viewmodels:SettingsWindowViewModel}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label FontSize="22" Grid.Row="0" Margin="10,0,0,0">Settings</Label>
<StackPanel Name="SettingsContainer" Grid.Row="1" Margin="10,0,10,10">
<Label Content="Team Fortress 2 Folder" FontSize="15" ToolTip="Path to Team Fortress 2 Installation. Used for extracting required HUD files." />
<TextBox Style="{StaticResource TextBoxStyle1}" HorizontalAlignment="Left" MinWidth="480" Text="{Binding TeamFortress2Folder, Mode=TwoWay}" />
</StackPanel>
<WrapPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Row="2" Margin="10,5,10,10">
<Button Style="{StaticResource EnabledButton}" FontSize="14" Padding="20,8" Command="{Binding CancelCommand}" Margin="0,0,10,0" IsCancel="True">Cancel</Button>
<Button Style="{StaticResource AccentButton}" FontSize="14" Padding="20,8" Command="{Binding ApplyCommand}">Apply</Button>
</WrapPanel>
</Grid>
</UserControl>
15 changes: 15 additions & 0 deletions src/HUDMerger/Views/SettingsWindowView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Windows.Controls;

namespace HUDMerger.Views;

/// <summary>
/// Interaction logic for SettingsWindowView.xaml
/// </summary>
public partial class SettingsWindowView : UserControl
{
public SettingsWindowView()
{
InitializeComponent();
}
}

0 comments on commit 1c0f8f5

Please sign in to comment.