Skip to content
Snippets Groups Projects
Select Git revision
  • fd60dbe8d128b506307878336e502cb0d7685aba
  • master default
  • JS-based-scroll-rendering
  • Paul_Marius_Level
  • Paul_Marius_2
  • Paul_Marius
  • Andi_Mark
  • be-UnityWebView
  • gitignoreFrameitServer
  • ZimmerBSc
  • Bugfix_StageLoading
  • stages
  • MAZIFAU_Experimental
  • tsc/coneworld
  • tsc/fact-interaction
  • marcel
  • MaZiFAU_TopSort
  • mergeHelper
  • zwischenSpeichern
  • tempAndrToMaster
  • SebBranch
  • 3.0
  • v2.1
  • v2.0
  • v1.0
25 results

GadgetBehaviour.cs

Blame
  • Stefan Richter's avatar
    Stefan Richter authored
    Toggle function for Gadgetscrolldirection. NPC Talkingbubble lets cursor interact with the world behind itself when not on Touchmode.
    fd60dbe8
    History
    GadgetBehaviour.cs 6.07 KiB
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using TMPro;
    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.UI;
    using static UIconfig;
    
    
    public class GadgetBehaviour : MonoBehaviour, ISerializationCallbackReceiver
    {
        // could be worked around of. But as long as not needed: this is better
        public static GadgetBehaviour Singelton { get; private set; }
    
        /// <summary>Which cursor to use</summary>
        /// <remarks>When not set in Inspector, will be searching for any <see cref="WorldCursor"/>.</remarks>
        public static WorldCursor Cursor;
        public static LineRenderer LineRenderer;
        public static GameObject GadgetButton;
        public static GameObject GadgetName;
        public static GameObject ParentMe;
        public static Material[] Materials;
        public static Sprite[] ButtonSprites;
        //public static String[] ButtonNames;
    
        public static Dictionary<Gadget.GadgetIDs, DataContainerGadgetInit> DataContainerGadgetDict;
    
        public static float ActiveGadgetScaleFactor = 1.5f;
    
        public static Gadget ActiveGadget { get => gadgets[ActiveGadgetInd]; }
        public static int ActiveGadgetInd;
        public static Gadget[] gadgets;
        public static Button[] buttons;
    
        private static UnityAction<RaycastHit[]> OnHit;
        private static RectTransform updateRect;
    
        #region Unity Serialization
        [SerializeField] private float _ActiveGadgetScaleFactor;
    
        [SerializeField] private GameObject _GadgetButton;
        [SerializeReference] private GameObject _GadgetName;
        [SerializeField] private GameObject _ParentMe;
        [SerializeField] private WorldCursor _Cursor;
        [SerializeField] private LineRenderer _LineRenderer;
    
        [SerializeField] private DataContainerGadgetCollection _DataContainerGadgetDict;
    
        public void OnBeforeSerialize() { }
    
        public void OnAfterDeserialize()
        {
            Cursor = _Cursor;
            LineRenderer = _LineRenderer;
            GadgetButton = _GadgetButton;
            GadgetName = _GadgetName;
            ParentMe = _ParentMe;
    
    #pragma warning disable UNT0008 // Null propagation on Unity objects
            Materials = _DataContainerGadgetDict?.Materials ?? new Material[0];
            ButtonSprites = _DataContainerGadgetDict?.ButtonSprites ?? new Sprite[0];
            //ButtonNames = _DataContainerGadgetDict?.GadgetNames ?? new string[0];
            DataContainerGadgetDict = _DataContainerGadgetDict?.DataContainerGadgetDict ?? new();
    #pragma warning restore UNT0008 // Null propagation on Unity objects
        }
        #endregion
    
        private void OnDestroy()
        {
            Singelton = null;
            CommunicationEvents.TriggerEvent.RemoveListener(OnHitCallback);
        }
    
        private void Awake()
        {
            Singelton ??= this;
            if (this != Singelton)
            {
                Debug.LogError("Only one Instance of GadgetManager allowed!");
                return;
            }
    
            gameObject.DestroyAllChildren();
    
            if (Cursor == null)
                Cursor = GameObject.FindObjectOfType<WorldCursor>();
            if (LineRenderer == null)
                LineRenderer = Cursor.GetComponent<LineRenderer>();
            if (ParentMe == null)
                ParentMe = this.gameObject;
    
            updateRect = this.gameObject.transform.parent as RectTransform;
    
            CommunicationEvents.TriggerEvent.AddListener(OnHitCallback);
    
            gadgets = (
                StageStatic.stage.AllowedGadgets == null
                || StageStatic.stage.AllowedGadgets.Count == 0
                ? Gadget.GadgetTypes
                    .Where(t => t != typeof(Gadget.UndefinedGadget))
                    .Select(t => (Gadget)Activator.CreateInstance(t))
                : StageStatic.stage.AllowedGadgets
                ).OrderBy(g => g.Rank).ToArray();
        }
    
        void Start()
        {
            void CreateButton(int gid)
            {
                GameObject button = GameObject.Instantiate(GadgetButton, parent: ParentMe.transform);
                button.GetComponent<Image>().sprite = ButtonSprites[gadgets[gid].ButtonIndx];
    
                (buttons[gid] = button.GetComponent<Button>())
                    .onClick.AddListener(() => ActivateGadget(gid));
            }
    
            buttons = new Button[gadgets.Length];
    
            for (int i = 0; i < gadgets.Length; i++)
            {
                gadgets[i].Awake();
                //gadgets[i].Start();
                CreateButton(i);
            }
    
            ActiveGadgetInd = 0;
            buttons[0].transform.localScale *= ActiveGadgetScaleFactor;
            GadgetName = _GadgetName;
    
            ActivateGadget(0);
        }
    
        void Update()
        {
            if (this != Singelton) return;
    
            if (!ParentMe.activeInHierarchy)
                return;
    
            int offset = 0;
    
            if (Input.GetButtonDown("ToolMode"))
            {
                offset = 1;
            }
            else if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                offset = (int)Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")) 
                    * UIconfig.scrolldirection; //Invert Offset if toggled in Optionsmenue->Controls.
            }
    
            if (offset != 0)
                ActivateGadget(ActiveGadgetInd + offset);
    
            gadgets[ActiveGadgetInd].Update();
        }
    
        void OnDisable()
        {
            if (this != Singelton) return;
    
            gadgets[ActiveGadgetInd].Disable();
        }
    
        void OnEnable()
        {
            if (this != Singelton) return;
    
            gadgets[ActiveGadgetInd].Enable();
        }
    
        public static void ActivateGadget(int gid)
        {
            ParentMe.SetActive(true);
            gid += gid >= gadgets.Length
                ? -gadgets.Length
                : gid < 0 ? +gadgets.Length : 0;
    
            //buttons[activeGadget].animator.StopPlayback();
            buttons[ActiveGadgetInd].transform.localScale /= ActiveGadgetScaleFactor;
            gadgets[ActiveGadgetInd].Disable();
    
    
            ActiveGadgetInd = gid;
            //buttons[gid].animator.StartPlayback();
            buttons[gid].transform.localScale *= ActiveGadgetScaleFactor;
            GadgetName.GetComponent<TMP_Text>().text = gadgets[gid].UiName;
            gadgets[gid].Enable();
    
            OnHit = gadgets[gid].Hit;
    
            CommunicationEvents.ToolModeChangedEvent.Invoke(gid);
            LayoutRebuilder.ForceRebuildLayoutImmediate(updateRect);
        }
    
        public static void OnHitCallback(RaycastHit[] hit) => OnHit.Invoke(hit);
    }