Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SettingsBindingBase.c
Go to the documentation of this file.
2 {
3  protected string m_sModule;
4  protected string m_sSubModule;
5  protected string m_sName;
6  protected string m_sWidgetName;
7  protected BaseContainer m_Setting;
8  protected int m_iItem;
9  protected bool m_bUseGameUserSettings;
10 
11  protected ref ScriptInvoker m_OnEntryChanged;
12 
13  // Values to be filled
14  protected float m_fMin, m_fMax, m_fStep, m_fCurrent;
15  protected ref array<string> m_aNames = new array<string>();
16  protected ref array<int> m_aVals = new array<int>();
17  protected bool m_bHasLimits = false;
18 
19  //------------------------------------------------------------------------------------------------
20  void SCR_SettingsBindingBase(string module, string name, string widgetName, string subModule = string.Empty)
21  {
22  m_sModule = module;
23  m_sName = name;
24  m_sWidgetName = widgetName;
25  m_sSubModule = subModule;
26  }
27 
28  //------------------------------------------------------------------------------------------------
29  SCR_WLibComponentBase GetComponent(string name, Widget parent)
30  {
31  if (!parent)
32  return null;
33 
34  Widget w = parent.FindAnyWidget(name);
35  if (!w)
36  return null;
37 
38  return SCR_WLibComponentBase.Cast(w.FindHandler(SCR_WLibComponentBase));
39  }
40 
41  //------------------------------------------------------------------------------------------------
42  ScriptInvoker GetEntryChangedInvoker()
43  {
44  if (!m_OnEntryChanged)
45  m_OnEntryChanged = new ScriptInvoker();
46 
47  return m_OnEntryChanged;
48  }
49 
50  //------------------------------------------------------------------------------------------------
51  string GetWidgetName()
52  {
53  return m_sWidgetName;
54  }
55 
56  //------------------------------------------------------------------------------------------------
57  void LoadEntry(Widget parent, bool forceLoadSettings = false, bool addEntryChangedEventHandler = true)
58  {
59  if ((forceLoadSettings || !m_Setting) && !SetBaseContainer())
60  return;
61 
62  if (!m_Setting || m_iItem < 0)
63  {
64  Print("Settings item not found: " + m_sName, LogLevel.WARNING);
65  return;
66  }
67 
68  SCR_WLibComponentBase comp = GetComponent(m_sWidgetName, parent);
69  if (!comp)
70  comp = GetComponent(m_sWidgetName, parent.GetParent()); // VideoSettings Quality Preset "shortcut"
71 
72  if (!comp)
73  {
74  Print("Settings widget not found: " + m_sWidgetName, LogLevel.WARNING);
75  return;
76  }
77 
78  if (InitSpinBox(comp, addEntryChangedEventHandler))
79  return;
80 
81  if (InitSlider(comp, addEntryChangedEventHandler))
82  return;
83 
84  if (InitCheckBox(comp, addEntryChangedEventHandler))
85  return;
86 
87  if (InitComboBox(comp, addEntryChangedEventHandler))
88  return;
89  }
90 
91  //------------------------------------------------------------------------------------------------
92  protected SCR_SpinBoxComponent InitSpinBox(SCR_WLibComponentBase comp, bool addEntryChangedEventHandler)
93  {
95  if (!spin)
96  return null;
97 
98  int current;
99  m_Setting.Get(m_sName, current);
100  spin.SetCurrentItem(current);
101  m_Setting.GetEnumValues(m_iItem, m_aNames, m_aVals);
102  if (m_Setting.GetLimits(m_iItem, m_fMin, m_fMax, m_fStep) == 3)
103  {
104  m_bHasLimits = true;
105 
106  int intendedSize = Math.Max(m_aNames.Count(), m_fMax + 1);
107  if (intendedSize != spin.GetNumItems())
108  Print(string.Format("Settings: %1 has wrong number of items: %2/%3", m_sName, spin.GetNumItems(), intendedSize), LogLevel.WARNING);
109  }
110 
111  if (addEntryChangedEventHandler)
112  spin.m_OnChanged.Insert(OnSelectableChanged);
113 
114  return spin;
115  }
116 
117  //------------------------------------------------------------------------------------------------
118  protected SCR_SliderComponent InitSlider(SCR_WLibComponentBase comp, bool addEntryChangedEventHandler)
119  {
120  SCR_SliderComponent slider = SCR_SliderComponent.Cast(comp);
121  if (!slider)
122  return null;
123 
124  float val;
125  if (m_Setting.GetLimits(m_iItem, m_fMin, m_fMax, m_fStep) == 3)
126  m_bHasLimits = true;
127  m_Setting.Get(m_sName, val);
128  slider.SetMin(m_fMin);
129  slider.SetMax(m_fMax);
130  slider.SetStep(m_fStep);
131  slider.SetValue(val);
132 
133  if (addEntryChangedEventHandler)
134  slider.GetOnChangedFinal().Insert(OnSliderChanged);
135 
136  return slider;
137  }
138 
139  //------------------------------------------------------------------------------------------------
140  protected SCR_CheckboxComponent InitCheckBox(SCR_WLibComponentBase comp, bool addEntryChangedEventHandler)
141  {
142  SCR_CheckboxComponent check = SCR_CheckboxComponent.Cast(comp);
143  if (!check)
144  return null;
145 
146  bool checked;
147  m_Setting.Get(m_sName, checked);
148  check.SetChecked(checked);
149  if (addEntryChangedEventHandler)
150  check.m_OnChanged.Insert(OnCheckboxChanged);
151 
152  return check;
153  }
154 
155  //------------------------------------------------------------------------------------------------
156  protected SCR_ComboBoxComponent InitComboBox(SCR_WLibComponentBase comp, bool addEntryChangedEventHandler)
157  {
158  SCR_ComboBoxComponent combo = SCR_ComboBoxComponent.Cast(comp);
159  if (!combo)
160  return null;
161 
162  int current;
163  m_Setting.Get(m_sName, current);
164  combo.SetCurrentItem(current);
165 
166  // Check for array size
167  m_Setting.GetEnumValues(m_iItem, m_aNames, m_aVals);
168  if (m_Setting.GetLimits(m_iItem, m_fMin, m_fMax, m_fStep) == 3)
169  {
170  m_bHasLimits = true;
171 
172  int intendedSize = Math.Max(m_aNames.Count(), m_fMax);
173  if (intendedSize != combo.GetNumItems())
174  Print(string.Format("Settings: %1 has wrong number of items: %2/%3", m_sName, combo.GetNumItems(), intendedSize), LogLevel.WARNING);
175  }
176 
177  if (addEntryChangedEventHandler)
178  combo.m_OnChanged.Insert(OnSelectableChanged);
179 
180  return combo;
181  }
182 
183  //------------------------------------------------------------------------------------------------
184  protected BaseContainer SetBaseContainer()
185  {
186  UserSettings userSettings;
187  if (m_bUseGameUserSettings)
188  userSettings = GetGame().GetGameUserSettings();
189  else
190  userSettings = GetGame().GetEngineUserSettings();
191 
192  if (!userSettings)
193  return null;
194 
195  if (m_sSubModule.IsEmpty())
196  m_Setting = userSettings.GetModule(m_sModule);
197  else
198  {
199  BaseContainer setting = GetGame().GetEngineUserSettings().GetModule(m_sModule);
200  if (setting)
201  m_Setting = setting.GetObject(m_sSubModule);
202  }
203 
204  return m_Setting;
205  }
206 
207  //------------------------------------------------------------------------------------------------
208  protected void OnSelectableChanged(SCR_WLibComponentBase comp, int i)
209  {
210  if (!m_Setting && !SetBaseContainer())
211  return;
212 
213  if (i >= 0 && i < m_aVals.Count())
214  {
215  m_Setting.Set(m_sName, m_aVals[i]);
216  }
217  else if (m_bHasLimits)
218  {
219  int value = m_fMin + m_fStep * i;
220  m_Setting.Set(m_sName, value);
221  }
222  else
223  {
224  m_Setting.Set(m_sName, i);
225  }
226 
227  m_OnEntryChanged.Invoke(this);
228  }
229 
230  //------------------------------------------------------------------------------------------------
231  protected void OnSliderChanged(SCR_WLibComponentBase comp, float value)
232  {
233  if (!m_Setting && !SetBaseContainer())
234  return;
235 
236  m_Setting.Set(m_sName, value);
237  m_OnEntryChanged.Invoke(this);
238  }
239 
240  //------------------------------------------------------------------------------------------------
241  protected void OnCheckboxChanged(SCR_WLibComponentBase comp, bool checked)
242  {
243  if (!m_Setting && !SetBaseContainer())
244  return;
245 
246  m_Setting.Set(m_sName, checked);
247  m_OnEntryChanged.Invoke(this);
248  }
249 };
SCR_CheckboxComponent
This is refactored checkbox preserving its own class and API for compatability purposes.
Definition: SCR_CheckboxComponent.c:3
SCR_SettingsBindingBase
Definition: SCR_SettingsBindingBase.c:1
SCR_ComboBoxComponent
Definition: SCR_ComboBoxComponent.c:1
SCR_WLibComponentBase
Base class for all final Reforger interactive elements.
Definition: SCR_WLibComponentBase.c:4
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_fMax
protected float m_fMax
Definition: SCR_BaseEditorAttribute.c:520
SCR_SliderComponent
Definition: SCR_SliderComponent.c:2
SCR_SpinBoxComponent
Definition: SCR_SpinBoxComponent.c:1
m_fStep
protected float m_fStep
Definition: SCR_BaseEditorAttribute.c:521
m_sName
protected LocalizedString m_sName
Definition: SCR_GroupIdentityComponent.c:19
m_fMin
SCR_BaseEditorAttributeEntryUIInfo m_fMin