Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_BudgetEditorUIComponent.c
Go to the documentation of this file.
2 {
3  [Attribute("0.1")]
4  protected float m_fDisabledOpacity;
5 
6  const string WIDGET_BUDGET_TEXT = "PercentageText";
7  const string WIDGET_BUDGET_ICON = "Icon";
8  const string WIDGET_BUDGET_PROGRESSBAR = "RadialProgressBar";
9  const string WIDGET_BUDGETPREVIEW_TEXT = "Preview";
10  const string WIDGET_ICON_AREA = "IconArea";
11  const string WIDGET_LOCK_VISUALS = "LockVisuals";
12 
13  protected SCR_BudgetEditorComponent m_BudgetManager;
14  protected ResourceName m_BudgetEntryPrefab;
15 
16  protected Widget m_Layout;
17 
18  protected ref map<EEditableEntityBudget, Widget> m_BudgetWidgets = new map<EEditableEntityBudget, Widget>();
19 
20  //------------------------------------------------------------------------------------------------
21  protected void OnBudgetMaxUpdate(EEditableEntityBudget budgetType, int currentBudgetValue, int maxBudgetValue)
22  {
23  TextWidget budgetWidget;
24  if (m_BudgetWidgets.Find(budgetType, budgetWidget))
25  {
26  SetBudgetData(budgetWidget, currentBudgetValue, maxBudgetValue);
27  SetBudgetPreviewData(budgetWidget, 0, 0);
28  }
29  }
30 
31  //------------------------------------------------------------------------------------------------
32  protected void OnBudgetUpdate(EEditableEntityBudget budgetType, int originalBudgetValue, int updatedBudgetValue, int maxBudgetValue)
33  {
34  TextWidget budgetWidget;
35  if (m_BudgetWidgets.Find(budgetType, budgetWidget))
36  {
37  SetBudgetData(budgetWidget, updatedBudgetValue, maxBudgetValue);
38  SetBudgetPreviewData(budgetWidget, 0, 0);
39  }
40  }
41 
42  //------------------------------------------------------------------------------------------------
43  protected void OnBudgetAdd(SCR_EditableEntityCoreBudgetSetting budget, int maxBudget)
44  {
45  WorkspaceWidget workspace = GetGame().GetWorkspace();
46  if (!workspace || !m_Layout || m_BudgetEntryPrefab.IsEmpty())
47  return;
48 
49  EEditableEntityBudget budgetType = budget.GetBudgetType();
50  if (m_BudgetWidgets.Contains(budgetType))
51  return;
52 
53  Widget budgetWidget = workspace.CreateWidgets(m_BudgetEntryPrefab, m_Layout);
54  SetBudgetData(budgetWidget, budget.GetCurrentBudget(), maxBudget, budget.GetInfo());
55 
56  m_BudgetWidgets.Insert(budgetType, budgetWidget);
57  }
58 
59  //------------------------------------------------------------------------------------------------
60  protected void OnBudgetPreviewUpdate(EEditableEntityBudget budgetType, float previewBudgetValue, float budgetChange)
61  {
62  Widget budgetWidget;
63  if (m_BudgetWidgets.Find(budgetType, budgetWidget))
64  SetBudgetPreviewData(budgetWidget, previewBudgetValue, budgetChange);
65  }
66 
67  //------------------------------------------------------------------------------------------------
68  protected void InitializeBudgets()
69  {
70  array<ref SCR_EditableEntityCoreBudgetSetting> budgets = {};
71  m_BudgetManager.GetBudgets(budgets);
72 
73  foreach (SCR_EditableEntityCoreBudgetSetting budget : budgets)
74  {
75  int maxBudget;
76  m_BudgetManager.GetMaxBudgetValue(budget.GetBudgetType(), maxBudget);
77 
78  OnBudgetAdd(budget, maxBudget);
79  }
80  }
81 
82  //------------------------------------------------------------------------------------------------
83  protected void SetBudgetData(Widget budgetWidget, int budgetValue, int maxBudgetValue, SCR_UIInfo info = null)
84  {
85  float budgetProgress;
86  if (maxBudgetValue > 0)
87  budgetProgress = budgetValue / (float) maxBudgetValue;
88  else
89  budgetProgress = 1;
90 
91  TextWidget budgetValueText = TextWidget.Cast(budgetWidget.FindAnyWidget(WIDGET_BUDGET_TEXT));
92  if (budgetValueText)
93  budgetValueText.SetTextFormat("#AR-ValueUnit_Percentage", (budgetProgress * 100).ToString(-1, 0));
94 
95  bool isBudgetEnabled = maxBudgetValue > 0;
96 
97  //~ TODO: Currently budget percentage is hidden. This should still show the server percentage
98  if (budgetValueText)
99  budgetValueText.SetVisible(isBudgetEnabled);
100 
101  Widget lockVisuals = budgetWidget.FindAnyWidget(WIDGET_LOCK_VISUALS);
102  if (lockVisuals)
103  lockVisuals.SetVisible(!isBudgetEnabled);
104 
105  Widget iconVisuals = budgetWidget.FindAnyWidget(WIDGET_ICON_AREA);
106  if (iconVisuals)
107  {
108  if (!isBudgetEnabled)
109  iconVisuals.SetOpacity(m_fDisabledOpacity);
110  else
111  iconVisuals.SetOpacity(1);
112  }
113 
114  if (info)
115  {
116  ImageWidget budgetIcon = ImageWidget.Cast(budgetWidget.FindAnyWidget(WIDGET_BUDGET_ICON));
117  info.SetIconTo(budgetIcon);
118  SCR_LinkTooltipTargetEditorUIComponent.SetInfo(budgetWidget, info);
119  }
120 
121  Widget progressBarWidget = budgetWidget.FindAnyWidget(WIDGET_BUDGET_PROGRESSBAR);
122  if (!progressBarWidget)
123  return;
124 
126  if (!progressBar)
127  return;
128 
129  if (isBudgetEnabled)
130  progressBar.SetProgress(budgetProgress);
131  else
132  progressBar.SetProgress(0);
133  }
134 
135  //------------------------------------------------------------------------------------------------
136  protected void SetBudgetPreviewData(Widget w, float previewBudgetValue, float budgetChange)
137  {
138  TextWidget previewText = TextWidget.Cast(w.FindAnyWidget(WIDGET_BUDGETPREVIEW_TEXT));
139 
140  Widget progressBarWidget = w.FindAnyWidget(WIDGET_BUDGET_PROGRESSBAR);
141  if (!progressBarWidget)
142  return;
143 
145  if (!progressBar)
146  return;
147 
148  if (previewText && budgetChange != 0)
149  {
150  previewText.SetVisible(true);
151 
152  // If budget change is <1%, only show +, once progress bars are implemented this will simply make the bar red
153  bool isLessThenOne = false;
154  string amount;
155  if (budgetChange > 0 && budgetChange < 1)
156  {
157  isLessThenOne = true;
158  amount = "1";
159  }
160  else
161  {
162  amount = budgetChange.ToString(-1, 0);
163  }
164 
165  if (!isLessThenOne)
166  previewText.SetTextFormat("#AR-ValueUnit_Percentage_Add", amount);
167  else
168  previewText.SetTextFormat("#AR-ValueUnit_Percentage_AddLessThen", amount);
169 
170  if (progressBar)
171  progressBar.SetPreviewProgress(previewBudgetValue * 0.01);
172  }
173  else if (previewText)
174  {
175  previewText.SetVisible(false);
176 
177  if(progressBar)
178  progressBar.SetPreviewProgress(0);
179  }
180  }
181 
182  //------------------------------------------------------------------------------------------------
183  private void ResetWidgetPreviewData()
184  {
185  Widget budgetWidget = m_Layout.GetChildren();
186  int i = 0;
187  while (budgetWidget && i++ < 100)
188  {
189  SetBudgetPreviewData(budgetWidget, 0, 0);
190  budgetWidget = budgetWidget.GetSibling();
191  }
192  }
193 
194  //------------------------------------------------------------------------------------------------
195  protected void UnregisterEvents()
196  {
197  if (!m_BudgetManager)
198  return;
199 
200  m_BudgetManager.Event_OnBudgetUpdated.Remove(OnBudgetUpdate);
201  m_BudgetManager.Event_OnBudgetMaxUpdated.Remove(OnBudgetMaxUpdate);
202  m_BudgetManager.Event_OnBudgetPreviewUpdated.Remove(OnBudgetPreviewUpdate);
203  m_BudgetManager.Event_OnBudgetPreviewReset.Remove(ResetWidgetPreviewData);
204  }
205 
206  //------------------------------------------------------------------------------------------------
207  override void HandlerAttachedScripted(Widget w)
208  {
209  m_Layout = w;
210 
211  m_BudgetEntryPrefab = SCR_LayoutTemplateComponent.GetLayout(m_Layout);
212  if (m_BudgetEntryPrefab.IsEmpty())
213  return;
214 
215  // Remove any existing budget widgets, used for configuring UI layouts
216  Widget debugBudgetWidget = m_Layout.GetChildren();
217  int i = 0;
218  while(debugBudgetWidget && i++ < 100)
219  {
220  Widget debugFilterSibling = debugBudgetWidget.GetSibling();
221  debugBudgetWidget.RemoveFromHierarchy();
222  debugBudgetWidget = debugFilterSibling;
223  }
224 
225  m_BudgetManager = SCR_BudgetEditorComponent.Cast(SCR_BudgetEditorComponent.GetInstance(SCR_BudgetEditorComponent, false, true));
226  if (m_BudgetManager)
227  {
228  InitializeBudgets();
229  m_BudgetManager.Event_OnBudgetUpdated.Insert(OnBudgetUpdate);
230  m_BudgetManager.Event_OnBudgetMaxUpdated.Insert(OnBudgetMaxUpdate);
231  m_BudgetManager.Event_OnBudgetPreviewUpdated.Insert(OnBudgetPreviewUpdate);
232  m_BudgetManager.Event_OnBudgetPreviewReset.Insert(ResetWidgetPreviewData);
233  }
234 
235  if (m_BudgetManager)
236  m_BudgetManager.DemandBudgetUpdateFromServer();
237  }
238 
239  //------------------------------------------------------------------------------------------------
240  override void HandlerDeattached(Widget w)
241  {
242  m_BudgetWidgets.Clear();
243 
244  UnregisterEvents();
245  }
246 }
SCR_EditableEntityCoreBudgetSetting
Definition: SCR_EditableEntityCoreBudgetSetting.c:2
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_Layout
ResourceName m_Layout
Definition: SCR_ActionsToolbarEditorUIComponent.c:6
SCR_LayoutTemplateComponent
Definition: SCR_LayoutTemplateComponent.c:1
SCR_RadialProgressBarUIComponent
Definition: SCR_RadialProgressBarUIComponent.c:1
EEditableEntityBudget
EEditableEntityBudget
Definition: EEditableEntityBudget.c:1
SCR_BudgetEditorComponent
Definition: SCR_BudgetEditorComponent.c:18
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_BudgetEditorUIComponent
Definition: SCR_BudgetEditorUIComponent.c:1
SCR_UIInfo
Definition: SCR_UIInfo.c:7
SCR_BaseEditorUIComponent
Definition: SCR_BaseEditorUIComponent.c:3