Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
MenuRootComponent.c
Go to the documentation of this file.
1 
6 class MenuRootComponent : ScriptedWidgetComponent
7 {
8  protected Widget m_Widget;
9  protected MenuRootBase m_Menu;
10  protected ref array<MenuRootSubComponent> m_aComponents = {};
11 
12  //------------------------------------------------------------------------------------------------
18  static MenuRootComponent GetRootOf(Widget widget)
19  {
20  if (!widget)
21  return null;
22 
23  WorkspaceWidget workspace = GetGame().GetWorkspace();
24  Widget widgetRoot = widget;
25  while (widgetRoot)
26  {
27  Widget widgetRootParent = widgetRoot.GetParent();
28  if (!widgetRootParent || widgetRootParent == workspace)
29  break;
30  widgetRoot = widgetRootParent;
31  }
32 
33  MenuRootComponent rootComponent = MenuRootComponent.Cast(widgetRoot.FindHandler(MenuRootComponent));
34  if (!rootComponent)
35  {
36  rootComponent = new MenuRootComponent;
37  widgetRoot.AddHandler(rootComponent);
38  }
39  return rootComponent;
40  }
41 
42  //------------------------------------------------------------------------------------------------
48  static MenuRootComponent GetRootOf(MenuBase menu)
49  {
50  Widget widgetRoot = menu.GetRootWidget();
51  return GetRootOf(widgetRoot);
52  }
53 
54  //------------------------------------------------------------------------------------------------
59  MenuRootBase GetMenu()
60  {
61  return m_Menu;
62  }
63 
64  //------------------------------------------------------------------------------------------------
69  Widget GetWidget()
70  {
71  return m_Widget;
72  }
73 
74  //------------------------------------------------------------------------------------------------
79  void AddComponent(MenuRootSubComponent component)
80  {
81  if (m_aComponents.Find(component) == -1)
82  m_aComponents.InsertAt(component, 0); //--- Place at the beginning to make sure the newest component is found first
83  }
84 
85  //------------------------------------------------------------------------------------------------
90  void RemoveComponent(MenuRootSubComponent component)
91  {
92  m_aComponents.RemoveItem(component);
93  }
94 
95  //------------------------------------------------------------------------------------------------
101  MenuRootSubComponent FindComponent(typename type, bool showError = false)
102  {
103  foreach (MenuRootSubComponent component: m_aComponents)
104  {
105  if (component && component.Type() == type)
106  return component;
107  }
108 
109  if (showError)
110  Print(string.Format("Cannot find UI component '%1'!", type), LogLevel.ERROR);
111 
112  return null;
113  }
114 
115  //------------------------------------------------------------------------------------------------
122  int FindComponents(typename type, notnull out array<MenuRootSubComponent> outComponents)
123  {
124  outComponents.Clear();
125  foreach (auto component: m_aComponents)
126  {
127  if (component && component.Type() == type)
128  outComponents.Insert(component);
129  }
130  return outComponents.Count();
131  }
132 
133  //------------------------------------------------------------------------------------------------
134  void Init(MenuRootBase menu)
135  {
136  if (m_Menu)
137  return;
138 
139  m_Menu = menu;
140 
141  int count = m_aComponents.Count();
142  foreach (auto component : m_aComponents)
143  {
144  component.HandlerAttachedScripted(component.GetWidget());
145 
146  //--- Show error when the components creates new widgets here, changing the array and breaking indexing
147  if (count != m_aComponents.Count())
148  {
149  Print(string.Format("Error when initializing MenuRootComponent - new components were added during init of %1! The menu will not function correctly!", component.Type()), LogLevel.ERROR);
150  return;
151  }
152  }
153  }
154 
155  //------------------------------------------------------------------------------------------------
156  protected void InitNotRoot()
157  {
158  Init(null);
159  }
160 
161  //------------------------------------------------------------------------------------------------
162  protected void ExecInit(Widget w)
163  {
164  //--- Recursively iterate through children
165  Widget child = w.GetChildren();
166  while (child)
167  {
168  ExecInit(child);
169  child = child.GetSibling();
170  }
171 
172  //--- Iterate through handlers
173  int subComponentsCount = w.GetNumHandlers();
174  for (int i; i < subComponentsCount; i++)
175  {
176  MenuRootSubComponent subComponent = MenuRootSubComponent.Cast(w.GetHandler(i));
177  if (subComponent)
178  subComponent.HandlerAttachedScripted(w);
179  }
180  }
181 
182  //------------------------------------------------------------------------------------------------
183  override void HandlerAttached(Widget w)
184  {
185  m_Widget = w;
186  super.HandlerAttached(w);
187  GetGame().GetCallqueue().CallLater(InitNotRoot, 1, false); //--- Initialize components in case the root is not part of MenuRootBase
188  }
189 };
MenuRootSubComponent
Definition: MenuRootSubComponent.c:5
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
MenuRootBase
Definition: MenuRootBase.c:6
MenuRootComponent
Definition: MenuRootComponent.c:6
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32