private void RenderSourceEventSelector(CordCircuit circuit, Rect SourceUnityEventRect)
        {
            if (circuit.SourceMonoBehaviourTypeName != null)
            {
                EventSelectionEditorModule <CordCircuit> SourceESEM
                    = RetrieveSourceESEM(circuit);

                EnsureSourceEventSelectionIsUpdated(circuit, SourceESEM);
                circuit.SourceUnityEventName
                    = RenderComplexSelectionAndGenerateNewSelectionName(
                          circuit,
                          SourceESEM,
                          circuit.SourceUnityEventName,
                          SourceEventLabelString,
                          SourceUnityEventRect
                          );
                //Debug.Log("SourceMonoBehaviourTypeName was not null");
            }
            else
            {
                string SourceEventName = circuit.SourceUnityEventName;
                circuit.SourceUnityEventName
                    = EditorGUI.TextField(
                          SourceUnityEventRect,
                          SourceEventLabel,
                          SourceEventName
                          );
                //Debug.Log("SourceMonoBehaviourTypeName was null");
            }
        }
        private void MaybeRenderRegistryKeySelector(CordCircuit circuit, Rect rect)
        {
            if (Switchboard.RegistryKeyType == null)
            {
                return;
            }

            Type EnumType = Switchboard.RegistryKeyType;
            int  oldKey   = circuit.SourceObjectRegistryKey;

            string[] EnumNames = Enum.GetNames(EnumType);
            int      oldIndex  = ConvertKeyToIndex(oldKey, EnumType);
            int      newIndex
                = EditorGUI.Popup(
                      rect,
                      SourceKeyLabelString,
                      oldIndex,
                      EnumNames
                      );

            if (oldIndex != newIndex)
            {
                circuit.SourceObjectRegistryKey
                    = ConvertIndexToKey(newIndex, EnumType);
                var SourceESEM
                    = RetrieveSourceESEM(circuit);
                EnsureSourceEventSelectionIsUpdated(circuit, SourceESEM);
            }
        }
 private void RenderRearJackNamePopupSelector(CordCircuit circuit, Rect RearJackNameRect)
 {
     if (circuit.RearJackType == EventSwitchboard.JackType.Method)
     {
         circuit.RearJackName
             = RenderComplexSelectionAndGenerateNewSelectionName(
                   circuit,
                   TargetMSEM,
                   circuit.RearJackName,
                   RearJackNameLabelString,
                   RearJackNameRect
                   );
     }
     else if (circuit.RearJackType == EventSwitchboard.JackType.Event)
     {
         circuit.RearJackName
             = RenderComplexSelectionAndGenerateNewSelectionName(
                   circuit,
                   TargetESEM,
                   circuit.RearJackName,
                   RearJackNameLabelString,
                   RearJackNameRect
                   );
     }
     else
     {
         RenderRearJackNameBackupSelector(circuit, RearJackNameRect);
     }
 }
        private void RenderRearJackNameBackupSelector(CordCircuit circuit, Rect RearJackNameRect)
        {
            string RearJackName = circuit.RearJackName;

            circuit.RearJackName
                = EditorGUI.TextField(
                      RearJackNameRect,
                      RearJackNameLabel,
                      RearJackName
                      );
        }
 private void RenderRearJackNameSelector(CordCircuit circuit, Rect RearJackNameRect)
 {
     if (Switchboard.Target != null)
     {
         RenderRearJackNamePopupSelector(circuit, RearJackNameRect);
     }
     else
     {
         RenderRearJackNameBackupSelector(circuit, RearJackNameRect);
     }
 }
 private void RenderRearJackTypeSelector(CordCircuit circuit, Rect RearJackTypeRect)
 {
     EventSwitchboard.JackType OldJackType = circuit.RearJackType;
     circuit.RearJackType
         = (EventSwitchboard.JackType)
           EditorGUI.EnumPopup(RearJackTypeRect, RearJackTypeLabel, OldJackType);
     if (OldJackType != circuit.RearJackType)
     {
         ToggleSelectionModule(circuit);
     }
 }
        private void ReorderableListRenderCircuitEntry(Rect rect, int index, bool isActive, bool isFocused)
        {
            CordCircuit circuit = Switchboard.CordCircuits[index];

            Debug.Assert(reorderableList != null);
            Debug.Assert(reorderableList.serializedProperty != null);

            string CircuitName = CreateCircuitLabelString(index, circuit);

            SerializedProperty CircuitEntryProperty
                = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
            bool IsExpanded = CircuitEntryProperty.isExpanded;

            if (IsExpanded)
            {
                Rect FoldoutRect          = GeneratePropertyRect(rect, 0);
                Rect RegistryKeyRect      = GeneratePropertyRect(rect, 1);
                Rect SourceUnityEventRect = GeneratePropertyRect(rect, 2);
                Rect RearJackTypeRect     = GeneratePropertyRect(rect, 3);
                Rect RearJackNameRect     = GeneratePropertyRect(rect, 4);
                rect.min = FoldoutRect.min;
                rect.max = RearJackNameRect.max;
                //Debug.Log("Calculated total rect: " + rect.ToString());
                //Debug.Log("Calculated individual rects: "
                //    + PrintRects(FoldoutRect,
                //                 RegistryKeyRect,
                //                 SourceUnityEventRect,
                //                 RearJackTypeRect,
                //                 RearJackNameRect));

                IsExpanded = EditorGUI.Foldout(FoldoutRect, IsExpanded, CircuitName);
                MaybeRenderRegistryKeySelector(circuit, RegistryKeyRect);
                RenderSourceEventSelector(circuit, SourceUnityEventRect);
                RenderRearJackTypeSelector(circuit, RearJackTypeRect);
                RenderRearJackNameSelector(circuit, RearJackNameRect);
            }
            else
            {
                IsExpanded = EditorGUI.Foldout(rect, IsExpanded, CircuitName);
            }

            // Do expand change
            CircuitEntryProperty.isExpanded = IsExpanded;
            //bool WasExpanded = ExpandedCircuits.Contains(circuit);
            //if (IsExpanded && !WasExpanded)
            //{
            //    ExpandedCircuits.Add(circuit);
            //}
            //else if (!IsExpanded && WasExpanded)
            //{
            //    ExpandedCircuits.Remove(circuit);
            //}
        }
        private EventSelectionEditorModule <CordCircuit> RetrieveSourceESEM(CordCircuit circuit)
        {
            if (!UIElementSourceESEMs
                .ContainsKey(circuit.SourceObjectRegistryKey))
            {
                int NewKey = circuit.SourceObjectRegistryKey;
                var NewSourceESEM
                    = new EventSelectionEditorModule <CordCircuit>();
                UIElementSourceESEMs.Add(NewKey, NewSourceESEM);
            }

            return(UIElementSourceESEMs[circuit.SourceObjectRegistryKey]);
        }
        private void AddCordCircuit(EventSwitchboard switchboard)
        {
            CordCircuit NewEntry = new CordCircuit();

            if (switchboard.CordCircuits == null)
            {
                Debug.Log("CordCircuits null - creating list");
                switchboard.CordCircuits
                    = new List <EventSwitchboard.CordCircuit>();
            }
            switchboard.CordCircuits.Add(NewEntry);
            NewEntry.RearJackType = EventSwitchboard.JackType.Method;
            //TargetMSEM.SetMemberSelection(NewEntry, DefaultMethodIndex);
            EditorUtility.SetDirty(target);
        }
        private static string CreateCircuitLabelString(int index, CordCircuit circuit)
        {
            string CircuitName = index.ToString();

            if (circuit.SourceUnityEventName != null &&
                circuit.RearJackName != null)
            {
                CircuitName  = circuit.SourceUnityEventName;
                CircuitName += " => ";
                CircuitName += circuit.RearJackName;
                CircuitName += " (";
                CircuitName += circuit.RearJackType;
                CircuitName += ")";
            }

            return(CircuitName);
        }
        RenderComplexSelectionAndGenerateNewSelectionName
            (CordCircuit circuit,
            ComplexSelectionEditorModule <CordCircuit> selectionModule,
            string previousSelectionName,
            string labelString,
            Rect position)
        {
            // RetrieveEntrysMemberSelection rebuilds SelectionNames
            // as a side-effect, so must be done first
            int Selection
                = selectionModule.RetrieveEntrysMemberSelection(
                      circuit,
                      previousSelectionName
                      );


            string[] MemberNames = selectionModule.SelectionNames.ToArray();
            int      NewSelection
                = EditorGUI.Popup(
                      position,
                      labelString,
                      Selection,
                      MemberNames
                      );

            if (Selection != NewSelection)
            {
                //Debug.Log("Selection changed to " + NewSelection
                //        + " from " + Selection);
                selectionModule.SetMemberSelection(circuit, NewSelection);
            }

            if (selectionModule.SelectionNames != null &&
                selectionModule.SelectionNames.Count > 0)
            {
                return(selectionModule.SelectionNames[NewSelection]);
            }
            else
            {
                return(null);
            }
        }
        RemoveFromSelectionModules
            (CordCircuit itemToDelete)
        {
            var SourceESEM
                = RetrieveSourceESEM(itemToDelete);

            SourceESEM.RemoveEntry(itemToDelete);
            switch (itemToDelete.RearJackType)
            {
            case EventSwitchboard.JackType.Event:
                TargetESEM.RemoveEntry(itemToDelete); break;

            case EventSwitchboard.JackType.Method:
                TargetMSEM.RemoveEntry(itemToDelete); break;

            default:
                throw new UnexpectedEnumValueException
                      <EventSwitchboard.JackType>
                          (itemToDelete.RearJackType);
            }
        }
        private void ToggleSelectionModule(CordCircuit itemToChange)
        {
            // Toggles just the selection modules,
            // after the jack type has been changed
            switch (itemToChange.RearJackType)
            {
            case EventSwitchboard.JackType.Event:
                TargetMSEM.RemoveEntry(itemToChange);
                TargetESEM.SetMemberSelection(itemToChange, DefaultMethodIndex);
                break;

            case EventSwitchboard.JackType.Method:
                TargetESEM.RemoveEntry(itemToChange);
                TargetMSEM.SetMemberSelection(itemToChange, DefaultMethodIndex);
                break;

            default:
                throw new UnexpectedEnumValueException
                      <EventSwitchboard.JackType>
                          (itemToChange.RearJackType);
            }
        }
        EnsureSourceEventSelectionIsUpdated
            (CordCircuit circuit,
            EventSelectionEditorModule <CordCircuit> SourceESEM)
        {
            if (Switchboard.RegistryKeyType == typeof(UIElements) &&
                ((UIElements)circuit.SourceObjectRegistryKey)
                .ManagerClass() != null)
            {
                UIElements CircuitElement
                    = (UIElements)circuit.SourceObjectRegistryKey;

                circuit.SourceMonoBehaviourTypeName
                    = CircuitElement.ManagerClass().ToString();

                UpdateSourceSelectionModule(CircuitElement.ManagerClass(),
                                            SourceESEM);

                //Debug.Log("Updated SourceESEM using "
                //        + CircuitElement.ManagerClass().ToString());
            }
            else
            {
                string SourceMonoName = circuit.SourceMonoBehaviourTypeName;
                circuit.SourceMonoBehaviourTypeName
                    = EditorGUILayout.TextField(SourceMonoLabel, SourceMonoName);

                Type SourceMonoType = SourceMonoName == null
                                    ? null
                                    : Type.GetType(SourceMonoName);
                UpdateSourceSelectionModule(SourceMonoType, SourceESEM);

                //Debug.Log("Updated SourceESEM using "
                //        + SourceMonoName);
            }
            //SourceEventSelectionLabels = SourceESEM.SelectionNames;
        }
        //private void
        //RenderCordCircuitList
        //(EventSwitchboard switchboard, List<string> method_names)
        //{
        //    if (switchboard.CordCircuits != null)
        //    {
        //        for (int CircuitIndex = 0;
        //            CircuitIndex < switchboard.CordCircuits.Count;
        //            CircuitIndex++)
        //        {
        //            CordCircuit Circuit
        //                = switchboard.CordCircuits[CircuitIndex];
        //            bool ShouldDeleteElement
        //                = RenderCircuitEntry(Circuit, CircuitIndex);
        //            if (ShouldDeleteElement)
        //            {
        //                CircuitsToDelete.Push(Circuit);
        //            }
        //        }
        //    }
        //    while (CircuitsToDelete.Count > 0)
        //    {
        //        CordCircuit ItemToDelete = CircuitsToDelete.Pop();
        //        DeleteCircuit(switchboard, ItemToDelete);
        //    }
        //    EditorGUI.indentLevel--;

        //    // static call to EventSelectionEditorModule<CordCircuit>,
        //    // but this is shorter
        //    bool ShouldAddEntry = TargetMSEM.RenderListButtons();
        //    if (ShouldAddEntry)
        //    {
        //        AddCordCircuit(switchboard);
        //    }
        //}

        private void DeleteCircuit(EventSwitchboard switchboard, CordCircuit ItemToDelete)
        {
            switchboard.CordCircuits.Remove(ItemToDelete);
            RemoveFromSelectionModules(ItemToDelete);
            EditorUtility.SetDirty(target);
        }