<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Max;
using Outliner.Scene;
using Outliner.Controls.Tree;
using Outliner.Filters;
using Outliner.MaxUtils;
using System.Runtime.InteropServices;
//using Outliner.LayerTools;
using Outliner.Plugins;

namespace Outliner.Modes.SelectionSet
{
[OutlinerPlugin(OutlinerPluginType.TreeMode)]
[LocalizedDisplayName(typeof(Resources), "Mode_SelectionSets")]
public class SelectionSetMode : TreeMode
{
   private AllObjectsSelectionSet allObjectsSelSet;

   public SelectionSetMode(TreeView tree) : base(tree)
   {
      proc_NamedSelSetCreated    = new GlobalDelegates.Delegate5(this.NamedSelSetCreated);
      proc_NamedSelSetDeleted    = new GlobalDelegates.Delegate5(this.NamedSelSetDeleted);
      proc_NamedSelSetPreModify  = new GlobalDelegates.Delegate5(this.NamedSelSetPreModify);
      proc_NamedSelSetPostModify = new GlobalDelegates.Delegate5(this.NamedSelSetPostModify);
      proc_NamedSelSetRenamed    = new GlobalDelegates.Delegate5(this.NamedSelSetRenamed);

      this.allObjectsSelSet = new AllObjectsSelectionSet();
      this.Tree.DragDropHandler = new TreeViewDragDropHandler();
   }

   protected override void FillTree()
   {
      this.Tree.BeginUpdate();

      TreeNode allObjTn = this.AddNode(this.allObjectsSelSet, this.Tree.Nodes);
      allObjTn.FontStyle = System.Drawing.FontStyle.Italic;

      IINamedSelectionSetManager selSetMan = MaxInterfaces.SelectionSetManager;
      for (int i = 0; i < selSetMan.NumNamedSelSets; i++)
      {
         this.AddNode(new SelectionSetWrapper(i), this.Tree.Nodes);
      }

      this.Tree.Sort();
      this.Tree.EndUpdate();
   }

   public override TreeNode AddNode(IMaxNodeWrapper wrapper, TreeNodeCollection parentCol)
   {
      TreeNode tn = base.AddNode(wrapper, parentCol);

      if (wrapper is SelectionSetWrapper)
      {
         foreach (Object child in wrapper.ChildNodes)
            this.AddNode(child, tn.Nodes);
      }

      return tn;
   }

   override public DragDropHandler CreateDragDropHandler(IMaxNodeWrapper wrapper)
   {
      if (wrapper is SelectionSetWrapper)
         return new SelectionSetDragDropHandler((SelectionSetWrapper)wrapper);
      else if (wrapper is IINodeWrapper)
         return new IINodeDragDropHandler(wrapper);

      return base.CreateDragDropHandler(wrapper);
   }


   public override void Start()
   {
      this.RegisterSystemNotification(proc_NamedSelSetCreated, SystemNotificationCode.NamedSelSetCreated);
      this.RegisterSystemNotification(proc_NamedSelSetDeleted, SystemNotificationCode.NamedSelSetDeleted);
      this.RegisterSystemNotification(proc_NamedSelSetRenamed, SystemNotificationCode.NamedSelSetRenamed);
      this.RegisterSystemNotification(proc_NamedSelSetPreModify, SystemNotificationCode.NamedSelSetPreModify);
      this.RegisterSystemNotification(proc_NamedSelSetPostModify, SystemNotificationCode.NamedSelSetPostModify);

      this.RegisterNodeEventCallbackObject(new SelectionSetNodeEventCallbacks(this));

      base.Start();
   }


   #region NodeEventCallbacks

   protected class SelectionSetNodeEventCallbacks : TreeModeNodeEventCallbacks
   {
      protected new SelectionSetMode TreeMode;
      public SelectionSetNodeEventCallbacks(SelectionSetMode treeMode)
         : base(treeMode)
      {
         this.TreeMode = treeMode;
      }

      public override void Added(ITab<UIntPtr> nodes)
      {
         TreeNode allObjSelSetTn = this.TreeMode.GetFirstTreeNode(this.TreeMode.allObjectsSelSet);
         if (allObjSelSetTn == null)
            return;

         foreach (IINode node in IINodeHelpers.NodeKeysToINodeList(nodes))
         {
            this.TreeMode.AddNode(node, allObjSelSetTn.Nodes);
            this.Tree.AddToSortQueue(allObjSelSetTn.Nodes);
         }
         this.Tree.StartTimedSort(true);
      }
   }

   #endregion


   #region System notifications

   private String modifyingSelSetName = null;

   protected GlobalDelegates.Delegate5 proc_NamedSelSetCreated;
   protected virtual void NamedSelSetCreated(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);

      if (this.modifyingSelSetName == null || this.modifyingSelSetName != selSetName)
      {
         SelectionSetWrapper wrapper = new SelectionSetWrapper(selSetName);
         TreeNode tn = this.AddNode(wrapper, this.Tree.Nodes);
         this.Tree.AddToSortQueue(tn);
         this.Tree.AddToSortQueue(tn.Nodes);
         this.Tree.StartTimedSort(true);
      }
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetDeleted;
   protected virtual void NamedSelSetDeleted(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);

      if (this.modifyingSelSetName == null || this.modifyingSelSetName != selSetName)
         this.RemoveNode(selSetName);
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetPreModify;
   protected virtual void NamedSelSetPreModify(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);
      this.modifyingSelSetName = selSetName;
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetPostModify;
   protected virtual void NamedSelSetPostModify(IntPtr param, IntPtr info)
   {
      if (this.modifyingSelSetName == null)
         return;

      TreeNode tn = this.GetFirstTreeNode(this.modifyingSelSetName);
      IMaxNodeWrapper wrapper = HelperMethods.GetMaxNode(tn);
      if (tn == null || wrapper == null)
         return;

      List<TreeNode> tnNodes = tn.Nodes.ToList();
      foreach (TreeNode childTn in tnNodes)
      {
         //TODO: verify that this is correct.
         this.UnregisterNode(HelperMethods.GetMaxNode(childTn), childTn);
      }

      foreach (Object node in wrapper.ChildNodes)
      {
         this.AddNode(node, tn.Nodes);<commit_msg>Fix in selectionset mode<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Max;
using Outliner.Scene;
using Outliner.Controls.Tree;
using Outliner.Filters;
using Outliner.MaxUtils;
using System.Runtime.InteropServices;
//using Outliner.LayerTools;
using Outliner.Plugins;

namespace Outliner.Modes.SelectionSet
{
[OutlinerPlugin(OutlinerPluginType.TreeMode)]
[LocalizedDisplayName(typeof(Resources), "Mode_SelectionSets")]
public class SelectionSetMode : TreeMode
{
   private AllObjectsSelectionSet allObjectsSelSet;

   public SelectionSetMode(TreeView tree) : base(tree)
   {
      proc_NamedSelSetCreated    = new GlobalDelegates.Delegate5(this.NamedSelSetCreated);
      proc_NamedSelSetDeleted    = new GlobalDelegates.Delegate5(this.NamedSelSetDeleted);
      proc_NamedSelSetPreModify  = new GlobalDelegates.Delegate5(this.NamedSelSetPreModify);
      proc_NamedSelSetPostModify = new GlobalDelegates.Delegate5(this.NamedSelSetPostModify);
      proc_NamedSelSetRenamed    = new GlobalDelegates.Delegate5(this.NamedSelSetRenamed);

      this.allObjectsSelSet = new AllObjectsSelectionSet();
      this.Tree.DragDropHandler = new TreeViewDragDropHandler();
   }

   protected override void FillTree()
   {
      this.Tree.BeginUpdate();

      TreeNode allObjTn = this.AddNode(this.allObjectsSelSet, this.Tree.Nodes);
      allObjTn.FontStyle = System.Drawing.FontStyle.Italic;

      IINamedSelectionSetManager selSetMan = MaxInterfaces.SelectionSetManager;
      for (int i = 0; i < selSetMan.NumNamedSelSets; i++)
      {
         this.AddNode(new SelectionSetWrapper(i), this.Tree.Nodes);
      }

      this.Tree.Sort();
      this.Tree.EndUpdate();
   }

   public override TreeNode AddNode(IMaxNodeWrapper wrapper, TreeNodeCollection parentCol)
   {
      TreeNode tn = base.AddNode(wrapper, parentCol);

      if (wrapper is SelectionSetWrapper)
      {
         foreach (Object child in wrapper.ChildNodes)
            this.AddNode(child, tn.Nodes);
      }

      return tn;
   }

   override public DragDropHandler CreateDragDropHandler(IMaxNodeWrapper wrapper)
   {
      if (wrapper is SelectionSetWrapper)
         return new SelectionSetDragDropHandler((SelectionSetWrapper)wrapper);
      else if (wrapper is IINodeWrapper)
         return new IINodeDragDropHandler(wrapper);

      return base.CreateDragDropHandler(wrapper);
   }


   public override void Start()
   {
      this.RegisterSystemNotification(proc_NamedSelSetCreated, SystemNotificationCode.NamedSelSetCreated);
      this.RegisterSystemNotification(proc_NamedSelSetDeleted, SystemNotificationCode.NamedSelSetDeleted);
      this.RegisterSystemNotification(proc_NamedSelSetRenamed, SystemNotificationCode.NamedSelSetRenamed);
      this.RegisterSystemNotification(proc_NamedSelSetPreModify, SystemNotificationCode.NamedSelSetPreModify);
      this.RegisterSystemNotification(proc_NamedSelSetPostModify, SystemNotificationCode.NamedSelSetPostModify);

      this.RegisterNodeEventCallbackObject(new SelectionSetNodeEventCallbacks(this));

      base.Start();
   }


   #region NodeEventCallbacks

   protected class SelectionSetNodeEventCallbacks : TreeModeNodeEventCallbacks
   {
      protected new SelectionSetMode TreeMode;
      public SelectionSetNodeEventCallbacks(SelectionSetMode treeMode)
         : base(treeMode)
      {
         this.TreeMode = treeMode;
      }

      public override void Added(ITab<UIntPtr> nodes)
      {
         TreeNode allObjSelSetTn = this.TreeMode.GetFirstTreeNode(this.TreeMode.allObjectsSelSet);
         if (allObjSelSetTn == null)
            return;

         foreach (IINode node in IINodeHelpers.NodeKeysToINodeList(nodes))
         {
            this.TreeMode.AddNode(node, allObjSelSetTn.Nodes);
            this.Tree.AddToSortQueue(allObjSelSetTn.Nodes);
         }
         this.Tree.StartTimedSort(true);
      }
   }

   #endregion


   #region System notifications

   private String modifyingSelSetName = null;

   protected GlobalDelegates.Delegate5 proc_NamedSelSetCreated;
   protected virtual void NamedSelSetCreated(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);

      if (this.modifyingSelSetName == null || this.modifyingSelSetName != selSetName)
      {
         SelectionSetWrapper wrapper = new SelectionSetWrapper(selSetName);
         TreeNode tn = this.AddNode(wrapper, this.Tree.Nodes);
         this.Tree.AddToSortQueue(tn);
         this.Tree.AddToSortQueue(tn.Nodes);
         this.Tree.StartTimedSort(true);
      }
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetDeleted;
   protected virtual void NamedSelSetDeleted(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);

      if (this.modifyingSelSetName == null || this.modifyingSelSetName != selSetName)
         this.RemoveNode(selSetName);
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetPreModify;
   protected virtual void NamedSelSetPreModify(IntPtr param, IntPtr info)
   {
      IntPtr callParam = (IntPtr)MaxUtils.HelperMethods.GetCallParam(info);
      String selSetName = Marshal.PtrToStringUni(callParam);
      this.modifyingSelSetName = selSetName;
   }

   protected GlobalDelegates.Delegate5 proc_NamedSelSetPostModify;
   protected virtual void NamedSelSetPostModify(IntPtr param, IntPtr info)
   {
      if (this.modifyingSelSetName == null)
         return;

      TreeNode tn = this.GetFirstTreeNode(this.modifyingSelSetName);
      IMaxNodeWrapper wrapper = HelperMethods.GetMaxNode(tn);
      if (tn == null || wrapper == null)
         return;

      List<TreeNode> tnNodes = tn.Nodes.ToList();
      foreach (TreeNode childTn in tnNodes)
      {
         //TODO: verify that this is correct.
         this.UnregisterNode(HelperMethods.GetMaxNode(childTn), childTn);
         childTn.Remove();
      }

      foreach (Object node in wrapper.ChildNodes)
      {
         this.AddNode(node, tn.Nodes);
{'fraction_non_alphanumeric': 0.08035133376707873, 'fraction_numerical': 0.0016265452179570592, 'mean_word_length': 3.753382296095864, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5813775', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3524, 'n_words': 635}