1 [
EntityEditorProps(
category:
"GameScripted/Editor", description:
"Manager of image generators", color:
"255 0 0 255", dynamicBox:
true)]
15 protected int m_iImageWidth;
18 protected int m_iImageHeight;
20 [
Attribute(
desc:
"When enabled, the system will not iterate through prefabs on its own.\nInstead, you'll have to press 'Space' to continue to next prefab.")]
21 protected bool m_bManualProgress;
23 [
Attribute(
desc:
"When enabled, the system will not check for correct viewport resolution and screenshot format upon start, and not capture any screenshots.\nFOR DEBUGGING ONLY!")]
24 protected bool m_bDebugMode;
27 protected int m_iInit = 0;
28 protected ref SCR_SortedArray<SCR_EditorImagePositionEntity> m_aPositions =
new SCR_SortedArray<SCR_EditorImagePositionEntity>();
29 protected ref array<ref SCR_EditorImageGeneratorPrefab> m_aSelectedPrefabs = {};
30 protected int m_iSelectedPrefabsCount;
31 protected static ref array<vector> s_aSelectedPositions = {};
33 protected float m_fTimeNext = -1;
34 protected float m_fTimeRemaining;
35 protected int m_iPrefabIndex = -1;
37 protected bool m_bIsScreenshotMade;
47 for (
int i = m_aPositions.Count() - 1; i >= 0; i--)
57 if (s_aSelectedPositions.IsEmpty())
60 for (
int i = 0, count = s_aSelectedPositions.Count(); i < count; i++)
62 if (vector.DistanceSq(
position.GetOrigin(), s_aSelectedPositions[i]) < 1)
74 s_aSelectedPositions.Insert(
position.GetOrigin());
76 protected void AddSelectedPrefab(ResourceName prefab,
string filePath)
79 FilePath.StripExtension(prefab, ext);
83 Resource res = Resource.Load(prefab);
84 if (!res || !res.IsValid())
88 if (!editableEntitySource)
94 Print(
string.Format(
"Prefab @\"%1\" does not have UI info defined in SCR_EditableEntityComponent!", prefab.GetPath()), LogLevel.WARNING);
98 ResourceName imagePath = info.GetImage();
99 if (imagePath.IsEmpty())
101 Print(
string.Format(
"Prefab @\"%1\" does not have image path defined in UI info of SCR_EditableEntityComponent!", prefab.GetPath()), LogLevel.WARNING);
105 array<EEditableEntityLabel> labels = {};
106 info.GetEntityLabels(labels);
110 if (s_aSelectedPositions.IsEmpty())
120 Print(
string.Format(
"No suitable position found for @\"%1\" with labels %2!", prefab, labelsLine), LogLevel.WARNING);
125 imagePath = FilePath.StripExtension(imagePath.GetPath());
128 protected void RequestClose()
132 protected bool Init()
134 if (m_iInit <= 1 && !
SCR_Global.IsEditMode(
this))
141 float screenW =
GetGame().GetWorkspace().GetWidth();
142 float screenH =
GetGame().GetWorkspace().GetHeight();
143 if (screenW != m_iImageWidth || screenH != m_iImageHeight)
145 Debug.Error2(Type().ToString(),
string.Format(
"Cannot generate images! Current resolution is %1x%2, expected %3x%4!\n", screenW, screenH, m_iImageWidth, m_iImageHeight));
146 GetGame().GetCallqueue().CallLater(RequestClose,
false, 1);
151 WorldEditor worldEditor = Workbench.GetModule(WorldEditor);
152 worldEditor.GetResourceBrowserSelection(AddSelectedPrefab,
true);
153 if (m_aSelectedPrefabs.IsEmpty())
155 Debug.Error2(Type().ToString(),
"No suitable prefabs selected!\n");
156 GetGame().GetCallqueue().CallLater(RequestClose,
false, 1);
158 m_iSelectedPrefabsCount = m_aSelectedPrefabs.Count();
164 if (s_aSelectedPositions.IsEmpty())
165 Print(
string.Format(
"Initiating image generation for %1 prefab(s).", m_iSelectedPrefabsCount), LogLevel.DEBUG);
167 Print(
string.Format(
"Initiating image generation for %1 prefab(s) on %2 pre-selected position(s).", m_iSelectedPrefabsCount, s_aSelectedPositions.Count()), LogLevel.DEBUG);
173 override void EOnFrame(IEntity owner,
float timeSlice)
179 if (m_fTimeNext != -1 || !m_CurrentPrefab)
181 canContinue =
m_fTime > m_fTimeNext;
183 else if (Debug.KeyState(KeyCode.KC_SPACE))
185 Debug.ClearKey(KeyCode.KC_SPACE);
192 if (m_CurrentPrefab && !m_bIsScreenshotMade)
194 m_bIsScreenshotMade =
true;
197 string addonName = SCR_AddonTool.GetResourceLastAddon(m_CurrentPrefab.m_Prefab);
198 addonName = SCR_AddonTool.ToFileSystem(addonName);
199 System.MakeScreenshot(addonName + m_CurrentPrefab.m_ImagePath);
200 Print(
string.Format(
"Image of prefab '%1' at position '%2' saved to @\"%3.png\"", FilePath.StripPath(m_CurrentPrefab.m_Prefab), m_CurrentPrefab.m_Position.GetName(), m_CurrentPrefab.m_ImagePath), LogLevel.DEBUG);
205 Print(
string.Format(
"SIMULATION: Image of prefab '%1' at position '%2' would be saved to @\"%3.png\"", FilePath.StripPath(m_CurrentPrefab.m_Prefab), m_CurrentPrefab.m_Position.GetName(), m_CurrentPrefab.m_ImagePath), LogLevel.VERBOSE);
211 if (m_iPrefabIndex >= m_iSelectedPrefabsCount)
213 GetGame().GetCallqueue().CallLater(RequestClose,
false, 1);
219 m_CurrentPrefab.m_Position.DeactivatePosition();
222 m_bIsScreenshotMade =
false;
224 if (currentPrefab.m_Position.ActivatePosition(currentPrefab.m_Prefab))
226 m_CurrentPrefab = currentPrefab;
228 if (m_bManualProgress)
231 m_fTimeNext =
m_fTime + currentPrefab.m_Position.GetDelay();
238 m_fTimeRemaining = 0;
239 for (
int i = m_iPrefabIndex, count = m_aSelectedPrefabs.Count(); i < count; i++)
241 m_fTimeRemaining += m_aSelectedPrefabs[i].m_Position.GetDelay();
246 DbgUI.Begin(
"SCR_EditorImageGeneratorEntity");
249 DbgUI.Text(
string.Format(
"%1\n\n", m_CurrentPrefab.m_Prefab.GetPath()));
250 DbgUI.Text(
string.Format(
"Progress: %1 of %2", m_iPrefabIndex, m_iSelectedPrefabsCount));
252 if (m_fTimeNext == -1)
253 DbgUI.Text(
"Press 'Space' to take screenshot and continue to next prefab\n\n");
259 DbgUI.Text(
"Initializing...");
264 m_fTimeRemaining -= timeSlice;
268 SetName(
"SCR_EditorImageGeneratorEntity");
269 SetEventMask(EntityEvent.FRAME);
272 s_aSelectedPositions.Clear();
274 override void _WB_AfterWorldUpdate(
float timeSlice)
276 WorldEditorAPI api = _WB_GetEditorAPI();
277 if (!api.IsEntitySelected(api.EntityToSource(
this)))
280 int screenW = api.GetScreenWidth();
281 int screenH = api.GetScreenHeight();
283 string textCurrent =
string.Format(
"Current resolution: %1x%2", screenW, screenH);
284 string textTarget =
string.Format(
"Target resolution: %1x%2", m_iImageWidth, m_iImageHeight);
286 int color = Color.RED;
287 if (screenW == m_iImageWidth && screenH == m_iImageHeight)
289 else if (screenW == m_iImageWidth || screenH == m_iImageHeight)
290 color = Color.ORANGE;
292 DebugTextScreenSpace.Create(GetWorld(), textCurrent, DebugTextFlags.ONCE, 0, 0, 18, color, Color.BLACK);
293 DebugTextScreenSpace.Create(GetWorld(), textTarget, DebugTextFlags.ONCE, 0, 18, 18, Color.WHITE, Color.BLACK);
295 override void _WB_GetBoundBox(inout vector min, inout vector max, IEntitySource src)
297 GetWorld().GetBoundBox(min, max);
346 ResourceName m_Prefab;
353 m_ImagePath = imagePath;