Detroit Sample: how do I reset ViewModels on state change? Where should C++ VMs come from?
Posted: 11 Dec 2022, 16:41
I've implemented my menu screens by following the Detroit example; with VisualStateManager.VisualStateGroups -> and one VisualState per menu screen, which hide/unhide the different views (which can have their own ViewModel), ala
The issue is that one of these has a ViewModel with state in it, and I'd like that state to be reset on each return. (In this case, it's the pre-game setup, i.e. a game a lobby, with per-session settings that shouldn't persist if you back out to the main menu and then come back to the lobby screen). How should I go about that in this setup?
-----------------------
Second question; if I'm trying to control the menu screens in Xaml using these visual states, how do/should I create and manage my ViewModel C++ classes? I suppose I can have the xaml/Noesis create them when the view is instantiated, but what if those VMs need to communicate with the rest of the game? Right now, for my current VMs, they're all created from C++ manually with constructors that give them references to various engine systems, but this would be an inversion of that so I'm a bit lost on that part of the concept. To be fair, the existing VMs are essentially; are we in the menu? Create Menu VM. Are we entering gameplay? Create Game VM. Whereas this menu switching is more complicated! But I am enjoying the VisualState switching concept from that sample, these issues aside :)
Thanks.
Code: Select all
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="States">
<VisualState x:Name="Home">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="HomeScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="StartScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SettingsScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Start">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="HomeScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="StartScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SettingsScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Settings">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="HomeScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="StartScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SettingsScreen">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
-----------------------
Second question; if I'm trying to control the menu screens in Xaml using these visual states, how do/should I create and manage my ViewModel C++ classes? I suppose I can have the xaml/Noesis create them when the view is instantiated, but what if those VMs need to communicate with the rest of the game? Right now, for my current VMs, they're all created from C++ manually with constructors that give them references to various engine systems, but this would be an inversion of that so I'm a bit lost on that part of the concept. To be fair, the existing VMs are essentially; are we in the menu? Create Menu VM. Are we entering gameplay? Create Game VM. Whereas this menu switching is more complicated! But I am enjoying the VisualState switching concept from that sample, these issues aside :)
Thanks.