-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
194 lines (171 loc) · 6.72 KB
/
MainViewModel.cs
File metadata and controls
194 lines (171 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.ComponentModel;
using Models;
using Models.Scene;
using Services;
using SimpleFileBrowser;
using Unity.AppUI.MVVM;
using Unity.AppUI.Redux;
using Unity.AppUI.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
using Utils.Types;
namespace UI.ViewModels
{
[ObservableObject]
public partial class MainViewModel
{
#region Services
private readonly StoreService _storeService;
private readonly IDisposableSubscription _mainStateSubscription;
private readonly IDisposableSubscription _sceneStateSubscription;
#endregion
#region Properties
[ObservableProperty]
private bool _isAutomationModeActive;
[ObservableProperty]
private SplitView.State _mainSplitViewState;
[ObservableProperty]
private int _leftSidePanelTabIndex;
[ObservableProperty]
private InspectorDisplayType _inspectorDisplayType;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="MainViewModel" /> class.
/// Registers state, initializes properties from the store, and subscribes to state changes.
/// </summary>
/// <param name="storeService">The store service for state management.</param>
/// <param name="probeService">The probe service for getting probe info.</param>
public MainViewModel(StoreService storeService)
{
// Register services.
_storeService = storeService;
// Subscribe to state changes and initialize properties.
_mainStateSubscription = _storeService.Store.Subscribe(
state => state.Get<MainState>(SliceNames.MAIN_SLICE),
OnMainStateChanged,
new SubscribeOptions<MainState> { fireImmediately = true }
);
_sceneStateSubscription = storeService.Store.Subscribe(
state => state.Get<SceneState>(SliceNames.SCENE_SLICE),
OnSceneStateChanged,
new SubscribeOptions<SceneState> { fireImmediately = true }
);
PropertyChanged += OnPropertyChanged;
App.shuttingDown += OnShuttingDown;
}
private void OnMainStateChanged(MainState mainState)
{
MainSplitViewState = mainState.MainSplitViewState;
LeftSidePanelTabIndex = mainState.LeftSidePanelTabIndex;
}
private void OnSceneStateChanged(SceneState state)
{
if (!string.IsNullOrEmpty(state.ActiveProbeName))
InspectorDisplayType = InspectorDisplayType.Probe;
else if (!string.IsNullOrEmpty(state.ActiveManipulatorId))
// Use automation inspector if automation mode is active.
InspectorDisplayType = IsAutomationModeActive
? InspectorDisplayType.Automation
: InspectorDisplayType.Manipulator;
else
InspectorDisplayType = InspectorDisplayType.Nothing;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IsAutomationModeActive):
Debug.Log("Automation mode changed: " + IsAutomationModeActive);
// Re-evaluate inspector display type when automation mode changes.
OnSceneStateChanged(
_storeService.Store.GetState<SceneState>(SliceNames.SCENE_SLICE)
);
break;
}
}
private void OnShuttingDown()
{
_storeService.Save();
_mainStateSubscription.Dispose();
_sceneStateSubscription.Dispose();
App.shuttingDown -= OnShuttingDown;
}
#region Commands
[ICommand]
private void SetAutomationModeActive(bool isActive)
{
_storeService.Store.Dispatch(MainActions.SET_IS_AUTOMATION_ENABLED, isActive);
_storeService.Store.Dispatch(SceneActions.SET_ALL_PROBES_TO_LINE, isActive);
}
[ICommand]
private void SetMainSplitViewState(SplitView.State state)
{
_storeService.Store.Dispatch(MainActions.SET_MAIN_SPLIT_VIEW_STATE, state);
}
[ICommand]
private void SetLeftSidePanelTabIndex(int index)
{
_storeService.Store.Dispatch(MainActions.SET_LEFT_SIDE_PANEL_TAB_INDEX, index);
}
[ICommand]
private void SaveStateToFile()
{
FileBrowser.ShowSaveDialog(
onSuccess: (paths) =>
{
if (paths.Length > 0)
{
var filePath = paths[0];
if (_storeService.SaveToFile(filePath))
{
Debug.Log($"Scene state saved successfully to: {filePath}");
}
else
{
Debug.LogError("Failed to save scene state");
}
}
},
onCancel: () => { },
pickMode: FileBrowser.PickMode.Files,
allowMultiSelection: false,
initialPath: null,
initialFilename: "scene_state.json",
title: "Save Scene State",
saveButtonText: "Save"
);
}
[ICommand]
private void LoadStateFromFile()
{
FileBrowser.ShowLoadDialog(
onSuccess: (paths) =>
{
if (paths.Length > 0)
{
var filePath = paths[0];
if (_storeService.LoadFromFile(filePath))
{
Debug.Log($"Scene state loaded successfully from: {filePath}");
Debug.Log("Reloading scene to apply loaded state...");
// Reload the scene to apply the loaded state from local storage
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
else
{
Debug.LogError("Failed to load scene state");
}
}
},
onCancel: () => { },
pickMode: FileBrowser.PickMode.Files,
allowMultiSelection: false,
initialPath: null,
initialFilename: null,
title: "Load Scene State",
loadButtonText: "Load"
);
}
#endregion
}
}