Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SlotCompositionComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Editor (Editables)", description: "")]
2 class SCR_SlotCompositionComponentClass : ScriptComponentClass
3 {
4  [Attribute(params: "et", category: "Composition", desc: "Slot this composition fits into.")]
5  protected ResourceName m_SlotPrefab;
6 
7  [Attribute(defvalue: "1", category: "Composition", desc: "When enabled, children will be snapped and oriented to terrain when the composition is transformed.")]
8  protected bool m_bOrientChildrenToTerrain;
9 
10  [Attribute(defvalue: "1", category: "Composition", desc: "When enabled, children will not be turned into editable entities. The composition will appear as a single entity in in-game editor.")]
11  protected bool m_bEditableChildren; //--- Not read in the script, but accessed by EditablePrefabsConfig when generating editable prefabs
12 
13  //------------------------------------------------------------------------------------------------
15  ResourceName GetSlotPrefab()
16  {
17  return m_SlotPrefab;
18  }
19 
20  //------------------------------------------------------------------------------------------------
23  bool CanOrientChildrenToTerrain()
24  {
25  return m_bOrientChildrenToTerrain;
26  }
27 
28  //------------------------------------------------------------------------------------------------
33  static ResourceName GetSlotPrefab(IEntityComponentSource componentSource)
34  {
35  ResourceName slotPrefab;
36  componentSource.Get("m_SlotPrefab", slotPrefab);
37  return slotPrefab;
38  }
39 };
40 
42 
45 {
46  protected GenericEntity m_Owner;
47  protected static bool m_bIgnoreOrientChildrenToTerrain;
48 
49  //------------------------------------------------------------------------------------------------
52  ResourceName GetSlotPrefab()
53  {
55  if (prefabData)
56  return prefabData.GetSlotPrefab();
57  else
58  return ResourceName.Empty;
59  }
60 
61  //------------------------------------------------------------------------------------------------
64  bool CanOrientChildrenToTerrain()
65  {
67  if (prefabData)
68  return prefabData.CanOrientChildrenToTerrain();
69  else
70  return false;
71  }
72 
73  //------------------------------------------------------------------------------------------------
75  static void IgnoreOrientChildrenToTerrain()
76  {
77  m_bIgnoreOrientChildrenToTerrain = true;
78  }
79 
80  //------------------------------------------------------------------------------------------------
83  void OrientToTerrain()
84  {
85  //--- Don't initialize child compositions (they will be initialized as part of parent's init)
86  if (!CanOrientChildrenToTerrain() || m_Owner.GetParent())
87  return;
88 
89  BaseWorld world = m_Owner.GetWorld();
90  SetChildTransform(m_Owner, world);
91  m_Owner.Update();
92  }
93 
94  //------------------------------------------------------------------------------------------------
95  protected void SetChildTransform(IEntity owner, BaseWorld world)
96  {
97  //--- Find entity's height
98  float height = 0;
99  if (owner.GetParent())
100  {
101  //--- Use original local height (used only during initial transformation)
102  height = owner.GetLocalTransformAxis(3)[1];
103  }
104  else
105  {
106  //--- Maintain existing height ATL
107  height = SCR_TerrainHelper.GetHeightAboveTerrain(owner.GetOrigin(), world);
108  }
109 
110  //--- Apply transformation
111  vector transform[4];
112  owner.GetWorldTransform(transform);
113  if (owner == m_Owner)
114  SCR_TerrainHelper.SnapToTerrain(transform, world); //--- Don't rotate root entity. Its transformation is broadcasted to clients, who would then think the rotated state is default
115  else
116  SCR_TerrainHelper.SnapAndOrientToTerrain(transform, world);
117 
118  transform[3] = transform[3] + Vector(0, height, 0);
119 
120  //If entity should be horizontally aligned rather then aligned to terrain
121  SCR_HorizontalAlignComponent horizontalAlignComponent = SCR_HorizontalAlignComponent.Cast(owner.FindComponent(SCR_HorizontalAlignComponent));
122  if (horizontalAlignComponent)
123  {
124  vector angles = Math3D.MatrixToAngles(transform);
125  angles[1] = 0;
126  angles[2] = 0;
127  Math3D.AnglesToMatrix(angles, transform);
128  }
129 
130 #ifdef WORKBENCH
131  //--- When in World Editor, apply changes to attributes directly
132  if (SCR_Global.IsEditMode(owner))
133  {
134  WorldEditorAPI api = GenericEntity.Cast(owner)._WB_GetEditorAPI();
135  IEntity parent = owner.GetParent();
136  vector pos = transform[3];
137  if (parent)
138  {
139  vector parentTransform[3];
140  parent.GetWorldTransform(parentTransform);
141  Math3D.MatrixInvMultiply3(parentTransform, transform, transform);
142  pos = parent.CoordToLocal(pos);
143  }
144  vector angles = Math3D.MatrixToAngles(transform);
145  IEntitySource ownerSrc = api.EntityToSource(owner);
146  api.SetVariableValue(ownerSrc, null, "coords", pos.ToString(false));
147  api.SetVariableValue(ownerSrc, null, "angleX", angles[1].ToString());
148  api.SetVariableValue(ownerSrc, null, "angleY", angles[0].ToString());
149  api.SetVariableValue(ownerSrc, null, "angleZ", angles[2].ToString());
150  }
151  else
152  {
153  owner.SetWorldTransform(transform);
154  }
155 #else
156  owner.SetWorldTransform(transform);
157 #endif
158 
159  //--- Process children recursively
161  if (composition && composition.CanOrientChildrenToTerrain())
162  {
163  IEntity child = owner.GetChildren();
164  while (child)
165  {
166  SetChildTransform(child, world);
167  child = child.GetSibling();
168  }
169  }
170  }
171 
172  //------------------------------------------------------------------------------------------------
173  override void EOnInit(IEntity owner)
174  {
175  if (SCR_Global.IsEditMode(owner))
176  return;
177 
178  if (m_bIgnoreOrientChildrenToTerrain)
179  {
180  m_bIgnoreOrientChildrenToTerrain = false;
181  return;
182  }
183 
184  //--- Initialize transform on every machine (ToDo: Remove SCR_EditableEntityComponent check; rather, have editable entities suppress this functionality)
185  if (!owner.FindComponent(SCR_EditableEntityComponent))
186  OrientToTerrain();
187  }
188 
189  //------------------------------------------------------------------------------------------------
190  override void OnPostInit(IEntity owner)
191  {
192  m_Owner = GenericEntity.Cast(owner);
193 
194  SetEventMask(owner, EntityEvent.INIT);
195  }
196 
197 #ifdef WORKBENCH
198  //------------------------------------------------------------------------------------------------
205  static bool _WB_ConfigureComposition(WorldEditorAPI api, IEntitySource entitySource, bool isRoot = true, bool delayed = false)
206  {
207  //--- Skip sub-compositions
208  if (!isRoot && SCR_BaseContainerTools.FindComponentSource(entitySource, SCR_SlotCompositionComponent))
209  return false;
210 
211  bool isChange;
212  IEntityComponentSource hierarchySource = SCR_BaseContainerTools.FindComponentSource(entitySource, "Hierarchy");
213  if (hierarchySource)
214  {
215  //--- Has hierarchy, make sure it's enabled
216  hierarchySource.Set("Enabled", true);
217  }
218  else
219  {
220  //--- Doesn't have hierarchy, add one
221  isChange = true;
222  if (delayed)
223  SCR_SlotCompositionHelperEntity.CreateHierarchy(api, entitySource);
224  else
225  api.CreateComponent(entitySource, "Hierarchy");
226  }
227 
228  //--- Process children recursively
229  for (int i = 0, count = entitySource.GetNumChildren(); i < count; i++)
230  {
231  isChange |= _WB_ConfigureComposition(api, entitySource.GetChild(i), false);
232  }
233 
234  return isChange;
235  }
236 
237  //------------------------------------------------------------------------------------------------
238  override array<ref WB_UIMenuItem> _WB_GetContextMenuItems(IEntity owner)
239  {
240  return {
241  new WB_UIMenuItem("Configure composition", 0),
242  new WB_UIMenuItem("Orient to terrain", 1)
243  };
244  }
245 
246  //------------------------------------------------------------------------------------------------
247  override void _WB_OnContextMenu(IEntity owner, int id)
248  {
249  WorldEditorAPI api = GenericEntity.Cast(owner)._WB_GetEditorAPI();
250  switch (id)
251  {
252  case 0:
253 
254  //--- Gen entity instance
255  IEntitySource entitySource = api.EntityToSource(owner);
256  if (!entitySource) break;
257 
258  //--- Get entity prefab
259  entitySource = entitySource.GetAncestor();
260  if (!entitySource) break;
261 
262  //--- Process the prefab
263  api.BeginEntityAction();
264  _WB_ConfigureComposition(api, entitySource, true, true);
265  api.EndEntityAction();
266  break;
267  case 1:
268  api.BeginEntityAction();
269  OrientToTerrain();
270  api.EndEntityAction();
271  break;
272  }
273  }
274 #endif
275 }
276 
277 #ifdef WORKBENCH
278 //--- Adding a component from inside a context action causes error, this is a hotfix
279 class SCR_SlotCompositionHelperEntityClass : GenericEntityClass {}
280 class SCR_SlotCompositionHelperEntity : GenericEntity
281 {
282  protected IEntitySource m_EntitySource;
283 
284  //------------------------------------------------------------------------------------------------
288  static void CreateHierarchy(WorldEditorAPI api, IEntitySource entitySource)
289  {
290  IEntitySource src = api.CreateEntity("SCR_SlotCompositionHelperEntity", "SCR_SlotCompositionHelperEntity", 0, null, vector.Zero, vector.Zero);
291  SCR_SlotCompositionHelperEntity helper = SCR_SlotCompositionHelperEntity.Cast(api.SourceToEntity(src));
292  helper.m_EntitySource = src;
293  }
294 
295  //------------------------------------------------------------------------------------------------
296  override void _WB_AfterWorldUpdate(float timeSlice)
297  {
298  if (!m_EntitySource)
299  return;
300 
301  WorldEditorAPI api = _WB_GetEditorAPI();
302  api.BeginEntityAction();
303  api.CreateComponent(m_EntitySource, "Hierarchy");
304  api.DeleteEntity(m_EntitySource);
305  api.EndEntityAction();
306  }
307 }
308 #endif
SCR_TerrainHelper
Definition: SCR_TerrainHelper.c:1
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
m_SlotPrefab
protected ResourceName m_SlotPrefab
Definition: SCR_EntitiesEditorUIComponent.c:9
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
SCR_BaseContainerTools
Definition: SCR_BaseContainerTools.c:3
SCR_Global
Definition: Functions.c:6
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_SlotCompositionComponent
Entity composition which is supposed to fit into a slot.
Definition: SCR_SlotCompositionComponent.c:44
SCR_SlotCompositionComponentClass
Definition: SCR_SlotCompositionComponent.c:2
m_Owner
SCR_AIGroupUtilityComponentClass m_Owner
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180