Esempio n. 1
0
 bool GetOrCreateMapping(string address, OscMessageType type, out OscMapping mapping)
 {
     mapping = _mappings.Find(m => m.address == address);
     if (mapping == null)
     {
         mapping = new OscMapping(address, type);
         //mapping = ScriptableObject.CreateInstance<OscMapping>();
         //mapping.Init( address, type );
         _mappings.Add(mapping);
     }
     else if (mapping.type != type)
     {
         if (OscGlobals.logWarnings)
         {
             StringBuilder sb = OscDebug.BuildText(this);
             sb.Append("Failed to map address \""); sb.Append(address);
             sb.Append("\" to method with argument type "); sb.Append(type);
             sb.Append(". \nAddress is already mapped to a method with argument type "); sb.Append(mapping.type);
             sb.Append(", either in the editor, or by a script. Only one type per address is allowed.\n");
             Debug.LogWarning(sb.ToString());
         }
         return(false);
     }
     return(true);
 }
Esempio n. 2
0
    /// <summary>
    /// Same as above for methods with no arguments.
    /// </summary>
    public void UnmapImpulseNullOrEmpty(UnityAction method)
    {
        // UnityEvent is secret about whether we removed a runtime handler, so we have to iterate the whole array og mappings
        for (int m = _mappings.Count - 1; m >= 0; m--)
        {
            OscMapping mapping = _mappings[m];

            // Check if runtime method exists
            System.Reflection.MethodInfo mappedRuntimeHandlerInfo = mapping.runtimeMethodInfo.Find(h => h == method.Method);
            if (mappedRuntimeHandlerInfo == null)
            {
                continue;
            }

            // Remove inspector info
            string methodLabel = GetMethodLabel(method.Target, method.Method);
            mapping.runtimeMethodInfo.Remove(mappedRuntimeHandlerInfo);
            mapping.runtimeMethodLabels.Remove(methodLabel);
            int runtimeCount = mapping.runtimeMethodInfo.Count;

            // Remove the method from the UnityEvent. We have no way of accesing information about methods added at runtime.
            mapping.ImpulseNullEmptyHandler.RemoveListener(method);
            int editorCount = mapping.ImpulseNullEmptyHandler.GetPersistentEventCount();

            // If there are no editor or runtime methods mapped to the hanlder left, then remove mapping.
            if (editorCount + runtimeCount == 0)
            {
                _mappings.RemoveAt(m);
            }
        }
        _dirtyMappings = true;
    }
Esempio n. 3
0
    /// <summary>
    /// Request that all methods that are mapped to OSC 'address' will no longer be invoked.
    /// This is useful for unmapping delegates.
    /// </summary>
    public void UnmapAll(string address)
    {
        OscMapping mapping = _mappings.Find(m => m.address == address);

        if (mapping != null)
        {
            mapping.Clear();
            _mappings.Remove(mapping);
        }
    }
Esempio n. 4
0
    /// <summary>
    /// Request that 'method' is no longer invoked by OscIn.
    /// </summary>
    public void UnmapImpulseNullOrEmpty(UnityAction method)
    {
        // UnityEvent is secret about whether we removed a runtime handler, so we have to iterate the whole array og mappings.
        for (int m = _mappings.Count - 1; m >= 0; m--)
        {
            OscMapping mapping = _mappings[m];

            // If there are no methods mapped to the hanlder left, then remove mapping.
            mapping.Unmap(method.Target, method.Method);
            //if( mapping.ImpulseNullEmptyHandler.GetPersistentEventCount() == 0 ) _mappings.RemoveAt( m );
        }
        _dirtyMappings = true;
    }
Esempio n. 5
0
 bool GetOrCreateMapping(string address, OscMessageType type, out OscMapping mapping)
 {
     mapping = _mappings.Find(m => m.address == address);
     if (mapping == null)
     {
         mapping = new OscMapping(address, type);
         _mappings.Add(mapping);
     }
     else if (mapping.type != type)
     {
         Debug.LogWarning(BuildFailedToMapMessage(address, type, mapping.type));
         return(false);
     }
     return(true);
 }
Esempio n. 6
0
    void Unmap <T>(Action <T> method, OscMessageType type)
    {
        for (int m = _mappings.Count - 1; m >= 0; m--)
        {
            OscMapping mapping = _mappings[m];

            mapping.Unmap(method.Target, method.Method);

            // If there are no methods mapped to the hanlder left, then remove mapping.
            if (mapping.entryCount == 0)
            {
                _mappings.RemoveAt(m);
            }
        }
        _dirtyMappings = true;
    }
Esempio n. 7
0
    void Map <T>(string address, UnityAction <T> method, OscMessageType type)
    {
        // Validate.
        if (!ValidateAddressForMapping(address) || method == null || !ValidateMethodTarget(method.Target, address))
        {
            return;
        }

        // Get or create mapping.
        OscMapping mapping = null;

        GetOrCreateMapping(address, type, out mapping);

        // Add listener.
        switch (type)
        {
        case OscMessageType.OscMessage: mapping.OscMessageHandler.AddPersistentListener(method as UnityAction <OscMessage>); break;

        case OscMessageType.Bool: mapping.BoolHandler.AddPersistentListener(method as UnityAction <bool>); break;

        case OscMessageType.Float: mapping.FloatHandler.AddPersistentListener(method as UnityAction <float>); break;

        case OscMessageType.Int: mapping.IntHandler.AddPersistentListener(method as UnityAction <int>); break;

        case OscMessageType.Char: mapping.CharHandler.AddPersistentListener(method as UnityAction <char>); break;

        case OscMessageType.Color: mapping.ColorHandler.AddPersistentListener(method as UnityAction <Color32>); break;

        case OscMessageType.Midi: mapping.MidiHandler.AddPersistentListener(method as UnityAction <OscMidiMessage>); break;

        case OscMessageType.Double: mapping.DoubleHandler.AddPersistentListener(method as UnityAction <double>); break;

        case OscMessageType.Long: mapping.LongHandler.AddPersistentListener(method as UnityAction <long>); break;

        case OscMessageType.TimeTag: mapping.TimeTagHandler.AddPersistentListener(method as UnityAction <OscTimeTag>); break;

        case OscMessageType.String: mapping.StringHandler.AddPersistentListener(method as UnityAction <string>); break;

        case OscMessageType.Blob: mapping.BlobHandler.AddPersistentListener(method as UnityAction <byte[]>); break;
        }

        // Set dirty flag.
        _dirtyMappings = true;
    }
Esempio n. 8
0
    void Map <T>(string address, Action <T> method, OscMessageType type)
    {
        // Validate.
        if (!ValidateAddressForMapping(address) || method == null || !ValidateMethodTarget(method.Target, address))
        {
            return;
        }

        // Get or create mapping.
        OscMapping mapping = null;

        GetOrCreateMapping(address, type, out mapping);

        // Add listener.
        mapping.Map(method.Target, method.Method);

        // Set dirty flag.
        _dirtyMappings = true;
    }
Esempio n. 9
0
    /// <summary>
    /// Request that 'method' is invoked when a message with matching 'address' is received with type tag Impulse (i), Null (N) or simply without arguments.
    /// </summary>
    public void MapImpulseNullOrEmpty(string address, UnityAction method)
    {
        // Validate.
        if (!ValidateAddressForMapping(address) || method == null || !ValidateMethodTarget(method.Target, address))
        {
            return;
        }

        // Get or create mapping.
        OscMapping mapping = null;

        GetOrCreateMapping(address, OscMessageType.ImpulseNullEmpty, out mapping);

        // Add listener.
        mapping.Map(method.Target, method.Method);

        // Set dirty flag.
        _dirtyMappings = true;
    }
Esempio n. 10
0
 bool GetOrCreateMapping(string address, OscMessageType type, out OscMapping mapping)
 {
     mapping = _mappings.Find(m => m.address == address);
     if (mapping == null)
     {
         mapping = new OscMapping(address, type);
         _mappings.Add(mapping);
     }
     else if (mapping.type != type)
     {
         StringBuilder sb = OscDebug.BuildText(this);
         sb.Append("Failed to map address'"); sb.Append(address);
         sb.Append("' to method with argument type '"); sb.Append(type);
         sb.Append("'. Address is already set to receive type '"); sb.Append(mapping.type);
         sb.Append("', either in the editor, or by a script.\nOnly one type per address is allowed.\n");
         Debug.LogWarning(sb.ToString());
         return(false);
     }
     return(true);
 }
Esempio n. 11
0
    /// <summary>
    /// Request that 'method' is invoked when a message with OSC 'address' is received with type tag Impulse (i), Null (N) or simply without arguments.
    /// </summary>
    public void MapImpulseNullOrEmpty(string address, UnityAction method)
    {
        if (!ValidateAddressForMapping(address))
        {
            return;
        }

        // Get or create mapping
        OscMapping mapping = null;

        GetOrCreateMapping(address, OscMessageType.ImpulseNullEmpty, out mapping);

        // Add listener
        mapping.ImpulseNullEmptyHandler.AddListener(method);

        // Add mapping inspector info (To unmap runtime handler, knowing we did it. Because UnityEvent is a black box)
        mapping.runtimeMethodInfo.Add(method.Method);
        string methodLabel = method.Target.GetType() + "." + method.Method.Name;         // For display in inspector

        mapping.runtimeMethodLabels.Add(methodLabel);

        // Set dirty flag
        _dirtyMappings = true;
    }
Esempio n. 12
0
    void Map <T>(string address, UnityAction <T> method, OscMessageType type)
    {
        if (string.IsNullOrEmpty(address))
        {
            return;
        }
        if (method == null)
        {
            return;
        }
        if (!ValidateAddressForMapping(address))
        {
            return;
        }

        // Get or create mapping
        OscMapping mapping = null;

        GetOrCreateMapping(address, type, out mapping);

        // Add listener
        switch (type)
        {
        case OscMessageType.OscMessage: mapping.OscMessageHandler.AddListener(method as UnityAction <OscMessage>); break;

        case OscMessageType.Float:              mapping.FloatHandler.AddListener(method as UnityAction <float>); break;

        case OscMessageType.Double:             mapping.DoubleHandler.AddListener(method as UnityAction <double>); break;

        case OscMessageType.Int:                mapping.IntHandler.AddListener(method as UnityAction <int>); break;

        case OscMessageType.Long:               mapping.LongHandler.AddListener(method as UnityAction <long>); break;

        case OscMessageType.String:             mapping.StringHandler.AddListener(method as UnityAction <string>); break;

        case OscMessageType.Char:               mapping.CharHandler.AddListener(method as UnityAction <char>); break;

        case OscMessageType.Bool:               mapping.BoolHandler.AddListener(method as UnityAction <bool>); break;

        case OscMessageType.Color:              mapping.ColorHandler.AddListener(method as UnityAction <Color32>); break;

        case OscMessageType.Blob:               mapping.BlobHandler.AddListener(method as UnityAction <byte[]>); break;

        case OscMessageType.TimeTag:    mapping.TimeTagHandler.AddListener(method as UnityAction <OscTimeTag>); break;
        }

        // Add mapping inspector info (To unmap runtime handler, knowing we did it. Because UnityEvent is a black box)
        mapping.runtimeMethodInfo.Add(method.Method);

        // For display in inspector
        string handlerLabel;

        if (method.Target == null)
        {
            handlerLabel = "delegate";
        }
        else
        {
            handlerLabel = GetMethodLabel(method.Target, method.Method);
        }
        mapping.runtimeMethodLabels.Add(handlerLabel);

        // Set dirty flag
        _dirtyMappings = true;
    }
Esempio n. 13
0
    void Update()
    {
        if (_mappings == null)
        {
            return;
        }

        if (!isOpen)
        {
            return;
        }

        if (_mappingLookup == null || _dirtyMappings)
        {
            UpdateMappings();
        }

        if (invokeUnmappedQueue == null)
        {
            invokeUnmappedQueue = new Queue <OscMessage>();
        }
        if (invokeMappedQueue == null)
        {
            invokeMappedQueue = new Queue <KeyValuePair <OscMapping, OscMessage> >();
        }
        deadAddresses.Clear();

        // Lock while we mess with '_messages'
        lock ( _lock )
        {
            foreach (KeyValuePair <string, List <OscMessage> > pair in _messages)
            {
                List <OscMessage> groupedMessages = pair.Value;
                int gmCount = groupedMessages.Count;

                // Collect the dead
                if (gmCount == 0)
                {
                    deadAddresses.Add(pair.Key);
                    continue;
                }

                // Get messages and invoke mapped handlers
                int gmBegin = _filterDuplicates ? gmCount - 1 : 0;
                for (int gm = gmBegin; gm < gmCount; gm++)
                {
                    OscMessage message = groupedMessages[gm];
                    OscMapping mapping;
                    if (_mappingLookup.TryGetValue(message.address, out mapping))
                    {
                        // Enqueue mapped call
                        invokeMappedQueue.Enqueue(new KeyValuePair <OscMapping, OscMessage>(mapping, message));
                    }
                    else
                    {
                        // Enqueue unmapped (catch all) call
                        invokeUnmappedQueue.Enqueue(message);
                    }
                }

                // Clear address group
                groupedMessages.Clear();
            }

            // Remove the dead
            foreach (string address in deadAddresses)
            {
                _messages.Remove(address);
            }
        }

        // Count
        _messageCount = invokeUnmappedQueue.Count + invokeMappedQueue.Count;

        // Invoke handlers outside lock
        while (invokeUnmappedQueue.Count > 0)
        {
            _onAnyMessage.Invoke(invokeUnmappedQueue.Dequeue());
        }
        while (invokeMappedQueue.Count > 0)
        {
            KeyValuePair <OscMapping, OscMessage> pair = invokeMappedQueue.Dequeue();
            OscMapping mapping = pair.Key;
            OscMessage message = pair.Value;

            _onAnyMessage.Invoke(message);

            switch (mapping.type)
            {
            case OscMessageType.OscMessage:
                mapping.OscMessageHandler.Invoke(message);
                break;

            case OscMessageType.Float:
                float floatValue;
                if (message.TryGet(0, out floatValue))
                {
                    mapping.FloatHandler.Invoke(floatValue);
                }
                break;

            case OscMessageType.Double:
                double doubleValue;
                if (message.TryGet(0, out doubleValue))
                {
                    mapping.DoubleHandler.Invoke(doubleValue);
                }
                break;

            case OscMessageType.Int:
                int intValue;
                if (message.TryGet(0, out intValue))
                {
                    mapping.IntHandler.Invoke(intValue);
                }
                break;

            case OscMessageType.Long:
                long longValue;
                if (message.TryGet(0, out longValue))
                {
                    mapping.LongHandler.Invoke(longValue);
                }
                break;

            case OscMessageType.String:
                string stringValue;
                if (message.TryGet(0, out stringValue))
                {
                    mapping.StringHandler.Invoke(stringValue);
                }
                break;

            case OscMessageType.Char:
                char charValue;
                if (message.TryGet(0, out charValue))
                {
                    mapping.CharHandler.Invoke(charValue);
                }
                break;

            case OscMessageType.Bool:
                bool boolValue;
                if (message.TryGet(0, out boolValue))
                {
                    mapping.BoolHandler.Invoke(boolValue);
                }
                break;

            case OscMessageType.Color:
                Color32 colorValue;
                if (message.TryGet(0, out colorValue))
                {
                    mapping.ColorHandler.Invoke(colorValue);
                }
                break;

            case OscMessageType.Blob:
                byte[] blobValue;
                if (message.TryGet(0, out blobValue))
                {
                    mapping.BlobHandler.Invoke(blobValue);
                }
                break;

            case OscMessageType.TimeTag:
                OscTimeTag timeTagValue;
                if (message.TryGet(0, out timeTagValue))
                {
                    mapping.TimeTagHandler.Invoke(timeTagValue);
                }
                break;

            case OscMessageType.ImpulseNullEmpty:
                mapping.ImpulseNullEmptyHandler.Invoke();
                break;
            }
        }
    }
Esempio n. 14
0
    void Unmap <T>(UnityAction <T> method)
    {
        // UnityEvent is secret about whether we removed a runtime handler, so we have to iterate the whole array og mappings
        Type type = typeof(T);

        for (int m = _mappings.Count - 1; m >= 0; m--)
        {
            OscMapping mapping = _mappings[m];

            // Check if runtime method exists
            System.Reflection.MethodInfo mappedRuntimeHandlerInfo = mapping.runtimeMethodInfo.Find(h => h == method.Method);
            if (mappedRuntimeHandlerInfo == null)
            {
                continue;
            }

            // Remove inspector info
            string methodLabel = GetMethodLabel(method.Target, method.Method);
            mapping.runtimeMethodInfo.Remove(mappedRuntimeHandlerInfo);
            mapping.runtimeMethodLabels.Remove(methodLabel);
            int runtimeCount = mapping.runtimeMethodInfo.Count;

            // Remove the method from the UnityEvent. We have no way of accesing information about methods added at runtime.
            int editorCount = 0;
            if (type == typeof(OscMessage))
            {
                mapping.OscMessageHandler.RemoveListener(method as UnityAction <OscMessage>);
                editorCount = mapping.OscMessageHandler.GetPersistentEventCount();
            }
            else if (type == typeof(float))
            {
                mapping.FloatHandler.RemoveListener(method as UnityAction <float>);
                editorCount = mapping.FloatHandler.GetPersistentEventCount();
            }
            else if (type == typeof(double))
            {
                mapping.DoubleHandler.RemoveListener(method as UnityAction <double>);
                editorCount = mapping.DoubleHandler.GetPersistentEventCount();
            }
            else if (type == typeof(int))
            {
                mapping.IntHandler.RemoveListener(method as UnityAction <int>);
                editorCount = mapping.DoubleHandler.GetPersistentEventCount();
            }
            else if (type == typeof(long))
            {
                mapping.LongHandler.RemoveListener(method as UnityAction <long>);
                editorCount = mapping.LongHandler.GetPersistentEventCount();
            }
            else if (type == typeof(string))
            {
                mapping.StringHandler.RemoveListener(method as UnityAction <string>);
                editorCount = mapping.StringHandler.GetPersistentEventCount();
            }
            else if (type == typeof(char))
            {
                mapping.CharHandler.RemoveListener(method as UnityAction <char>);
                editorCount = mapping.CharHandler.GetPersistentEventCount();
            }
            else if (type == typeof(bool))
            {
                mapping.BoolHandler.RemoveListener(method as UnityAction <bool>);
                editorCount = mapping.BoolHandler.GetPersistentEventCount();
            }
            else if (type == typeof(Color32))
            {
                mapping.ColorHandler.RemoveListener(method as UnityAction <Color32>);
                editorCount = mapping.ColorHandler.GetPersistentEventCount();
            }
            else if (type == typeof(byte[]))
            {
                mapping.BlobHandler.RemoveListener(method as UnityAction <byte[]>);
                editorCount = mapping.BlobHandler.GetPersistentEventCount();
            }
            else if (type == typeof(OscTimeTag))
            {
                mapping.TimeTagHandler.RemoveListener(method as UnityAction <OscTimeTag>);
                editorCount = mapping.TimeTagHandler.GetPersistentEventCount();
            }

            // If there are no editor or runtime methods mapped to the hanlder left, then remove mapping.
            if (editorCount + runtimeCount == 0)
            {
                _mappings.RemoveAt(m);
            }
        }
        _dirtyMappings = true;
    }
Esempio n. 15
0
    void InvokeMapping(OscMapping mapping, OscMessage message)
    {
        switch (mapping.type)
        {
        case OscMessageType.OscMessage:
            mapping.Invoke(message);
            break;

        case OscMessageType.Float:
            float floatValue;
            if (message.TryGet(0, out floatValue))
            {
                mapping.Invoke(floatValue);
                //Debug.Log( floatValue );
            }
            break;

        case OscMessageType.Double:
            double doubleValue;
            if (message.TryGet(0, out doubleValue))
            {
                mapping.Invoke(doubleValue);
            }
            break;

        case OscMessageType.Int:
            int intValue;
            if (message.TryGet(0, out intValue))
            {
                mapping.Invoke(intValue);
            }
            break;

        case OscMessageType.Long:
            long longValue;
            if (message.TryGet(0, out longValue))
            {
                mapping.Invoke(longValue);
            }
            break;

        case OscMessageType.String:
            string stringValue = string.Empty;
            if (message.TryGet(0, ref stringValue))
            {
                mapping.Invoke(stringValue);
            }
            break;

        case OscMessageType.Char:
            char charValue;
            if (message.TryGet(0, out charValue))
            {
                mapping.Invoke(charValue);
            }
            break;

        case OscMessageType.Bool:
            bool boolValue;
            if (message.TryGet(0, out boolValue))
            {
                mapping.Invoke(boolValue);
            }
            break;

        case OscMessageType.Color:
            Color32 colorValue;
            if (message.TryGet(0, out colorValue))
            {
                mapping.Invoke(colorValue);
            }
            break;

        case OscMessageType.Midi:
            OscMidiMessage midiValue;
            if (message.TryGet(0, out midiValue))
            {
                mapping.Invoke(midiValue);
            }
            break;

        case OscMessageType.Blob:
            byte[] blobValue = null;
            if (message.TryGet(0, ref blobValue))
            {
                mapping.Invoke(blobValue);
            }
            break;

        case OscMessageType.TimeTag:
            OscTimeTag timeTagValue;
            if (message.TryGet(0, out timeTagValue))
            {
                mapping.Invoke(timeTagValue);
            }
            break;

        case OscMessageType.ImpulseNullEmpty:
            mapping.Invoke();
            break;
        }
    }
Esempio n. 16
0
    void Unmap <T>(UnityAction <T> method, OscMessageType type)
    {
        for (int m = _mappings.Count - 1; m >= 0; m--)
        {
            OscMapping mapping = _mappings[m];

            int eventCount = 0;
            switch (type)
            {
            case OscMessageType.OscMessage:
                mapping.OscMessageHandler.RemovePersistentListener(method as UnityAction <OscMessage>);
                eventCount = mapping.OscMessageHandler.GetPersistentEventCount(); break;

            case OscMessageType.Bool:
                mapping.BoolHandler.RemovePersistentListener(method as UnityAction <bool>);
                eventCount = mapping.BoolHandler.GetPersistentEventCount(); break;

            case OscMessageType.Float:
                mapping.FloatHandler.RemovePersistentListener(method as UnityAction <float>);
                eventCount = mapping.FloatHandler.GetPersistentEventCount(); break;

            case OscMessageType.Int:
                mapping.IntHandler.RemovePersistentListener(method as UnityAction <int>);
                eventCount = mapping.IntHandler.GetPersistentEventCount(); break;

            case OscMessageType.Char:
                mapping.CharHandler.RemovePersistentListener(method as UnityAction <char>);
                eventCount = mapping.CharHandler.GetPersistentEventCount(); break;

            case OscMessageType.Color:
                mapping.ColorHandler.RemovePersistentListener(method as UnityAction <Color32>);
                eventCount = mapping.ColorHandler.GetPersistentEventCount(); break;

            case OscMessageType.Midi:
                mapping.MidiHandler.RemovePersistentListener(method as UnityAction <OscMidiMessage>);
                eventCount = mapping.MidiHandler.GetPersistentEventCount(); break;

            case OscMessageType.Double:
                mapping.DoubleHandler.RemovePersistentListener(method as UnityAction <double>);
                eventCount = mapping.DoubleHandler.GetPersistentEventCount(); break;

            case OscMessageType.Long:
                mapping.LongHandler.RemovePersistentListener(method as UnityAction <long>);
                eventCount = mapping.LongHandler.GetPersistentEventCount(); break;

            case OscMessageType.TimeTag:
                mapping.TimeTagHandler.RemovePersistentListener(method as UnityAction <OscTimeTag>);
                eventCount = mapping.TimeTagHandler.GetPersistentEventCount(); break;

            case OscMessageType.String:
                mapping.StringHandler.RemovePersistentListener(method as UnityAction <string>);
                eventCount = mapping.StringHandler.GetPersistentEventCount(); break;

            case OscMessageType.Blob:
                mapping.BlobHandler.RemovePersistentListener(method as UnityAction <byte[]>);
                eventCount = mapping.BlobHandler.GetPersistentEventCount(); break;
            }

            // If there are no methods mapped to the hanlder left, then remove mapping.
            if (eventCount == 0)
            {
                _mappings.RemoveAt(m);
            }
        }
        _dirtyMappings = true;
    }