<commit_before>﻿using BrewLib.Graphics;
using BrewLib.Graphics.Cameras;
using BrewLib.Util;
using OpenTK;
using System;
using System.Collections.Generic;

namespace StorybrewEditor.Storyboarding
{
    public class LayerManager
    {
        private List<EditorStoryboardLayer> layers = new List<EditorStoryboardLayer>();

        public int LayersCount => layers.Count;
        public IEnumerable<EditorStoryboardLayer> Layers => layers;
        public List<EditorStoryboardLayer> FindLayers(Predicate<EditorStoryboardLayer> predicate) => layers.FindAll(predicate);

        public event EventHandler OnLayersChanged;

        public void Add(EditorStoryboardLayer layer)
        {
            layers.Insert(findLayerIndex(layer), layer);
            layer.OnChanged += layer_OnChanged;
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(EditorStoryboardLayer oldLayer, EditorStoryboardLayer newLayer)
        {
            var index = layers.IndexOf(oldLayer);
            if (index != -1)
            {
                newLayer.CopySettings(oldLayer);
                newLayer.OnChanged += layer_OnChanged;
                oldLayer.OnChanged -= layer_OnChanged;
                layers[index] = newLayer;
            }
            else throw new InvalidOperationException($"Cannot replace layer '{oldLayer.Name}' with '{newLayer.Name}', old layer not found");
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(List<EditorStoryboardLayer> oldLayers, List<EditorStoryboardLayer> newLayers)
        {
            oldLayers = new List<EditorStoryboardLayer>(oldLayers);
            foreach (var newLayer in newLayers)
            {
                var oldLayer = oldLayers.Find(l => l.Identifier == newLayer.Identifier);
                if (oldLayer != null)
                {
                    var index = layers.IndexOf(oldLayer);
                    if (index != -1)
                    {
                        newLayer.CopySettings(layers[index]);
                        layers[index] = newLayer;
                    }
                    oldLayers.Remove(oldLayer);
                }
                else layers.Add(newLayer);
                newLayer.OnChanged += layer_OnChanged;
            }
            foreach (var oldLayer in oldLayers)
            {
                oldLayer.OnChanged -= layer_OnChanged;
                layers.Remove(oldLayer);
            }
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(EditorStoryboardLayer oldLayer, List<EditorStoryboardLayer> newLayers)
        {
            var index = layers.IndexOf(oldLayer);
            if (index != -1)
            {
                foreach (var newLayer in newLayers)
                {
                    newLayer.CopySettings(oldLayer);<commit_msg>Fix layers sometimes becoming unable to move.<commit_after>﻿using BrewLib.Graphics;
using BrewLib.Graphics.Cameras;
using BrewLib.Util;
using OpenTK;
using System;
using System.Collections.Generic;

namespace StorybrewEditor.Storyboarding
{
    public class LayerManager
    {
        private List<EditorStoryboardLayer> layers = new List<EditorStoryboardLayer>();

        public int LayersCount => layers.Count;
        public IEnumerable<EditorStoryboardLayer> Layers => layers;
        public List<EditorStoryboardLayer> FindLayers(Predicate<EditorStoryboardLayer> predicate) => layers.FindAll(predicate);

        public event EventHandler OnLayersChanged;

        public void Add(EditorStoryboardLayer layer)
        {
            layers.Insert(findLayerIndex(layer), layer);
            layer.OnChanged += layer_OnChanged;
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(EditorStoryboardLayer oldLayer, EditorStoryboardLayer newLayer)
        {
            var index = layers.IndexOf(oldLayer);
            if (index != -1)
            {
                newLayer.CopySettings(oldLayer);
                newLayer.OnChanged += layer_OnChanged;
                oldLayer.OnChanged -= layer_OnChanged;
                layers[index] = newLayer;
            }
            else throw new InvalidOperationException($"Cannot replace layer '{oldLayer.Name}' with '{newLayer.Name}', old layer not found");
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(List<EditorStoryboardLayer> oldLayers, List<EditorStoryboardLayer> newLayers)
        {
            oldLayers = new List<EditorStoryboardLayer>(oldLayers);
            foreach (var newLayer in newLayers)
            {
                var oldLayer = oldLayers.Find(l => l.Identifier == newLayer.Identifier);
                if (oldLayer != null)
                {
                    var index = layers.IndexOf(oldLayer);
                    if (index != -1)
                    {
                        newLayer.CopySettings(layers[index]);
                        layers[index] = newLayer;
                    }
                    oldLayers.Remove(oldLayer);
                }
                else layers.Insert(findLayerIndex(newLayer), newLayer);
                newLayer.OnChanged += layer_OnChanged;
            }
            foreach (var oldLayer in oldLayers)
            {
                oldLayer.OnChanged -= layer_OnChanged;
                layers.Remove(oldLayer);
            }
            OnLayersChanged?.Invoke(this, EventArgs.Empty);
        }

        public void Replace(EditorStoryboardLayer oldLayer, List<EditorStoryboardLayer> newLayers)
        {
            var index = layers.IndexOf(oldLayer);
            if (index != -1)
            {
                foreach (var newLayer in newLayers)
                {
                    newLayer.CopySettings(oldLayer);
{'fraction_non_alphanumeric': 0.08351950506959958, 'fraction_numerical': 0.0010311050008592541, 'mean_word_length': 1.8854734754586018, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4994448', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1405, 'n_words': 300}