diff --git a/Controls/osVisFilter.xaml.cs b/Controls/osVisFilter.xaml.cs index 4368856..ff2dca2 100644 --- a/Controls/osVisFilter.xaml.cs +++ b/Controls/osVisFilter.xaml.cs @@ -1,5 +1,7 @@ using System.Collections.ObjectModel; +using System.ComponentModel; // Add this for PropertyChangedEventHandler and PropertyChangedEventArgs using System.Reflection; +using System.Text.Json.Serialization; using System.Windows; using System.Windows.Controls; using CommunityToolkit.Mvvm.ComponentModel; @@ -7,6 +9,16 @@ using CtrEditor.ObjetosSim; namespace CtrEditor.Controls { + public class FilterChangedEventArgs : EventArgs + { + public osVisFilterViewModel FilterViewModel { get; private set; } + + public FilterChangedEventArgs(osVisFilterViewModel viewModel) + { + FilterViewModel = viewModel; + } + } + /// /// Interaction logic for osVisFilter.xaml /// @@ -15,11 +27,18 @@ namespace CtrEditor.Controls public osVisFilter() { InitializeComponent(); - DataContext = FilterViewModel = new osVisFilterViewModel(); + DataContext = FilterViewModel = new osVisFilterViewModel { Parent = this }; // Set the Parent property } public osVisFilterViewModel FilterViewModel { get; private set; } + public event EventHandler FilterChanged; + + public virtual void OnFilterChanged() // Changed from protected to public + { + FilterChanged?.Invoke(this, new FilterChangedEventArgs(FilterViewModel)); + } + /// /// Updates the type filters based on the provided types /// @@ -52,9 +71,66 @@ namespace CtrEditor.Controls [ObservableProperty] private ObservableCollection typeFilters = new ObservableCollection(); + partial void OnShowAllChanged(bool value) + { + NotifyFilterChanged(); + } + + partial void OnShowClonedChanged(bool value) + { + NotifyFilterChanged(); + } + + partial void OnShowAutoCreatedChanged(bool value) + { + NotifyFilterChanged(); + } + + partial void OnShowEnableOnAllPagesChanged(bool value) + { + NotifyFilterChanged(); + } + + partial void OnShowOnThisPageChanged(bool value) + { + NotifyFilterChanged(); + } + + partial void OnTypeFiltersChanged(ObservableCollection value) + { + if (value != null) + { + foreach (var filter in value) + { + filter.PropertyChanged += (s, e) => + { + if (e.PropertyName == nameof(TypeFilterItem.IsSelected)) + { + NotifyFilterChanged(); + } + }; + } + } + } + + private void NotifyFilterChanged() + { + if (this.Parent is osVisFilter filter) + { + filter.OnFilterChanged(); + } + } + + [JsonIgnore] + public osVisFilter Parent { get; set; } + public osVisFilterViewModel() { // PropertyChanged listeners could be added here if needed + if (this.Parent is osVisFilter filter) + { + filter.OnFilterChanged(); + } } /// @@ -69,6 +145,7 @@ namespace CtrEditor.Controls foreach (var item in typesToRemove) { + UnsubscribeFromTypeFilter(item); TypeFilters.Remove(item); } @@ -77,15 +154,35 @@ namespace CtrEditor.Controls { if (!TypeFilters.Any(tf => tf.Type == type)) { - TypeFilters.Add(new TypeFilterItem(type) + var newFilter = new TypeFilterItem(type) { DisplayName = GetTypeDisplayName(type), IsSelected = true - }); + }; + SubscribeToTypeFilter(newFilter); + TypeFilters.Add(newFilter); } } } + private void SubscribeToTypeFilter(TypeFilterItem filter) + { + filter.PropertyChanged += TypeFilter_PropertyChanged; + } + + private void UnsubscribeFromTypeFilter(TypeFilterItem filter) + { + filter.PropertyChanged -= TypeFilter_PropertyChanged; + } + + private void TypeFilter_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(TypeFilterItem.IsSelected)) + { + NotifyFilterChanged(); + } + } + /// /// Gets the display name for a type, using the NombreClase static method if available /// @@ -117,5 +214,10 @@ namespace CtrEditor.Controls Type = type; DisplayName = type?.Name ?? "Unknown Type"; } + + partial void OnIsSelectedChanged(bool value) + { + OnPropertyChanged(nameof(IsSelected)); + } } } \ No newline at end of file diff --git a/MainViewModel.cs b/MainViewModel.cs index a3d6e0a..89efb45 100644 --- a/MainViewModel.cs +++ b/MainViewModel.cs @@ -22,6 +22,7 @@ using CommunityToolkit.Mvvm.Input; using System.Text.RegularExpressions; using System.Collections.Specialized; using CtrEditor.Serialization; // Add this line +using CtrEditor.Controls; // Add this using directive namespace CtrEditor { @@ -127,6 +128,11 @@ namespace CtrEditor { mainWindow = value; _objectManager = mainWindow._objectManager; // Initialize _objectManager + // Add the filter changed event handler here instead of in constructor + if (mainWindow.VisFilter != null) + { + mainWindow.VisFilter.FilterChanged += (s, e) => OnVisFilterChanged(e.FilterViewModel); + } } } @@ -292,9 +298,22 @@ namespace CtrEditor partial void OnObjetosSimulablesChanged(ObservableCollection value) { - + if (value != null) + { + value.CollectionChanged += ObjetosSimulables_CollectionChanged; + UpdateVisFilterTypes(); + } } + private void ObjetosSimulables_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + UpdateVisFilterTypes(); + } + + private void UpdateVisFilterTypes() + { + MainWindow?.VisFilter?.UpdateAvailableTypes(ObjetosSimulables.Select(o => o.GetType()).Distinct()); + } // // Constructor @@ -366,9 +385,47 @@ namespace CtrEditor _stateManager = new StateManager(EstadoPersistente.Instance.directorio, this); _stateSerializer = new StateSerializer(this, datosDeTrabajo, simulationManager); - } + private void OnVisFilterChanged(osVisFilterViewModel filter) // Changed signature to accept viewmodel directly + { + foreach (var obj in ObjetosSimulables) + { + bool isVisible = true; + + // Apply Show All filter + if (!filter.ShowAll) + { + // Check type filter + var typeFilter = filter.TypeFilters.FirstOrDefault(tf => tf.Type == obj.GetType()); + if (typeFilter == null || !typeFilter.IsSelected) + { + isVisible = false; + } + + // Check other filters + if (filter.ShowCloned && !obj.Cloned) + isVisible = false; + + if (filter.ShowAutoCreated && !obj.AutoCreated) + isVisible = false; + + if (filter.ShowEnableOnAllPages && !obj.Enable_On_All_Pages) + isVisible = false; + + if (filter.ShowOnThisPage && !obj.Show_On_This_Page) + isVisible = false; + } + + obj.IsVisFilter = isVisible; + + // Update Canvas object visibility + if (obj.VisualRepresentation != null) + { + obj.VisualRepresentation.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed; + } + } + } public void LoadStateObjetosSimulables() { @@ -376,11 +433,10 @@ namespace CtrEditor { _stateSerializer.LoadState(SelectedImage); - // Restaurar visibilidad según el filtro actual - foreach (var obj in ObjetosSimulables) + // Aplicar los filtros actuales a los objetos recién cargados + if (MainWindow?.VisFilter?.FilterViewModel != null) { - if (obj.VisualRepresentation != null) - obj.VisualRepresentation.Visibility = Visibility.Visible; + OnVisFilterChanged(MainWindow.VisFilter.FilterViewModel); } } } diff --git a/MainWindow.xaml b/MainWindow.xaml index 6f366f1..e90630e 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -217,19 +217,12 @@ - - - - - - - - - - - + + + + @@ -262,43 +255,64 @@ + + + + - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - + - + - +