Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_RefPreviewEntity.c
Go to the documentation of this file.
1 //#define PREVIEW_ENTITY_SHOW_REFERENCE //--- Don't delete reference entity after transformation ended, to show how it looked like.
2 
3 [EntityEditorProps(category: "GameScripted/Preview", description: "")]
5 {
6 };
11 {
12  protected ref set<SCR_EditableEntityComponent> m_aEditedEntities;
13  protected bool m_bIsInstant;
14 
20  static void SpawnAndApplyReference(SCR_EditableEntityComponent entity, SCR_EditorPreviewParams param)
21  {
22  ResourceName material;
23 #ifdef PREVIEW_ENTITY_SHOW_REFERENCE
24  material = "{D0126AF0E6A27141}Common/Materials/Colors/colorRed.emat";
25 #endif
26  EPreviewEntityFlag flags = EPreviewEntityFlag.IGNORE_TERRAIN | EPreviewEntityFlag.IGNORE_PREFAB | EPreviewEntityFlag.ONLY_EDITABLE;
27 
28  SCR_RefPreviewEntity refEntity = SCR_RefPreviewEntity.Cast(SCR_RefPreviewEntity.SpawnPreviewFromEditableEntity(entity, "SCR_RefPreviewEntity", GetGame().GetWorld(), null, material, flags));
29  if (!refEntity)
30  {
31  Print("SCR_RefPreviewEntity:: refEntity was not spawned!", LogLevel.WARNING);
32  return;
33  }
34 
35  refEntity.m_EditableEntity = entity;
36  refEntity.SetAsInstant();
37  refEntity.ApplyReference(param);
38 
39 #ifndef PREVIEW_ENTITY_SHOW_REFERENCE
40  delete refEntity;
41 #endif
42  }
43 
48  void ApplyReference(SCR_EditorPreviewParams param)
49  {
50  //--- Get navmesh rebuild areas *BEFORE* the change
51  SCR_AIWorld aiWorld = SCR_AIWorld.Cast(GetGame().GetAIWorld());
52  array<ref Tuple2<vector, vector>> areas = new array<ref Tuple2<vector, vector>>; //--- Min, max
53  array<bool> redoAreas = new array<bool>;
54  if (aiWorld)
55  {
56  foreach (SCR_BasePreviewEntity child: m_aChildren)
57  {
58  if (child.m_Entity)
59  aiWorld.GetNavmeshRebuildAreas(child.m_Entity, areas, redoAreas);
60  }
61  }
62 
63  //--- Update reference (ToDo: Do it only when needed)
64  vector transform[4];
65  param.GetWorldTransform(transform);
66  UpdateReference(transform, param.m_VerticalMode, param.m_bIsUnderwater);
67 
68  //--- Apply changes on all children, and get navmesh areas *AFTER* the change
69  SCR_RefPreviewEntity childRef;
70  if (aiWorld)
71  {
72  if (m_aChildren)
73  {
74  foreach (SCR_BasePreviewEntity child: m_aChildren)
75  {
76  childRef = SCR_RefPreviewEntity.Cast(child);
77  if (childRef)
78  {
79  childRef.ApplyChild(param, true, m_aEditedEntities, m_bIsInstant);
80  if (child.m_Entity)
81  aiWorld.GetNavmeshRebuildAreas(child.m_Entity, areas, redoAreas);
82  }
83  }
84  }
85  aiWorld.RequestNavmeshRebuildAreas(areas, redoAreas);
86  }
87  else
88  {
89  if (m_aChildren)
90  {
91  foreach (SCR_BasePreviewEntity child: m_aChildren)
92  {
93  childRef = SCR_RefPreviewEntity.Cast(child);
94  if (childRef)
95  childRef.ApplyChild(param, true, m_aEditedEntities, m_bIsInstant);
96  }
97  }
98  }
99  }
103  void SetAsInstant()
104  {
105  m_bIsInstant = true;
106  }
107  protected void ApplyChild(SCR_EditorPreviewParams param, bool isDirectChild, set<SCR_EditableEntityComponent> editedEntities, bool isInstant)
108  {
109  if (!m_EditableEntity)
110  return;
111 
112  vector transform[4];
113  GetWorldTransform(transform);
114 
115  bool changeTransformation = true;
116  if (isDirectChild)
117  {
118  SCR_EditableEntityComponent currentParent = m_EditableEntity.GetParentEntity();
119  SCR_EditableEntityComponent newParent;
120  bool changeParent = false;
121 
122  if (param.m_Target && (param.m_TargetInteraction == EEditableEntityInteraction.LAYER || param.m_TargetInteraction == EEditableEntityInteraction.PASSENGER))
123  {
124  //--- Hovering over target layer
125  newParent = param.m_Target;
126  changeParent = true;
127  changeTransformation = false;
128  }
129  else if (param.m_Parent != currentParent && param.m_Parent != m_EditableEntity && (param.m_bParentChanged || !currentParent || !currentParent.HasEntityFlag(EEditableEntityFlag.INDIVIDUAL_CHILDREN)))
130  {
131  //--- Entered target layer (and the target is not the entity itself)
132  newParent = param.m_Parent;
133  changeParent = true;
134  }
135  if (changeParent)
136  {
137  //--- Don't change parent of group members when their group is also being edited
138  SCR_EditableEntityComponent editableGroup = m_EditableEntity.GetAIGroup();
139  if (m_EditableEntity == editableGroup || !editableGroup || editedEntities.Find(editableGroup) == -1)
140  {
141  //--- Set the parent, and update the reference so all other entities use it as well (in case it changed, like when moving character to root)
142  param.m_Parent = m_EditableEntity.SetParentEntity(newParent, true);
143  }
144  }
145  }
146 
147  if (changeTransformation)
148  {
149  //--- Don't transform vehicle crew when the vehicle is also being edited
150  SCR_EditableEntityComponent editableVehicle = m_EditableEntity.GetVehicle();
151  if (!editableVehicle || editedEntities.Find(editableVehicle) == -1)
152  {
153  if (isInstant)
154  {
155  vector localPos = m_EditableEntity.GetOwner().CoordToLocal(transform[3]);
156  localPos -= m_EditableEntity.GetIconPos();
157  transform[3] = m_EditableEntity.GetOwner().CoordToParent(localPos);
158  }
159  m_EditableEntity.SetTransform(transform, isDirectChild && !isInstant);
160  }
161 
162  if (m_aChildren)
163  {
164  SCR_RefPreviewEntity childRef;
165  foreach (SCR_BasePreviewEntity child: m_aChildren)
166  {
167  childRef = SCR_RefPreviewEntity.Cast(child);
168  if (childRef)
169  childRef.ApplyChild(param, false, editedEntities, isInstant);
170  }
171  }
172  }
173  }
174  protected void UpdateReference(vector transform[4], EEditorTransformVertical verticalMode, bool isUnderwater)
175  {
176  TraceParam trace;
177  if (verticalMode == EEditorTransformVertical.GEOMETRY)
178  {
179  trace = new TraceParam();
180  trace.ExcludeArray = m_aExcludeArray;
181  }
182 
183  float currentHeight;
184  if (!SCR_Enum.HasFlag(m_Flags, EPreviewEntityFlag.IGNORE_TERRAIN))
185  {
186  vector currentPos = GetWorldTransformAxis(3);
187  currentHeight = currentPos[1] - SCR_TerrainHelper.GetTerrainY(currentPos, GetWorld(), !SCR_Enum.HasFlag(m_Flags, EPreviewEntityFlag.UNDERWATER), trace);
188  }
189  float height = transform[3][1] - SCR_TerrainHelper.GetTerrainY(transform[3], GetWorld(), !isUnderwater, trace);
190 
191  m_fHeightTerrain = 0;
192  SetPreviewTransform(transform, verticalMode, height - currentHeight, isUnderwater, trace);
193  }
194  override protected void EOnPreviewInit(SCR_BasePreviewEntry entry, SCR_BasePreviewEntity root)
195  {
196  super.EOnPreviewInit(entry, root);
197 
198  SCR_RefPreviewEntity refRoot = SCR_RefPreviewEntity.Cast(root);
199  if (refRoot)
200  {
201  if (!refRoot.m_aEditedEntities)
202  refRoot.m_aEditedEntities = new set<SCR_EditableEntityComponent>();
203 
204  refRoot.m_aEditedEntities.Insert(m_EditableEntity);
205  }
206  }
207 };
SCR_TerrainHelper
Definition: SCR_TerrainHelper.c:1
EEditableEntityInteraction
EEditableEntityInteraction
Type of suggested interaction when hovering edited entity on top of another entity.
Definition: EEditableEntityInteraction.c:5
EEditableEntityFlag
EEditableEntityFlag
Unique flags of the entity.
Definition: EEditableEntityFlag.c:5
SCR_Enum
Definition: SCR_Enum.c:1
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
SCR_BasePreviewEntry
Definition: SCR_BasePreviewEntry.c:2
SCR_EditablePreviewEntity
Definition: SCR_EditablePreviewEntity.c:8
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_BasePreviewEntity
Definition: SCR_BasePreviewEntity.c:9
SCR_EditablePreviewEntityClass
Definition: SCR_EditablePreviewEntity.c:2
EPreviewEntityFlag
EPreviewEntityFlag
Definition: EPreviewEntityFlag.c:1
SCR_AIWorld
Definition: SCR_AIWorld.c:23
EEditorTransformVertical
EEditorTransformVertical
Vertical transformation mode.
Definition: EEditorTransformVertical.c:5
m_aChildren
protected ref array< SCR_ScenarioFrameworkLayerBase > m_aChildren
Definition: SCR_ScenarioFrameworkLayerBase.c:57
SCR_RefPreviewEntityClass
Definition: SCR_RefPreviewEntity.c:4
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
SCR_RefPreviewEntity
Definition: SCR_RefPreviewEntity.c:10
SCR_EditorPreviewParams
Network packet of variables for entity placing and transformation.
Definition: SCR_EditorPreviewParams.c:4
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180