<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using NetworkSkins.Controller;
using NetworkSkins.GUI;
using NetworkSkins.Net;
using NetworkSkins.Skins;
using UnityEngine;

namespace NetworkSkins
{
    public class SkinController : MonoBehaviour {
        public static SkinController Instance;

        public delegate void ToolStateChangedEventHandler(bool state);
        public event ToolStateChangedEventHandler EventToolStateChanged;
        public delegate void PrefabChangedEventHandler(NetInfo netInfo);
        public event PrefabChangedEventHandler EventPrefabChanged;

        private bool _ignoreModifierEvents = false;
        public bool TabClicked { get; set; } = false;

        public TerrainSurfaceFeatureController TerrainSurface;

        public ColorFeatureController Color;

        public StreetLightFeatureController StreetLight;

        public bool TreesEnabled => LeftTree.Enabled || MiddleTree.Enabled || RighTree.Enabled;
        public LanePosition LanePosition { get; set; } = LanePosition.Left;
        public TreeFeatureController LeftTree;
        public TreeFeatureController MiddleTree;
        public TreeFeatureController RighTree;
        
        public bool PillarsEnabled => ElevatedBridgePillar.Enabled || ElevatedMiddlePillar.Enabled || BridgeBridgePillar.Enabled || BridgeMiddlePillar.Enabled;
        public Pillar PillarElevationCombination { get; set; } = Pillar.Bridge;
        public PillarFeatureController ElevatedBridgePillar;
        public PillarFeatureController ElevatedMiddlePillar;
        public PillarFeatureController BridgeBridgePillar;
        public PillarFeatureController BridgeMiddlePillar;

        public CatenaryFeatureController Catenary;

        public NetInfo Prefab { get; private set; }

        private bool isNetToolEnabled;

        private void Awake() {
            Instance = this;

            NetworkSkinManager.instance.EventSegmentPlaced += OnSegmentPlaced;

            TerrainSurface = new TerrainSurfaceFeatureController();
            TerrainSurface.EventModifiersChanged += OnModifiersChanged;

            Color = new ColorFeatureController();
            Color.EventModifiersChanged += OnModifiersChanged;

            StreetLight = new StreetLightFeatureController();
            StreetLight.EventModifiersChanged += OnModifiersChanged;

            LeftTree = new TreeFeatureController(LanePosition.Left);
            LeftTree.EventModifiersChanged += OnModifiersChanged;

            MiddleTree = new TreeFeatureController(LanePosition.Middle);
            MiddleTree.EventModifiersChanged += OnModifiersChanged;

            RighTree = new TreeFeatureController(LanePosition.Right);
            RighTree.EventModifiersChanged += OnModifiersChanged;
            
            var availablePillars = PillarUtils.GetAvailablePillars();

            ElevatedBridgePillar = new PillarFeatureController(PillarType.Bridge, availablePillars);
            ElevatedBridgePillar.EventModifiersChanged += OnModifiersChanged;

            ElevatedMiddlePillar = new PillarFeatureController(PillarType.Middle, availablePillars);
            ElevatedMiddlePillar.EventModifiersChanged += OnModifiersChanged;

            BridgeBridgePillar = new PillarFeatureController(PillarType.Bridge, availablePillars);
            BridgeBridgePillar.EventModifiersChanged += OnModifiersChanged;

            BridgeMiddlePillar = new PillarFeatureController(PillarType.Middle, availablePillars);
            BridgeMiddlePillar.EventModifiersChanged += OnModifiersChanged;

            Catenary = new CatenaryFeatureController();
            Catenary.EventModifiersChanged += OnModifiersChanged;
        }

        public void SetActivePillarElevation(Pillar pillar) {
            PillarElevationCombination = pillar;
            EventPrefabChanged?.Invoke(Prefab);
        }

        public void SetActiveLane(LanePosition value) {
            LanePosition = value;
            EventPrefabChanged?.Invoke(Prefab);
        }

        private void Update() {
            if (ToolsModifierControl.toolController.CurrentTool is NetTool netTool) {
                if (!isNetToolEnabled) {
                    isNetToolEnabled = true;
                    EventToolStateChanged?.Invoke(true);
                }
                if (netTool.Prefab != null && Prefab != netTool.Prefab) {
                    Prefab = netTool.m_prefab;
                    //UpdateSelectedOptions();
                    OnPrefabChanged(Prefab);
                    EventPrefabChanged?.Invoke(Prefab);
                }
            } else {
                if (isNetToolEnabled) {
                    isNetToolEnabled = false;
                    EventToolStateChanged?.Invoke(false);
                    NetworkSkinManager.instance.ClearActiveModifiers();
                }
            }
        }

        private void OnDestroy()
        {
            NetworkSkinManager.instance.EventSegmentPlaced -= OnSegmentPlaced;
        }

        private void OnPrefabChanged(NetInfo prefab)
        {
            _ignoreModifierEvents = true;

            TerrainSurface.OnPrefabChanged(prefab);

            Color.OnPrefabChanged(prefab);<commit_msg>fix skin not applied when NetTool is opened for the second time<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using NetworkSkins.Controller;
using NetworkSkins.GUI;
using NetworkSkins.Net;
using NetworkSkins.Skins;
using UnityEngine;

namespace NetworkSkins
{
    public class SkinController : MonoBehaviour {
        public static SkinController Instance;

        public delegate void ToolStateChangedEventHandler(bool state);
        public event ToolStateChangedEventHandler EventToolStateChanged;
        public delegate void PrefabChangedEventHandler(NetInfo netInfo);
        public event PrefabChangedEventHandler EventPrefabChanged;

        private bool _ignoreModifierEvents = false;
        public bool TabClicked { get; set; } = false;

        public TerrainSurfaceFeatureController TerrainSurface;

        public ColorFeatureController Color;

        public StreetLightFeatureController StreetLight;

        public bool TreesEnabled => LeftTree.Enabled || MiddleTree.Enabled || RighTree.Enabled;
        public LanePosition LanePosition { get; set; } = LanePosition.Left;
        public TreeFeatureController LeftTree;
        public TreeFeatureController MiddleTree;
        public TreeFeatureController RighTree;
        
        public bool PillarsEnabled => ElevatedBridgePillar.Enabled || ElevatedMiddlePillar.Enabled || BridgeBridgePillar.Enabled || BridgeMiddlePillar.Enabled;
        public Pillar PillarElevationCombination { get; set; } = Pillar.Bridge;
        public PillarFeatureController ElevatedBridgePillar;
        public PillarFeatureController ElevatedMiddlePillar;
        public PillarFeatureController BridgeBridgePillar;
        public PillarFeatureController BridgeMiddlePillar;

        public CatenaryFeatureController Catenary;

        public NetInfo Prefab { get; private set; }

        private bool isNetToolEnabled;

        private void Awake() {
            Instance = this;

            NetworkSkinManager.instance.EventSegmentPlaced += OnSegmentPlaced;

            TerrainSurface = new TerrainSurfaceFeatureController();
            TerrainSurface.EventModifiersChanged += OnModifiersChanged;

            Color = new ColorFeatureController();
            Color.EventModifiersChanged += OnModifiersChanged;

            StreetLight = new StreetLightFeatureController();
            StreetLight.EventModifiersChanged += OnModifiersChanged;

            LeftTree = new TreeFeatureController(LanePosition.Left);
            LeftTree.EventModifiersChanged += OnModifiersChanged;

            MiddleTree = new TreeFeatureController(LanePosition.Middle);
            MiddleTree.EventModifiersChanged += OnModifiersChanged;

            RighTree = new TreeFeatureController(LanePosition.Right);
            RighTree.EventModifiersChanged += OnModifiersChanged;
            
            var availablePillars = PillarUtils.GetAvailablePillars();

            ElevatedBridgePillar = new PillarFeatureController(PillarType.Bridge, availablePillars);
            ElevatedBridgePillar.EventModifiersChanged += OnModifiersChanged;

            ElevatedMiddlePillar = new PillarFeatureController(PillarType.Middle, availablePillars);
            ElevatedMiddlePillar.EventModifiersChanged += OnModifiersChanged;

            BridgeBridgePillar = new PillarFeatureController(PillarType.Bridge, availablePillars);
            BridgeBridgePillar.EventModifiersChanged += OnModifiersChanged;

            BridgeMiddlePillar = new PillarFeatureController(PillarType.Middle, availablePillars);
            BridgeMiddlePillar.EventModifiersChanged += OnModifiersChanged;

            Catenary = new CatenaryFeatureController();
            Catenary.EventModifiersChanged += OnModifiersChanged;
        }

        public void SetActivePillarElevation(Pillar pillar) {
            PillarElevationCombination = pillar;
            EventPrefabChanged?.Invoke(Prefab);
        }

        public void SetActiveLane(LanePosition value) {
            LanePosition = value;
            EventPrefabChanged?.Invoke(Prefab);
        }

        private void Update() {
            if (ToolsModifierControl.toolController.CurrentTool is NetTool netTool) {
                if (!isNetToolEnabled) {
                    isNetToolEnabled = true;
                    EventToolStateChanged?.Invoke(true);
                }
                if (netTool.Prefab != null && Prefab != netTool.Prefab) {
                    Prefab = netTool.m_prefab;
                    //UpdateSelectedOptions();
                    OnPrefabChanged(Prefab);
                    EventPrefabChanged?.Invoke(Prefab);
                }
            } else {
                if (isNetToolEnabled) {
                    Prefab = null;
                    isNetToolEnabled = false;
                    EventToolStateChanged?.Invoke(false);
                    NetworkSkinManager.instance.ClearActiveModifiers();
                }
            }
        }

        private void OnDestroy()
        {
            NetworkSkinManager.instance.EventSegmentPlaced -= OnSegmentPlaced;
        }

        private void OnPrefabChanged(NetInfo prefab)
        {
            _ignoreModifierEvents = true;

            TerrainSurface.OnPrefabChanged(prefab);

            Color.OnPrefabChanged(prefab);
{'fraction_non_alphanumeric': 0.060744549176425786, 'fraction_numerical': 0.0, 'mean_word_length': 2.6083819993129507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1998489', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2400, 'n_words': 502}