Ejemplo n.º 1
0
        static void InitializeEditModes()
        {
            // First, for every generator definition we find the component type
            generatorComponentLookup = new Dictionary <Type, Type>();
            foreach (var type in ReflectionExtensions.AllNonAbstractClasses)
            {
                if (!ReflectionExtensions.HasBaseClass <ChiselGeneratorComponent>(type))
                {
                    continue;
                }

                // Our generator component needs to inherit from ChiselDefinedGeneratorComponent<DefinitionType>
                //  in the type definition we pass along the definition type we belong to
                var baseType = type.GetGenericBaseClass(typeof(ChiselNodeGeneratorComponent <>));
                if (baseType == null)
                {
                    continue;
                }

                // We get the generic parameters from our type, and get our definition type.
                // we store both in a dictionary so we can find the component type back based on the definition
                var typeParameters = baseType.GetGenericArguments();
                var definitionType = typeParameters[0];
                generatorComponentLookup[definitionType] = type;
            }

            var generatorModeList = new List <ChiselPlacementToolInstance>();

            // Now, we find all BOUNDS placement tools, create a generic class for it, instantiate it, and register it
            foreach (var placementToolType in ReflectionExtensions.AllNonAbstractClasses)
            {
                var baseType = placementToolType.GetGenericBaseClass(typeof(ChiselBoundsPlacementTool <>));
                if (baseType == null)
                {
                    continue;
                }

                // Our definitionType is part of the generic type definition, so we retrieve it
                var typeParameters = baseType.GetGenericArguments();
                var definitionType = typeParameters[0];

                // Using the definition type, we can find the generator Component it belongs to
                if (!generatorComponentLookup.TryGetValue(definitionType, out var componentType))
                {
                    continue;
                }

                string group, toolName;
                var    attributes = placementToolType.GetCustomAttributes(typeof(ChiselPlacementToolAttribute), true);
                if (attributes != null &&
                    attributes.Length > 0)
                {
                    var attribute = attributes[0] as ChiselPlacementToolAttribute;
                    group    = attribute.Group;
                    toolName = attribute.ToolName;
                }
                else
                {
                    group    = ChiselToolGroups.kDefault;
                    toolName = definitionType.Name;
                }

                // Now that we have our placement tool Type, our definition type and our generator component type, we can create a tool instance
                var placementTool         = typeof(ChiselBoundsPlacementToolInstance <, ,>).MakeGenericType(placementToolType, definitionType, componentType);
                var placementToolInstance = (ChiselPlacementToolInstance)Activator.CreateInstance(placementTool, toolName, group);
                if (placementToolInstance == null)
                {
                    continue;
                }

                generatorModeList.Add(placementToolInstance);
            }

            // Now, we find all SHAPE placement tools, create a generic class for it, instantiate it, and register it
            foreach (var placementToolType in ReflectionExtensions.AllNonAbstractClasses)
            {
                var baseType = placementToolType.GetGenericBaseClass(typeof(ChiselShapePlacementTool <>));
                if (baseType == null)
                {
                    continue;
                }

                // Our definitionType is part of the generic type definition, so we retrieve it
                var typeParameters = baseType.GetGenericArguments();
                var definitionType = typeParameters[0];

                // Using the definition type, we can find the generator Component it belongs to
                if (!generatorComponentLookup.TryGetValue(definitionType, out var componentType))
                {
                    continue;
                }

                string group, toolName;
                var    attributes = placementToolType.GetCustomAttributes(typeof(ChiselPlacementToolAttribute), true);
                if (attributes != null &&
                    attributes.Length > 0)
                {
                    var attribute = attributes[0] as ChiselPlacementToolAttribute;
                    group    = attribute.Group;
                    toolName = attribute.ToolName;
                }
                else
                {
                    group    = ChiselToolGroups.kDefault;
                    toolName = definitionType.Name;
                }

                // Now that we have our placement tool Type, our definition type and our generator component type, we can create a tool instance
                var placementTool         = typeof(ChiselShapePlacementToolInstance <, ,>).MakeGenericType(placementToolType, definitionType, componentType);
                var placementToolInstance = (ChiselPlacementToolInstance)Activator.CreateInstance(placementTool, toolName, group);
                if (placementToolInstance == null)
                {
                    continue;
                }

                generatorModeList.Add(placementToolInstance);
            }

            generatorModeList.Sort(delegate(ChiselPlacementToolInstance x, ChiselPlacementToolInstance y)
            {
                int difference = x.Group.CompareTo(y.Group);
                if (difference != 0)
                {
                    return(difference);
                }
                return(x.ToolName.CompareTo(y.ToolName));
            });
            generatorModes = generatorModeList.ToArray();
        }
        static ChiselRectSelection()
        {
            reflectionSucceeded = false;

            unityRectSelectionType = ReflectionExtensions.GetTypeByName("UnityEditor.RectSelection");
            if (unityRectSelectionType == null)
            {
                return;
            }

            unityEnumSelectionType = ReflectionExtensions.GetTypeByName("UnityEditor.RectSelection+SelectionType");
            if (unityEnumSelectionType == null)
            {
                return;
            }

            rectSelectionField = typeof(SceneView).GetField("m_RectSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            if (rectSelectionField == null)
            {
                return;
            }

            rectSelectionIDField = unityRectSelectionType.GetField("s_RectSelectionID", BindingFlags.NonPublic | BindingFlags.Static);
            if (rectSelectionIDField == null)
            {
                return;
            }

            RectSelectionID       = (int)rectSelectionIDField.GetValue(null);
            rectSelectingField    = unityRectSelectionType.GetField("m_RectSelecting", BindingFlags.NonPublic | BindingFlags.Instance);
            selectStartPointField = unityRectSelectionType.GetField("m_SelectStartPoint", BindingFlags.NonPublic | BindingFlags.Instance);
            selectionStartField   = unityRectSelectionType.GetField("m_SelectionStart", BindingFlags.NonPublic | BindingFlags.Instance);
            lastSelectionField    = unityRectSelectionType.GetField("m_LastSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            currentSelectionField = unityRectSelectionType.GetField("m_CurrentSelection", BindingFlags.NonPublic | BindingFlags.Instance);
            selectMousePointField = unityRectSelectionType.GetField("m_SelectMousePoint", BindingFlags.NonPublic | BindingFlags.Instance);

            updateSelectionMethod = unityRectSelectionType.GetMethod("UpdateSelection", BindingFlags.NonPublic | BindingFlags.Static,
                                                                     null,
                                                                     new Type[] {
                typeof(UnityEngine.Object[]),
                typeof(UnityEngine.Object[]),
                unityEnumSelectionType,
                typeof(bool)
            },
                                                                     null);
            selectionTypeAdditive    = Enum.Parse(unityEnumSelectionType, "Additive");
            selectionTypeSubtractive = Enum.Parse(unityEnumSelectionType, "Subtractive");
            selectionTypeNormal      = Enum.Parse(unityEnumSelectionType, "Normal");

            reflectionSucceeded = rectSelectingField != null &&
                                  selectStartPointField != null &&
                                  selectionStartField != null &&
                                  lastSelectionField != null &&
                                  currentSelectionField != null &&
                                  selectMousePointField != null &&
                                  updateSelectionMethod != null &&

                                  selectionTypeAdditive != null &&
                                  selectionTypeSubtractive != null &&
                                  selectionTypeNormal != null;
        }