using System.Collections.Generic; using CommunityToolkit.Diagnostics; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Constants; using ComputeSharp.SwapChain.Core.Models; using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP using ComputeSharp.Uwp; #else using ComputeSharp.WinUI; #endif #nullable enable namespace ComputeSharp.SwapChain.Core.ViewModels; /// /// The viewmodel for for a view that allows users to select shaders and customize settings. /// public sealed class MainViewModel : ObservableObject { /// /// The instance currently in use. /// private readonly IAnalyticsService analyticsService; /// /// Creates a new instance. /// public MainViewModel(IAnalyticsService analyticsService) { Guard.IsNotNull(analyticsService); this.analyticsService = analyticsService; this.isVerticalSyncEnabled = true; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; this.selectedComputeShader.IsSelected = true; SetResolutionScaleCommand = new RelayCommand(SetResolutionScale); ToggleRenderingPausedCommand = new RelayCommand(ToggleRenderingPaused); } /// /// Gets the available resolution scaling options (as percentage values). /// public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; private bool isVerticalSyncEnabled; /// /// Gets or sets whether the vertical sync is enabled. /// public bool IsVerticalSyncEnabled { get => this.isVerticalSyncEnabled; set { if (SetProperty(ref this.isVerticalSyncEnabled, value)) { this.analyticsService.Log(Event.IsVerticalSyncEnabledChanged, (nameof(value), value)); } } } private bool isDynamicResolutionEnabled; /// /// Gets or sets whether the dynamic resolution is enabled. /// public bool IsDynamicResolutionEnabled { get => this.isDynamicResolutionEnabled; set { if (SetProperty(ref this.isDynamicResolutionEnabled, value)) { this.analyticsService.Log(Event.IsDynamicResolutionEnabledChanged, (nameof(value), value)); } } } private int selectedResolutionScale; /// /// Gets the currently selected resolution scale setting (as percentage valaue). /// public int SelectedResolutionScale { get => this.selectedResolutionScale; private set { if (SetProperty(ref this.selectedResolutionScale, value)) { this.analyticsService.Log(Event.SelectedResolutionScaleChanged, (nameof(value), value)); } } } /// /// Gets the command that sets . /// public IRelayCommand SetResolutionScaleCommand { get; } /// /// Sets . /// private void SetResolutionScale(int resolutionScale) { SelectedResolutionScale = resolutionScale; } /// /// Gets the collection of available compute shader. /// public IReadOnlyList ComputeShaderOptions { get; } = new ComputeShader[] { new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))), new("Contoured layers", new ContouredLayersRunner()) }; private ComputeShader selectedComputeShader; /// /// Gets or sets the currently selected compute shader. /// public ComputeShader SelectedComputeShader { get => this.selectedComputeShader; set { this.selectedComputeShader.IsSelected = false; if (SetProperty(ref this.selectedComputeShader, value) && value is not null) { this.analyticsService.Log(Event.SelectedComputeShaderChanged, (nameof(value.ShaderRunner), value.ShaderRunner)); value.IsSelected = true; } } } private bool isRenderingPaused; /// /// Gets or sets whether the rendering is currently paused. /// public bool IsRenderingPaused { get => this.isRenderingPaused; set { if (SetProperty(ref this.isRenderingPaused, value)) { this.analyticsService.Log(Event.IsRenderingPausedChanged, (nameof(value), value)); } } } /// /// Gets the command that toggles . /// public IRelayCommand ToggleRenderingPausedCommand { get; } /// /// Toggles . /// private void ToggleRenderingPaused() { IsRenderingPaused = !IsRenderingPaused; } }