Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PreviewEntityComponent.c
Go to the documentation of this file.
1[ComponentEditorProps(category: "GameScripted/Editor", description: "")]
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")]
9
10 [Attribute()]
11 protected ref array<ref SCR_BasePreviewEntry> m_aEntries;
12
13 //------------------------------------------------------------------------------------------------
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
55class SCR_PreviewEntityComponent : ScriptComponent
56{
57 protected const string FILE_SUFFIX = "_Preview";
58
59 //------------------------------------------------------------------------------------------------
62 {
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 {
77 if (prefabData)
78 return prefabData.GetPreviewEntries(outEntries, entry);
79 else
80 return false;
81 }
82
83 //------------------------------------------------------------------------------------------------
86 bool IsRuntime()
87 {
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}
SCR_EAIThreatSectorFlags flags
void ContainerIdPathEntry(string propertyName, int index=-1)
Definition worldEditor.c:30
EPreviewEntityFlag
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_CharacterSoundComponentClass GetComponentData()
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
ResourceName GetPreviewPrefab()
SCR_PreviewEntityComponentClass FILE_SUFFIX
int GetPreviewEntries(out notnull array< ref SCR_BasePreviewEntry > outEntries, SCR_BasePreviewEntry entry)
bool IsRuntime()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
enum EVehicleType IEntity
event void _WB_OnContextMenu(IEntity owner, int id)
User has chosen any of your menu item from editor's "Component" menu which you have recently provided...
event array< ref WB_UIMenuItem > _WB_GetContextMenuItems(IEntity owner)
An opportunity to append items into editor's "Component" context menu. Do not call editor API here!
proto external WorldEditorAPI _WB_GetEditorAPI()
This returns world editor API, which is safe to use from editor events bellow.
int GetPreviewEntries(out notnull array< ref SCR_BasePreviewEntry > outEntries, SCR_BasePreviewEntry entry)
ref array< ref SCR_BasePreviewEntry > m_aEntries
proto external GenericEntity GetOwner()
Get owner entity.
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute