5 protected ResourceName m_SlotPrefab;
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;
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;
15 ResourceName GetSlotPrefab()
23 bool CanOrientChildrenToTerrain()
25 return m_bOrientChildrenToTerrain;
33 static ResourceName GetSlotPrefab(IEntityComponentSource componentSource)
35 ResourceName slotPrefab;
36 componentSource.Get(
"m_SlotPrefab", slotPrefab);
47 protected static bool m_bIgnoreOrientChildrenToTerrain;
52 ResourceName GetSlotPrefab()
56 return prefabData.GetSlotPrefab();
58 return ResourceName.Empty;
64 bool CanOrientChildrenToTerrain()
68 return prefabData.CanOrientChildrenToTerrain();
75 static void IgnoreOrientChildrenToTerrain()
77 m_bIgnoreOrientChildrenToTerrain =
true;
83 void OrientToTerrain()
86 if (!CanOrientChildrenToTerrain() ||
m_Owner.GetParent())
89 BaseWorld world =
m_Owner.GetWorld();
90 SetChildTransform(
m_Owner, world);
95 protected void SetChildTransform(IEntity owner, BaseWorld world)
99 if (owner.GetParent())
102 height = owner.GetLocalTransformAxis(3)[1];
112 owner.GetWorldTransform(transform);
118 transform[3] = transform[3] + Vector(0, height, 0);
121 SCR_HorizontalAlignComponent horizontalAlignComponent = SCR_HorizontalAlignComponent.Cast(owner.FindComponent(SCR_HorizontalAlignComponent));
122 if (horizontalAlignComponent)
124 vector angles = Math3D.MatrixToAngles(transform);
127 Math3D.AnglesToMatrix(angles, transform);
134 WorldEditorAPI api =
GenericEntity.Cast(owner)._WB_GetEditorAPI();
135 IEntity parent = owner.GetParent();
136 vector pos = transform[3];
139 vector parentTransform[3];
140 parent.GetWorldTransform(parentTransform);
141 Math3D.MatrixInvMultiply3(parentTransform, transform, transform);
142 pos = parent.CoordToLocal(pos);
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());
153 owner.SetWorldTransform(transform);
156 owner.SetWorldTransform(transform);
161 if (composition && composition.CanOrientChildrenToTerrain())
163 IEntity child = owner.GetChildren();
166 SetChildTransform(child, world);
167 child = child.GetSibling();
173 override void EOnInit(IEntity owner)
178 if (m_bIgnoreOrientChildrenToTerrain)
180 m_bIgnoreOrientChildrenToTerrain =
false;
190 override void OnPostInit(IEntity owner)
194 SetEventMask(owner, EntityEvent.INIT);
205 static bool _WB_ConfigureComposition(WorldEditorAPI api, IEntitySource entitySource,
bool isRoot =
true,
bool delayed =
false)
212 IEntityComponentSource hierarchySource =
SCR_BaseContainerTools.FindComponentSource(entitySource,
"Hierarchy");
216 hierarchySource.Set(
"Enabled",
true);
223 SCR_SlotCompositionHelperEntity.CreateHierarchy(api, entitySource);
225 api.CreateComponent(entitySource,
"Hierarchy");
229 for (
int i = 0, count = entitySource.GetNumChildren(); i < count; i++)
231 isChange |= _WB_ConfigureComposition(api, entitySource.GetChild(i),
false);
238 override array<ref WB_UIMenuItem> _WB_GetContextMenuItems(IEntity owner)
241 new WB_UIMenuItem(
"Configure composition", 0),
242 new WB_UIMenuItem(
"Orient to terrain", 1)
247 override void _WB_OnContextMenu(IEntity owner,
int id)
249 WorldEditorAPI api =
GenericEntity.Cast(owner)._WB_GetEditorAPI();
255 IEntitySource entitySource = api.EntityToSource(owner);
256 if (!entitySource)
break;
259 entitySource = entitySource.GetAncestor();
260 if (!entitySource)
break;
263 api.BeginEntityAction();
264 _WB_ConfigureComposition(api, entitySource,
true,
true);
265 api.EndEntityAction();
268 api.BeginEntityAction();
270 api.EndEntityAction();
279 class SCR_SlotCompositionHelperEntityClass : GenericEntityClass {}
282 protected IEntitySource m_EntitySource;
288 static void CreateHierarchy(WorldEditorAPI api, IEntitySource entitySource)
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;
296 override void _WB_AfterWorldUpdate(
float timeSlice)
301 WorldEditorAPI api = _WB_GetEditorAPI();
302 api.BeginEntityAction();
303 api.CreateComponent(m_EntitySource,
"Hierarchy");
304 api.DeleteEntity(m_EntitySource);
305 api.EndEntityAction();