Tanoda
pb_Selection.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using GILES.Interface;
6
7namespace GILES
8{
12 public class pb_Selection : pb_MonoBehaviourSingleton<pb_Selection>
13 {
14 protected override void Awake()
15 {
16 base.Awake();
17
18 Undo.AddUndoPerformedListener( UndoRedoPerformed );
19 Undo.AddRedoPerformedListener( UndoRedoPerformed );
20 }
21
24 public event Callback<IEnumerable<GameObject>> OnSelectionChange;
25
29 public event Callback<IEnumerable<GameObject>> OnRemovedFromSelection;
30
34 public static void AddOnSelectionChangeListener(Callback<IEnumerable<GameObject>> del)
35 {
36 if(instance.OnSelectionChange != null)
37 instance.OnSelectionChange += del;
38 else
39 instance.OnSelectionChange = del;
40 }
41
45 public static void AddOnRemovedFromSelectionListener(Callback<IEnumerable<GameObject>> del)
46 {
47 if(instance.OnRemovedFromSelection != null)
48 instance.OnRemovedFromSelection += del;
49 else
50 instance.OnRemovedFromSelection = del;
51 }
52
53 private List<GameObject> _gameObjects = new List<GameObject>();
54
56 public static List<GameObject> gameObjects { get { return instance._gameObjects; } }
57
61 public static void Clear()
62 {
63 if(instance._gameObjects.Count > 0)
64 {
65 if( instance.OnRemovedFromSelection != null)
66 instance.OnRemovedFromSelection(instance._gameObjects);
67 }
68
69 int cleared = instance._Clear();
70
71 if(cleared > 0)
72 {
73 if( instance.OnSelectionChange != null )
74 instance.OnSelectionChange(null);
75 }
76 }
77
81 public static GameObject activeGameObject
82 {
83 get
84 {
85 return instance._gameObjects.Count > 0 ? instance._gameObjects[0] : null;
86 }
87 }
88
92 public static void SetSelection(IEnumerable<GameObject> selection)
93 {
94 if( instance.OnRemovedFromSelection != null)
95 instance.OnRemovedFromSelection(instance._gameObjects);
96
97 instance._Clear();
98
99 foreach(GameObject go in selection)
100 instance._AddToSelection(go);
101
102 if( instance.OnSelectionChange != null )
103 instance.OnSelectionChange(selection);
104 }
105
109 public static void SetSelection(GameObject selection)
110 {
111 if (!selection) return;
112 SetSelection(new List<GameObject>() { selection });
113 }
114
118 public static void AddToSelection(GameObject go)
119 {
120 instance._AddToSelection(go);
121
122 if(instance.OnSelectionChange != null)
123 instance.OnSelectionChange(new List<GameObject>(){ go });
124 }
125
129 public static void RemoveFromSelection(GameObject go)
130 {
131 if(instance._RemoveFromSelection(go))
132 {
133 if( instance.OnRemovedFromSelection != null )
134 instance.OnRemovedFromSelection(new List<GameObject>() { go } );
135
136 if( instance.OnSelectionChange != null )
137 instance.OnSelectionChange(null);
138 }
139 }
140
141 private static void UndoRedoPerformed()
142 {
143 if( instance.OnSelectionChange != null )
144 instance.OnSelectionChange(null);
145 }
146
152 public static void OnExternalUpdate()
153 {
154 if( instance.OnSelectionChange != null )
155 instance.OnSelectionChange(null);
156 }
157
158#region Implementation
159
160 private void _InitializeSelected(GameObject go)
161 {
162 go.AddComponent<pb_SelectionHighlight>();
163 pb_Hierarchy.instance.HighlightSelected(go);
164 }
165
166 private void _DeinitializeSelected(GameObject go)
167 {
168 var highlight = go.GetComponentsInChildren<pb_SelectionHighlight>();
169
170 if(highlight != null)
171 pb_ObjectUtility.Destroy(highlight);
172
173 pb_Hierarchy.instance.RemoveSelected(go);
174 }
175
176 private bool _AddToSelection(GameObject go)
177 {
178 if(go != null && !_gameObjects.Contains(go))
179 {
180 _InitializeSelected(go);
181 _gameObjects.Add(go);
182
183 return true;
184 }
185 return false;
186 }
187
188 private bool _RemoveFromSelection(GameObject go)
189 {
190 if(go != null && _gameObjects.Contains(go) )
191 {
192 pb_ObjectUtility.Destroy(go.GetComponentsInChildren<pb_SelectionHighlight>());
193 _gameObjects.Remove(go);
194 pb_Hierarchy.instance.RemoveSelected(go);
195
196 return true;
197 }
198
199 return false;
200 }
201
202 private int _Clear()
203 {
204 int count = _gameObjects.Count;
205
206 for(int i = 0; i < count; i++)
207 _DeinitializeSelected(_gameObjects[i]);
208
209 _gameObjects.Clear();
210
211 return count;
212 }
213#endregion
214 }
215}
static void AddRedoPerformedListener(Callback callback)
Definition: Undo.cs:77
static void AddUndoPerformedListener(Callback callback)
Definition: Undo.cs:66
static List< GameObject > gameObjects
A list of the currently selected GameObjects.
Definition: pb_Selection.cs:56
static void AddOnSelectionChangeListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:34
static void RemoveFromSelection(GameObject go)
static void AddOnRemovedFromSelectionListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:45
static void AddToSelection(GameObject go)
static void SetSelection(IEnumerable< GameObject > selection)
Definition: pb_Selection.cs:92
static void Clear()
Definition: pb_Selection.cs:61
Callback< IEnumerable< GameObject > > OnSelectionChange
Definition: pb_Selection.cs:24
static GameObject activeGameObject
Definition: pb_Selection.cs:82
static void SetSelection(GameObject selection)
Callback< IEnumerable< GameObject > > OnRemovedFromSelection
Definition: pb_Selection.cs:29
override void Awake()
Definition: pb_Selection.cs:14
static void OnExternalUpdate()
delegate void Callback()