예제 #1
0
        private void DevicePicker(Rect pos, SerializedProperty property, TypeRestrictionAttribute attrib, Component comp, UnityEngine.Object field, TableComponent ta)
        {
            // retrieve selected reference
            MonoBehaviour obj = null;

            if (_component == null)
            {
                if (field != null)
                {
                    obj = ta.GetComponentsInChildren(attrib.Type, true)
                          .FirstOrDefault(s => s == field) as MonoBehaviour;
                    _component = obj;
                }
            }
            else
            {
                obj = _component;
            }

            // render content
            var content = obj == null
                                ? new GUIContent(attrib.NoneLabel)
                                : new GUIContent(obj.name, Icons.ByComponent(obj, IconSize.Small, IconColor.Orange));

            // render drawer
            var id = GUIUtility.GetControlID(FocusType.Keyboard, pos);
            var objectFieldButton = GUI.skin.GetStyle("ObjectFieldButton");
            var suffixButtonPos   = new Rect(pos.xMax - 19f, pos.y + 1, 19f, pos.height - 2);

            EditorGUIUtility.SetIconSize(new Vector2(12f, 12f));

            // handle click
            if (Event.current.type == EventType.MouseDown && pos.Contains(Event.current.mousePosition))
            {
                // click on ping
                if (obj != null && !suffixButtonPos.Contains(Event.current.mousePosition))
                {
                    EditorGUIUtility.PingObject(obj.gameObject);

                    // click on picker
                }
                else
                {
                    _itemPickDropdownState ??= new AdvancedDropdownState();

                    var dropdown = new ItemSearchableDropdownUntyped(
                        attrib.Type,
                        _itemPickDropdownState,
                        ta,
                        attrib.PickerLabel,
                        item => {
                        switch (item)
                        {
                        case null:
                            _component = null;
                            property.objectReferenceValue = null;
                            break;

                        case MonoBehaviour mb:
                            _component = mb;
                            property.objectReferenceValue = mb;
                            break;
                        }
                        property.serializedObject.ApplyModifiedProperties();
                        if (comp is IMainRenderableComponent renderable)
                        {
                            if (attrib.RebuildMeshes)
                            {
                                renderable.RebuildMeshes();
                            }
                            if (attrib.UpdateTransforms)
                            {
                                renderable.UpdateTransforms();
                            }
                        }
                    }
                        );
                    dropdown.Show(pos);
                }
            }
            if (Event.current.type == EventType.Repaint)
            {
                EditorStyles.objectField.Draw(pos, content, id, DragAndDrop.activeControlID == id, pos.Contains(Event.current.mousePosition));
                objectFieldButton.Draw(suffixButtonPos, GUIContent.none, id, DragAndDrop.activeControlID == id, suffixButtonPos.Contains(Event.current.mousePosition));
            }
        }
예제 #2
0
        private void DeviceItemDropdown(Rect pos, SerializedProperty property, TypeRestrictionAttribute attrib, UnityEngine.Object field)
        {
            var deviceItemPropField = property.serializedObject.FindProperty(attrib.DeviceItem);

            var count = 0;
            var firstItemDescription = string.Empty;
            var labels       = Array.Empty <string>();
            var ids          = Array.Empty <string>();
            var currentIndex = 0;

            if (attrib.DeviceType == typeof(ICoilDeviceComponent) && field is ICoilDeviceComponent coilDeviceComponent)
            {
                count = coilDeviceComponent.AvailableCoils.Count();
                firstItemDescription = count > 0 ? coilDeviceComponent.AvailableCoils.First().Description : string.Empty;
                labels       = coilDeviceComponent.AvailableCoils.Select(s => s.Description).ToArray();
                ids          = coilDeviceComponent.AvailableCoils.Select(s => s.Id).ToArray();
                currentIndex = coilDeviceComponent.AvailableCoils.TakeWhile(s => s.Id != deviceItemPropField.stringValue).Count();
            }
            if (attrib.DeviceType == typeof(ISwitchDeviceComponent) && field is ISwitchDeviceComponent switchDeviceComponent)
            {
                count = switchDeviceComponent.AvailableSwitches.Count();
                firstItemDescription = count > 0 ? switchDeviceComponent.AvailableSwitches.First().Description : string.Empty;
                labels       = switchDeviceComponent.AvailableSwitches.Select(s => s.Description).ToArray();
                ids          = switchDeviceComponent.AvailableSwitches.Select(s => s.Id).ToArray();
                currentIndex = switchDeviceComponent.AvailableSwitches.TakeWhile(s => s.Id != deviceItemPropField.stringValue).Count();
            }
            if (attrib.DeviceType == typeof(ILampDeviceComponent) && field is ILampDeviceComponent lampDeviceComponent)
            {
                count = lampDeviceComponent.AvailableLamps.Count();
                firstItemDescription = count > 0 ? lampDeviceComponent.AvailableLamps.First().Description : string.Empty;
                labels       = lampDeviceComponent.AvailableLamps.Select(s => s.Description).ToArray();
                ids          = lampDeviceComponent.AvailableLamps.Select(s => s.Id).ToArray();
                currentIndex = lampDeviceComponent.AvailableLamps.TakeWhile(s => s.Id != deviceItemPropField.stringValue).Count();
            }

            if (field != null && count == 0)
            {
                deviceItemPropField.stringValue = null;
                deviceItemPropField.serializedObject.ApplyModifiedProperties();
                return;
            }

            if (field != null && count == 1)
            {
                deviceItemPropField.stringValue = ids[0];
                deviceItemPropField.serializedObject.ApplyModifiedProperties();
                if (string.IsNullOrEmpty(firstItemDescription))
                {
                    return;
                }
            }
            EditorGUI.BeginChangeCheck();
            var newIndex = EditorGUI.Popup(pos, currentIndex, labels);

            if (EditorGUI.EndChangeCheck() && field != null)
            {
                if (currentIndex != newIndex)
                {
                    deviceItemPropField.stringValue = ids[newIndex];
                    deviceItemPropField.serializedObject.ApplyModifiedProperties();
                }
            }
        }