Example #1
0
        private void NewUnit(Vector2 unitPosition, UnitOptionTree options, Action <IUnit> then = null)
        {
            delayCall += () =>
            {
                var activatorPosition = new Rect(e.mousePosition, new Vector2(200, 1));

                var context = this.context;

                LudiqGUI.FuzzyDropdown
                (
                    activatorPosition,
                    options,
                    null,
                    delegate(object _option)
                {
                    context.BeginEdit();

                    var option = (IUnitOption)_option;
                    var unit   = option.InstantiateUnit();
                    AddUnit(unit, unitPosition);
                    option.PreconfigureUnit(unit);
                    then?.Invoke(unit);
                    GUI.changed = true;

                    context.EndEdit();
                }
                );
            };
        }
        public static FuzzyWindow Open(GameObject[] targets, Rect activatorPosition)
        {
            if (!TransformOperations.WarnRestructurable(targets.Select(go => go.transform).ToArray()))
            {
                return(null);
            }

            // GameObject menu creators change the selection, so we need to cache it
            var selectionSnapshot = Selection.objects;

            LudiqGUI.FuzzyDropdown
            (
                activatorPosition,
                new CreateGameObjectOptionTree("Replace with..."),
                null,
                (_instance) =>
            {
                var template = (GameObject)_instance;

                var allSelected = new HashSet <GameObject>();

                foreach (var target in targets)
                {
                    var selected     = selectionSnapshot.Contains(target);
                    var position     = target.transform.position;
                    var rotation     = target.transform.rotation;
                    var scale        = target.transform.localScale;
                    var parent       = target.transform.parent;
                    var siblingIndex = target.transform.GetSiblingIndex();
                    var scene        = target.scene;

                    Undo.DestroyObjectImmediate(target);
                    var replacement = DuplicateGameObject(template);
                    Undo.MoveGameObjectToScene(replacement, scene, "Move Replacement To Scene");

                    replacement.transform.position = position;
                    replacement.transform.rotation = rotation;

                    if (PeekPlugin.Configuration.preserveScaleOnReplace)
                    {
                        replacement.transform.localScale = scale;
                    }

                    replacement.transform.SetParent(parent, true);
                    replacement.transform.SetSiblingIndex(siblingIndex);

                    if (selected)
                    {
                        allSelected.Add(replacement);
                    }
                }

                Selection.objects = allSelected.ToArray();

                UnityObject.DestroyImmediate(template);
            }
            );

            return(FuzzyWindow.instance);
        }
 private static void CreateMenu(Rect activatorPosition, Action <GameObject> created)
 {
     LudiqGUI.FuzzyDropdown
     (
         activatorPosition,
         new CreateGameObjectOptionTree(),
         null,
         (_instance) => { created?.Invoke((GameObject)_instance); }
     );
 }
Example #4
0
        private void ReplaceUnit()
        {
            var oldUnit      = unit;
            var unitPosition = oldUnit.position;
            var preservation = UnitPreservation.Preserve(oldUnit);

            var options = new UnitOptionTree(new GUIContent("Unit"));

            options.reference = reference;

            var activatorPosition = new Rect(e.mousePosition, new Vector2(200, 1));

            var context = this.context;

            LudiqGUI.FuzzyDropdown
            (
                activatorPosition,
                options,
                null,
                delegate(object _option)
            {
                var option = (IUnitOption)_option;

                context.BeginEdit();
                UndoUtility.RecordEditedObject("Replace Unit");
                var graph = oldUnit.graph;
                oldUnit.graph.units.Remove(oldUnit);
                var newUnit      = option.InstantiateUnit();
                newUnit.guid     = Guid.NewGuid();
                newUnit.position = unitPosition;
                graph.units.Add(newUnit);
                preservation.RestoreTo(newUnit);
                option.PreconfigureUnit(newUnit);
                selection.Select(newUnit);
                GUI.changed = true;
                context.EndEdit();
            }
            );
        }
Example #5
0
        internal static void OnSceneGUI(SceneView sceneView)
        {
            if (!PeekPlugin.Configuration.enableCreator.Display(sceneView.maximized))
            {
                return;
            }

            if (SceneViewIntegration.used)
            {
                return;
            }

            try
            {
                Profiler.BeginSample("Peek." + nameof(Creator));

                var position = sceneView.GetInnerGuiPosition();

                var shortcut = PeekPlugin.Configuration.creatorShortcut;
                var preview  = shortcut.Preview(e);
                var activate = shortcut.Check(e);

                if (position.Contains(e.mousePosition) && (preview || activate))
                {
                    Handles.BeginGUI();

                    var filter = ProbeFilter.@default;
                    filter.proBuilder = false;                     // Too slow and useless here anyway
                    var hit = Probe.Pick(filter, sceneView, e.mousePosition, out var point);

                    if (preview)
                    {
                        var createIndicatorStyle   = LudiqStyles.CommandButton(true, true);
                        var createIndicatorContent = LudiqGUIUtility.TempContent(PeekPlugin.Icons.createGameObjectOptions?[IconSize.Small]);
                        var createIndicatorSize    = createIndicatorStyle.CalcSize(createIndicatorContent);

                        var createIndicatorPosition = new Rect
                                                      (
                            e.mousePosition.x - (createIndicatorSize.x / 2),
                            e.mousePosition.y + Styles.indicatorMargin,
                            createIndicatorSize.x,
                            createIndicatorSize.y
                                                      );

                        GUI.Label
                        (
                            createIndicatorPosition,
                            createIndicatorContent,
                            createIndicatorStyle
                        );
                    }

                    if (activate)
                    {
                        var activatorPosition = new Rect(e.mousePosition, Vector2.zero);
                        activatorPosition.width = 220;
                        activatorPosition       = LudiqGUIUtility.GUIToScreenRect(activatorPosition);

                        // Delay closure allocations
                        var _hit       = hit;
                        var _point     = point;
                        var _sceneView = sceneView;

                        LudiqGUI.FuzzyDropdown
                        (
                            activatorPosition,
                            new CreateGameObjectOptionTree(),
                            null,
                            (_instance) =>
                        {
                            var instance = (GameObject)_instance;

                            var is2D = instance.GetComponent <RectTransform>() != null ||
                                       instance.GetComponent <SpriteRenderer>() != null;

                            if (_hit != null)
                            {
                                instance.transform.SetParent(_hit.Value.transform.parent, true);
                            }

                            instance.transform.position = _point;

                            if (!is2D && PeekPlugin.Configuration.createOnBounds && instance.CalculateBounds(out var bounds, Space.World, true, false, false, false, false))
                            {
                                var difference = _point.y - bounds.min.y;

                                instance.transform.position += difference * Vector3.up;
                            }

                            Selection.activeGameObject = instance;

                            if (_hit == null && !_sceneView.in2DMode)
                            {
                                _sceneView.FrameSelected();
                            }
                        }
                        );

                        FuzzyWindow.instance.Focus();

                        e.Use();
                    }
Example #6
0
        internal static void OnSceneGUI(SceneView sceneView)
        {
            if (!PeekPlugin.Configuration.enableProbe)
            {
                return;
            }

            Profiler.BeginSample("Peek." + nameof(Probe));

            try
            {
                ProgramHighlight(sceneView);

#if PROBUILDER_4_OR_NEWER
                PeekProBuilderIntegration.DrawHighlight(proBuilderHighlight);
#endif

                var shortcut = PeekPlugin.Configuration.probeShortcut;

                // Make sure not to conflict with right-click pan
                shortcut.mouseShortcut.checkRelease         = true;
                shortcut.mouseShortcut.requireStaticRelease = true;

                if (shortcut.Check(e) && !SceneViewIntegration.used)
                {
                    var hits = ListPool <ProbeHit> .New();

                    try
                    {
                        PickAllNonAlloc(hits, ProbeFilter.@default, sceneView, e.mousePosition, PeekPlugin.Configuration.probeLimit);

                        if (hits.Count > 0)
                        {
                            var add = e.shift;

                            var activatorPosition = new Rect(e.mousePosition, Vector2.zero);
                            activatorPosition.width = 220;
                            activatorPosition       = LudiqGUIUtility.GUIToScreenRect(activatorPosition);

                            // Note: Had to make FuzzyWindow use OnMouseUp for select here instead
                            // of OnMouseDown because otherwise escaping the default RectSelection
                            // behaviour with the event order and DefaultControl ID's was... hell.

                            LudiqGUI.FuzzyDropdown
                            (
                                activatorPosition,
                                new ProbeOptionTree(hits),
                                null,
                                (_hit) =>
                            {
                                add    |= e?.shift ?? false;
                                var hit = (ProbeHit)_hit;
                                hit.Select(add);
                            }
                            );

                            FuzzyWindow.instance.Focus();
                            GUIUtility.hotControl = 0;                             // Escape the default RectSelection control
                            e.Use();
                        }
                    }
                    finally
                    {
                        hits.Free();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }

            Profiler.EndSample();
        }
        protected override void AfterCategoryGUI()
        {
            GUILayout.Label(" ", new GUIStyle(GUI.skin.label)
            {
                stretchWidth = true
            });
            var lastRect = GUILayoutUtility.GetLastRect();

            HUMEditor.Vertical().Box(HUMEditorColor.DefaultEditorBackground.Darken(0.1f), Color.black, new RectOffset(4, 4, 4, 4), new RectOffset(1, 1, 1, 1), () =>
            {
                var isGeneric = ((Type)type.value).IsGenericType;

                HUMEditor.Horizontal(() =>
                {
                    GUILayout.Label("Delegate", GUILayout.Width(80));
                    if (GUILayout.Button(((Type)type.value)?.As().CSharpName(false).RemoveHighlights().RemoveMarkdown()))
                    {
                        LudiqGUI.FuzzyDropdown(lastRect, new TypeOptionTree(delegateTypes), type.value, (val) =>
                        {
                            generics.value = new List <GenericDeclaration>();

                            if (type.value != val)
                            {
                                type.value = val;

                                Type[] constraints = null;
                                var _type          = ((Type)type.value);

                                if (((Type)type.value).IsGenericTypeDefinition)
                                {
                                    var generic   = ((Type)type.value)?.GetGenericTypeDefinition();
                                    var _generics = generic?.GetGenericArguments();
                                    if (_type.IsGenericParameter)
                                    {
                                        constraints = ((Type)type.value).GetGenericParameterConstraints();
                                    }

                                    for (int i = 0; i < _generics.Length; i++)
                                    {
                                        var declaration  = new GenericDeclaration();
                                        declaration.name = _generics[i].Name;
                                        if (_type.IsGenericParameter)
                                        {
                                            declaration.constraint.type = constraints[i];
                                        }
                                        ((List <GenericDeclaration>)generics.value).Add(declaration);
                                    }
                                }
                            }
                        });
                    }
                });

                HUMEditor.Vertical(() =>
                {
                    if (isGeneric)
                    {
                        var gen = ((List <GenericDeclaration>)generics.value);

                        for (int i = 0; i < gen.Count; i++)
                        {
                            var index = i;
                            HUMEditor.Horizontal(() =>
                            {
                                GUILayout.Label(string.IsNullOrEmpty(gen[index].name) ? "T" + index.ToString() : gen[index].name);
                                if (GUILayout.Button(gen[index].type.type?.As().CSharpName(false).RemoveHighlights().RemoveMarkdown()))
                                {
                                    LudiqGUI.FuzzyDropdown(lastRect, new TypeOptionTree(types.Where((t) =>
                                    {
                                        return(t.Inherits(gen[index].constraint.type));
                                    })), type.value, (val) =>
                                    {
                                        gen[index].type.type = (Type)val;
                                    });
                                }
                            });
                        }
                    }
                });
            });
        }
Example #8
0
        internal static void OnSceneGUI(SceneView sceneView)
        {
            if (!PeekPlugin.Configuration.enableProbe)
            {
                return;
            }

            Profiler.BeginSample("Peek." + nameof(Probe));

            try
            {
                ProgramHighlight(sceneView);

#if PROBUILDER_4_OR_NEWER
                PeekProBuilderIntegration.DrawHighlight(proBuilderHighlight);
#endif

                if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Alpha0)
                {
                    Selection.activeGameObject = sceneView.camera.gameObject;
                }

                if (e.IsContextMouseButton() && !e.alt && !e.CtrlOrCmd() && !SceneViewIntegration.used)
                {
                    if (e.type == EventType.MouseDown)
                    {
                        pressPosition = e.mousePosition;
                    }
                    else if (e.type == EventType.MouseUp)
                    {
                        if (pressPosition.HasValue)
                        {
                            var pressDistance = Vector2.Distance(e.mousePosition, pressPosition.Value);

                            if (pressDistance <= 1)
                            {
                                var hits = ListPool <ProbeHit> .New();

                                try
                                {
                                    PickAllNonAlloc(hits, ProbeFilter.@default, sceneView, e.mousePosition, PeekPlugin.Configuration.probeLimit);

                                    if (hits.Count > 0)
                                    {
                                        var add = e.shift;

                                        var activatorPosition = new Rect(e.mousePosition, Vector2.zero);
                                        activatorPosition.width = 220;
                                        activatorPosition       = LudiqGUIUtility.GUIToScreenRect(activatorPosition);

                                        // Note: Had to make FuzzyWindow use OnMouseUp for select here instead
                                        // of OnMouseDown because otherwise escaping the default RectSelection
                                        // behaviour with the event order and DefaultControl ID's was... hell.

                                        LudiqGUI.FuzzyDropdown
                                        (
                                            activatorPosition,
                                            new ProbeOptionTree(hits),
                                            null,
                                            (_hit) =>
                                        {
                                            add    |= e?.shift ?? false;
                                            var hit = (ProbeHit)_hit;
                                            hit.Select(add);
                                        }
                                        );

                                        FuzzyWindow.instance.Focus();
                                        GUIUtility.hotControl = 0;                                         // Escape the default RectSelection control
                                        e.Use();
                                    }
                                }
                                finally
                                {
                                    hits.Free();
                                }
                            }

                            pressPosition = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }

            Profiler.EndSample();
        }