Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PreviewEntityComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Editor", description: "")]
2 class SCR_PreviewEntityComponentClass : ScriptComponentClass
3 {
4  [Attribute("1", desc: "When enabled, preview prefab can be used also in runtime, not only when spawning from prefab.")]
5  protected bool m_bRuntime;
6 
7  [Attribute(uiwidget: UIWidgets.ResourcePickerThumbnail, params: "et")]
8  protected ResourceName m_PreviewPrefab;
9 
10  [Attribute()]
11  protected ref array<ref SCR_BasePreviewEntry> m_aEntries;
12 
13  //------------------------------------------------------------------------------------------------
15  ResourceName GetPreviewPrefab()
16  {
17  return m_PreviewPrefab;
18  }
19 
20  //------------------------------------------------------------------------------------------------
24  int GetPreviewEntries(out notnull array<ref SCR_BasePreviewEntry> outEntries, SCR_BasePreviewEntry entry)
25  {
26  //outEntries.Clear();
27  int count = m_aEntries.Count();
28  SCR_BasePreviewEntry entryCopy;
29  for (int i = 0; i < count; i++)
30  {
31  if (m_aEntries[i].m_iParentID == -1)
32  {
33  entry.m_Mesh = m_aEntries[i].m_Mesh;
34  }
35  else
36  {
37  entryCopy = new SCR_BasePreviewEntry();
38  entryCopy.CopyFrom(m_aEntries[i]);
39  //entryCopy.m_iParentID += entry.m_iParentID + 1;
40  outEntries.Insert(entryCopy);
41  }
42  }
43  return count;
44  }
45 
46  //------------------------------------------------------------------------------------------------
49  bool IsRuntime()
50  {
51  return m_bRuntime;
52  }
53 }
54 
55 class SCR_PreviewEntityComponent : ScriptComponent
56 {
57  protected const string FILE_SUFFIX = "_Preview";
58 
59  //------------------------------------------------------------------------------------------------
61  ResourceName GetPreviewPrefab()
62  {
63  SCR_PreviewEntityComponentClass prefabData = SCR_PreviewEntityComponentClass.Cast(GetComponentData(GetOwner()));
64  if (prefabData)
65  return prefabData.GetPreviewPrefab();
66  else
67  return ResourceName.Empty;
68  }
69 
70  //------------------------------------------------------------------------------------------------
74  int GetPreviewEntries(out notnull array<ref SCR_BasePreviewEntry> outEntries, SCR_BasePreviewEntry entry)
75  {
76  SCR_PreviewEntityComponentClass prefabData = SCR_PreviewEntityComponentClass.Cast(GetComponentData(GetOwner()));
77  if (prefabData)
78  return prefabData.GetPreviewEntries(outEntries, entry);
79  else
80  return false;
81  }
82 
83  //------------------------------------------------------------------------------------------------
86  bool IsRuntime()
87  {
88  SCR_PreviewEntityComponentClass prefabData = SCR_PreviewEntityComponentClass.Cast(GetComponentData(GetOwner()));
89  if (prefabData)
90  return prefabData.IsRuntime();
91  else
92  return false;
93  }
94 
95 #ifdef WORKBENCH
96 
97  //------------------------------------------------------------------------------------------------
101  static void GeneratePreviewEntries(WorldEditorAPI api, IEntitySource entitySource)
102  {
103  //--- Find this component
104  IEntityComponentSource componentSource;
105  for (int i = 0, count = entitySource.GetComponentCount(); i < count; i++)
106  {
107  componentSource = entitySource.GetComponent(i);
108  if (componentSource.GetClassName().ToType().IsInherited(SCR_PreviewEntityComponent))
109  break;
110  }
111  if (!componentSource)
112  return;
113 
114  //--- Clear existing array
115  BaseContainerList entriesList = componentSource.GetObjectArray("m_aEntries");
116  int index = 0;
117  while (entriesList && entriesList.Count() > index)
118  {
119  if (!api.RemoveObjectArrayVariableMember(entitySource, {ContainerIdPathEntry("SCR_PreviewEntityComponent")}, "m_aEntries", 0))
120  index++;
121  }
122 
123  array<ref SCR_BasePreviewEntry> entries = {};
124  SCR_PrefabPreviewEntity.GetPreviewEntries(entitySource, entries, flags: EPreviewEntityFlag.IGNORE_PREFAB);
125  for (int i = 0, count = entries.Count(); i < count; i++)
126  {
127  if (api.CreateObjectArrayVariableMember(entitySource, {ContainerIdPathEntry("SCR_PreviewEntityComponent")}, "m_aEntries", "SCR_BasePreviewEntry", i))
128  {
129  entries[i].Log(i);
130  entries[i].SaveToContainer(api, entitySource, {ContainerIdPathEntry("SCR_PreviewEntityComponent"), ContainerIdPathEntry("m_aEntries", i)});
131  }
132  }
133  }
134 
135  //------------------------------------------------------------------------------------------------
140  void GeneratePreviewPrefab(WorldEditorAPI api, IEntitySource entitySource, IEntityComponentSource componentSource = null)
141  {
142  //--- Find this component
143  if (!componentSource)
144  {
145  for (int i = 0, count = entitySource.GetComponentCount(); i < count; i++)
146  {
147  componentSource = entitySource.GetComponent(i);
148  if (componentSource.GetClassName() == "SCR_PreviewEntityComponent")
149  break;
150  }
151  }
152 
153  ResourceName prefab, previewPrefab;
154  componentSource.Get("m_PreviewPrefab", previewPrefab);
155 
156  prefab = SCR_BaseContainerTools.GetPrefabResourceName(entitySource);
157  if (!prefab)
158  {
159  Print("Entity is not a prefab!", LogLevel.WARNING);
160  return;
161  }
162 
163  //--- No preview prefab assigned, create one
164  if (!previewPrefab)
165  {
166  //--- Get absolute path
167  string absolutePath, absolutePathPreview, ext;
168  Workbench.GetAbsolutePath(prefab.GetPath(), absolutePath, false);
169  absolutePathPreview = FilePath.AppendExtension(FilePath.StripExtension(absolutePath, ext) + FILE_SUFFIX, ext);
170 
171  //--- Create dummy preview prefab
172  IEntitySource emptyEntitySource = api.CreateEntity("SCR_PrefabPreviewEntity", string.Empty, 0, null, vector.Zero, vector.Zero);
173  api.SetVariableValue(emptyEntitySource, null, "m_SourcePrefab", prefab);
174  api.CreateEntityTemplate(emptyEntitySource, absolutePathPreview);
175  api.DeleteEntity(emptyEntitySource);
176 
177  //--- Link preview prefab
178  ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
179  MetaFile meta = resourceManager.GetMetaFile(absolutePathPreview);
180  previewPrefab = meta.GetResourceID();
181  api.SetVariableValue(entitySource, {ContainerIdPathEntry("SCR_PreviewEntityComponent")}, "m_PreviewPrefab", previewPrefab);
182  //api.CreateEntityTemplate(entitySource, absolutePath);
183  }
184 
185  IEntitySource previewSource = api.CreateEntity(previewPrefab, "", 0, null, vector.Zero, vector.Zero);
186  SCR_PrefabPreviewEntity previewEntity = SCR_PrefabPreviewEntity.Cast(api.SourceToEntity(previewSource));
187  previewEntity.CreatePrefabFromSource(api, prefab);
188  api.DeleteEntity(previewSource);
189  }
190 
191  //------------------------------------------------------------------------------------------------
192  override void _WB_OnContextMenu(IEntity owner, int id)
193  {
194  switch (id)
195  {
196  case 0:
197  {
198  GenericEntity genericOwner = GenericEntity.Cast(owner);
199  WorldEditorAPI api = genericOwner._WB_GetEditorAPI();
200  IEntitySource ownerSrc = api.EntityToSource(owner);
201  //ownerSrc = ownerSrc.GetAncestor(); //--- Get prefab
202 
203  WBProgressDialog progress = new WBProgressDialog("Processing...", Workbench.GetModule(WorldEditor));
204 
205  api.BeginEntityAction();
206  api.BeginEditSequence(ownerSrc);
207  GeneratePreviewEntries(api, ownerSrc);
208  api.EndEditSequence(ownerSrc);
209  api.EndEntityAction();
210  break;
211  }
212 
213  case 1:
214  {
215  GenericEntity genericOwner = GenericEntity.Cast(owner);
216  WorldEditorAPI api = genericOwner._WB_GetEditorAPI();
217  IEntitySource ownerSrc = api.EntityToSource(owner);
218 
219  api.BeginEntityAction();
220  GeneratePreviewPrefab(api, ownerSrc);
221  api.EndEntityAction();
222  }
223  }
224  }
225 
226  //------------------------------------------------------------------------------------------------
227  override array<ref WB_UIMenuItem> _WB_GetContextMenuItems(IEntity owner)
228  {
229  return { new WB_UIMenuItem("Generate preview entries", 0), new WB_UIMenuItem("Generate preview prefab", 1) };
230  }
231 #endif
232 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_BasePreviewEntry
Definition: SCR_BasePreviewEntry.c:2
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
EPreviewEntityFlag
EPreviewEntityFlag
Definition: EPreviewEntityFlag.c:1
IsRuntime
bool IsRuntime()
Definition: SCR_PreviewEntityComponent.c:86
m_aEntries
protected ref array< ref SCR_TextsTaskManagerEntry > m_aEntries
Definition: SCR_TextsTaskManagerComponent.c:3
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_PrefabPreviewEntity
Definition: SCR_PrefabPreviewEntity.c:8
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
SCR_BaseContainerTools
Definition: SCR_BaseContainerTools.c:3
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
GetPreviewPrefab
ResourceName GetPreviewPrefab()
Definition: SCR_PreviewEntityComponent.c:61
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_PreviewEntityComponentClass
Definition: SCR_PreviewEntityComponent.c:2
FILE_SUFFIX
SCR_PreviewEntityComponentClass FILE_SUFFIX
GetPreviewEntries
int GetPreviewEntries(out notnull array< ref SCR_BasePreviewEntry > outEntries, SCR_BasePreviewEntry entry)
Definition: SCR_PreviewEntityComponent.c:74
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180