using Beyond; using DanielLochner.Assets.SimpleScrollSnap; using Invector; using Invector.vCharacterController; using Invector.vItemManager; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class ButtonItemSwapper : MonoBehaviour { private const float scrollLockDurationOnSwap = 1f; [SerializeField] protected GameObject m_panel, m_toggle; private float m_toggleWidth; [SerializeField] private SimpleScrollSnap scrollSnap; [SerializeField] private ScrollRect scrollRect; [SerializeField] private CanvasGroup canvasGroup; [SerializeField] private IButtonItemBindController buttonItemBindController; [SerializeField] private bool instantiatePanels = true; // Start is called before the first frame update private void Start() { buttonItemBindController = GetComponent(); InitializeSwapper(); } private void OnEnable() { if (!scrollRect.enabled) { ItemPanelController imageSetter = buttonItemBindController.GetCurrentItemPanel(); imageSetter.EnableButtonInteraction(); scrollRect.enabled = true; } } private void InitializeSwapper() { int equipSlotsCount = buttonItemBindController.GetEquipSlotsCount(); for (int i = 0; i < equipSlotsCount; i++) { AddNewItemPanel(); } if (m_toggle) { m_toggleWidth = m_toggle.GetComponent().sizeDelta.x * (Screen.width / 2048f); } scrollSnap.onPanelChanged.AddListener(SwapItem); } public void AddNewItemPanel() { AddItemPanel(scrollSnap.NumberOfPanels); } private void AddItemPanel(int index) { //Pagination if (instantiatePanels) { Instantiate(m_toggle, scrollSnap.pagination.transform.position + new Vector3(m_toggleWidth * (scrollSnap.NumberOfPanels + 1), 0, 0), Quaternion.identity, scrollSnap.pagination.transform); scrollSnap.pagination.transform.position -= new Vector3(m_toggleWidth / 2f, 0, 0); //Panel scrollSnap.Add(m_panel, index); } buttonItemBindController.BindPanelToItemSlot(index); } private void SwapItem() { buttonItemBindController.SetEquipSlot(); StartCoroutine(BrieflyLockScrollCoroutine()); } public void SwapWeaponWithCheck() { if (scrollRect.enabled) { SwapItem(); } } private IEnumerator BrieflyLockScrollCoroutine() { ItemPanelController imageSetter = buttonItemBindController.GetCurrentItemPanel(); imageSetter.DisableButtonInteraction(); scrollRect.enabled = false; yield return new WaitForSeconds(scrollLockDurationOnSwap); imageSetter.EnableButtonInteraction(); scrollRect.enabled = true; } public void HideSwapper() { canvasGroup.blocksRaycasts = false; canvasGroup.alpha = 0; } public void ShowSwapper() { canvasGroup.blocksRaycasts = true; canvasGroup.alpha = 1; } private void OnDestroy() { RemoveListeners(); } private void RemoveListeners() { if (scrollSnap) { scrollSnap.onPanelChanged.RemoveListener(SwapItem); } } }