Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PositionEditorAttributeUIComponent.c
Go to the documentation of this file.
2 {
3  [Attribute("EditboxX")]
4  protected string m_sEditBoxXName;
5  [Attribute("EditboxY")]
6  protected string m_sEditBoxYName;
7  [Attribute("EditboxZ")]
8  protected string m_sEditBoxZName;
9  [Attribute("EditBoxRoot0")]
10  protected string m_sEditBoxComponentName;
11 
12  protected BaseWorld m_World;
13  protected vector m_vMinWorldBounding;
14  protected vector m_vMaxWorldBounding;
15 
16  protected SCR_EditBoxComponent m_EditBoxX;
17  protected SCR_EditBoxComponent m_EditBoxY;
18  protected SCR_EditBoxComponent m_EditBoxZ;
19 
20  protected Widget m_root;
21  protected vector m_vStartingValue;
22  protected vector m_vHideValue;
23 
24  protected bool m_bSnapToSurface;
25  protected float m_fPositionY;
26 
27  protected int m_iInitCalledAmount = 0;
28 
29  override void Init(Widget w, SCR_BaseEditorAttribute attribute)
30  {
31  m_root = w;
32  GetGame().OnInputDeviceIsGamepadInvoker().Insert(OnInputDeviceIsGamepad);
33 
34  m_World = GetGame().GetWorld();
35  if (!m_World)
36  return;
37 
38  m_World.GetBoundBox(m_vMinWorldBounding, m_vMaxWorldBounding);
39 
40  Widget editBoxX = w.FindAnyWidget(m_sEditBoxXName);
41  Widget editBoxY = w.FindAnyWidget(m_sEditBoxYName);
42  Widget editBoxZ = w.FindAnyWidget(m_sEditBoxZName);
43 
44  if (!editBoxX || !editBoxY || !editBoxZ)
45  return;
46 
47  Widget editBoxComponentX = editBoxX.FindAnyWidget(m_sEditBoxComponentName);
48  Widget editBoxComponentY = editBoxY.FindAnyWidget(m_sEditBoxComponentName);
49  Widget editBoxComponentZ = editBoxZ.FindAnyWidget(m_sEditBoxComponentName);
50 
51  if (!editBoxComponentX || !editBoxComponentY || !editBoxComponentZ)
52  return;
53 
54  m_EditBoxX = SCR_EditBoxComponent.Cast(editBoxComponentX.FindHandler(SCR_EditBoxComponent));
55  m_EditBoxY = SCR_EditBoxComponent.Cast(editBoxComponentY.FindHandler(SCR_EditBoxComponent));
56  m_EditBoxZ = SCR_EditBoxComponent.Cast(editBoxComponentZ.FindHandler(SCR_EditBoxComponent));
57 
58  if (!m_EditBoxX || !m_EditBoxY || !m_EditBoxZ)
59  return;
60 
61  m_EditBoxX.m_OnConfirm.Insert(OnEditBoxValueChanged);
62  m_EditBoxY.m_OnConfirm.Insert(OnEditBoxValueChanged);
63  m_EditBoxZ.m_OnConfirm.Insert(OnEditBoxValueChanged);
64 
65  //Enable disable Y
66  SCR_BaseEditorAttribute snapAattribute;
67  if (m_AttributeManager.GetActiveAttribute(SCR_SnapYPositionEditorAttribute, snapAattribute))
68  {
69  SCR_SnapYPositionEditorAttribute snapYAttribute = SCR_SnapYPositionEditorAttribute.Cast(snapAattribute);
70  if (snapYAttribute)
71  {
72  snapYAttribute.GetOnChanged().Insert(OnSnapYChanged);
73  OnSnapYChanged(snapYAttribute.GetVariableOrCopy().GetBool());
74  }
75  }
76  //Always snap if snap attribute not found
77  else
78  {
79  OnSnapYChanged(true);
80  }
81 
82  SCR_BaseEditorAttributeVar var = attribute.GetVariableOrCopy();
83 
84  if (var)
85  m_vStartingValue = var.GetVector();
86 
87  //Hide if gamepad
88  OnInputDeviceIsGamepad(!GetGame().GetInputManager().IsUsingMouseAndKeyboard());
89  super.Init(w, attribute);
90  }
91 
92  protected void OnSnapYChanged(bool snapEnabled)
93  {
94  m_bSnapToSurface = snapEnabled;
95  m_EditBoxY.SetEnabled(!snapEnabled);
96 
97  if (snapEnabled)
98  {
99  m_fPositionY = m_EditBoxY.GetValue().ToFloat();
100  m_EditBoxY.SetValue(" ");
101  }
102  else
103  {
104  m_EditBoxY.SetValue(CapValueToString(m_fPositionY));
105  }
106 
107  OnChange(null, 1, 0, false);
108  }
109 
110  protected void OnInputDeviceIsGamepad(bool isGamepad)
111  {
112  SetVisible(!isGamepad);
113  }
114 
115  protected void SetVisible(bool newVisible)
116  {
117  SCR_BaseEditorAttribute attribute = GetAttribute();
118  if (!attribute)
119  return;
120 
121  SCR_BaseEditorAttributeVar var = attribute.GetVariableOrCopy();
122  if (!var)
123  return;
124 
125  if (!newVisible)
126  {
127  m_vHideValue = var.GetVector();
128  var.SetVector(m_vStartingValue);
129  }
130  else
131  {
132  var.SetVector(m_vHideValue);
133  }
134 
135  m_root.SetVisible(newVisible);
136  }
137 
138  //Sets a default state for the UI and var value if conflicting attribute
139  override void SetVariableToDefaultValue(SCR_BaseEditorAttributeVar var)
140  {
141  m_EditBoxX.SetValue(CapValueToString(m_vMinWorldBounding[0]));
142  m_EditBoxZ.SetValue(CapValueToString(m_vMinWorldBounding[2]));
143 
144  if (!m_bSnapToSurface)
145  m_EditBoxY.SetValue(CapValueToString(m_vMinWorldBounding[1]));
146  else
147  m_fPositionY = m_vMinWorldBounding[1];
148  }
149 
150  override void SetFromVar(SCR_BaseEditorAttributeVar var)
151  {
152  super.SetFromVar(var);
153 
154  if (!var)
155  return;
156 
157  vector value = var.GetVector();
158  m_EditBoxX.SetValue(CapValueToString(value[0]));
159  m_EditBoxZ.SetValue(CapValueToString(value[2]));
160 
161  if (!m_bSnapToSurface)
162  m_EditBoxY.SetValue(CapValueToString(value[1]));
163  else
164  m_fPositionY = value[1];
165  }
166 
167  override bool OnChange(Widget w, int x, int y, bool finished)
168  {
169  if (x != 1)
170  return false;
171 
172  SCR_BaseEditorAttribute attribute = GetAttribute();
173  if (!attribute)
174  return false;
175 
176  SCR_BaseEditorAttributeVar var = attribute.GetVariable(true);
177  if (!var)
178  return false;
179 
180  float xValue = m_EditBoxX.GetValue().ToFloat();
181  float yValue;
182  float zValue = m_EditBoxZ.GetValue().ToFloat();
183 
184  if (m_bSnapToSurface)
185  yValue = m_World.GetSurfaceY(xValue, zValue);
186  else
187  yValue = m_EditBoxY.GetValue().ToFloat();
188 
189  vector Value = Vector(xValue, yValue, zValue);
190 
191  var.SetVector(Value);
192  return false;
193  }
194 
195  protected void OnEditBoxValueChanged(SCR_EditBoxComponent editBox, string stringValue)
196  {
197  float value = stringValue.ToFloat();
198 
199  //X box
200  if (editBox == m_EditBoxX)
201  {
202  if (value < m_vMinWorldBounding[0])
203  value = m_vMinWorldBounding[0];
204  else if (value > m_vMaxWorldBounding[0])
205  value = m_vMaxWorldBounding[0];
206  }
207  //Y box
208  else if (editBox == m_EditBoxY)
209  {
210  if (!m_bSnapToSurface)
211  {
212  if (value < m_vMinWorldBounding[1])
213  value = m_vMinWorldBounding[1];
214  else if (value > m_vMaxWorldBounding[1])
215  value = m_vMaxWorldBounding[1];
216  }
217  }
218  //Z box
219  else
220  {
221  if (value < m_vMinWorldBounding[2])
222  value = m_vMinWorldBounding[2];
223  else if (value > m_vMaxWorldBounding[2])
224  value = m_vMaxWorldBounding[2];
225  }
226 
227  editBox.SetValue(CapValueToString(value));
228 
229  OnChange(null, 1, 0, false);
230  }
231 
232  protected string CapValueToString(float value)
233  {
234  int decimals = 2;
235 
236  float coef = Math.Pow(10, decimals);
237  value = Math.Round(value * coef);
238  string valueText = value.ToString();
239 
240  for (int i = 0, count = decimals - valueText.Length() + 1; i < count; i++)
241  {
242  valueText = "0" + valueText;
243  }
244  int length = valueText.Length();
245  valueText = valueText.Substring(0, length - decimals) + "." + valueText.Substring(length - decimals, decimals);
246 
247 
248  return valueText;
249  }
250 
251  override void HandlerDeattached(Widget w)
252  {
253  GetGame().OnInputDeviceIsGamepadInvoker().Remove(OnInputDeviceIsGamepad);
254 
255  if (m_AttributeManager && m_EditBoxY)
256  {
257  SCR_BaseEditorAttribute attribute;
258  if (m_AttributeManager.GetActiveAttribute(SCR_SnapYPositionEditorAttribute, attribute))
259  {
261  if (snapYAttribute)
262  {
263  snapYAttribute.GetOnChanged().Remove(OnSnapYChanged);
264  }
265  }
266  }
267 
268  }
269 };
270 
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SnapYPositionEditorAttribute
Definition: SCR_SnapYPositionEditorAttribute.c:5
SCR_PositionEditorAttributeUIComponent
Definition: SCR_PositionEditorAttributeUIComponent.c:1
SCR_BaseEditorAttributeVar
Definition: SCR_BaseEditorAttributeVar.c:1
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_BaseEditorAttribute
Base Attribute Script for other attributes to inherent from to get and set varriables in Editor Attri...
Definition: SCR_BaseEditorAttribute.c:3
m_World
protected BaseWorld m_World
Definition: SCR_PreviewEntityEditorUIComponent.c:46
SCR_BaseEditorAttributeUIComponent
Definition: SCR_BaseEditorAttributeUIComponent.c:3
GetInputManager
protected InputManager GetInputManager()
Definition: SCR_BaseManualCameraComponent.c:65
SCR_EditBoxComponent
Definition: SCR_EditBoxComponent.c:8