Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ManualCamera.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Camera", description: "Manual camera", color: "0 255 255 255")]
3 {
4 };
5 
8 
10 
17 {
18  [Attribute("1", UIWidgets.Auto, "True to enable manual control.", category: "Manual Camera")]
19  private bool m_bInputEnabled;
20 
21  [Attribute("0", UIWidgets.Auto, "True to create the camera at the position of this entity.", category: "Manual Camera")]
22  private bool m_bStartAtEntity;
23 
24  [Attribute("ManualCameraContext", UIWidgets.Auto, "", category: "Manual Camera")]
25  private string m_sActionContext;
26 
27  [Attribute("", UIWidgets.Flags, enums: ParamEnumArray.FromEnum(EManualCameraFlag), category: "Manual Camera")]
28  private EManualCameraFlag m_DefaultFlags;
29 
30  [Attribute(category: "Manual Camera Components")]
31  private ref array<ref SCR_BaseManualCameraComponent> m_aComponents;
32 
34  //--- Variables
35  private BaseWorld m_World;
36  private CameraManager m_CameraManager;
37  private InputManager m_InputManager;
38  private MenuManager m_MenuManager;
39  private IEntity m_Owner;
40  private ref SCR_SortedArray<SCR_BaseManualCameraComponent> m_aComponentsSorted = new SCR_SortedArray<SCR_BaseManualCameraComponent>;
41  private ref array<SCR_BaseManualCameraComponent> m_aComponentsOnFrame = new array<SCR_BaseManualCameraComponent>;
42  private int m_iComponentsCount;
43  private int m_iComponentsOnFrameCount;
44  private float m_fFOVDefault = 1;
45  private ref SCR_ManualCameraParam m_Param;
46  private ref ScriptInvoker Event_OnCursorPos;
47  private vector m_vTransformPrev[4];
48  private bool m_bIsDirtyExternal;
49  private Widget m_Widget;
50 
52  //--- Custom methods
54 
55  //------------------------------------------------------------------------------------------------
56  protected void RegisterComponents()
57  {
58  if (!m_aComponents)
59  return;
60 
61  //--- Get camera components
62  foreach (SCR_BaseManualCameraComponent cameraComponent: m_aComponents)
63  {
64  if (cameraComponent.IsEnabled())
65  m_aComponentsSorted.Insert(cameraComponent.GetPriority(), cameraComponent);
66  }
67  m_iComponentsCount = m_aComponentsSorted.Count();
68  }
69 
70  //------------------------------------------------------------------------------------------------
71  protected void InitComponents()
72  {
73  for (int i = m_iComponentsCount - 1; i >= 0; i--)
74  {
75  if (m_aComponentsSorted[i].InitBase(this))
76  m_aComponentsOnFrame.Insert(m_aComponentsSorted[i]);
77  }
78  m_iComponentsOnFrameCount = m_aComponentsOnFrame.Count();
79  }
80 
81  //------------------------------------------------------------------------------------------------
82  protected void ProcessComponents(float timeSlice = 0)
83  {
84  vector transform[4];
85  //GetWorldCameraTransform(transform);
86  GetLocalTransform(transform);
87  bool inputEnabled = m_bInputEnabled && !IsDisabledByMenu();
88  vector velocity = vector.Zero;
89  if (m_Param) velocity = m_Param.velocityOriginal;
90  m_Param = new SCR_ManualCameraParam(transform, timeSlice, m_World, GetVerticalFOV(), inputEnabled, velocity, m_DefaultFlags);
91 
92  //--- External influence
93  m_Param.isDirtyExternal = m_bIsDirtyExternal;
94  m_bIsDirtyExternal = false;
95 
96  //--- Mark first call as dirty to make sure that all features refresh
97  if (timeSlice == 0)
98  m_Param.isDirty = true;
99 
100  //--- Activate context
101  if (inputEnabled)
102  {
103  m_InputManager.ActivateContext(m_sActionContext);
104  //m_InputManager.SetContextDebug(m_sActionContext, true);
105  }
106 
107  //--- Iterate through all components (ToDo: Don't check for IsEnabled here, hold array with just enabled components)
109  for (int c = 0; c < m_iComponentsOnFrameCount; c++)
110  {
111  component = m_aComponentsOnFrame[c];
112  if (component.IsEnabled() && component.HasFlag(m_Param.flag))
113  component.EOnCameraFrame(m_Param);
114  }
115 
116  //-- Show on-screen debug
117  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_MANUAL_CAMERA_DEBUG))
118  m_Param.ShowDebug(this);
119 
120  //--- Apply changes
121  if (m_Param.isDirty)
122  {
123  m_Param.ProcessTransform();
124 
125  SetLocalTransform(m_Param.transform);
126  SetVerticalFOV(m_Param.fov);
127 
128  m_vTransformPrev = m_Param.transform;
129  }
130  m_Param.velocityOriginal = m_Param.transform[3] - m_Param.transformOriginal[3];
131  }
132 
133  //------------------------------------------------------------------------------------------------
136  void SaveComponents(notnull array<ref SCR_ManualCameraComponentSave> outData)
137  {
139  for (int i = m_iComponentsCount - 1; i >= 0; i--)
140  {
141  if (!m_aComponentsSorted[i].IsEnabled())
142  continue;
143 
144  save = new SCR_ManualCameraComponentSave();
145  m_aComponentsSorted[i].EOnCameraSave(save);
146 
147  if (save.m_aValues && !save.m_aValues.IsEmpty())
148  {
149  save.m_sTypeName = m_aComponentsSorted[i].Type().ToString();
150 
151  int d = 0;
152  int dataCount = outData.Count();
153  while (d < dataCount)
154  {
155  //~ Check if data already exists. If it does, replace it. If not insert it at the end
156  if (outData[d].m_sTypeName == save.m_sTypeName)
157  {
158  outData.RemoveOrdered(d);
159  break;
160  }
161  d++;
162  }
163 
164  outData.InsertAt(save, d);
165  }
166  }
167  }
168 
169  //------------------------------------------------------------------------------------------------
172  void LoadComponents(array<ref SCR_ManualCameraComponentSave> data)
173  {
174  typename type;
175  foreach (SCR_ManualCameraComponentSave dataEntry: data)
176  {
177  type = dataEntry.m_sTypeName.ToType();
178  for (int i = m_iComponentsCount - 1; i >= 0; i--)
179  {
180  if (m_aComponentsSorted[i].Type() == type && m_aComponentsSorted[i].IsEnabled())
181  m_aComponentsSorted[i].EOnCameraLoad(dataEntry);
182  }
183  }
184  }
185 
186  //------------------------------------------------------------------------------------------------
188  void ResetComponents()
189  {
190  for (int i = m_iComponentsCount - 1; i >= 0; i--)
191  {
192  if (m_aComponentsSorted[i].IsEnabled())
193  m_aComponentsSorted[i].EOnCameraReset();
194  }
195  }
196  //------------------------------------------------------------------------------------------------
200  SCR_BaseManualCameraComponent FindCameraComponent(typename type)
201  {
202  for (int i = m_iComponentsCount - 1; i >= 0; i--)
203  {
204  if (m_aComponentsSorted[i].Type() == type)
205  return m_aComponentsSorted[i];
206  }
207  return null;
208  }
209 
210  //------------------------------------------------------------------------------------------------
211  protected void MoveToCamera(CameraBase camera = null)
212  {
213  if (!camera) camera = GetPreviousCamera();
214  if (!camera) return;
215  vector matrix[4];
216  camera.GetTransform(matrix);
217  SetTransform(matrix);
218  }
219 
220  //------------------------------------------------------------------------------------------------
221  protected CameraBase GetPreviousCamera()
222  {
223  if (!m_CameraManager) return null;
224 
225  //--- Get all registered cameras
226  array<CameraBase> cameras = new array<CameraBase>();
227  m_CameraManager.GetCamerasList(cameras);
228 
229  //--- Get the most recently registered camera
230  for (int i = cameras.Count() - 1; i >= 0; i--)
231  {
232  if (cameras[i] != this) return cameras[i];
233  }
234  return null;
235  }
236 
237  //------------------------------------------------------------------------------------------------
240  bool TrySwitchToControlledEntityCamera()
241  {
242  //--- Don't switch when this camera is not active
243  if (!m_CameraManager || m_CameraManager.CurrentCamera() != this)
244  return false;
245 
246  // We primarily want the player to return to his controlled entity camera
247  CameraBase camera = GetGame().GetPlayerController().GetPlayerCamera();
248 
249  // Only if that fails we just use the next best previously created camera
250  if (!camera)
251  camera = GetPreviousCamera();
252 
253  if (camera)
254  {
255  m_CameraManager.SetCamera(camera);
256  return true;
257  }
258 
259  return false;
260 
261  }
262 
263  //------------------------------------------------------------------------------------------------
265  void SwitchToPreviousCamera()
266  {
267  //--- Don't switch when this camera is not current
268  if (!m_CameraManager || m_CameraManager.CurrentCamera() != this)
269  return;
270 
271  CameraBase camera = GetPreviousCamera();
272  if (camera)
273  m_CameraManager.SetCamera(camera);
274  }
275 
276  //------------------------------------------------------------------------------------------------
277  protected bool IsFirstCamera()
278  {
279  if (!m_CameraManager)
280  return false;
281 
282  array<CameraBase> cameras = new array<CameraBase>();
283  m_CameraManager.GetCamerasList(cameras);
284 
285  int count = cameras.Count();
286  if (count == 0)
287  return true;
288 
289  if (count == 1)
290  return cameras[0] == this; //--- When this camera is already registered, return true when it's the only one
291 
292  return false;
293  }
294 
295  //------------------------------------------------------------------------------------------------
296  protected bool IsDisabledByMenu()
297  {
298  if (!m_MenuManager) return false;
299 
300  if (m_MenuManager.IsAnyDialogOpen()) return true;
301 
302  MenuBase topMenu = m_MenuManager.GetTopMenu();
303  return topMenu && !topMenu.IsInherited(EditorMenuUI);
304  }
305 
306  //------------------------------------------------------------------------------------------------
309  void AttachTo(IEntity parent)
310  {
311  if (parent == GetParent()) return;
312 
313  vector transform[4];
314  GetWorldTransform(transform);
315  parent.AddChild(this, 0);
316  SetWorldTransform(transform);
317 
318  for (int i = m_iComponentsCount - 1; i >= 0; i--)
319  {
320  m_aComponentsSorted[i].EOnCameraParentChange(true, parent);
321  }
322  }
323 
324  //------------------------------------------------------------------------------------------------
326  void Detach()
327  {
328  IEntity parent = GetParent();
329  if (!parent) return;
330 
331  vector transform[4];
332  GetWorldTransform(transform);
333  parent.RemoveChild(this);
334  SetWorldTransform(transform);
335 
336  for (int i = m_iComponentsCount - 1; i >= 0; i--)
337  {
338  m_aComponentsSorted[i].EOnCameraParentChange(false, parent);
339  }
340  }
341 
342  //------------------------------------------------------------------------------------------------
345  void SetInputEnabled(bool enable)
346  {
347  m_bInputEnabled = enable;
348  }
349 
350  //------------------------------------------------------------------------------------------------
353  bool IsInputEnabled()
354  {
355  return m_bInputEnabled;
356  }
357 
358  //------------------------------------------------------------------------------------------------
361  float GetDefaultFOV()
362  {
363  return m_fFOVDefault;
364  }
365 
366  //------------------------------------------------------------------------------------------------
369  bool IsManualInput()
370  {
371  return m_Param.isManualInput;
372  }
373 
374  //------------------------------------------------------------------------------------------------
377  void SetDirty(bool isDirty)
378  {
379  m_bIsDirtyExternal = isDirty;
380  }
381 
382  //------------------------------------------------------------------------------------------------
385  Widget GetWidget()
386  {
387  return m_Widget;
388  }
389 
390  //------------------------------------------------------------------------------------------------
395  Widget CreateCameraWidget(ResourceName layout, bool isVisible = true)
396  {
397  if (!m_Widget || layout.IsEmpty()) return null;
398 
399  Widget widget = GetGame().GetWorkspace().CreateWidgets(layout, m_Widget);
400  if (!widget) return null;
401 
402  widget.SetVisible(isVisible);
403  return widget;
404  }
405 
406  //------------------------------------------------------------------------------------------------
409  SCR_ManualCameraParam GetCameraParam()
410  {
411  return m_Param;
412  }
413 
414 
415  //------------------------------------------------------------------------------------------------
419  void Terminate()
420  {
421  //--- Already marked as destroyed
422  if (m_iComponentsCount == -1) return;
423 
424  //--- Terminate components
425  for (int i = m_iComponentsCount - 1; i >= 0; i--)
426  {
427  m_aComponentsSorted[i].EOnCameraExit();
428  }
429  m_iComponentsCount = -1;
430  //DiagMenu.Unregister(SCR_DebugMenuID.DEBUGUI_MANUAL_CAMERA);
431  //DiagMenu.Unregister(SCR_DebugMenuID.DEBUGUI_MANUAL_CAMERA_DEBUG);
432  }
433 
435  //--- Default functions
437 
438  //------------------------------------------------------------------------------------------------
439  override protected void EOnPostFrame(IEntity owner, float timeSlice)
440  {
441  if (m_CameraManager && m_CameraManager.CurrentCamera() != this)
442  return;
443 
444  timeSlice = timeSlice / m_World.GetTimeScale();
445  ProcessComponents(timeSlice);
446  }
447 
448  //------------------------------------------------------------------------------------------------
449  override protected void EOnInit(IEntity owner)
450  {
451  if (SCR_Global.IsEditMode(this)) return;
452 
453  if (owner.GetFlags() & EntityFlags.EDITOR_ONLY)
454  {
455  owner.ClearEventMask(EntityEvent.POSTFRAME);
456  return;
457  }
458 
459  ChimeraWorld world = GetGame().GetWorld();
460  if (world)
461  {
462  world.RegisterEntityToBeUpdatedWhileGameIsPaused(this);
463  }
464 
465  m_Owner = owner;
466  if (m_CameraManager) m_CameraManager.SetCamera(this);
467 
468  //--- Move to starting position
469  if (!m_bStartAtEntity && !IsFirstCamera())
470  {
471  MoveToCamera()
472  }
473 
474  //--- Create parent widget
475  m_Widget = GetGame().GetWorkspace().CreateWidget(WidgetType.FrameWidgetTypeID, WidgetFlags.VISIBLE | WidgetFlags.IGNORE_CURSOR | WidgetFlags.NOFOCUS, Color.FromInt(Color.WHITE), 0);
476  m_Widget.SetName(Type().ToString());
477  FrameSlot.SetAnchorMin(m_Widget, 0, 0);
478  FrameSlot.SetAnchorMax(m_Widget, 1, 1);
479  FrameSlot.SetOffsets(m_Widget, 0, 0, 0, 0);
480 
481  InitComponents();
482 
483  DiagMenu.RegisterMenu(SCR_DebugMenuID.DEBUGUI_MANUAL_CAMERA, "Manual Camera", "");
484  DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_MANUAL_CAMERA_DEBUG, "", "Show debug menu", "Manual Camera", false);
485 
486  //--- Initial activation (timeSlice=0 will mark it as dirty to process all components)
487  ProcessComponents(0);
488  }
489 
490  //------------------------------------------------------------------------------------------------
491  // constructor
494  void SCR_ManualCamera(IEntitySource src, IEntity parent)
495  {
496  if (SCR_Global.IsEditMode(this)) return;
497 
498  m_MenuManager = GetGame().GetMenuManager();
499  m_InputManager = GetGame().GetInputManager();
500  m_World = GetWorld();
501  m_fFOVDefault = GetVerticalFOV();
502 
503  if (m_Widget) m_Widget.RemoveFromHierarchy();
504 
505  ArmaReforgerScripted game = GetGame();
506  if (!game) return;
507 
508  m_CameraManager = game.GetCameraManager();
509  //if (!m_CameraManager) return; //--- Camera manager is not required, so the camera can be used for quick debugging in test worlds
510 
511  RegisterComponents();
512 
513  SetFlags(EntityFlags.ACTIVE);
514  SetEventMask(EntityEvent.INIT | EntityEvent.POSTFRAME);
515 
516  SetName("ManualCamera");
517  }
518 
519  //------------------------------------------------------------------------------------------------
520  // destructor
521  void ~SCR_ManualCamera()
522  {
523  ChimeraWorld world = GetGame().GetWorld();
524  if (world)
525  {
526  world.UnregisterEntityToBeUpdatedWhileGameIsPaused(this);
527  }
528 
529  Terminate();
530  SwitchToPreviousCamera();
531  }
532 }
ChimeraWorld
Definition: ChimeraWorld.c:12
SCR_ManualCamera
Definition: SCR_ManualCamera.c:16
m_InputManager
protected InputManager m_InputManager
Definition: SCR_BaseManualCameraComponent.c:15
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
SCR_BaseManualCameraComponent
Parent class from which all SCR_ManualCamera components inherit.
Definition: SCR_BaseManualCameraComponent.c:5
SetVerticalFOV
proto external void SetVerticalFOV(float fov)
EditorMenuUI
Definition: EditorMenuUI.c:3
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_CameraBaseClass
Definition: SCR_CameraBase.c:2
EManualCameraFlag
EManualCameraFlag
Definition: EManualCameraFlag.c:7
SCR_CameraBase
Definition: SCR_CameraBase.c:9
Attribute
typedef Attribute
Post-process effect of scripted camera.
IsEnabled
int IsEnabled()
Definition: SCR_BaseManualCameraComponent.c:99
GetVerticalFOV
proto external float GetVerticalFOV()
m_World
protected BaseWorld m_World
Definition: SCR_PreviewEntityEditorUIComponent.c:46
SCR_ManualCameraComponentSave
Definition: SCR_ManualCameraSave.c:15
InitBase
bool InitBase(SCR_ManualCamera camera)
Definition: SCR_BaseManualCameraComponent.c:125
SCR_ManualCameraParam
Parameter for carrying information between individual camera components.
Definition: SCR_ManualCameraParam.c:5
layout
UI layouts HUD CampaignMP CampaignMainHUD layout
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:20
SetTransform
override void SetTransform(vector transform[4], bool changedByUser=false)
Definition: SCR_EditableCharacterComponent.c:455
SCR_Global
Definition: Functions.c:6
SCR_ManualCameraClass
Definition: SCR_ManualCamera.c:2
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
m_MenuManager
protected MenuManager m_MenuManager
Definition: SCR_InfoDisplayExtended.c:27
m_Owner
SCR_AIGroupUtilityComponentClass m_Owner
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180