Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_HighlightedEditorComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Editor", description: "Manages Highlights on entities. Such as notification icons on Faction UI", icon: "WBData/ComponentEditorProps/componentEditor.png")]
3 {
4 };
5 
12 {
13  //References
14  protected SCR_DelegateFactionManagerComponent m_DelegateFactionManager;
15 
16  //Main Script invokers
17  protected ref ScriptInvoker Event_OnAddHighlight = new ScriptInvoker;
18  protected ref ScriptInvoker Event_OnUpdateHighlight = new ScriptInvoker;
19  protected ref ScriptInvoker Event_OnRemoveHighlight = new ScriptInvoker;
20 
21  //State
22  protected bool m_bReplicationIsRunning;
23 
24  //Highlighted entities
25  protected ref map<SCR_EditableEntityComponent, EEditableEntityHighlight> m_HighlightedEntities = new map<SCR_EditableEntityComponent, EEditableEntityHighlight>;
26 
27  //-------------------------- Add Highlight types --------------------------\\
28  protected void AddHighlight(SCR_EditableEntityComponent entity, EEditableEntityHighlight highlight) //Highlights instead of highlight singilar
29  {
30  if (!entity)
31  return;
32  RplId entityID = Replication.FindId(entity);
33 
34  EEditableEntityHighlight allHighlights;
35 
36  //Entity in map
37  if (m_HighlightedEntities.Find(entity, allHighlights))
38  {
39  //Already has highlight
40  if ((allHighlights & highlight))
41  return;
42 
43  //Add highlight
44  allHighlights |= highlight;
45 
46  //Update Map
47  m_HighlightedEntities.Set(entity, allHighlights);
48  Event_OnUpdateHighlight.Invoke(entity, allHighlights, GetHighlightCount(allHighlights));
49 
50  //Send to owner
51  Rpc(AddHighlightRpc, entityID, allHighlights);
52 
53  }
54  //Entity not in map
55  else
56  {
57  //Add highlight
58  allHighlights |= highlight;
59 
60  //Add to map
61  m_HighlightedEntities.Insert(entity, allHighlights);
62 
63  //Send to owner
64  Rpc(AddHighlightToListRpc, entityID, allHighlights);
65  }
66 
67  //Cleanup
68  CleanupHighlightedMap();
69 
70  //Server Event
71  Event_OnAddHighlight.Invoke(entity);
72  }
73 
74  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
75  protected void AddHighlightRpc(RplId entityID, EEditableEntityHighlight allHighlights)
76  {
77  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
78  if (!entity)
79  return;
80 
81  //Update map
82  m_HighlightedEntities.Set(entity, allHighlights);
83 
84  //Cleanup
85  CleanupHighlightedMap();
86 
87  //Script invokers
88  Event_OnAddHighlight.Invoke(entity);
89  Event_OnUpdateHighlight.Invoke(entity, allHighlights, GetHighlightCount(allHighlights));
90 
91  }
92 
93  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
94  protected void AddHighlightToListRpc(RplId entityID, EEditableEntityHighlight allHighlights)
95  {
96  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
97  if (!entity)
98  return;
99 
100  //Add to map
101  m_HighlightedEntities.Insert(entity, allHighlights);
102 
103  //Cleanup
104  CleanupHighlightedMap();
105 
106  //Script invoker
107  Event_OnAddHighlight.Invoke(entity);
108  }
109 
110 
111  //-------------------------- Remove Highlight types --------------------------\\
112  protected void RemoveHighlight(SCR_EditableEntityComponent entity, EEditableEntityHighlight highlight)
113  {
114  if (!entity)
115  return;
116  RplId entityID = Replication.FindId(entity);
117 
118  EEditableEntityHighlight allHighlights;
119 
120  //Not found in map
121  if (!m_HighlightedEntities.Find(entity, allHighlights))
122  return;
123 
124  //Does not have highlight
125  if (!(allHighlights & highlight))
126  return;
127 
128  //Remove highlight
129  allHighlights &= ~highlight;
130 
131  //Not highlighted so remove from map
132  if (allHighlights == 0)
133  {
134  //Remove from map
135  m_HighlightedEntities.Remove(entity);
136 
137  //Send to owner
138  Rpc(RemoveHighlightFromListRpc, entityID);
139  }
140  //Update map
141  else
142  {
143  //Update map
144  m_HighlightedEntities.Set(entity, allHighlights);
145  Event_OnUpdateHighlight.Invoke(entity, allHighlights, GetHighlightCount(allHighlights));
146 
147  //Send to owner
148  Rpc(RemoveHighlightRpc, entityID, allHighlights);
149  }
150 
151  //Clean up
152  CleanupHighlightedMap();
153 
154  //Server Event
155  Event_OnRemoveHighlight.Invoke(entity);
156  }
157 
158 
159  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
160  protected void RemoveHighlightRpc(RplId entityID, EEditableEntityHighlight allHighlights)
161  {
162  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
163  if (!entity)
164  return;
165 
166  //Update map
167  m_HighlightedEntities.Set(entity, allHighlights);
168 
169  //Cleanup
170  CleanupHighlightedMap();
171 
172  //Script invokers
173  Event_OnRemoveHighlight.Invoke(entity);
174  Event_OnUpdateHighlight.Invoke(entity, allHighlights, GetHighlightCount(allHighlights));
175  }
176 
177 
178  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
179  protected void RemoveHighlightFromListRpc(RplId entityID)
180  {
181  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
182  if (!entity)
183  return;
184 
185  //Remove from map
186  m_HighlightedEntities.Remove(entity);
187 
188  //Cleanup
189  CleanupHighlightedMap();
190 
191  //Script invoker
192  Event_OnRemoveHighlight.Invoke(entity);
193  }
194 
195 
196  //-------------------------- Cleanup --------------------------\\
197  protected void CleanupHighlightedMap()
198  {
199  foreach (SCR_EditableEntityComponent entity, EEditableEntityHighlight highlight: m_HighlightedEntities)
200  {
201  if (entity == null)
202  {
203  m_HighlightedEntities.Remove(entity);
204  }
205  }
206  }
207 
208  //-------------------------- Get is Highlight --------------------------\\
209 
214  bool IsEntityHighlighted(SCR_EditableEntityComponent entity)
215  {
216  if (entity == null)
217  return false;
218 
219  //Not found in map
220  EEditableEntityHighlight allHighlights;
221  if (!m_HighlightedEntities.Find(entity, allHighlights))
222  return false;
223 
224  return m_HighlightedEntities.Contains(entity);
225  }
226 
234  bool GetEntityHighlightedState(SCR_EditableEntityComponent entity, out EEditableEntityHighlight highlight, out int highlightAmount)
235  {
236  if (entity == null)
237  return false;
238 
239  //Not found in map
240  EEditableEntityHighlight allHighlights;
241  if (!m_HighlightedEntities.Find(entity, allHighlights))
242  return false;
243 
244  //Return highlight if one or default if many
245  if ((allHighlights & (allHighlights - 1)) == 0)
246  {
247  highlight = allHighlights;
248  highlightAmount = 1;
249  }
250  else
251  {
252  highlight = EEditableEntityHighlight.DEFAULT;
253  highlightAmount = GetHighlightCount(allHighlights);
254  }
255 
256  return true;
257  }
258 
265  bool HasSpecificHighlight(SCR_EditableEntityComponent entity, EEditableEntityHighlight highlight)
266  {
267  if (!entity)
268  return false;
269 
270  //Not found in map
271  EEditableEntityHighlight allHighlights;
272  if (!m_HighlightedEntities.Find(entity, allHighlights))
273  return false;
274 
275  //Return if has specific highlight
276  return (allHighlights & highlight);
277  }
278 
279 
280  protected int GetHighlightCount(EEditableEntityHighlight highlight)
281  {
282  array<int> highlightArray = new array<int>;
283  SCR_Enum.BitToIntArray(highlight, highlightArray);
284  return highlightArray.Count();
285  }
286 
287  //-------------------------- Faction Spawnpoint --------------------------\\
288  protected void FactionSpawnPointsInit()
289  {
290  if (!m_bReplicationIsRunning)
291  return;
292 
293  map<Faction, SCR_EditableFactionComponent> factionMap = new map<Faction, SCR_EditableFactionComponent>;
294  m_DelegateFactionManager.GetFactionDelegates(factionMap);
295 
296  foreach (Faction faction, SCR_EditableFactionComponent editableFaction: factionMap)
297  {
298  if (editableFaction.GetFactionSpawnPointCount() == 0)
299  AddHighlight(editableFaction, EEditableEntityHighlight.NO_FACTION_SPAWNPOINTS);
300  }
301  }
302 
303  protected void OnFactionSpawnPointsUpdated(Faction faction, int factionSpawnPointCount)
304  {
305  if (!m_bReplicationIsRunning)
306  return;
307 
308  //Get editable entity
309  SCR_EditableFactionComponent factionEntity = m_DelegateFactionManager.GetFactionDelegate(faction);
310  if (!factionEntity)
311  return;
312 
313  //Send to owner
314  if (factionSpawnPointCount == 0)
315  {
316  if (!HasSpecificHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_SPAWNPOINTS))
317  AddHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_SPAWNPOINTS);
318  }
319  else
320  {
321  if (HasSpecificHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_SPAWNPOINTS))
322  RemoveHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_SPAWNPOINTS);
323  }
324  }
325 
326 
327  //-------------------------- Faction Tasks --------------------------\\
328  protected void FactionTasksInit()
329  {
330  if (!m_bReplicationIsRunning)
331  return;
332 
333  map<Faction, SCR_EditableFactionComponent> factionMap = new map<Faction, SCR_EditableFactionComponent>;
334  m_DelegateFactionManager.GetFactionDelegates(factionMap);
335 
336  foreach (Faction faction, SCR_EditableFactionComponent editableFaction: factionMap)
337  {
338  if (editableFaction.GetFactionTasksCount() == 0)
339  AddHighlight(editableFaction, EEditableEntityHighlight.NO_FACTION_TASKS);
340  }
341  }
342 
343  protected void OnFactionTasksUpdated(Faction faction, int factionTaskCount)
344  {
345  if (!m_bReplicationIsRunning)
346  return;
347 
348  //Get editable entity
349  SCR_EditableFactionComponent factionEntity = m_DelegateFactionManager.GetFactionDelegate(faction);
350  if (!factionEntity)
351  return;
352 
353  //Send to owner
354  if (factionTaskCount == 0)
355  {
356  if (!HasSpecificHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_TASKS))
357  AddHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_TASKS);
358  }
359  else
360  {
361  if (HasSpecificHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_TASKS))
362  RemoveHighlight(factionEntity, EEditableEntityHighlight.NO_FACTION_TASKS);
363  }
364  }
365 
366 
367  //-------------------------- Get Invokers --------------------------\\
368  ScriptInvoker GetOnAddHighlight()
369  {
370  return Event_OnAddHighlight;
371  }
372 
373  ScriptInvoker GetOnRemoveHighlight()
374  {
375  return Event_OnRemoveHighlight;
376  }
377 
378  ScriptInvoker GetOnUpdateHighlight()
379  {
380  return Event_OnUpdateHighlight;
381  }
382 
383  //-------------------------- On Editor Open/Close --------------------------\\
384  override void EOnEditorOpenServer()
385  {
386  m_bReplicationIsRunning = Replication.IsRunning();
387 
388  m_DelegateFactionManager = SCR_DelegateFactionManagerComponent.GetInstance();
389 
390  if (m_DelegateFactionManager)
391  {
392  FactionSpawnPointsInit();
393  FactionTasksInit();
394 
395  map<Faction, SCR_EditableFactionComponent> factionMap = new map<Faction, SCR_EditableFactionComponent>;
396  m_DelegateFactionManager.GetFactionDelegates(factionMap);
397 
398  foreach (Faction faction, SCR_EditableFactionComponent editableFaction: factionMap)
399  {
400  editableFaction.GetOnSpawnPointCountChanged().Insert(OnFactionSpawnPointsUpdated);
401  editableFaction.GetOnTaskCountChanged().Insert(OnFactionTasksUpdated);
402  }
403  }
404  }
405 
406  override void EOnEditorCloseServer()
407  {
408  if (m_DelegateFactionManager)
409  {
410  map<Faction, SCR_EditableFactionComponent> factionMap = new map<Faction, SCR_EditableFactionComponent>;
411  m_DelegateFactionManager.GetFactionDelegates(factionMap);
412 
413  foreach (Faction faction, SCR_EditableFactionComponent editableFaction: factionMap)
414  {
415  editableFaction.GetOnSpawnPointCountChanged().Remove(OnFactionSpawnPointsUpdated);
416  editableFaction.GetOnTaskCountChanged().Remove(OnFactionTasksUpdated);
417  }
418  }
419 
420  //Clear highlighted list
421  m_HighlightedEntities.Clear();
422  }
423 };
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_Enum
Definition: SCR_Enum.c:1
SCR_HighlightedEditorComponent
Definition: SCR_HighlightedEditorComponent.c:11
EEditableEntityHighlight
EEditableEntityHighlight
Definition: EEditableEntityHighlight.c:2
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
SCR_HighlightedEditorComponentClass
Definition: SCR_HighlightedEditorComponent.c:2
EOnEditorCloseServer
override protected void EOnEditorCloseServer()
Definition: SCR_CampaignBuildingEditorComponent.c:539
SCR_BaseEditorComponent
Definition: SCR_BaseEditorComponent.c:119
SCR_BaseEditorComponentClass
Definition: SCR_BaseEditorComponent.c:2
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
Faction
Definition: Faction.c:12
SCR_DelegateFactionManagerComponent
Definition: SCR_DelegateFactionManagerComponent.c:15
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180