private static void _AddItemsFromFile(UnityEngine.Object obj, UniOSCMappingFileObj mfo)
        {
            string filepath = String.Empty;

            if (obj == null)
            {
                filepath = EditorUtility.OpenFilePanel("Select Mapping or Session file", "", UniOSCUtils.MAPPINGFILEEXTENSION);
            }
            else
            {
                filepath = AssetDatabase.GetAssetPath(obj);
            }

            if (String.IsNullOrEmpty(filepath))
            {
                return;
            }

            string           relativFilepath = filepath.Replace(Application.dataPath, "Assets");
            ScriptableObject go = AssetDatabase.LoadAssetAtPath(relativFilepath, typeof(ScriptableObject)) as ScriptableObject;

            if (go == null)
            {
                return;
            }

            if (go is UniOSCSessionFileObj)
            {
                foreach (var si in ((UniOSCSessionFileObj)go).oscSessionItemList)
                {
                    var mil = mfo.oscMappingItemList.FindAll(o => o.address == si.address);

                    if (mil.Count == 0)
                    {
                        UniOSCMappingItem mi = new UniOSCMappingItem(mfo);
                        mi.address = si.address;
                        mfo.oscMappingItemList.Add(mi);
                    }
                }                //foreach
            }

            if (go is UniOSCMappingFileObj)
            {
                foreach (var mi in ((UniOSCMappingFileObj)go).oscMappingItemList)
                {
                    var mil = mfo.oscMappingItemList.FindAll(o => o.address == mi.address);

                    if (mil.Count == 0)
                    {
                        UniOSCMappingItem _mi = new UniOSCMappingItem(mfo);
                        _mi.address = mi.address;
                        mfo.oscMappingItemList.Add(_mi);
                    }
                }                //foreach
            }

            EditorUtility.SetDirty(mfo);            //Without this after a restart of unity all data would be gone
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Removes the OSC Mapping item from the list and destroys the item instance.
 /// <see cref="UniOSC.UniOSCMappingItem.OnOSCDataDispatcherDelete"/>
 /// </summary>
 /// <param name="obj">Object to remove.</param>
 public void RemoveOSCMappingItem(UniOSCMappingItem obj)
 {
     if (oscMappingItemList.Contains(obj))
     {
         oscMappingItemList.Remove(obj);
         //Destroy(obj);
         obj = null;
     }
 }
        public static void OnGUI_Editor(UniOSCMappingItem obj)
        {
            EditorGUI.BeginChangeCheck();

            EditorGUILayout.BeginHorizontal();

            #region Values
            obj.address    = EditorGUILayout.TextField(new GUIContent("", "Tooltip for Address"), obj.address, GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[0].width));
            obj.min        = EditorGUILayout.FloatField(new GUIContent("", "Minimal value"), obj.min, GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[1].width));
            obj.max        = EditorGUILayout.FloatField(new GUIContent("", "Maximal value"), obj.max, GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[2].width));
            obj.mappingMIN = EditorGUILayout.FloatField(new GUIContent("", "Minimal Mapping value"), obj.mappingMIN, GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[3].width));
            obj.mappingMAX = EditorGUILayout.FloatField(new GUIContent("", "Maximal Mapping value"), obj.mappingMAX, GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[4].width));
            #endregion Values

            #region Learn
            Rect r = GUILayoutUtility.GetRect(UniOSCUtils.MAPPINGLISTLABELRECTS[5].width, UniOSCUtils.MAPPINGLISTLABELRECTS[5].height, GUI.skin.button, GUILayout.ExpandWidth(false));
            r.width *= 0.9f;
            if (obj.hostObj.IsLearning)
            {
                GUIStyle gs = new GUIStyle(GUI.skin.button);
                gs.active.textColor = UniOSCUtils.LEARN_COLOR_ON;
                gs.normal.textColor = UniOSCUtils.LEARN_COLOR_OFF;

                if (GUI.RepeatButton(r, new GUIContent("Learn", "Press to Learn OSC"), gs))
                {
                    obj.isLearning = true;
                }
                else
                {
                    obj.isLearning = false;
                }
            }
            #endregion Learn

            #region Delete
            if (GUILayout.Button("Delete", GUILayout.Width(UniOSCUtils.MAPPINGLISTLABELRECTS[6].width), GUILayout.Height(UniOSCUtils.MAPPINGLISTLABELRECTS[6].height)))
            {
                bool deleteItem = EditorUtility.DisplayDialog("Delete", "Do you want to delete this OSCMappingItem with the Address:\n" + obj.address + " ?", "OK", "Cancel");
                if (deleteItem)
                {
                    obj.OnOSCMappingItemDelete();
                }
            }
            #endregion Delete

            EditorGUILayout.EndHorizontal();

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(obj.hostObj);
            }
        }
        //
        public static void OnGUI_OSCMappingData_Editor(UniOSCMappingFileObj obj, float screenWidth, float screenHeight)
        {
            EditorGUILayout.BeginVertical(GUILayout.MaxHeight(screenHeight));

            #region Path
            EditorGUILayout.BeginHorizontal();
            GUIContent guic = new GUIContent("Path: " + AssetDatabase.GUIDToAssetPath(obj.my_guid) + "  .", "");
            Rect       area = GUILayoutUtility.GetRect(guic, GUIStyle.none, GUILayout.MinHeight(30f));     //GUILayout.MaxWidth(400f),
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUI.Label(area, guic);
            if (UniOSCUtils.IsMouseUpInArea(area))
            {
                UniOSCUtils.SelectObjectInHierachyFromGUID(obj.my_guid);
            }
            _StartDrag(area, obj);
            EditorGUILayout.EndHorizontal();
            #endregion Path

            #region Button
            _OnGUI_DrawButtons(obj, true);
            #endregion Button

            #region Header
            EditorGUILayout.BeginHorizontal();

            GUIContent con0  = new GUIContent("OSC Address ");
            Rect       rect0 = GUILayoutUtility.GetRect(con0, GUI.skin.label, GUILayout.MaxWidth(300f));
            GUI.Label(rect0, con0);

            GUIContent con1  = new GUIContent("Min ");
            Rect       rect1 = GUILayoutUtility.GetRect(con1, GUI.skin.label, GUILayout.MaxWidth(50f));
            GUI.Label(rect1, con1);

            GUIContent con2  = new GUIContent("Max ");
            Rect       rect2 = GUILayoutUtility.GetRect(con2, GUI.skin.label, GUILayout.MaxWidth(50f));
            GUI.Label(rect2, con2);

            GUIContent con3  = new GUIContent("Mapping Min ");
            Rect       rect3 = GUILayoutUtility.GetRect(con3, GUI.skin.label, GUILayout.MaxWidth(120f));
            GUI.Label(rect3, con3);

            GUIContent con4  = new GUIContent("Mapping Max ");
            Rect       rect4 = GUILayoutUtility.GetRect(con4, GUI.skin.label, GUILayout.MaxWidth(120f));
            GUI.Label(rect4, con4);

            GUIContent con5  = new GUIContent("");           //Learn
            Rect       rect5 = GUILayoutUtility.GetRect(con5, GUI.skin.toggle, GUILayout.Width(80f));
            GUI.Label(rect5, con5);

            GUIContent con6  = new GUIContent("");           //Delete
            Rect       rect6 = GUILayoutUtility.GetRect(con6, GUI.skin.label, GUILayout.Width(70f));
            GUI.Label(rect6, con6);

            if (Event.current.type == EventType.Repaint)
            {
                UniOSCUtils.MAPPINGLISTLABELRECTS[0] = rect0;
                UniOSCUtils.MAPPINGLISTLABELRECTS[1] = rect1;
                UniOSCUtils.MAPPINGLISTLABELRECTS[2] = rect2;
                UniOSCUtils.MAPPINGLISTLABELRECTS[3] = rect3;
                UniOSCUtils.MAPPINGLISTLABELRECTS[4] = rect4;
                UniOSCUtils.MAPPINGLISTLABELRECTS[5] = rect5;
                UniOSCUtils.MAPPINGLISTLABELRECTS[6] = rect6;
            }

            EditorGUILayout.EndHorizontal();
            #endregion Header


            #region List

            style = new GUIStyle(GUI.skin.box);
            style.normal.background = _tex;
            style.margin            = new RectOffset(0, 0, 0, 2);

            //draw the mapping items
            EditorGUILayout.BeginHorizontal();

            if (Event.current.type == EventType.Repaint)
            {
                screenWidth = UniOSCUtils.MAPPINGLISTHEADERLABELWIDTH;
            }

            obj.scrollpos = EditorGUILayout.BeginScrollView(obj.scrollpos, GUILayout.ExpandHeight(true), GUILayout.Width(screenWidth), GUILayout.MaxWidth(UniOSCUtils.MAPPINGLISTHEADERLABELWIDTH));            //GUILayout.Width(screenWidth) ,GUILayout.MaxWidth(OSCUtils.MAPPINGLISTHEADERLABELWIDTH
            int rowIndex = 0;

            try{
                for (var i = 0; i < obj.oscMappingItemList.Count; i++)
                {
                    GUI.backgroundColor = rowIndex % 2 == 0  ?  UniOSCUtils.ITEM_LIST_COLOR_A : UniOSCUtils.ITEM_LIST_COLOR_B;
                    UniOSCMappingItem c = obj.oscMappingItemList[i];

                    GUILayout.BeginHorizontal(style);
                    UniOSCMappingItemEditor.OnGUI_Editor(c);
                    GUILayout.EndHorizontal();

                    rowIndex++;
                }
            }catch (Exception) {
                //drag&drop Exception handling
            }
            GUI.backgroundColor = Color.white;
            EditorGUILayout.EndScrollView();

            EditorGUILayout.EndHorizontal();


            #endregion List

            EditorGUILayout.EndVertical();
        }
        public static void OnGUI_OSCMappingData_Inspector(UniOSCMappingFileObj obj, float screenWidth, float screenHeight)
        {
            EditorGUILayout.BeginVertical(GUILayout.MaxHeight(screenHeight));

            #region Path

            EditorGUILayout.BeginHorizontal();
            GUIContent guic = new GUIContent("Path: " + AssetDatabase.GUIDToAssetPath(obj.my_guid) + "  .", "");
            Rect       area = GUILayoutUtility.GetRect(guic, GUIStyle.none, GUILayout.MinHeight(30f));
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUI.Label(area, guic);
            if (UniOSCUtils.IsMouseUpInArea(area))
            {
                UniOSCUtils.SelectObjectInHierachyFromGUID(obj.my_guid);
            }
            _StartDrag(area, obj);
            EditorGUILayout.EndHorizontal();

            #endregion Path

            #region Button
            _OnGUI_DrawButtons(obj, false);
            #endregion Button

            #region ScrollView

            obj.scrollposInspector = EditorGUILayout.BeginScrollView(obj.scrollposInspector, GUILayout.ExpandHeight(true), GUILayout.Width(screenWidth));            //GUILayout.Width (100)

            EditorGUILayout.BeginHorizontal();
            //Hardcoding layout for wrapping
            maxCols = (int)Math.Max(minCols, Math.Floor((screenWidth - 20f) / (UniOSCMappingItem.MAXWIDTH * 1.0f)));          //Floor Ceiling
            try{
                for (var i = 0; i < obj.oscMappingItemList.Count; i++)
                {
                    // Begin new row?
                    if (i % maxCols == 0 && i > 0)
                    {
                        GUILayout.EndHorizontal();
                        GUILayout.BeginHorizontal();
                    }

                    UniOSCMappingItem c = obj.oscMappingItemList[i];
                    UniOSCMappingItemEditor.OnGUI_Inspector(c);
                }                //for
            }catch (Exception) {
                //drag&drop Exception handling
            }



            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndScrollView();
            #endregion ScrollView


            EditorGUILayout.EndVertical();
        }
        public static void OnGUI_Inspector(UniOSCMappingItem obj)
        {
            EditorGUI.BeginChangeCheck();

            EditorGUILayout.BeginVertical(GUI.skin.box, GUILayout.Width(UniOSCMappingItem.MAXWIDTH), GUILayout.Height(obj.collapsed ? UniOSCMappingItem.MAXHEIGTH : 10f));
            //EditorGUIUtility.LookLikeControls(UniOSCMappingItem.MAXWIDTH *0.35f,UniOSCMappingItem.MAXWIDTH *0.55f);
            EditorGUIUtility.labelWidth = UniOSCMappingItem.MAXWIDTH * 0.35f;
            EditorGUIUtility.fieldWidth = UniOSCMappingItem.MAXWIDTH * 0.55f;

            #region IN
            if (obj.hostObj.IsLearning)
            {
                obj.collapsed = true;
            }

            EditorGUILayout.BeginHorizontal();

            obj.collapsed = EditorGUILayout.Foldout(obj.collapsed, "OSC Mapping Item " + (obj.collapsed ? "" : obj.address));
            EditorGUILayout.EndHorizontal();
            #endregion IN

            if (obj.collapsed)
            {
                obj.address = EditorGUILayout.TextField(new GUIContent("OSC Address:", "Tooltip for Address"), obj.address);

                #region DataMinMax
                GUILayout.Label(new GUIContent("Data Range:", "The range where the incomming date is in. Most of the times this is in normalized range of 0-1."));

                EditorGUILayout.BeginHorizontal();
                //EditorGUIUtility.LookLikeControls(40f,50f);
                EditorGUIUtility.labelWidth = 40f;
                EditorGUIUtility.fieldWidth = 50f;

                obj.min = EditorGUILayout.FloatField(new GUIContent("Min:", "Minimal value"), obj.min);
                GUILayout.FlexibleSpace();
                obj.max = EditorGUILayout.FloatField(new GUIContent("Max:", "Maximal value"), obj.max);
                EditorGUILayout.EndHorizontal();
                #endregion DataMinMax

                #region DataMapping
                GUILayout.Label(new GUIContent("Data Mapping:", "The incomming data from the OSC Message is changed and mapped from the min-max data range to the min-max mapping range.If you don't want any mapping these values should match with the 'Data Range' above."));

                EditorGUILayout.BeginHorizontal();
                //EditorGUIUtility.LookLikeControls(40f,50f);
                EditorGUIUtility.labelWidth = 40f;
                EditorGUIUtility.fieldWidth = 50f;

                obj.mappingMIN = EditorGUILayout.FloatField(new GUIContent("Min:", "Minimal Mapping value"), obj.mappingMIN);
                GUILayout.FlexibleSpace();
                obj.mappingMAX = EditorGUILayout.FloatField(new GUIContent("Max:", "Maximal Mapping value"), obj.mappingMAX);
                EditorGUILayout.EndHorizontal();
                #endregion DataMapping

                GUILayout.Space(10f);

                #region Bottom
                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.BeginVertical(GUILayout.Height(40f));

                #region Delete
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Delete", GUILayout.Width(100)))
                {
                    bool deleteDispatcher = EditorUtility.DisplayDialog("Delete", "Do you want to delete this OSCMappingItem with the Address:\n" + obj.address + " ?", "OK", "Cancel");
                    if (deleteDispatcher)
                    {
                        obj.OnOSCMappingItemDelete();
                    }
                }
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndHorizontal();
                #endregion Delete

                EditorGUILayout.EndVertical();

                #region Learn

                if (obj.hostObj.IsLearning)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();

                    if (obj.hostObj.IsLearning)
                    {
                        if (GUILayout.RepeatButton(new GUIContent("Learn", "Press to Learn OSC"), GUILayout.Width(80f), GUILayout.Height(40f)))
                        {
                            obj.isLearning = true;
                        }
                        else
                        {
                            obj.isLearning = false;
                        }
                    }
                    GUILayout.FlexibleSpace();
                    EditorGUILayout.EndHorizontal();
                }

                #endregion Learn


                EditorGUILayout.EndHorizontal();
                #endregion Bottom
            }            //collapsed

            EditorGUILayout.EndVertical();

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(obj.hostObj);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds a new OSC Mapping item.
        /// </summary>
        public void AddOSCMappingItem()
        {
            UniOSCMappingItem newOSCDD = new UniOSCMappingItem(this);

            oscMappingItemList.Add(newOSCDD);
        }
Ejemplo n.º 8
0
        void _Update()
        {
            //should I only dequeue one item per frame?
            lock (_eventLock){
                while (_eventQueue.Count > 0)
                {
                    UniOSCEventArgs args = _eventQueue.Dequeue();
                    //Event for the Editor.
                    if (OSCMessageReceivedRaw != null)
                    {
                        OSCMessageReceivedRaw(this, args);
                    }

                    //in learning mode we don't propagate the event to our targets.
                    if (isOSCLearning || !dispatchOSC)
                    {
                        continue;
                    }
                                        #if UNITY_EDITOR
                    if (!Application.isPlaying && !isEditorEnabled)
                    {
                        continue;
                    }
                                        #endif
                    //If no MapingFileObj is attached all messages are passed through without any mapping
                    if (!hasOSCMappingFileAttached)
                    {
                        if (OSCMessageReceived != null)
                        {
                            OSCMessageReceived(this, args);
                        }
                        continue;
                    }

                    foreach (var mfo in oscMappingFileObjList)
                    {
                        if (mfo == null)
                        {
                            continue;
                        }

                        UniOSCMappingItem m = mfo.oscMappingItemList.Find(mi => mi.address == args.Address);
                        if (m == null)
                        {
                            if (OSCMessageReceived != null)
                            {
                                OSCMessageReceived(this, args);
                            }
                        }
                        else
                        {
                            m.MapData(args);
                            if (OSCMessageReceived != null)
                            {
                                OSCMessageReceived(this, args);
                            }
                            //only one message with a certain value is used
                            break;
                        }
                    }    //for mfo
                }
            }            //lock

            if (!hasOSCSessionFileAttached)
            {
                return;
            }
            lock (_sessionEventLock){
                sessionCounter = 16;
                while (_sessionEventQueue.Count > 0 && sessionCounter > 0)
                {
                    SendOSCMessage(this, _sessionEventQueue.Dequeue());
                    sessionCounter--;
                }
            }            //lock
        }