using System.Collections; using System.Collections.Generic; using UnityEngine; using Cyber.Util; namespace Cyber.Controls { /// /// Handles displaying and interacting with the inventory. /// public class InventoryInterface : MonoBehaviour { /// /// The camera that is displaying this inventory interface. /// public Camera Camera; /// /// The hologram that acts as the root for the inventory. /// public Hologram Hologram; /// /// The text that contains the item list. /// public TextTextureApplier ItemListText; /// /// How many items can be shown on the screen at the same time. /// public int ItemsPerScreen; private CursorHandler CursorHandler; private bool InventoryOpen = false; private int TestingInventorySize = 20; private int ScrollingIndex = 0; private int SelectedIndex = -1; private void Start() { CursorHandler = GameObject.Find("/Systems/CursorHandler").GetComponent(); RebuildItemList(-1); } private void Update() { if (Input.GetButtonDown("Inventory")) { InventoryOpen = !InventoryOpen; Hologram.Visible = InventoryOpen; CursorHandler.RequestLockState(!InventoryOpen); } RaycastHit LookedAt = CameraUtil.GetLookedAtHit(Camera, 1f, true); if (LookedAt.collider != null) { TextTextureApplier Text = LookedAt.collider.GetComponent(); if (Text != null && Text == ItemListText) { // Interacting with the item list // Calculate the index float ScaledY = (Text.transform.InverseTransformPoint(LookedAt.point).z * 0.1f) + 0.5f; int CurrentIndex = ScrollingIndex + (int)(ScaledY * ItemsPerScreen); // Update inputs if (Input.GetAxis("Mouse ScrollWheel") > 0 && ScrollingIndex > 0) { ScrollingIndex--; } if (Input.GetAxis("Mouse ScrollWheel") < 0 && ScrollingIndex < TestingInventorySize - 1) { ScrollingIndex++; } if (Input.GetButtonDown("Activate")) { SelectedIndex = CurrentIndex; } // Rebuild the list RebuildItemList(CurrentIndex); } } else { // Outside of the inventory, clicking will unselect if (Input.GetButtonDown("Activate")) { SelectedIndex = -1; RebuildItemList(-1); } } } private void RebuildItemList(int focused) { string Inv = ""; for (int i = ScrollingIndex; i < TestingInventorySize; i++) { if (i == focused) { Inv += ""; } if (i == SelectedIndex) { Inv += "·"; } Inv += "Item #" + i + "\n"; if (i == focused) { Inv += ""; } } TextTextureProperties NewProps = ItemListText.TextProperties; NewProps.Text = Inv; ItemListText.SetTextProperties(NewProps); } } }