Example #1
0
        public static bool      Switch(Rect position, string label, bool value)
        {
            AnimFloat af          = null;
            int       id          = EditorGUIUtility.GetControlID("NGEditorGUILayout.Switch".GetHashCode(), FocusType.Keyboard, position);
            bool      mouseIn     = position.Contains(Event.current.mousePosition);
            float     switchWidth = position.height * 2F;

            if (switchWidth < position.width)
            {
                Rect r2 = position;
                r2.xMin += switchWidth;
                Utility.content.text = label;
                EditorGUI.PrefixLabel(r2, id, Utility.content);

                position.width = switchWidth;
            }

            if (mouseIn == true)
            {
                EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
            }

            if (Event.current.type == EventType.MouseMove)
            {
                // Need to check, in rare case, a MouseMove can be triggered without any mouseOverWindow.
                if (EditorWindow.mouseOverWindow != null)
                {
                    EditorWindow.mouseOverWindow.Repaint();
                }
            }
            else if (Event.current.type == EventType.MouseDown)
            {
                if (mouseIn == true)
                {
                    value = !value;

                    GUI.changed = true;

                    if (switchAnimations.TryGetValue(id, out af) == true)
                    {
                        if (value == true)
                        {
                            af.target = 1F;
                        }
                        else
                        {
                            af.target = 0F;
                        }
                    }
                    else
                    {
                        af       = new AnimFloat(0F, EditorWindow.mouseOverWindow.Repaint);
                        af.speed = NGEditorGUILayout.SwitchAnimationSpeed;
                        switchAnimations.Add(id, af);

                        if (value == true)
                        {
                            af.value  = 0F;
                            af.target = 1F;
                        }
                        else
                        {
                            af.value  = 1F;
                            af.target = 0F;
                        }
                    }

                    Event.current.Use();
                }
            }

            float radius = position.height * .5F - 1F;

            if (radius < 3F)
            {
                radius = 3F;
            }

            position.xMin += radius + 1F + 4F;
            position.xMax -= radius + 1F;
            position.y    += radius + 1F;

            Color color;
            float xOffset;

            if (af == null && switchAnimations.TryGetValue(id, out af) == true)
            {
                if (af.isAnimating == false)
                {
                    if (value == false)
                    {
                        color   = NGEditorGUILayout.FalseColor;
                        xOffset = 0F;
                    }
                    else
                    {
                        color   = NGEditorGUILayout.TrueColor;
                        xOffset = position.width;
                    }

                    switchAnimations.Remove(id);
                }
                else
                {
                    color   = Color.Lerp(NGEditorGUILayout.FalseColor, NGEditorGUILayout.TrueColor, af.value);
                    xOffset = Mathf.Lerp(0F, position.width, af.value);
                }
            }
            else
            {
                if (value == false)
                {
                    color   = NGEditorGUILayout.FalseColor;
                    xOffset = 0F;
                }
                else
                {
                    color   = NGEditorGUILayout.TrueColor;
                    xOffset = position.width;
                }
            }

            using (HandlesColorRestorer.Get(color))
            {
                Handles.BeginGUI();
                Handles.DrawSolidDisc(new Vector3(position.x + xOffset, position.y, 0F), new Vector3(0F, 0F, 1F), radius - 2F);
                Handles.DrawWireArc(new Vector3(position.x, position.y), Vector3.forward, Vector3.up, 180F, radius);
                Handles.DrawWireArc(new Vector3(position.x + position.width, position.y), Vector3.forward, Vector3.down, 180F, radius);
                Handles.DrawLine(new Vector3(position.x, position.y + radius), new Vector3(position.x + position.width, position.y + radius));
                Handles.DrawLine(new Vector3(position.x, position.y - radius + 1F), new Vector3(position.x + position.width, position.y - radius + 1F));
                Handles.EndGUI();
            }

            return(value);
        }
Example #2
0
 public static HandlesColorRestorer      Get(bool condition, Color color)
 {
     return(condition ? HandlesColorRestorer.Get(color) : null);
 }