Skip to content

Commit

Permalink
Add the option to change configuration in the toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Golle committed May 26, 2023
1 parent 8a3836e commit ac85f40
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Titan.Tools.Editor/Project/ITitanProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TitanProjectBuildSettings
public const string DefaultOutputPath = "release";
public required string CSharpProjectFile { get; init; }
public required string OutputPath { get; set; }
public required string CurrentConfiguration { get; init; }
public required string CurrentConfiguration { get; set; }
public List<GameBuildConfiguration> Configurations { get; set; } = new();

public GameBuildConfiguration GetCurrentOrDefaultConfiguration()
Expand Down
7 changes: 3 additions & 4 deletions src/Titan.Tools.Editor/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ private static IServiceCollection AddConfiguration(this IServiceCollection colle

private static IServiceCollection AddTools(this IServiceCollection collection) =>
collection
.AddSingleton<ToolsProvider>()
.AddSingleton<CompileTool>()
.AddSingleton<RunGameTool>()
.AddSingleton<PublishTool>()
.AddSingleton<ITool, CompileTool>()
.AddSingleton<ITool, RunGameTool>()
.AddSingleton<ITool, PublishTool>()

;
}
Expand Down
1 change: 1 addition & 0 deletions src/Titan.Tools.Editor/Tools/CompileTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public CompileTool(IApplicationState applicationState, IProcessRunner processRun
_applicationState = applicationState;
_processRunner = processRunner;
}

public async Task<Result> Execute()
{
var buildSettings = _applicationState.Project.BuildSettings;
Expand Down
20 changes: 0 additions & 20 deletions src/Titan.Tools.Editor/Tools/ToolsProvider.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Titan.Tools.Editor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async void Startup()

//NOTE(Jens): implement the rest of the view models. should we maybe use service collection here?
ProjectExplorer = new ProjectExplorerViewModel();
Toolbar.IsProjectLoaded = true;
await Toolbar.LoadContents();
Greetings = $"Path: {result.ProjectPath}";
await Browser.LoadContents();
}
Expand Down
70 changes: 61 additions & 9 deletions src/Titan.Tools.Editor/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Titan.Platform.Win32;
using Titan.Tools.Editor.Common;
using Titan.Tools.Editor.Services;
using Titan.Tools.Editor.Tools;
using Titan.Tools.Editor.Services.State;

namespace Titan.Tools.Editor.ViewModels;

Expand All @@ -16,12 +17,27 @@ public interface ITool
public partial class ToolbarViewModel : ViewModelBase
{
private readonly IDialogService _dialogService;
private readonly IApplicationState _applicationState;

[ObservableProperty]
private bool _isProjectLoaded;
private ObservableCollection<string> _configurations = new();

[ObservableProperty]
private bool _isProjectLoaded;
public ITool[] Tools { get; }

private string? _selecteed;
public string? SelectedConfiguration
{
set
{
_selecteed = value;
ChangeConfigurationCommand.Execute(_selecteed);
OnPropertyChanged();
}
get => _selecteed;
}

[RelayCommand]
private async Task Execute(ITool tool)
{
Expand All @@ -31,18 +47,54 @@ private async Task Execute(ITool tool)
await _dialogService.ShowMessageBox($"{tool.Name} failed", $"The command failed. Error = {result.Error}");
}
}
public ToolbarViewModel(ToolsProvider toolsProvider, IDialogService dialogService)

[RelayCommand]
private async Task ChangeConfiguration(string? config)
{
if (config == null)
{
return;
}

var buildSettings = _applicationState.Project.BuildSettings;
if (buildSettings.CurrentConfiguration == config)
{
return;
}
if (buildSettings.Configurations.All(c => c.Name != config))
{
throw new InvalidOperationException($"The config '{config}' does not exist.");
}

buildSettings.CurrentConfiguration = config;
await _applicationState.SaveChanges();
}

public Task LoadContents()
{
Configurations.Clear();
var buildSettings = _applicationState.Project.BuildSettings;
var configurations = buildSettings
.Configurations
.Select(c => c.Name);
Configurations.AddRange(configurations);
SelectedConfiguration = buildSettings.CurrentConfiguration;
IsProjectLoaded = true;
return Task.CompletedTask;
}

public ToolbarViewModel(IEnumerable<ITool> tools, IDialogService dialogService, IApplicationState applicationState)
{
_dialogService = dialogService;
Tools = toolsProvider
.GetTools()
.ToArray();
_applicationState = applicationState;
Tools = tools.ToArray();
}

public ToolbarViewModel()
: this(
App.GetRequiredService<ToolsProvider>(),
App.GetRequiredService<IDialogService>()
App.GetRequiredService<IEnumerable<ITool>>(),
App.GetRequiredService<IDialogService>(),
App.GetRequiredService<IApplicationState>()
)
{
Helper.CheckDesignMode(nameof(ToolbarViewModel));
Expand Down
2 changes: 1 addition & 1 deletion src/Titan.Tools.Editor/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<MenuItem Header="Cook Assets"/>
</MenuItem>
</Menu>
<ContentControl Grid.Column="1" VerticalAlignment="Top" Height="30" Width="300" Content="{Binding Toolbar}"/>
<ContentControl Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Height="30" Content="{Binding Toolbar}"/>
</Grid>

<Grid Name="TheGrid" >
Expand Down
25 changes: 14 additions & 11 deletions src/Titan.Tools.Editor/Views/ToolbarView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
x:Class="Titan.Tools.Editor.Views.ToolbarView">

<Design.DataContext>
<viewModels:ToolbarViewModel/>
<viewModels:ToolbarViewModel IsProjectLoaded="true"/>
</Design.DataContext>

<Border IsEnabled="{Binding IsProjectLoaded}">
<ItemsRepeater ItemsSource="{Binding Tools}">
<ItemsRepeater.Layout>
<StackLayout Orientation="Horizontal" Spacing="6"/>
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="viewModels:ITool">
<Button Content="{Binding Name}" Command="{Binding $parent[UserControl].DataContext.ExecuteCommand}" CommandParameter="{Binding .}"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
<StackPanel Orientation="Horizontal" Spacing="6" Height="30">
<ComboBox ItemsSource="{Binding Configurations}" SelectedIndex="0" SelectedItem="{Binding SelectedConfiguration}" Width="150"/>
<ItemsRepeater ItemsSource="{Binding Tools}">
<ItemsRepeater.Layout>
<StackLayout Orientation="Horizontal" Spacing="6"/>
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="viewModels:ITool">
<Button Content="{Binding Name}" Command="{Binding $parent[UserControl].DataContext.ExecuteCommand}" CommandParameter="{Binding .}"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</StackPanel>
</Border>
</UserControl>

0 comments on commit ac85f40

Please sign in to comment.