Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PIPSightsComponent.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Weapon/Sights/Picture in Picture Sights", description: "", color: "0 0 255 255")]
3 {
4 };
5 
6 //------------------------------------------------------------------------------------------------
7 class SCR_PIPSightsComponent : ScriptedSightsComponent
8 {
9  [Attribute("{EF091399D840192D}UI/layouts/Sights/PictureInPictureSightsLayout.layout", UIWidgets.ResourceNamePicker, "The layout used for this PIP component", params: "layout")]
10  protected ResourceName m_sLayoutResource;
11 
12  [Attribute("RTTexture0", UIWidgets.EditBox, "Name of RTTexture widget within provided layout")]
13  protected string m_sRTTextureWidgetName;
14 
15  [Attribute("RenderTarget0", UIWidgets.EditBox, "Name of RenderTarget widget within provided layout")]
16  protected string m_sRTargetWidgetName;
17 
18  [Attribute("1", UIWidgets.Slider, "Camera index used for this PIP component", params: "0 31 1")]
19  protected int m_iCameraIndex;
20 
21  [Attribute("12.5", UIWidgets.Slider, "Camera field of view used by this PIP component", params: "0 89.9 0.1")]
22  protected float m_fCameraFOV;
23 
24  [Attribute("0.2", UIWidgets.Slider, "Camera near clipping plane", params: "0 1000 0.01")]
25  protected float m_fNearPlane;
26 
27  [Attribute("1500", UIWidgets.Slider, "Camera field of view used by this PIP component", params: "0 5000 1")]
28  protected float m_fFarPlane;
29 
30  // Point info?
31  [Attribute("1.0", UIWidgets.EditBox, "Scale of resolution used by the PIP", params: "0.1 1 0.1")]
32  protected float m_fResolutionScale;
33 
34  // Point info?
35  [Attribute("0 0 0", UIWidgets.EditBox, "Camera offset used for this PIP")]
36  protected vector m_vCameraPoint;
37 
38 
40  protected bool m_bIsEnabled;
41 
43  protected ref Widget m_wRoot;
44 
46  protected IEntity m_pOwner;
47 
49  protected RTTextureWidget m_wRenderTargetTextureWidget;
50 
52  protected RenderTargetWidget m_wRenderTargetWidget;
53 
55  protected ScriptCamera m_PIPCamera;
56 
57  #ifdef WORKBENCH
58  //------------------------------------------------------------------------------------------------
59  protected override void _WB_AfterWorldUpdate(IEntity owner, float timeSlice)
60  {
61  const float axisLength = 0.1;
62 
63  vector origin = owner.GetOrigin() + m_vCameraPoint;
64  vector right = origin + owner.GetTransformAxis(0) * axisLength;
65  vector up = origin + owner.GetTransformAxis(1) * axisLength;
66  vector fwd = origin + owner.GetTransformAxis(2) * axisLength;
67 
68  ref auto rightArrow = Shape.CreateArrow(origin, right, 0.01, ARGB(240,255,0,0), ShapeFlags.ONCE);
69  ref auto upArrow = Shape.CreateArrow(origin, up, 0.01, ARGB(240,0,255,0), ShapeFlags.ONCE);
70  ref auto fwdArrow = Shape.CreateArrow(origin, fwd, 0.01, ARGB(240,0,0,255), ShapeFlags.ONCE);
71  }
72  #endif
73 
74  //------------------------------------------------------------------------------------------------
75  void SetEnabled(bool enabled)
76  {
77  // A component without an owner shouldnt exist
78  if (!m_pOwner)
79  return;
80 
81  // disabled->enabled
82  // Create neccessary items
83  if (enabled && !m_bIsEnabled)
84  {
85  // Try to create UI for PIP,
86  // output params are either set to valid ones,
87  // or root itself is set to null and destroyed
88  if (!m_wRoot || !m_wRenderTargetTextureWidget || !m_wRenderTargetWidget)
89  m_wRoot = CreateUI(m_sLayoutResource, m_sRTTextureWidgetName, m_sRTargetWidgetName, m_wRenderTargetTextureWidget, m_wRenderTargetWidget);
90 
91  if (!m_wRoot)
92  {
93  Print("Could not create PIP layout!");
94  return;
95  }
96 
97  // Create PIP camera
98  if (!m_PIPCamera)
99  m_PIPCamera = CreateCamera(m_pOwner, m_vCameraPoint, m_iCameraIndex, m_fCameraFOV, m_fNearPlane, m_fFarPlane);
100 
101  // Set camera index of render target widget
102  BaseWorld baseWorld = m_pOwner.GetWorld();
103  m_wRenderTargetWidget.SetWorld(baseWorld, m_iCameraIndex);
104 
105  // Set resolution scale
106  m_wRenderTargetWidget.SetResolutionScale(m_fResolutionScale, m_fResolutionScale);
107 
108  // Set RTT on parent
109  m_wRenderTargetTextureWidget.SetGUIWidget(m_pOwner, 0);
110 
111  m_bIsEnabled = true;
112  return;
113  }
114 
115  // enabled -> disabled
116  if (!enabled && m_bIsEnabled)
117  {
118  Destroy();
119 
120  m_bIsEnabled = false;
121  return;
122  }
123  }
124 
125  //------------------------------------------------------------------------------------------------
126  protected Widget CreateUI(string layout, string rtTextureName, string rtName, out RTTextureWidget RTTexture, out RenderTargetWidget RTWidget)
127  {
128  // Empty layout, cannot create any widget
129  if (layout == string.Empty)
130  return null;
131 
132  // Create layout
133  Widget root = GetGame().GetWorkspace().CreateWidgets(layout);
134 
135  // Layout was not created successfully
136  if (!root)
137  return null;
138 
139  // We dont have required RT widgets, delete layout and terminate
140  RTTexture = RTTextureWidget.Cast(root.FindAnyWidget(rtTextureName));
141  RTWidget = RenderTargetWidget.Cast(root.FindAnyWidget(rtName));
142  if (!RTTexture || !RTWidget)
143  {
144  root.RemoveFromHierarchy();
145  return null;
146  }
147 
148  return root;
149  }
150 
151  //------------------------------------------------------------------------------------------------
152  protected void DestroyUI(Widget root)
153  {
154  if (root)
155  {
156  root.RemoveFromHierarchy();
157  }
158  }
159 
160  //------------------------------------------------------------------------------------------------
167  protected ScriptCamera CreateCamera(IEntity parent, vector position, int cameraIndex, float fov, float nearPlane, float farPlane)
168  {
169  // Spawn camera
170  BaseWorld baseWorld = parent.GetWorld();
171  ScriptCamera pipCamera = ScriptCamera.Cast(GetGame().SpawnEntity(ScriptCamera, baseWorld));
172 
173  // Since we use autotransform,
174  // we set camera matrix to "local" coordinates
175  vector mat[4];
176  Math3D.MatrixIdentity3(mat);
177  mat[3] = position;
178 
179 
180  pipCamera.SetTransform(mat);
181  pipCamera.Index = cameraIndex;
182  pipCamera.FreeFly = false;
183 
184  // These are attributes used in constructor of
185  // scriptCamera so they will most likely not work
186 
187  /*
188  pipCamera.FOV = m_fCameraFOV;
189  pipCamera.NearPlane = nearPlane;
190  pipCamera.FarPlane = farPlane;
191  */
192 
193  // Instead we do it the old fashionate way
194  baseWorld.SetCameraVerticalFOV(cameraIndex, m_fCameraFOV);
195  baseWorld.SetCameraFarPlane(cameraIndex, farPlane);
196  baseWorld.SetCameraNearPlane(cameraIndex, nearPlane);
197 
198  // Set camera to hierarchy
199  parent.AddChild(pipCamera, -1, EAddChildFlags.AUTO_TRANSFORM);
200  return pipCamera;
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  protected void DestroyCamera(ScriptCamera camera)
205  {
206  if (camera)
207  {
208  IEntity cameraParent = camera.GetParent();
209  if (cameraParent)
210  cameraParent.RemoveChild(camera);
211 
212  delete camera;
213  }
214  }
215 
216  //------------------------------------------------------------------------------------------------
217  // Destroy everything this component created during its lifetime
218  protected void Destroy()
219  {
220  DestroyCamera(m_PIPCamera);
221  DestroyUI(m_wRoot);
222  }
223 
224  //------------------------------------------------------------------------------------------------
225  protected override void OnSightADSActivated()
226  {
227  SetEnabled(true);
228  super.OnSightADSActivated();
229  }
230 
231  //------------------------------------------------------------------------------------------------
232  protected override void OnSightADSDeactivated()
233  {
234  SetEnabled(false);
235  super.OnSightADSDeactivated();
236  }
237 
238  //------------------------------------------------------------------------------------------------
239  protected override void OnInit(IEntity owner)
240  {
241  m_pOwner = owner;
242  }
243 
244  //------------------------------------------------------------------------------------------------
246  void SCR_PIPSightsComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
247  {
248  }
249 
250  //------------------------------------------------------------------------------------------------
252  void ~SCR_PIPSightsComponent()
253  {
254  Destroy();
255  }
256 };
SpawnEntity
protected IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
Definition: SCR_CatalogEntitySpawnerComponent.c:1008
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
ScriptedSightsComponentClass
Definition: ScriptedSightsComponent.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_PIPSightsComponentClass
Definition: SCR_PIPSightsComponent.c:2
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_PIPSightsComponent
Definition: SCR_PIPSightsComponent.c:7
m_bIsEnabled
protected bool m_bIsEnabled
Definition: SCR_BaseSupportStationComponent.c:97
layout
UI layouts HUD CampaignMP CampaignMainHUD layout
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:20
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180