Advertisement
dimmpixeye

Untitled

Aug 12th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1.     public class ProcessingTags : IComponent
  2.     {
  3.         private Dictionary<int, int> tags = new Dictionary<int, int>();
  4.         private Action actionChanged;
  5.  
  6.      
  7.         public void BindTo(Action actionChanged)
  8.         {
  9.             this.actionChanged = actionChanged;
  10.         }
  11.  
  12.  
  13.         public bool ContainAny(params int[] filter)
  14.         {
  15.             for (var i = 0; i < filter.Length; i++)
  16.             {
  17.                 if (tags.ContainsKey(filter[i])) return true;
  18.             }
  19.  
  20.             return false;
  21.         }
  22.  
  23.         public bool ContainAll(params int[] filter)
  24.         {
  25.             for (var i = 0; i < filter.Length; i++)
  26.             {
  27.                 if (!tags.ContainsKey(filter[i])) return false;
  28.             }
  29.  
  30.             return true;
  31.         }
  32.  
  33.         public bool Contain(int val)
  34.         {
  35.             return tags.ContainsKey(val);
  36.         }
  37.  
  38.  
  39.         public void Remove(params int[] ids)
  40.         {
  41.             bool tagsChanged = false;
  42.             foreach (var index in ids)
  43.             {
  44.                 int val;
  45.                 if (tags.TryGetValue(index, out val))
  46.                 {
  47.                     val = Mathf.Max(0, val - 1);
  48.  
  49.                     if (val == 0)
  50.                     {
  51.                         tags.Remove(index);
  52.                         tagsChanged = true;
  53.                     }
  54.                     else tags[index] = val;
  55.                 }
  56.             }
  57.  
  58.             if (tagsChanged == false) return;
  59.             actionChanged();
  60.         }
  61.  
  62.         public void Remove(int id)
  63.         {
  64.             int val;
  65.             if (tags.TryGetValue(id, out val))
  66.             {
  67.                 val = Mathf.Max(0, val - 1);
  68.  
  69.                 if (val == 0)
  70.                 {
  71.                     tags.Remove(id);
  72.                     actionChanged();
  73.                 }
  74.                 else tags[id] = val;
  75.             }
  76.         }
  77.  
  78.         public void RemovAll(int id)
  79.         {
  80.             if (tags.ContainsKey(id))
  81.                 tags.Remove(id);
  82.  
  83.             actionChanged();
  84.         }
  85.  
  86.         public void Add(params int[] ids)
  87.         {
  88.             var c = false;
  89.             foreach (var index in ids)
  90.             {
  91.                 if (tags.ContainsKey(index))
  92.                 {
  93.                     tags[index] += 1;
  94.                     continue;
  95.                 }
  96.  
  97.  
  98.                 tags.Add(index, 1);
  99.                 c = true;
  100.             }
  101.  
  102.             if (!c) return;
  103.             actionChanged();
  104.         }
  105.  
  106.         public void Add(int id)
  107.         {
  108.             if (tags.ContainsKey(id))
  109.             {
  110.                 tags[id] += 1;
  111.  
  112.                 return;
  113.             }
  114.  
  115.             tags.Add(id, 1);
  116.             actionChanged();
  117.         }
  118.  
  119.         public void Dispose()
  120.         {
  121.             tags.Clear();
  122.             actionChanged = null;
  123.         }
  124.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement