// LOGIC

        public void Initialize(Button i_Button)
        {
            m_Button = i_Button;

            if (m_Button != null)
            {
                EventTrigger eventTrigger = m_Button.GetComponent <EventTrigger>();
                if (eventTrigger == null)
                {
                    eventTrigger = m_Button.gameObject.AddComponent <EventTrigger>();
                }

                EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
                pointerDownEntry.eventID = EventTriggerType.PointerDown;
                pointerDownEntry.callback.AddListener(OnButtonDown);

                eventTrigger.triggers.Add(pointerDownEntry);

                EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
                pointerUpEntry.eventID = EventTriggerType.PointerUp;
                pointerUpEntry.callback.AddListener(OnButtonUp);

                eventTrigger.triggers.Add(pointerUpEntry);
            }

            if (WiFiInputController.controllerDataDictionary != null)
            {
                string controllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_BUTTON, controlName);
                m_Controller = (ButtonControllerType)WiFiInputController.controllerDataDictionary[controllerKey];
            }
        }
        public void Clear()
        {
            m_Controller = null;

            if (m_Button != null)
            {
                EventTrigger eventTrigger = m_Button.GetComponent <EventTrigger>();
                if (eventTrigger != null)
                {
                    eventTrigger.triggers.Clear();
                }
            }

            m_Button = null;
        }
        public void Clear()
        {
            m_YController = null;
            m_XController = null;

            m_XPositiveButtonController = null;
            m_XNegativeButtonController = null;
            m_YPositiveButtonController = null;
            m_YNegativeButtonController = null;

            if (m_BackPanel != null && m_BackImage != null && m_Nub != null)
            {
                EventTrigger eventTrigger = m_BackPanel.GetComponent <EventTrigger>();
                if (eventTrigger != null)
                {
                    eventTrigger.triggers.Clear();
                }
            }

            m_Nub       = null;
            m_BackImage = null;
            m_BackPanel = null;
        }
Пример #4
0
        //this method is responsible for registering a control which will instance a specific controller type
        //add it to the data structure and set its key and return it
        //There are two ways this method is called, one on the client the UI script in its startup registers it client side
        //The second way is the server when a client sends over its inventory
        public static string registerControl(string type, string name, string IP = "0")
        {
            if (controllerDataDictionary == null)
            {
                return("");
            }

            BaseControllerType controller;
            string             key = "";

            switch (type)
            {
            case WiFiInputConstants.CONTROLLERTYPE_AXIS:
                controller = new AxisControllerType();
                controller.controllerType = WiFiInputConstants.CONTROLLERTYPE_AXIS;
                break;

            case WiFiInputConstants.CONTROLLERTYPE_BUTTON:
                controller = new ButtonControllerType();
                controller.controllerType = WiFiInputConstants.CONTROLLERTYPE_BUTTON;
                break;

            default:
                //should not occur
                controller = null;
                Debug.Log("Error: a controller type that isn't defined was registered");
                break;
            }


            //on the client we care what both the client and server key is but only client is known at registration time
            //the server key is discovered when the server responds to the inventory and will be populated then
            controller.clientKey = name;
            key = controller.clientKey;

            if (!controllerDataDictionary.ContainsKey(key))
            {
                //brand new first time here
                controllerDataDictionary.Add(key, controller);
                controllerDataDictionaryKeys.Add(key);
            }
            else if (controllerDataDictionary[key].logicalPlayerNumber == WiFiInputConstants.PLAYERNUMBER_DISCONNECTED)
            {
                //already here just reassign (original disconnected and new one is here)
                //we will only change the player number if it's been occupied in between
                controllerDataDictionary[key].lastReceivedPacketTime = DateTime.UtcNow;
                controllerDataDictionary[key].justReconnected        = true;
                if (!WiFiInputUtilities.isPlayerNumberOccupied(controllerDataDictionary[key].previousConnectionPlayerNumber, controllerDataDictionary[key].clientKey))
                {
                    //not occupied assign it's previous number
                    controllerDataDictionary[key].logicalPlayerNumber = controllerDataDictionary[key].previousConnectionPlayerNumber;
                }
                else
                {
                    //occupied give it the lowest available number
                    controllerDataDictionary[key].logicalPlayerNumber = controller.logicalPlayerNumber;
                }
            }

            return(key);
        }
        // LOGIC

        public void Initialize(RectTransform i_BackPanel, Image i_BackImage, Image i_Nub, float i_LowThreshold = 0.1f, float i_HighThreshold = 0.6f, float i_XButtonThreshold = 0.5f, float i_YButtonThreshold = 0.5f)
        {
            m_LowThreshold  = Mathf.Clamp01(i_LowThreshold);
            m_HighThreshold = Mathf.Clamp01(i_HighThreshold);

            m_XAxisThreshold = Mathf.Clamp01(i_XButtonThreshold);
            m_YAxisThreshold = Mathf.Clamp01(i_YButtonThreshold);

            m_BackPanel = i_BackPanel;
            m_BackImage = i_BackImage;
            m_Nub       = i_Nub;

            if (m_BackImage != null && m_BackPanel != null && m_Nub != null)
            {
                // Apply custom scale for tablet or big screens

                float scale = 1f;

                float screenDiagonal = Mathf.Sqrt(Screen.width * Screen.width + Screen.height * Screen.height);
                float displayInches  = (screenDiagonal / Screen.dpi);

                float refDisplayInches = 5f;

                float screenRatio = refDisplayInches / displayInches;
                if (screenRatio < 1f)
                {
                    scale = screenRatio;
                }

                m_BackImage.transform.localScale = new Vector3(scale, scale, scale);

                // Evaluate ray

                {
                    Vector2 centerPosition = m_BackImage.rectTransform.GetPosition2();

                    float ray = (m_BackImage.rectTransform.rect.width / 2f);
                    m_BackImage.rectTransform.anchoredPosition += Vector2.right * ray;

                    Vector2 p = m_BackImage.rectTransform.GetPosition2();

                    m_Ray = (p - centerPosition).magnitude * scale;

                    m_BackImage.rectTransform.SetPosition(centerPosition);
                }

                m_DefaultNubPosition = m_Nub.rectTransform.anchoredPosition;

                EventTrigger eventTrigger = m_BackPanel.GetComponent <EventTrigger>();
                if (eventTrigger == null)
                {
                    eventTrigger = m_BackPanel.gameObject.AddComponent <EventTrigger>();
                }

                EventTrigger.Entry pointerDown = new EventTrigger.Entry();
                pointerDown.eventID = EventTriggerType.PointerDown;
                pointerDown.callback.AddListener(OnPointerDown);

                eventTrigger.triggers.Add(pointerDown);

                EventTrigger.Entry pointerDrag = new EventTrigger.Entry();
                pointerDrag.eventID = EventTriggerType.Drag;
                pointerDrag.callback.AddListener(OnPointerDrag);

                eventTrigger.triggers.Add(pointerDrag);

                EventTrigger.Entry pointerEndDrag = new EventTrigger.Entry();
                pointerEndDrag.eventID = EventTriggerType.EndDrag;
                pointerEndDrag.callback.AddListener(OnPointerEndDrag);

                eventTrigger.triggers.Add(pointerEndDrag);

                EventTrigger.Entry pointerUp = new EventTrigger.Entry();
                pointerUp.eventID = EventTriggerType.PointerUp;
                pointerUp.callback.AddListener(OnPointerUp);

                eventTrigger.triggers.Add(pointerUp);
            }

            if (WiFiInputController.controllerDataDictionary != null)
            {
                string xControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_AXIS, xControlName);
                string yControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_AXIS, yControlName);

                m_XController = (AxisControllerType)WiFiInputController.controllerDataDictionary[xControllerKey];
                m_YController = (AxisControllerType)WiFiInputController.controllerDataDictionary[yControllerKey];

                if (m_SendXAsButton)
                {
                    string xPositiveButtonControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_BUTTON, m_XPositiveButtonControlName);
                    string xNegativeButtonControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_BUTTON, m_XNegativeButtonControlName);

                    m_XPositiveButtonController = (ButtonControllerType)WiFiInputController.controllerDataDictionary[xPositiveButtonControllerKey];
                    m_XNegativeButtonController = (ButtonControllerType)WiFiInputController.controllerDataDictionary[xNegativeButtonControllerKey];
                }

                if (m_SendYAsButton)
                {
                    string yPositiveButtonControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_BUTTON, m_YPositiveButtonControlName);
                    string yNegativeButtonControllerKey = WiFiInputController.registerControl(WiFiInputConstants.CONTROLLERTYPE_BUTTON, m_YNegativeButtonControlName);

                    m_YPositiveButtonController = (ButtonControllerType)WiFiInputController.controllerDataDictionary[yPositiveButtonControllerKey];
                    m_YNegativeButtonController = (ButtonControllerType)WiFiInputController.controllerDataDictionary[yNegativeButtonControllerKey];
                }
            }
        }