Firstborn/Assets/RPG Creation Kit/Scripts/Looting System/LootingInventoryUI.cs

269 lines
8.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using RPGCreationKit;
using RPGCreationKit.Player;
using UnityEngine.EventSystems;
using RPGCreationKit.AI;
namespace RPGCreationKit
{
public class LootingInventoryUI : MonoBehaviour
{
public static LootingInventoryUI instance;
private void Awake()
{
if (instance == null)
instance = this;
else
{
Debug.Log("Anomaly detected with the singleton pattern of 'LootingInventoryUI', do you have more than one LootingInventoryUI?");
Destroy(this);
}
}
public Inventory inventoryToDisplay;
public TMPro.TextMeshProUGUI lootingDisplayName;
public LootingPoint curLootingPoint;
public bool isDrawing = true;
public ItemsInListPoolManager itemsPoolManager;
public InventoryTabs selectedTab;
[Header("pickpocket add-on")]
public bool curIsPickPocket;
public RckAI curAI;
[Header("UI Elements")]
public GameObject UI;
public Transform scrollView;
public Toggle defaultToggle;
public DropItemsPanel takeDepositItemsPanel;
public TextMeshProUGUI currentWeightText;
public TextMeshProUGUI maxWeightText;
[HideInInspector] public bool isOpen = false;
[SerializeField] private Transform inventoryItemsContent;
[SerializeField] private Button takeAllButton;
private GameObject grabbed;
public void OpenLootingPanel(LootingPoint _lootingPoint, bool _referenceLootInventory = true, bool _isPickPocket = false, RckAI _AI = null)
{
curLootingPoint = _lootingPoint;
curIsPickPocket = _isPickPocket;
curAI = curAI;
if (_referenceLootInventory)
{
isDrawing = true;
inventoryToDisplay = curLootingPoint.inventory;
} else
{
isDrawing = false;
}
itemsPoolManager.inventoryToPool = inventoryToDisplay;
lootingDisplayName.text = curLootingPoint.pointName;
OpenLootUI();
}
private void OpenLootUI()
{
RckPlayer.instance.EnableDisableControls(false);
// Open
isOpen = true;
itemsPoolManager.PopulateAll();
UpdateStatsUI();
ShowTabItems();
UI.SetActive(true);
RckPlayer.instance.input.SwitchCurrentActionMap("LootingUI");
// Close
// isOpen = false;
// itemsPoolManager.ResetPools();
}
public void SwitchInventory(bool _isPlayerInventory)
{
itemsPoolManager.ResetPools();
isDrawing = !_isPlayerInventory;
if (_isPlayerInventory)
{
inventoryToDisplay = Inventory.PlayerInventory;
itemsPoolManager.inventoryToPool = inventoryToDisplay;
takeAllButton.gameObject.SetActive(false);
OpenLootingPanel(curLootingPoint, false);
}
else
{
inventoryToDisplay = curLootingPoint.inventory;
itemsPoolManager.inventoryToPool = inventoryToDisplay;
takeAllButton.gameObject.SetActive(true);
OpenLootingPanel(curLootingPoint, true);
}
}
public void ChangeTab(InventoryTabs tab)
{
selectedTab = tab;
/// Show Tab
ShowTabItems();
}
public void ShowTabItems()
{
switch (selectedTab)
{
case InventoryTabs.All:
itemsPoolManager.ShowAll();
break;
case InventoryTabs.Armors:
itemsPoolManager.ArmorsTab();
break;
case InventoryTabs.Keys:
itemsPoolManager.KeysTab();
break;
case InventoryTabs.MiscItems:
itemsPoolManager.MiscTab();
break;
case InventoryTabs.Weapons:
itemsPoolManager.WeaponsTab();
break;
}
SelectFirstButton();
}
public void UpdateStatsUI()
{
currentWeightText.text = ((int)Inventory.PlayerInventory.CurTotalWeight).ToString();
}
public void TakeAll()
{
// Click on all
for(int i = itemsPoolManager.usedObjects.Count-1; i >= 0; i--)
itemsPoolManager.usedObjects[i].OnClick(true);
CloseUI();
}
public void CloseUI()
{
curLootingPoint.SaveOnFile();
SwitchInventory(false);
RckPlayer.instance.isLooting = false;
itemsPoolManager.ResetPools();
isOpen = false;
defaultToggle.SetIsOnWithoutNotify(true);
UI.SetActive(false);
RckPlayer.instance.EnableDisableControls(true);
if (curLootingPoint.closeSound)
GameAudioManager.instance.PlayOneShot(AudioSources.GeneralSounds, curLootingPoint.closeSound);
takeDepositItemsPanel.CancelButton();
RckPlayer.instance.input.SwitchCurrentActionMap("Player");
if (curLootingPoint.anim != null) {
curLootingPoint.OpenCloseContainer();
}
curLootingPoint = null;
}
public void SelectFirstButton()
{
if (RckPlayer.instance.input.currentControlScheme != "Gamepad")
return;
StartCoroutine(SelectFirstButtonC());
}
private IEnumerator SelectFirstButtonC()
{
yield return new WaitForEndOfFrame();
EventSystem.current.SetSelectedGameObject(null);
var a = inventoryItemsContent.GetComponentInChildren<Button>();
if (a != null)
EventSystem.current.SetSelectedGameObject(a.gameObject, null);
}
public void Move(MoveDirection direction)
{
AxisEventData data = new AxisEventData(EventSystem.current);
data.moveDir = direction;
data.selectedObject = EventSystem.current.currentSelectedGameObject;
ExecuteEvents.Execute(data.selectedObject, data, ExecuteEvents.moveHandler);
}
public void MoveSafe(MoveDirection direction)
{
AxisEventData data = new AxisEventData(EventSystem.current);
data.moveDir = direction;
data.selectedObject = EventSystem.current.currentSelectedGameObject;
ExecuteEvents.Execute(data.selectedObject, data, ExecuteEvents.moveHandler);
grabbed = EventSystem.current.currentSelectedGameObject;
// Revert
if (data.moveDir == MoveDirection.Up)
data.moveDir = MoveDirection.Down;
else if (data.moveDir == MoveDirection.Down)
data.moveDir = MoveDirection.Up;
data.selectedObject = EventSystem.current.currentSelectedGameObject;
ExecuteEvents.Execute(data.selectedObject, data, ExecuteEvents.moveHandler);
Debug.Log(grabbed);
StartCoroutine(SelectNextButtonC(grabbed));
}
public void SelectNextButton(bool calledFromDropPanel = false)
{
if (RckPlayer.instance.input.currentControlScheme != "Gamepad")
return;
if (UINavigationHelper.CanNavigateDown())
MoveSafe(MoveDirection.Down);
else if (UINavigationHelper.CanNavigateUp())
MoveSafe(MoveDirection.Up);
else
StartCoroutine(SelectFirstButtonC());
}
private IEnumerator SelectNextButtonC(GameObject go)
{
yield return new WaitForEndOfFrame();
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(go);
}
}
}