3 protected const float HOLD_ANIMATION_FAILED_ANIMATION_SPEED = -2.5;
6 protected BaseContainer m_KeyData;
7 protected string m_sButtonText;
8 protected ref array<ref SCR_ButtonTexture> m_aButtonTextures = {};
10 protected ResourceName m_sButtonLayoutConfig;
13 protected ref array<ImageWidget> m_aAdditionalWidgets = {};
15 protected int m_iWidth;
16 protected float m_fMediumButtonWidhtModifier = 1.5;
17 protected float m_fLargeButtonWidhtModifier = 1.78;
18 protected float m_fButtonTextSizeModifier = 3;
20 float m_fAnimationRate;
21 float m_fAnimationTime;
22 protected float m_fMaxHoldtime;
23 protected float m_fDoubleTapTime;
24 protected float m_fHoldIndicatorDefaultPosition[2];
25 protected float m_fHoldIndicatorAnimationPosition[2];
27 protected bool m_bIsHoldAction;
28 protected bool m_bIsHoldActionOnce;
29 protected bool m_bIsDoubleTapAction;
30 protected bool m_bPressedInput;
31 protected bool m_bCanBeToggled;
32 protected bool m_bIsContinuous;
33 protected bool m_bIsToggled;
34 protected bool m_bIsOverwritten;
37 protected WidgetAnimationAlphaMask m_HoldingAnimation;
38 protected WidgetAnimationPosition m_HoldIndicatorAnimation;
40 protected Widget m_wRoot;
41 protected Widget m_wParent;
43 protected OverlayWidget m_wOverlay;
44 protected OverlayWidget m_wShadowOverlay;
45 protected ImageWidget m_wGlow;
46 protected ImageWidget m_wOutlineClear;
47 protected ImageWidget m_wOutline;
48 protected ImageWidget m_wKeyBG;
49 protected ImageWidget m_wDoubleTabIndicator;
50 protected ImageWidget m_wButtonImgGlow;
51 protected ImageWidget m_wButtonImg;
52 protected ImageWidget m_wHoldIndicator;
53 protected RichTextWidget m_wButtonText;
56 override void HandlerAttached(Widget w)
58 super.HandlerAttached(w);
63 m_wShadowOverlay = OverlayWidget.Cast(
m_wRoot.FindAnyWidget(
"Shadow_Overlay"));
64 m_wGlow = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_Glow"));
65 m_wOutlineClear = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_OutlineClear"));
67 m_wKeyBG = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_KeyBG"));
68 m_wDoubleTabIndicator = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_DoubleTabIndicator"));
69 m_wButtonImgGlow = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_ButtonImgGlow"));
70 m_wButtonImg = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_ButtonImg"));
71 m_wHoldIndicator = ImageWidget.Cast(
m_wRoot.FindAnyWidget(
"m_HoldIndicator"));
72 m_wButtonText = RichTextWidget.Cast(
m_wRoot.FindAnyWidget(
"m_ButtonText"));
78 void Init(Widget parent)
85 if (!m_InputButtonComp)
88 m_fAnimationRate = m_InputButtonComp.GetAnimationRate();
89 m_fAnimationTime = m_InputButtonComp.GetAnimationTime();
91 m_InputButtonComp.GetOnAnimateHover().Insert(AnimateHover);
92 m_InputButtonComp.GetOnUpdateEnableColor().Insert(UpdateEnableColor);
93 m_InputButtonComp.GetOnHoldAnimComplete().Insert(AnimateClick);
101 bool SetAction(BaseContainer
data, BaseContainer filter)
105 data.Get(
"m_sText", m_sButtonText);
106 data.Get(
"m_aTextures", m_aButtonTextures);
107 data.Get(
"m_eType", m_eButtonType);
109 GameProject.GetModuleConfig(
"ChimeraGlobalConfig").Get(
"InputButtonLayoutConfig", m_sButtonLayoutConfig);
111 Resource resource = BaseContainerTools.LoadContainer(m_sButtonLayoutConfig);
112 if (!resource || !resource.IsValid())
114 Print(
"Cannot load " + m_sButtonLayoutConfig +
" | " + FilePath.StripPath(__FILE__) +
":" + __LINE__, LogLevel.WARNING);
120 m_ButtonLayout = m_cInput.GetButtonSize(m_eButtonType);
122 m_fHoldIndicatorDefaultPosition = {0, m_InputButtonComp.m_fHoldIndicatorDefaultPosition};
123 m_fHoldIndicatorAnimationPosition = {0, m_InputButtonComp.m_fHoldIndicatorHoldPosition};
125 if (!m_ButtonLayout || m_bIsOverwritten)
129 ApplyTextureToButton();
132 m_wHoldIndicator.SetVisible(m_bIsHoldAction);
133 m_wOutlineClear.SetVisible(m_bIsHoldAction);
134 m_wDoubleTabIndicator.SetVisible(m_bIsDoubleTapAction);
137 GetGame().GetCallqueue().Call(UpdateEnableColor);
145 void OverrideTexture(
string imagePath,
string image =
string.Empty, Color color = Color.White)
147 m_bIsOverwritten =
true;
149 GameProject.GetModuleConfig(
"ChimeraGlobalConfig").Get(
"InputButtonLayoutConfig", m_sButtonLayoutConfig);
151 Resource resource = BaseContainerTools.LoadContainer(m_sButtonLayoutConfig);
152 if (!resource || !resource.IsValid())
154 Print(
"Cannot load " + m_sButtonLayoutConfig +
" | " + FilePath.StripPath(__FILE__) +
":" + __LINE__, LogLevel.WARNING);
161 Print(
"Wrong config type " + m_sButtonLayoutConfig +
" | " + FilePath.StripPath(__FILE__) +
":" + __LINE__, LogLevel.WARNING);
166 m_ButtonLayout = m_cInput.GetButtonSize(m_eButtonType);
171 DeleteAdditionalWidgets();
173 m_wKeyBG.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyBackground);
174 m_wOutline.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyOutline);
175 m_wOutlineClear.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyOutline);
176 m_wGlow.LoadImageFromSet(0, m_ButtonLayout.m_sGlowImageSet, m_ButtonLayout.m_sGlow);
177 m_wHoldIndicator.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sHoldIndicator);
181 m_wKeyBG.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
182 m_wOutline.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
183 m_wOutlineClear.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
184 m_wGlow.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
185 m_wHoldIndicator.SetSize(m_InputButtonComp.m_iHeightInPixel, (m_InputButtonComp.m_iHeightInPixel * 0.5));
187 m_wButtonText.SetVisible(
false);
190 m_wButtonImg.LoadImageFromSet(0, imagePath, image);
192 m_wButtonImg.LoadImageTexture(0, imagePath);
194 m_wButtonImg.SetColor(Color.FromInt(color.PackToInt()));
195 m_wButtonImg.SetVisible(
true);
196 m_wButtonImg.SetSize(m_InputButtonComp.m_iHeightInPixel * 0.5, m_InputButtonComp.m_iHeightInPixel * 0.5);
206 m_wKeyBG.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
207 m_wOutline.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
208 m_wOutlineClear.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
209 m_wGlow.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
210 m_wHoldIndicator.SetSize(m_InputButtonComp.m_iHeightInPixel, (m_InputButtonComp.m_iHeightInPixel * 0.5));
212 if (m_bIsOverwritten)
214 m_wButtonImg.SetSize(m_InputButtonComp.m_iHeightInPixel * 0.5, m_InputButtonComp.m_iHeightInPixel * 0.5);
218 m_wButtonImg.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
219 m_wButtonImgGlow.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
222 m_wButtonText.SetDesiredFontSize((
int)m_InputButtonComp.m_iHeightInPixel / m_fButtonTextSizeModifier);
225 if (m_aAdditionalWidgets && !m_aButtonTextures.IsEmpty())
227 foreach (ImageWidget widget : m_aAdditionalWidgets)
229 widget.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
235 protected void GetFilter(BaseContainer filter)
237 m_bIsHoldAction =
false;
238 m_bIsHoldActionOnce =
false;
239 m_bIsDoubleTapAction =
false;
240 m_bPressedInput =
false;
242 m_bIsContinuous =
false;
247 switch (filter.GetClassName())
249 case "InputFilterHold":
250 m_bIsHoldAction =
true;
252 case "InputFilterHoldOnce":
254 m_bIsHoldActionOnce =
true;
255 m_bIsHoldAction =
true;
258 case "InputFilterDoubleClick":
259 m_bIsDoubleTapAction =
true;
261 case "InputFilterPressed":
262 m_bPressedInput =
true;
264 case "InputFilterToggle":
267 case "InputFilterValue":
268 m_bIsContinuous =
true;
276 protected bool ApplyTextureToButton()
278 m_wKeyBG.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyBackground);
279 m_wOutline.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyOutline);
280 m_wOutlineClear.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sKeyOutline);
281 m_wHoldIndicator.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sHoldIndicator);
286 m_wGlow.LoadImageFromSet(0, m_ButtonLayout.m_sGlowImageSet, m_ButtonLayout.m_sGlowOutline);
287 m_wOutline.LoadMaskFromSet(m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sAlphaMask);
291 m_wGlow.LoadImageFromSet(0, m_ButtonLayout.m_sGlowImageSet, m_ButtonLayout.m_sGlow);
294 m_wDoubleTabIndicator.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_ButtonLayout.m_sDoubleTabText);
298 m_wKeyBG.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
299 m_wOutline.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
300 m_wOutlineClear.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
301 m_wGlow.SetSize(m_iWidth, m_InputButtonComp.m_iHeightInPixel);
302 m_wDoubleTabIndicator.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
303 m_wHoldIndicator.SetSize(m_InputButtonComp.m_iHeightInPixel, (m_InputButtonComp.m_iHeightInPixel * 0.5));
306 if (m_aButtonTextures && !m_aButtonTextures.IsEmpty())
308 m_wButtonImg.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_aButtonTextures[0].m_sTexture);
309 m_wButtonImg.SetColor(m_aButtonTextures[0].
m_Color);
310 m_wButtonImg.SetVisible(
true);
312 if (m_aButtonTextures[0].m_bHasShadow)
314 m_wButtonImgGlow.LoadImageFromSet(0, m_ButtonLayout.m_sGlowImageSet, m_aButtonTextures[0].m_sTexture);
315 m_wButtonImgGlow.SetVisible(
true);
319 m_wButtonImgGlow.SetVisible(
false);
322 m_wButtonImg.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
323 m_wButtonImgGlow.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
325 if (!m_aAdditionalWidgets.IsEmpty())
326 DeleteAdditionalWidgets();
328 if (m_aButtonTextures.Count() > 1)
329 CreateAdditionalWidgets();
333 m_wButtonText.SetVisible(
true);
334 m_wButtonImg.SetVisible(
false);
335 m_wButtonImgGlow.SetVisible(
false);
337 if (!m_aAdditionalWidgets.IsEmpty())
338 DeleteAdditionalWidgets();
341 m_wButtonText.SetText(m_sButtonText);
342 m_wButtonText.SetDesiredFontSize((
int)m_InputButtonComp.m_iHeightInPixel / m_fButtonTextSizeModifier);
344 m_wOutlineClear.SetColor(m_ButtonLayout.m_OutlineColor);
350 protected void GetWidth()
354 switch (m_eButtonType)
357 m_iWidth = m_InputButtonComp.m_iHeightInPixel * m_fMediumButtonWidhtModifier;
360 m_iWidth = m_InputButtonComp.m_iHeightInPixel * m_fLargeButtonWidhtModifier;
364 m_iWidth = m_InputButtonComp.m_iHeightInPixel;
372 protected bool CreateAdditionalWidgets()
374 for (
int i = 1, count = m_aButtonTextures.Count(); i < count; i++)
377 if (m_aButtonTextures[i].m_bHasShadow)
379 Widget w_additionalShadow =
GetGame().GetWorkspace().CreateWidget(WidgetType.ImageWidgetTypeID, WidgetFlags.VISIBLE | WidgetFlags.BLEND | WidgetFlags.STRETCH | WidgetFlags.IGNORE_CURSOR | WidgetFlags.INHERIT_CLIPPING,
new Color(0.000000, 0.000000, 0.000000, 0.502007), 0, m_wShadowOverlay);
380 ImageWidget additionalWidgetshadow = ImageWidget.Cast(w_additionalShadow);
381 additionalWidgetshadow.LoadImageFromSet(0, m_ButtonLayout.m_sGlowImageSet, m_aButtonTextures[i].m_sTexture);
382 additionalWidgetshadow.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
383 m_aAdditionalWidgets.Insert(additionalWidgetshadow);
386 Widget w_additional =
GetGame().GetWorkspace().CreateWidget(WidgetType.ImageWidgetTypeID, WidgetFlags.VISIBLE | WidgetFlags.BLEND | WidgetFlags.STRETCH | WidgetFlags.IGNORE_CURSOR | WidgetFlags.INHERIT_CLIPPING, m_aButtonTextures[i].m_Color, 0,
m_wOverlay);
387 ImageWidget additionalWidget = ImageWidget.Cast(w_additional);
388 additionalWidget.LoadImageFromSet(0, m_ButtonLayout.m_sImageSet, m_aButtonTextures[i].m_sTexture);
389 additionalWidget.SetSize(m_InputButtonComp.m_iHeightInPixel, m_InputButtonComp.m_iHeightInPixel);
390 m_aAdditionalWidgets.Insert(additionalWidget);
397 protected bool DeleteAdditionalWidgets()
399 foreach (ImageWidget img : m_aAdditionalWidgets)
402 img.RemoveFromHierarchy();
405 m_aAdditionalWidgets.Clear();
412 void ActionPressed(
bool isHoldAction =
false)
425 SetToggled(!m_bIsToggled);
429 if (m_bIsDoubleTapAction)
446 void ActionReleased()
453 if (!m_HoldIndicatorAnimation)
454 AnimateWidget.StopAnimation(m_wHoldIndicator, WidgetAnimationPosition);
456 AnimateWidget.Position(m_wHoldIndicator, m_fHoldIndicatorDefaultPosition, (1 / m_InputButtonComp.m_fHoldIndicatorAnimationTime));
459 if (m_HoldingAnimation && m_HoldingAnimation.GetValue() < 1)
462 m_HoldingAnimation.SetSpeed(HOLD_ANIMATION_FAILED_ANIMATION_SPEED);
467 AnimateWidget.Opacity(
m_wOutline, 0, m_fAnimationRate);
474 protected void SetToggled(
bool toggled,
bool animate =
true,
bool invokeChange =
true)
479 m_bIsToggled = toggled;
480 m_InputButtonComp.PlaySoundClicked();
495 WorldTimestamp replicationTime = world.GetServerTimestamp();
497 if (!m_InputButtonComp.m_bIsDoubleTapStated)
500 m_fDoubleTapTime = replicationTime.DiffMilliseconds(replicationTime);
501 !m_InputButtonComp.m_bIsDoubleTapStated =
true;
505 if (replicationTime.DiffMilliseconds(replicationTime) - m_fDoubleTapTime <= m_InputButtonComp.m_iDoubleTapThreshold)
514 !m_InputButtonComp.m_bIsDoubleTapStated =
false;
519 ImageWidget GetOutlineWidget()
525 ImageWidget GetBackgroundWidget()
532 protected void AnimateHover()
534 if (AnimateWidget.IsAnimating(m_wKeyBG))
537 if (m_InputButtonComp.m_bIsHovered)
539 AnimateWidget.Color(m_wKeyBG, m_InputButtonComp.m_ActionHovered, m_fAnimationRate);
552 protected void AnimateClick()
557 if (!m_bIsDoubleTapAction && !m_bPressedInput)
559 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
560 AnimateWidget.Color(m_wKeyBG, Color.FromInt(Color.WHITE), m_fAnimationRate);
563 GetGame().GetCallqueue().CallLater(ResetColor, m_fAnimationTime * 600 + 1,
false);
572 if (m_bIsHoldActionOnce)
577 m_HoldIndicatorAnimation = AnimateWidget.Position(m_wHoldIndicator, m_fHoldIndicatorAnimationPosition, (1 / m_InputButtonComp.m_fHoldIndicatorAnimationTime));
581 void AnimateHoldComplete()
583 if (!m_HoldIndicatorAnimation)
584 AnimateWidget.StopAnimation(m_wHoldIndicator, WidgetAnimationPosition);
586 AnimateWidget.Position(m_wHoldIndicator, m_fHoldIndicatorDefaultPosition, (1 / m_InputButtonComp.m_fHoldIndicatorAnimationTime));
588 m_InputButtonComp.GetOnHoldAnimComplete().Invoke(
this);
592 protected void AnimateDoubleTab()
594 if (!m_InputButtonComp.m_bIsDoubleTapStated)
596 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
598 m_wKeyBG.SetColor(m_InputButtonComp.m_ActionHovered);
600 GetGame().GetCallqueue().CallLater(ResetColor, m_fAnimationTime * 300 + 1,
false);
604 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
605 m_wKeyBG.SetColor(Color.FromInt(Color.WHITE));
607 GetGame().GetCallqueue().CallLater(ResetColor, m_fAnimationTime * 300 + 1,
false);
612 protected void AnimateToggle()
614 AnimateWidget.Color(m_wKeyBG, m_InputButtonComp.m_ActionToggled, m_fAnimationRate);
618 protected void AnimateContinous()
620 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
621 AnimateWidget.Color(m_wKeyBG, Color.FromInt(Color.WHITE), m_fAnimationRate);
628 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
630 if (!m_wParent.IsEnabled())
636 if (m_InputButtonComp.m_bIsHovered)
639 AnimateWidget.Color(m_wKeyBG, m_InputButtonComp.m_ActionDefault, m_fAnimationRate);
643 GetGame().GetCallqueue().Remove(ResetColor);
648 protected void UpdateEnableColor()
651 if (!m_wParent.IsEnabled() && m_InputButtonComp.m_bCanBeDisabled)
653 color = Color.FromInt(m_InputButtonComp.m_ActionDisabled.PackToInt());
654 if (m_InputButtonComp.m_bChangeLabelColorOnDisabled)
655 m_InputButtonComp.SetLabelColor(color);
659 color = m_InputButtonComp.m_ActionDefault;
660 m_InputButtonComp.SetLabelColor(Color.FromInt(m_InputButtonComp.m_LabelDefault.PackToInt()));
663 AnimateWidget.StopAnimation(m_wKeyBG, WidgetAnimationColor);
664 m_wKeyBG.SetColor(color);
666 if (m_bIsDoubleTapAction)
668 if (m_wParent.IsEnabled())
669 m_wDoubleTabIndicator.SetColor(Color.FromInt(Color.WHITE));
670 else if (m_InputButtonComp.m_bCanBeDisabled)
671 m_wDoubleTabIndicator.SetColor(Color.FromInt(m_InputButtonComp.m_ActionDisabled.PackToInt()));
676 float GetTextSizeModifier()
678 return m_fButtonTextSizeModifier;
682 bool GetIsOverwritten()
684 return m_bIsOverwritten;
688 void SetIsOverwritten(
bool setOverride)
690 m_bIsOverwritten = setOverride;