<commit_before>﻿//========= Copyright 2015, Valve Corporation, All rights reserved. ===========
//
// Purpose: Enables/disables left and right controller objects based on
// connectivity and relative positions.
//
//=============================================================================

using System;
using System.Collections.Generic;

namespace Valve.VR
{
    public class SteamVR_ControllerManager : System.IDisposable
    {
        public SteamVR_Controller left = new SteamVR_Controller("left");
        public SteamVR_Controller right = new SteamVR_Controller("right");
        public SteamVR_Controller[] objects; // populate with objects you want to assign to additional controllers

        public uint[] indices; // assigned
        public bool[] connected = new bool[OpenVR.k_unMaxTrackedDeviceCount]; // controllers only

        List<uint> trackers = new List<uint>();

        // cached roles - may or may not be connected
        uint leftIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
        uint rightIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
        
        public SteamVR_ControllerManager()
        {
            // Add left and right entries to the head of the list so we only have to operate on the list itself.
            var additional = (this.objects != null) ? this.objects.Length : 0;
            var objects = new SteamVR_Controller[2 + additional];
            indices = new uint[2 + additional];
            objects[0] = right;
            indices[0] = OpenVR.k_unTrackedDeviceIndexInvalid;
            objects[1] = left;
            indices[1] = OpenVR.k_unTrackedDeviceIndexInvalid;
            for (int i = 0; i < additional; i++)
            {
                objects[2 + i] = this.objects[i];
                indices[2 + i] = OpenVR.k_unTrackedDeviceIndexInvalid;
            }
            this.objects = objects;

            for (int i = 0; i < objects.Length; i++)
            {
                var obj = objects[i];
                if (obj != null)
                    obj.SetActive(false);
            }

            OnTrackedDeviceRoleChanged();

            for (int i = 0; i < SteamVR.connected.Length; i++)
                if (SteamVR.connected[i])
                    OnDeviceConnected(i, true);

            SteamVR_Event.Listen("input_focus", OnInputFocus);
            SteamVR_Event.Listen("device_connected", OnDeviceConnected);
            SteamVR_Event.Listen("TrackedDeviceRoleChanged", OnTrackedDeviceRoleChanged);
        }
        
        static string[] labels = { "left", "right" };
        
        // Hide controllers when the dashboard is up.
        private void OnInputFocus(params object[] args)
        {
            bool hasFocus = (bool)args[0];
            if (hasFocus)
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    var obj = objects[i];
                    if (obj != null)
                    {
                        var label = (i < 2) ? labels[i] : (i - 1).ToString();
                        //ShowObject(obj.transform, "hidden (" + label + ")");
                        //SteamVR_WebKit.SteamVR_WebKit.Log("Controller manager wants to show the " + label + " controller.");
                    }
                }
            }
            else
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    var obj = objects[i];
                    if (obj != null)
                    {
                        var label = (i < 2) ? labels[i] : (i - 1).ToString();
                        //HideObject(obj.transform, "hidden (" + label + ")");
                        //SteamVR_WebKit.SteamVR_WebKit.Log("Controller manager wants to hide the " + label + " controller.");
                    }
                }
            }
        }
        
        /*// Reparents to a new object and deactivates that object (this allows
        // us to call SetActive in OnDeviceConnected independently.
        private void HideObject(Transform t, string name)
        {
            var hidden = new GameObject(name).transform;
            hidden.parent = t.parent;
            t.parent = hidden;
            hidden.gameObject.SetActive(false);
        }
        private void ShowObject(Transform t, string name)
        {
            var hidden = t.parent;
            if (hidden.gameObject.name != name)
                return;
            t.parent = hidden.parent;
            Destroy(hidden.gameObject);
        }*/

        private void SetTrackedDeviceIndex(int objectIndex, uint trackedDeviceIndex)
        {
            // First make sure no one else is already using this index.
            if (trackedDeviceIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    if (i != objectIndex && indices[i] == trackedDeviceIndex)
                    {
                        var obj = objects[i];
                        if (obj != null)
                            obj.SetActive(false);

                        indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
                    }
                }
            }

            // Only set when changed.
            if (trackedDeviceIndex != indices[objectIndex])
            {
                indices[objectIndex] = trackedDeviceIndex;

                var obj = objects[objectIndex];
                if (obj != null)
                {
                    if (trackedDeviceIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
                        obj.SetActive(false);
                    else
                    {
                        obj.SetActive(true);
                        //obj.BroadcastMessage("SetDeviceIndex", (int)trackedDeviceIndex, SendMessageOptions.DontRequireReceiver);
                        //SteamVR_WebKit.SteamVR_WebKit.Log("obj.BroadcastMessage(\"SetDeviceIndex\", (int)trackedDeviceIndex="+ trackedDeviceIndex + ", SendMessageOptions.DontRequireReceiver);");
                    }
                }
            }
        }

        // Keep track of assigned roles.
        private void OnTrackedDeviceRoleChanged(params object[] args)
        {
            SteamVR_WebKit.SteamVR_WebKit.Log("[OPENVR] Device Role Changed: " + (args.Length > 0 ? args[0] : "Unknown"));
            //SteamVR_WebKit.SteamVR_WebKit.Log("Controller role change detected");<commit_msg>Hide that annoying log spam<commit_after>﻿//========= Copyright 2015, Valve Corporation, All rights reserved. ===========
//
// Purpose: Enables/disables left and right controller objects based on
// connectivity and relative positions.
//
//=============================================================================

using System;
using System.Collections.Generic;

namespace Valve.VR
{
    public class SteamVR_ControllerManager : System.IDisposable
    {
        public SteamVR_Controller left = new SteamVR_Controller("left");
        public SteamVR_Controller right = new SteamVR_Controller("right");
        public SteamVR_Controller[] objects; // populate with objects you want to assign to additional controllers

        public uint[] indices; // assigned
        public bool[] connected = new bool[OpenVR.k_unMaxTrackedDeviceCount]; // controllers only

        List<uint> trackers = new List<uint>();

        // cached roles - may or may not be connected
        uint leftIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
        uint rightIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
        
        public SteamVR_ControllerManager()
        {
            // Add left and right entries to the head of the list so we only have to operate on the list itself.
            var additional = (this.objects != null) ? this.objects.Length : 0;
            var objects = new SteamVR_Controller[2 + additional];
            indices = new uint[2 + additional];
            objects[0] = right;
            indices[0] = OpenVR.k_unTrackedDeviceIndexInvalid;
            objects[1] = left;
            indices[1] = OpenVR.k_unTrackedDeviceIndexInvalid;
            for (int i = 0; i < additional; i++)
            {
                objects[2 + i] = this.objects[i];
                indices[2 + i] = OpenVR.k_unTrackedDeviceIndexInvalid;
            }
            this.objects = objects;

            for (int i = 0; i < objects.Length; i++)
            {
                var obj = objects[i];
                if (obj != null)
                    obj.SetActive(false);
            }

            OnTrackedDeviceRoleChanged();

            for (int i = 0; i < SteamVR.connected.Length; i++)
                if (SteamVR.connected[i])
                    OnDeviceConnected(i, true);

            SteamVR_Event.Listen("input_focus", OnInputFocus);
            SteamVR_Event.Listen("device_connected", OnDeviceConnected);
            SteamVR_Event.Listen("TrackedDeviceRoleChanged", OnTrackedDeviceRoleChanged);
        }
        
        static string[] labels = { "left", "right" };
        
        // Hide controllers when the dashboard is up.
        private void OnInputFocus(params object[] args)
        {
            bool hasFocus = (bool)args[0];
            if (hasFocus)
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    var obj = objects[i];
                    if (obj != null)
                    {
                        var label = (i < 2) ? labels[i] : (i - 1).ToString();
                        //ShowObject(obj.transform, "hidden (" + label + ")");
                        //SteamVR_WebKit.SteamVR_WebKit.Log("Controller manager wants to show the " + label + " controller.");
                    }
                }
            }
            else
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    var obj = objects[i];
                    if (obj != null)
                    {
                        var label = (i < 2) ? labels[i] : (i - 1).ToString();
                        //HideObject(obj.transform, "hidden (" + label + ")");
                        //SteamVR_WebKit.SteamVR_WebKit.Log("Controller manager wants to hide the " + label + " controller.");
                    }
                }
            }
        }
        
        /*// Reparents to a new object and deactivates that object (this allows
        // us to call SetActive in OnDeviceConnected independently.
        private void HideObject(Transform t, string name)
        {
            var hidden = new GameObject(name).transform;
            hidden.parent = t.parent;
            t.parent = hidden;
            hidden.gameObject.SetActive(false);
        }
        private void ShowObject(Transform t, string name)
        {
            var hidden = t.parent;
            if (hidden.gameObject.name != name)
                return;
            t.parent = hidden.parent;
            Destroy(hidden.gameObject);
        }*/

        private void SetTrackedDeviceIndex(int objectIndex, uint trackedDeviceIndex)
        {
            // First make sure no one else is already using this index.
            if (trackedDeviceIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
            {
                for (int i = 0; i < objects.Length; i++)
                {
                    if (i != objectIndex && indices[i] == trackedDeviceIndex)
                    {
                        var obj = objects[i];
                        if (obj != null)
                            obj.SetActive(false);

                        indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
                    }
                }
            }

            // Only set when changed.
            if (trackedDeviceIndex != indices[objectIndex])
            {
                indices[objectIndex] = trackedDeviceIndex;

                var obj = objects[objectIndex];
                if (obj != null)
                {
                    if (trackedDeviceIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
                        obj.SetActive(false);
                    else
                    {
                        obj.SetActive(true);
                        //obj.BroadcastMessage("SetDeviceIndex", (int)trackedDeviceIndex, SendMessageOptions.DontRequireReceiver);
                        //SteamVR_WebKit.SteamVR_WebKit.Log("obj.BroadcastMessage(\"SetDeviceIndex\", (int)trackedDeviceIndex="+ trackedDeviceIndex + ", SendMessageOptions.DontRequireReceiver);");
                    }
                }
            }
        }

        // Keep track of assigned roles.
        private void OnTrackedDeviceRoleChanged(params object[] args)
        {
            //SteamVR_WebKit.SteamVR_WebKit.Log("[OPENVR] Device Role Changed: " + (args.Length > 0 ? args[0] : "Unknown"));
            //SteamVR_WebKit.SteamVR_WebKit.Log("Controller role change detected");
{'fraction_non_alphanumeric': 0.10957628433959336, 'fraction_numerical': 0.004035387241968028, 'mean_word_length': 1.5423160386664037, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '966231', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3115, 'n_words': 805}