3 protected static const ref Color COLOR_HINT_SELECTED =
UIColors.CONTRAST_COLOR;
4 protected static const ref Color COLOR_HINT_DESELECTED =
UIColors.WHITE_HOVERED;
8 protected TextWidget m_wText;
9 protected Widget m_wContent;
10 protected Widget m_wCountBar;
11 protected ref array<Widget> m_aHintElements =
new array<Widget>();
13 [
Attribute(
"false", UIWidgets.CheckBox,
"use light grey arrows instead of big yellow ones")]
14 protected bool m_bUseLightArrows;
16 [
Attribute(
"false", UIWidgets.CheckBox,
"On last item and pressing right arrow, it will go to the start of the list")]
17 protected bool m_bCycleMode;
19 [
Attribute(
"true", UIWidgets.CheckBox,
"Show bar of available elements and which one is selected.")]
20 protected bool m_bShowHints;
22 [
Attribute(
"24", UIWidgets.EditBox,
"Width of a hint element")]
23 protected float m_fHintElementWidth;
25 [
Attribute(
"4", UIWidgets.EditBox,
"Height of a hint element")]
26 protected float m_fHintElementHeight;
28 [
Attribute(
"1", UIWidgets.EditBox,
"How much wider should be the selected hint element")]
29 protected float m_fHintSelectedWidthMultiplier;
31 [
Attribute(
"true", UIWidgets.CheckBox,
"Should hints fill the available space")]
32 protected bool m_fHintFillMode;
34 [
Attribute(
"4", UIWidgets.EditBox,
"How large gaps between hints should there be")]
35 protected float m_fHintSpacing;
38 protected ResourceName m_sHintElementTexture;
41 protected ResourceName m_sHintElementImage;
43 [
Attribute(
"HintBarElement", UIWidgets.EditBox,
"Name for generated Hint bar widgets")]
44 protected string m_sHintBarElementName;
46 protected ref ScriptInvoker m_OnLeftArrowClick;
47 protected ref ScriptInvoker m_OnRightArrowClick;
49 protected bool m_bHasActionListeners;
50 protected bool m_bAllowSwitchingWithoutFocus;
53 override void HandlerAttached(Widget w)
55 super.HandlerAttached(w);
56 m_wText = TextWidget.Cast(w.FindAnyWidget(
"SelectionText"));
58 m_wCountBar = w.FindAnyWidget(
"HintBar");
61 Widget left = w.FindAnyWidget(
"ButtonLeft");
62 Widget right = w.FindAnyWidget(
"ButtonRight");
68 m_ButtonLeft.m_OnActivated.Insert(OnLeftArrowClick);
75 m_ButtonRight.m_OnActivated.Insert(OnRightArrowClick);
80 if (m_aElementNames && m_iSelectedItem > -1 && m_iSelectedItem < m_aElementNames.Count())
81 m_wText.SetText(m_aElementNames[m_iSelectedItem]);
83 m_wText.SetText(
string.Empty);
86 SetInitialState(
false);
90 override bool OnFocus(Widget w,
int x,
int y)
92 super.OnFocus(w, x, y);
96 UpdateHintBar(m_iSelectedItem, -1);
101 override bool OnFocusLost(Widget w,
int x,
int y)
103 super.OnFocusLost(w, x, y);
105 if (!m_bAllowSwitchingWithoutFocus)
106 RemoveActionListeners();
108 UpdateHintBar(m_iSelectedItem, -1);
114 override int AddItem(
string item,
bool last =
false, Managed
data =
null)
116 int i = super.AddItem(item, last,
data);
118 SetInitialState(last);
123 override void RemoveItem(
int item,
bool last =
false)
125 super.RemoveItem(item, last);
126 SetInitialState(last);
130 override void ClearAll()
133 SetInitialState(
true);
137 override bool SetCurrentItem(
int i,
bool playSound =
false,
bool animate =
false)
139 return SetCurrentItem_Internal(i, playSound, animate,
true);
144 protected bool SetCurrentItem_Internal(
int i,
bool playSound,
bool animate,
bool invokeOnChanged)
146 int lastIndex = m_iSelectedItem;
147 if (!super.SetCurrentItem(i, playSound, animate))
151 m_wText.SetText(m_aElementNames[i]);
154 UpdateHintBar(i, lastIndex);
156 EnableArrows(i, animate);
165 protected void CreateHintBar()
168 foreach (Widget w : m_aHintElements)
170 w.RemoveFromHierarchy();
172 m_aHintElements.Clear();
174 m_wCountBar.SetVisible(
true);
176 for (
int i = 0, len = m_aElementNames.Count(); i < len; i++)
178 Widget w =
GetGame().GetWorkspace().CreateWidget(WidgetType.ImageWidgetTypeID, WidgetFlags.VISIBLE | WidgetFlags.STRETCH | WidgetFlags.BLEND | WidgetFlags.INHERIT_CLIPPING, Color.FromInt(Color.WHITE), 0, m_wCountBar);
179 ImageWidget img = ImageWidget.Cast(w);
183 SetTexture(img, m_sHintElementTexture, m_sHintElementImage);
185 if (m_fHintElementWidth >= 0 && m_fHintElementHeight >= 0)
186 img.SetSize(m_fHintElementWidth, m_fHintElementHeight);
188 img.SetColor(COLOR_HINT_DESELECTED);
189 HorizontalLayoutSlot.SetPadding(img, m_fHintSpacing * 0.5, 0, m_fHintSpacing * 0.5, 0);
190 HorizontalLayoutSlot.SetHorizontalAlign(img, LayoutHorizontalAlign.Stretch);
194 HorizontalLayoutSlot.SetSizeMode(img, LayoutSizeMode.Fill);
196 m_aHintElements.Insert(img);
197 img.SetName(m_sHintBarElementName + i);
200 UpdateHintBar(m_iSelectedItem, -1);
204 protected void UpdateHintBar(
int currentIndex,
int oldIndex)
209 bool focused =
GetGame().GetWorkspace().GetFocusedWidget() ==
m_wRoot;
211 int count = m_aHintElements.Count();
212 if (oldIndex > -1 && oldIndex < count)
214 AnimateWidget.Color(m_aHintElements[oldIndex], COLOR_HINT_DESELECTED, m_fAnimationRate);
215 if (m_fHintSelectedWidthMultiplier != 1)
216 AnimateWidget.LayoutFill(m_aHintElements[oldIndex], 1, m_fAnimationRate);
219 if (currentIndex > -1 && currentIndex < count)
221 AnimateWidget.Color(m_aHintElements[currentIndex], COLOR_HINT_SELECTED, m_fAnimationRate);
222 if (m_fHintSelectedWidthMultiplier != 1)
223 AnimateWidget.LayoutFill(m_aHintElements[currentIndex], m_fHintSelectedWidthMultiplier, m_fAnimationRate);
228 protected void SetInitialState(
bool invokeOnChanged =
true)
230 int realIndex = m_iSelectedItem;
231 if (realIndex < 0 || realIndex >= m_aElementNames.Count())
234 m_iSelectedItem =
int.MIN;
235 SetCurrentItem_Internal(realIndex,
false,
false, invokeOnChanged);
237 if (m_bShowHints && m_aElementNames && m_wCountBar)
243 protected void EnableArrows(
int selected,
bool animate)
245 if (!m_ButtonLeft || !m_ButtonRight)
250 bool enabled = m_aElementNames.Count() > 1;
251 m_ButtonLeft.SetEnabled(enabled, animate);
252 m_ButtonRight.SetEnabled(enabled, animate);
256 m_ButtonLeft.SetEnabled(selected != 0, animate);
257 m_ButtonRight.SetEnabled(selected != (m_aElementNames.Count() - 1), animate);
262 protected void OnLeftArrowClick()
266 if (m_iSelectedItem <= 0)
269 SetCurrentItem(m_aElementNames.Count() - 1,
true,
true);
273 SetCurrentItem(m_iSelectedItem - 1,
true,
true);
276 if (m_OnLeftArrowClick)
277 m_OnLeftArrowClick.Invoke();
281 protected void OnRightArrowClick()
285 if (m_iSelectedItem >= (m_aElementNames.Count() - 1))
288 SetCurrentItem(0,
true,
true);
292 SetCurrentItem(m_iSelectedItem + 1,
true,
true);
295 if (m_OnRightArrowClick)
296 m_OnRightArrowClick.Invoke();
300 protected void OnMenuLeft()
302 if (
GetGame().GetWorkspace().GetFocusedWidget() !=
m_wRoot && !m_bAllowSwitchingWithoutFocus)
305 if (m_ButtonLeft && m_ButtonLeft.IsEnabled())
306 m_ButtonLeft.OnClick(m_ButtonLeft.m_wRoot, 0, 0, 0);
310 protected void OnMenuRight()
312 if (
GetGame().GetWorkspace().GetFocusedWidget() !=
m_wRoot && !m_bAllowSwitchingWithoutFocus)
315 if (m_ButtonRight && m_ButtonRight.IsEnabled())
316 m_ButtonRight.OnClick(m_ButtonRight.m_wRoot, 0, 0, 0);
321 void AddActionListeners()
326 GetGame().GetInputManager().AddActionListener(
"MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
327 GetGame().GetInputManager().AddActionListener(
"MenuRight", EActionTrigger.DOWN, OnMenuRight);
333 void RemoveActionListeners()
338 GetGame().GetInputManager().RemoveActionListener(
"MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
339 GetGame().GetInputManager().RemoveActionListener(
"MenuRight", EActionTrigger.DOWN, OnMenuRight);
346 void SetKeepActionListeners(
bool keep)
348 m_bAllowSwitchingWithoutFocus = keep;
352 void SetCycleMode(
bool cycle)
355 EnableArrows(m_iSelectedItem,
false);
359 ScriptInvoker GetOnLeftArrowClick()
361 if (!m_OnLeftArrowClick)
362 m_OnLeftArrowClick =
new ScriptInvoker();
364 return m_OnLeftArrowClick;
368 ScriptInvoker GetOnRightArrowClick()
370 if (!m_OnRightArrowClick)
371 m_OnRightArrowClick =
new ScriptInvoker();
373 return m_OnRightArrowClick;
379 static SCR_SpinBoxComponent GetSpinBoxComponent(
string name, Widget parent,
bool searchAllChildren =
true)