Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PlacingEditorUIComponent.c
Go to the documentation of this file.
1 
3 class SCR_PlacingEditorUIComponent : SCR_PreviewEntityEditorUIComponent
4 {
5  //--- When ASL vertical mode is on, how far from camera the entity appears when no ground intersection was found
6  protected static const float MAX_PREVIEW_DIS = 50;
7 
8  private InputManager m_InputManager;
9  private SCR_StatesEditorComponent m_StatesManager;
10  private SCR_CursorEditorUIComponent m_CursorComponent;
11  private SCR_EntitiesEditorUIComponent m_EntitiesComponent;
12  private SCR_SelectionEditorUIComponent m_SelectionComponent;
13  private SCR_MouseAreaEditorUIComponent m_MouseArea;
14  //private SCR_SiteSlotEntity m_Slot;
15  private bool m_bClicked;
16  private bool m_bCanPlace;
17  private ref array<Widget> m_aClickWhitelist = {};
18 
19  //------------------------------------------------------------------------------------------------
20  override void OnHoverChange(EEditableEntityState state, set<SCR_EditableEntityComponent> entitiesInsert, set<SCR_EditableEntityComponent> entitiesRemove)
21  {
22  if (m_StatesManager && m_StatesManager.GetState() == EEditorState.PLACING)
23  super.OnHoverChange(state, entitiesInsert, entitiesRemove);
24  }
25 
26  //------------------------------------------------------------------------------------------------
27  override void OnEditorTransformRotationModifierUp(float value, EActionTrigger reason)
28  {
29  if (m_StatesManager && m_StatesManager.GetState() == EEditorState.PLACING)
30  super.OnEditorTransformRotationModifierUp(value, reason);
31  }
32 
33  //------------------------------------------------------------------------------------------------
34  protected void OnEditorPlace(float value, EActionTrigger reason)
35  {
36  Place(false);
37  }
38 
39  //------------------------------------------------------------------------------------------------
40  protected void OnEditorPlaceAndCancel(float value, EActionTrigger reason)
41  {
42  Place(true);
43  }
44 
45  //------------------------------------------------------------------------------------------------
46  protected void OnEditorPlacePlayer(float value, EActionTrigger reason)
47  {
48  Place(false, true);
49  }
50 
51  //------------------------------------------------------------------------------------------------
52  protected void OnEditorPlacePlayerAndCancel(float value, EActionTrigger reason)
53  {
54  Place(true, true);
55 
56  //--- Prevent multi-selection from starting right afterwards (it's mapped to the same button by default; ToDo: More sandbox?)
57  if (m_SelectionComponent)
58  m_SelectionComponent.DisableMultiSelection();
59  }
60 
61  //------------------------------------------------------------------------------------------------
62  protected void Place(bool cancelAfterwards, bool canBePlayer = false)
63  {
64  //--- Placing not enabled
65  if (!CanClick() || !m_bCanPlace || (m_StatesManager && m_StatesManager.GetState() != EEditorState.PLACING))
66  return;
67 
68  if (!m_PreviewEntityManager.CanMoveInRoot() && !m_PreviewEntityManager.GetTarget())
69  return;
70 
71  //--- Map is opened, placing not possible
72  SCR_MapEntity mapEntity = SCR_MapEntity.GetMapInstance();
73  if (mapEntity && mapEntity.IsOpen())
74  return;
75 
76  if (!m_PlacingManager.CreateEntity(cancelAfterwards, canBePlayer))
77  return;
78 
79  //if (cancelAfterwards) CancelPlacing();
80  m_bClicked = false;
81  }
82 
83  //------------------------------------------------------------------------------------------------
84  protected void OnEditorCancelPlacingUp(float value, EActionTrigger reason)
85  {
86  if (OnCancelUp())
87  CancelPlacing();
88  }
89 
90  //------------------------------------------------------------------------------------------------
91  protected void CancelPlacing()
92  {
93  if (m_PlacingManager)
94  m_PlacingManager.SetSelectedPrefab();
95  }
96 
97  //------------------------------------------------------------------------------------------------
98  protected void EnablePlacing()
99  {
100  m_bCanPlace = true;
101  }
102 
103  //------------------------------------------------------------------------------------------------
104  protected bool CanClick()
105  {
106  if (m_CursorComponent && m_MouseArea && !m_MouseArea.IsMouseOn() && m_InputManager.IsUsingMouseAndKeyboard())
107  {
108  MenuRootComponent root = GetRootComponent();
109  if (root)
110  {
111  Widget rootWidget = root.GetWidget();
112  if (rootWidget)
113  {
114  //--- Get all widgets under cursor
115  int mouseX, mouseY;
116  m_CursorComponent.GetCursorPos(mouseX, mouseY);
117  array<Widget> widgets = {};
118  WidgetManager.TraceWidgets(mouseX, mouseY, rootWidget, widgets);
119 
120  //--- Check if the widgets are whitelisted. If not, terminate
121  foreach (Widget widget: widgets)
122  {
123  bool canClick = false;
124  foreach (Widget widgetWhitelisted: m_aClickWhitelist)
125  {
126  canClick |= IsChildOf(widget, widgetWhitelisted);
127  }
128 
129  if (!canClick)
130  return false;
131  }
132  }
133  }
134  }
135  return true;
136  }
137 
138  //------------------------------------------------------------------------------------------------
139  protected bool IsChildOf(Widget widget, Widget parent)
140  {
141  while (widget)
142  {
143  if (widget == parent)
144  return true;
145 
146  widget = widget.GetParent();
147  }
148  return false;
149  }
150 
151  //------------------------------------------------------------------------------------------------
152  protected void OnSelectedPrefabChange(ResourceName prefab, ResourceName prefabPrev)
153  {
154  ArmaReforgerScripted game = GetGame();
155  if (!game)
156  return;
157 
158  ScriptCallQueue queue = game.GetCallqueue();
159  if (!queue)
160  return;
161 
162  //--- Ignore input right after starting placing. Important for gamepads, because 'A' button is still held after selection.
163  queue.CallLater(EnablePlacing, 50);
164  m_bCanPlace = false;
165 
166  //--- Set default entity height when in ASL vertical mode
168  if (previewManager && previewManager.GetVerticalMode() == EEditorTransformVertical.SEA)
169  {
170  BaseWorld world = game.GetWorld();
171  if (!world)
172  return;
173 
174  WorkspaceWidget workspace = game.GetWorkspace();
175  if (!workspace)
176  return;
177 
178  //--- Get intersection in the middle of the screen (don't use cursor, it just returned form a sub-menu ans is not relevant)
179  float screenW, screenH;
180  workspace.GetScreenSize(screenW, screenH);
181  vector cameraDir;
182  vector cameraPos = workspace.ProjScreenToWorldNative(screenW * 0.5, screenH * 0.5, cameraDir, world, -1);
183 
184  //--- Find ground intersection, or use maximum distance when none is found
185  float traceDis = GetTraceDis(cameraPos, cameraDir * TRACE_DIS, cameraPos[1]);
186  if (traceDis == 1)
187  traceDis = MAX_PREVIEW_DIS;
188  else
189  traceDis *= TRACE_DIS;
190 
191  //--- Set default preview height
192  previewManager.SetPreviewHeight(cameraPos + cameraDir * traceDis);
193  }
194  }
195 
196  //------------------------------------------------------------------------------------------------
197  protected void OnMenuUpdate(float tDelta)
198  {
200 
201  if (m_StatesManager && m_StatesManager.GetState() != EEditorState.PLACING)
202  return;
203 
205  {
206  m_InputManager.ActivateContext("EditorPlacingContext");
207  if (m_PlacingManager.IsPlacingFlagCompatible(EEditorPlacingFlags.CHARACTER_PLAYER))
208  m_InputManager.ActivateContext("EditorPlacingPlayerContext");
209  }
210 
211  ProcessInput(tDelta);
212  }
213 
214  //------------------------------------------------------------------------------------------------
215  protected void OnMenuFocusGained()
216  {
217  GetMenu().GetOnMenuUpdate().Insert(OnMenuUpdate);
218  }
219 
220  //------------------------------------------------------------------------------------------------
221  protected void OnMenuFocusLost()
222  {
223  GetMenu().GetOnMenuUpdate().Remove(OnMenuUpdate);
224  }
225 
226  //------------------------------------------------------------------------------------------------
227  override protected SCR_EPreviewState GetPreviewStateToShow()
228  {
229  if (m_PlacingManager && m_PlacingManager.CanCreateEntity())
230  return SCR_EPreviewState.PLACEABLE;
231  else
232  return SCR_EPreviewState.BLOCKED;
233  }
234 
235  //------------------------------------------------------------------------------------------------
236  override void HandlerAttachedScripted(Widget w)
237  {
238  super.HandlerAttachedScripted(w);
239 
240  MenuRootComponent root = GetRootComponent();
241  if (root)
242  {
244  m_EntitiesComponent = SCR_EntitiesEditorUIComponent.Cast(root.FindComponent(SCR_EntitiesEditorUIComponent));
246  m_SelectionComponent = SCR_SelectionEditorUIComponent.Cast(root.FindComponent(SCR_SelectionEditorUIComponent));
247 
248  if (m_MouseArea)
249  m_aClickWhitelist.Insert(m_MouseArea.GetWidget());
250 
251  if (m_EntitiesComponent)
252  m_aClickWhitelist.Insert(m_EntitiesComponent.GetWidget());
253  }
254 
255  if (!m_CursorComponent)
256  Print("SCR_PlacingEditorUIComponent requires SCR_CursorEditorUIComponent!", LogLevel.ERROR);
257 
258  m_PlacingManager.GetOnSelectedPrefabChange().Insert(OnSelectedPrefabChange);
259 
261 
262  MenuRootBase menu = GetMenu();
263  if (menu)
264  {
265  menu.GetOnMenuUpdate().Insert(OnMenuUpdate);
266  menu.GetOnMenuFocusGained().Insert(OnMenuFocusGained);
267  menu.GetOnMenuFocusLost().Insert(OnMenuFocusLost);
268  }
269 
270  ArmaReforgerScripted game = GetGame();
271  if (game)
272  {
273  m_InputManager = game.GetInputManager();
274  if (m_InputManager)
275  {
276  m_InputManager.AddActionListener("EditorPlace", EActionTrigger.DOWN, OnEditorPlace);
277  m_InputManager.AddActionListener("EditorPlaceAndCancel", EActionTrigger.DOWN, OnEditorPlaceAndCancel);
278  m_InputManager.AddActionListener("EditorPlacePlayer", EActionTrigger.DOWN, OnEditorPlacePlayer);
279  m_InputManager.AddActionListener("EditorPlacePlayerAndCancel", EActionTrigger.DOWN, OnEditorPlacePlayerAndCancel);
280  m_InputManager.AddActionListener("EditorCancelPlacing", EActionTrigger.DOWN, OnCancelDown);
281  m_InputManager.AddActionListener("EditorCancelPlacing", EActionTrigger.UP, OnEditorCancelPlacingUp);
282  }
283  }
284  }
285 
286  //------------------------------------------------------------------------------------------------
287  override void HandlerDeattached(Widget w)
288  {
289  super.HandlerDeattached(w);
290 
291  if (m_PlacingManager)
292  m_PlacingManager.GetOnSelectedPrefabChange().Remove(OnSelectedPrefabChange);
293 
294  MenuRootBase menu = GetMenu();
295  if (menu)
296  {
297  menu.GetOnMenuUpdate().Remove(OnMenuUpdate);
298  menu.GetOnMenuFocusGained().Remove(OnMenuFocusGained);
299  menu.GetOnMenuFocusLost().Remove(OnMenuFocusLost);
300  }
301 
302  ArmaReforgerScripted game = GetGame();
303  if (game)
304  {
305  InputManager inputManager = game.GetInputManager();
306  if (m_InputManager)
307  {
308  inputManager.RemoveActionListener("EditorPlace", EActionTrigger.DOWN, OnEditorPlace);
309  inputManager.RemoveActionListener("EditorPlaceAndCancel", EActionTrigger.DOWN, OnEditorPlaceAndCancel);
310  inputManager.RemoveActionListener("EditorPlacePlayer", EActionTrigger.DOWN, OnEditorPlacePlayer);
311  inputManager.RemoveActionListener("EditorPlacePlayerAndCancel", EActionTrigger.DOWN, OnEditorPlacePlayerAndCancel);
312  m_InputManager.RemoveActionListener("EditorCancelPlacing", EActionTrigger.DOWN, OnCancelDown);
313  m_InputManager.RemoveActionListener("EditorCancelPlacing", EActionTrigger.UP, OnEditorCancelPlacingUp);
314  }
315  }
316  }
317 }
m_MouseArea
private SCR_MouseAreaEditorUIComponent m_MouseArea
Definition: SCR_CursorEditorUIComponent.c:38
EEditableEntityState
EEditableEntityState
Definition: EEditableEntityState.c:37
SCR_CursorEditorUIComponent
Definition: SCR_CursorEditorUIComponent.c:3
m_InputManager
protected InputManager m_InputManager
Definition: SCR_BaseManualCameraComponent.c:15
m_StatesManager
private SCR_StatesEditorComponent m_StatesManager
Definition: SCR_AttributesManagerEditorComponent.c:65
EEditorState
EEditorState
Unique editor state.
Definition: EEditorState.c:5
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetTraceDis
protected float GetTraceDis(vector pos, vector dir, float cameraHeight)
Definition: SCR_PreviewEntityEditorUIComponent.c:261
ProcessInput
protected void ProcessInput(float tDelta)
Definition: SCR_PreviewEntityEditorUIComponent.c:677
SCR_EPreviewState
SCR_EPreviewState
Definition: SCR_PreviewEntityEditorComponent.c:1138
SCR_EntitiesEditorUIComponent
Definition: SCR_EntitiesEditorUIComponent.c:1
m_PlacingManager
protected SCR_PlacingEditorComponent m_PlacingManager
Definition: SCR_PreviewEntityEditorUIComponent.c:43
EEditorTransformVertical
EEditorTransformVertical
Vertical transformation mode.
Definition: EEditorTransformVertical.c:5
SCR_PlacingEditorUIComponent
Definition: SCR_PlacingEditorUIComponent.c:3
GetMenu
SCR_RadialMenu GetMenu()
Definition: SCR_RadialMenuGameModeComponent.c:41
m_PreviewEntityManager
protected SCR_PreviewEntityEditorComponent m_PreviewEntityManager
Definition: SCR_PreviewEntityEditorUIComponent.c:41
MenuRootBase
Definition: MenuRootBase.c:6
SCR_MouseAreaEditorUIComponent
Definition: SCR_MouseAreaEditorUIComponent.c:3
m_bCanPlace
protected bool m_bCanPlace
Definition: SCR_ItemPlacementComponent.c:18
EEditorPlacingFlags
EEditorPlacingFlags
Definition: EEditorPlacingFlags.c:1
SCR_MapEntity
Map entity.
Definition: SCR_MapEntity.c:20
SCR_SelectionEditorUIComponent
Definition: SCR_SelectionEditorUIComponent.c:3
MenuRootComponent
Definition: MenuRootComponent.c:6
m_CursorComponent
protected SCR_CursorEditorUIComponent m_CursorComponent
Definition: SCR_EntitiesEditorUIComponent.c:16
ActivatePreviewContext
protected void ActivatePreviewContext()
Definition: SCR_PreviewEntityEditorUIComponent.c:756
SCR_StatesEditorComponent
Definition: SCR_StatesEditorComponent.c:9
SCR_PreviewEntityEditorComponent
Definition: SCR_PreviewEntityEditorComponent.c:12