18 protected IEntitySource m_ParentShapeSource;
20 protected bool m_bIsChangingWorkbenchKey;
22 protected static const ref Color BASE_GENERATOR_COLOR = Color.White;
23 protected static const ref array<string> ACCEPTED_PARENT_CLASSNAMES = {
"SplineShapeEntity",
"PolylineShapeEntity" };
26 override void _WB_OnParentChange(IEntitySource src, IEntitySource prevParentSrc)
28 super._WB_OnParentChange(src, prevParentSrc);
30 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
31 if (!worldEditorAPI || worldEditorAPI.UndoOrRedoIsRestoring())
34 m_ParentShapeSource = src.GetParent();
35 if (!m_ParentShapeSource || !ACCEPTED_PARENT_CLASSNAMES.Contains(m_ParentShapeSource.GetClassName()))
40 override bool _WB_OnKeyChanged(BaseContainer src,
string key, BaseContainerList ownerContainers, IEntity parent)
42 super._WB_OnKeyChanged(src, key, ownerContainers, parent);
44 if (m_bIsChangingWorkbenchKey)
47 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
48 if (!worldEditorAPI || worldEditorAPI.UndoOrRedoIsRestoring())
51 bool sameParentChange = parent && worldEditorAPI.EntityToSource(parent) == m_ParentShapeSource;
57 Print(
"Do not modify a generator entity itself! Change its parent shape instead", LogLevel.WARNING);
59 m_bIsChangingWorkbenchKey =
true;
60 worldEditorAPI.SetVariableValue(src,
null,
"scale",
"1");
61 m_bIsChangingWorkbenchKey =
false;
65 array<string> angles = {
"angleX",
"angleY",
"angleZ" };
66 foreach (
string angle : angles)
71 Print(
"Do not modify a generator entity itself! Change its parent shape instead", LogLevel.WARNING);
73 m_bIsChangingWorkbenchKey =
true;
74 worldEditorAPI.SetVariableValue(src,
null, angle,
"0");
75 m_bIsChangingWorkbenchKey =
false;
86 src.Get(
"coords", coords);
87 if (coords != vector.Zero)
90 Print(
"Do not modify a generator entity itself! Change its parent shape instead", LogLevel.WARNING);
92 m_bIsChangingWorkbenchKey =
true;
93 worldEditorAPI.SetVariableValue(src,
null,
"coords",
"0 0 0");
94 m_bIsChangingWorkbenchKey =
false;
98 ShapeEntity parentShape = ShapeEntity.Cast(parent);
100 m_ParentShapeSource = worldEditorAPI.EntityToSource(parentShape);
102 m_ParentShapeSource =
null;
111 override bool _WB_CanSelect(IEntitySource src)
117 override void OnShapeChangedInternal(IEntitySource shapeEntitySrc, ShapeEntity shapeEntity, array<vector> mins, array<vector> maxes)
119 super.OnShapeChangedInternal(shapeEntitySrc, shapeEntity, mins, maxes);
120 m_ParentShapeSource = shapeEntitySrc;
121 ResetGeneratorPosition(shapeEntity);
125 override void OnShapeInitInternal(IEntitySource shapeEntitySrc, ShapeEntity shapeEntity)
127 super.OnShapeInitInternal(shapeEntitySrc, shapeEntity);
128 m_ParentShapeSource = shapeEntitySrc;
129 ResetGeneratorPosition(shapeEntity);
133 protected void ResetGeneratorPosition(ShapeEntity shapeEntity =
null)
135 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
136 if (!worldEditorAPI || worldEditorAPI.UndoOrRedoIsRestoring())
139 bool manageEditAction = !worldEditorAPI.IsDoingEditAction();
140 if (manageEditAction)
141 worldEditorAPI.BeginEntityAction();
143 worldEditorAPI.SetVariableValue(
m_Source,
null,
"coords",
"0 0 0");
145 if (manageEditAction)
146 worldEditorAPI.EndEntityAction();
151 protected void DeleteAllChildren()
153 int count =
m_Source.GetNumChildren();
157 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
161 array<IEntitySource> entities = {};
162 for (
int i =
m_Source.GetNumChildren() - 1; i >= 0; --i)
164 entities.Insert(
m_Source.GetChild(i));
166 worldEditorAPI.DeleteEntities(entities);
171 protected static array<vector> GetPoints(notnull IEntitySource shapeEntitySrc)
173 BaseContainerList points = shapeEntitySrc.GetObjectArray(
"Points");
177 array<vector> result = {};
179 for (
int i, count = points.Count(); i < count; ++i)
181 points.Get(i).Get(
"Position", pos);
190 protected array<vector> GetWorldAnchorPoints(notnull IEntitySource shapeEntitySrc)
192 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
196 ShapeEntity shapeEntity = ShapeEntity.Cast(worldEditorAPI.SourceToEntity(shapeEntitySrc));
200 array<vector> result = {};
201 shapeEntity.GetPointsPositions(result);
204 shapeEntity.GetTransform(matrix);
206 for (
int i, count = result.Count(); i < count; ++i)
208 result[i] = result[i].Multiply4(matrix);
216 protected array<vector> GetTesselatedShapePoints(notnull IEntitySource shapeEntitySrc)
218 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
222 ShapeEntity shapeEntity = ShapeEntity.Cast(worldEditorAPI.SourceToEntity(shapeEntitySrc));
226 array<vector> result = {};
227 shapeEntity.GenerateTesselatedShape(result);
234 protected array<vector> GetWorldTesselatedShapePoints(notnull IEntitySource shapeEntitySrc)
236 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
240 ShapeEntity shapeEntity = ShapeEntity.Cast(worldEditorAPI.SourceToEntity(shapeEntitySrc));
244 array<vector> result = {};
245 shapeEntity.GenerateTesselatedShape(result);
248 shapeEntity.GetTransform(matrix);
250 for (
int i, count = result.Count(); i < count; ++i)
252 result[i] = result[i].Multiply4(matrix);
263 return Color.FromInt(BASE_GENERATOR_COLOR.PackToInt());
265 return Color.FromInt(prefabData.m_Color.PackToInt());
269 override void _WB_OnCreate(IEntitySource src)
271 super._WB_OnCreate(src);
276 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
277 if (!worldEditorAPI || worldEditorAPI.UndoOrRedoIsRestoring())
281 IEntitySource shapeEntitySrc = src.GetParent();
284 ShapeEntity shapeEntity = ShapeEntity.Cast(worldEditorAPI.SourceToEntity(shapeEntitySrc));
286 OnShapeInit(shapeEntitySrc, shapeEntity);
290 src.Get(
"Flags", flags);
291 bool manageEditAction = !worldEditorAPI.IsDoingEditAction();
292 if (manageEditAction)
293 worldEditorAPI.BeginEntityAction();
295 worldEditorAPI.SetVariableValue(src,
null,
"Flags", (flags | EntityFlags.EDITOR_ONLY).ToString());
297 if (manageEditAction)
298 worldEditorAPI.EndEntityAction();
303 protected void ColorShape()
308 WorldEditorAPI worldEditorAPI = _WB_GetEditorAPI();
309 if (!worldEditorAPI || worldEditorAPI.UndoOrRedoIsRestoring())
312 IEntitySource parentSource =
m_Source.GetParent();
316 array<ref ContainerIdPathEntry> containerPath = {};
319 string colorString =
string.Format(
"%1 %2 %3 %4", color.R(), color.G(), color.B(), color.A());
321 bool manageEditAction = !worldEditorAPI.IsDoingEditAction();
322 if (manageEditAction)
323 worldEditorAPI.BeginEntityAction();
325 worldEditorAPI.SetVariableValue(parentSource, containerPath,
"LineColor", colorString);
327 if (manageEditAction)
328 worldEditorAPI.EndEntityAction();
340 if (!_WB_GetEditorAPI())
345 WorldEditor worldEditor = Workbench.GetModule(WorldEditor);
346 if (worldEditor && !worldEditor.IsPrefabEditMode() && !ShapeEntity.Cast(parent))
348 Print(
"A Generator is not a direct child of a shape at " +
GetOrigin(), LogLevel.WARNING);
352 m_ParentShapeSource =
m_Source.GetParent();