// Reset function for initializing the controller.
    void Reset()
    {
        // Reset the primary 2D axis.
        m_Primary2DAxis               = new Input2DAxis();
        m_Primary2DAxis.Name          = "Primary 2D Axis";
        m_Primary2DAxis.TouchIncluded = true;
        m_Primary2DAxis.ClickIncluded = true;

        // Reset the trigger.
        m_Trigger                = new Input1DAxis();
        m_Trigger.Name           = "Trigger";
        m_Trigger.ButtonIncluded = true;

        // Reset the grip.
        m_Grip                = new Input1DAxis();
        m_Grip.Name           = "Grip";
        m_Grip.ButtonIncluded = true;

        // Reset the primary button (i.e., sandwich button on the Vive).
        m_PrimaryButton       = new InputButton();
        m_PrimaryButton.Name  = "Primary";
        m_PrimaryButton.Touch = false;
    }
Пример #2
0
    // Draws a 2D axis field.
    void DrawAxis2DField(ref Input2DAxis axis2D, ref Rect position)
    {
        // Prepare the labels and tooltips.
        GUIContent axisLabel  = new GUIContent(axis2D.Name, "The 2D axis used to simulate the XR controller's " + axis2D.Name + ".");
        GUIContent touchLabel = new GUIContent(axis2D.Name + " Touch", "The binary used to simulate touching the XR controller's " + axis2D.Name + ".");
        GUIContent clickLabel = new GUIContent(axis2D.Name + " Click", "The binary used to simulate clicking the XR controller's " + axis2D.Name + ".");

        // Size ratios.
        float size      = 3.6f;
        float touchSize = 0.25f;
        float clickSize = 0.5f;

        // Get the current editor GUI label width.
        float labelWidth = EditorGUIUtility.labelWidth;

        // Set a width for the individual fields.
        float fieldWidth = labelWidth + 70.0f;

        // Calculate the space and position for the 2D axis.
        float axisSpace    = EditorGUIUtility.currentViewWidth - fieldWidth - (VerticalSpace * size);
        float axisPosition = fieldWidth + (axisSpace / 2.0f);

        // Create a rectangle for the 2D axis ranges.
        Rect axis2DRange = new Rect(position);

        axis2DRange.x      = axisPosition;
        axis2DRange.width  = VerticalSpace * size;
        axis2DRange.height = VerticalSpace * size;
        Color axis2DRangeColor;

        // Create a rectangle for the 2D axis value.
        Rect axis2DValue = new Rect(position);

        axis2DValue.x = axis2DRange.center.x + (axis2D.Axis.x * (axis2DRange.width / 2.0f));
        axis2DValue.y = axis2DRange.center.y - (axis2D.Axis.y * (axis2DRange.height / 2.0f));
        Color axis2DValueColor;

        // Create a large green rectangle on a dark gray background if the axis is clicked.
        if (axis2D.Click)
        {
            axis2DValue.x     -= (EditorGUIUtility.standardVerticalSpacing * clickSize * size);
            axis2DValue.y     -= (EditorGUIUtility.standardVerticalSpacing * clickSize * size);
            axis2DValue.width  = EditorGUIUtility.standardVerticalSpacing * clickSize * size * 2.0f;
            axis2DValue.height = EditorGUIUtility.standardVerticalSpacing * clickSize * size * 2.0f;
            axis2DValueColor   = Color.green;
            axis2DRangeColor   = Color.Lerp(Color.gray, Color.black, 0.25f);
        }
        // Create a small yellow rectangle on a light gray background if the axis is touched.
        else if (axis2D.Touch)
        {
            axis2DValue.x     -= (EditorGUIUtility.standardVerticalSpacing * touchSize * size);
            axis2DValue.y     -= (EditorGUIUtility.standardVerticalSpacing * touchSize * size);
            axis2DValue.width  = EditorGUIUtility.standardVerticalSpacing * touchSize * size * 2.0f;
            axis2DValue.height = EditorGUIUtility.standardVerticalSpacing * touchSize * size * 2.0f;
            axis2DValueColor   = Color.yellow;
            axis2DRangeColor   = Color.gray;
        }
        // Otherwise, don't show the value rectangle on the background.
        else
        {
            axis2DValue.width  = 0.0f;
            axis2DValue.height = 0.0f;
            axis2DValueColor   = Color.gray;
            axis2DRangeColor   = Color.gray;
        }

        // Draw the 2D axis.
        Handles.BeginGUI();
        Handles.DrawSolidRectangleWithOutline(axis2DRange, axis2DRangeColor, Color.black);
        Handles.DrawSolidRectangleWithOutline(axis2DValue, axis2DValueColor, axis2DValueColor);
        Handles.EndGUI();

        // Create a horizontal group for the axis label and axis x value.
        EditorGUILayout.BeginHorizontal();
        // Create the axis label.
        EditorGUILayout.LabelField(axisLabel, GUILayout.Width(labelWidth));
        // Create the float field for the 2D axis X value.
        EditorGUIUtility.labelWidth = 10;
        float axisXValue = EditorGUILayout.FloatField("X", float.Parse(axis2D.Axis.x.ToString("F4")));

        EditorGUIUtility.labelWidth = labelWidth;
        // Create a flexible space to draw the 2D axis in.
        GUILayout.FlexibleSpace();
        // End the horizontal group and update the position.
        EditorGUILayout.EndHorizontal();
        position.y += VerticalSpace;

        // Set the lower limit for the axis x value.
        if (axisXValue < -1.0f)
        {
            axisXValue = -1.0f;
        }
        // Set the upper limit for the axis x value.
        if (axisXValue > 1.0f)
        {
            axisXValue = 1.0f;
        }

        // Create a horizontal group for the axis y value.
        EditorGUILayout.BeginHorizontal();
        // Create an empty label.
        EditorGUILayout.LabelField("", GUILayout.Width(labelWidth));
        // Create the float field for the 2D axis y value.
        EditorGUIUtility.labelWidth = 10;
        float axisYValue = EditorGUILayout.FloatField("Y", float.Parse(axis2D.Axis.y.ToString("F4")));

        EditorGUIUtility.labelWidth = labelWidth;
        // Create a flexible space to draw the 2D axis in.
        GUILayout.FlexibleSpace();
        // End the horizontal group and update the position.
        EditorGUILayout.EndHorizontal();
        position.y += VerticalSpace;

        // Set the lower limit for the axis y value.
        if (axisYValue < -1.0f)
        {
            axisYValue = -1.0f;
        }
        // Set the upper limit for the axis y value.
        if (axisYValue > 1.0f)
        {
            axisYValue = 1.0f;
        }

        // Update the 2D axis if the current event is a mouse down or drag event.
        if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag)
        {
            // Get the current mouse position.
            Vector2 mouse2D = Event.current.mousePosition;

            // If the mouse is within the vertical 2D axis range.
            if (axis2DRange.y <= mouse2D.y && mouse2D.y <= axis2DRange.y + axis2DRange.height)
            {
                // If the mouse is within the horizontal 2D axis range.
                if (axis2DRange.x <= mouse2D.x && mouse2D.x <= axis2DRange.x + axis2DRange.width)
                {
                    // If the event is mouse down.
                    if (Event.current.type == EventType.MouseDown)
                    {
                        // Reset the click.
                        axis2D.ClickAxis(false);
                        // At least touching the axis.
                        axis2D.TouchAxis();
                    }
                    // If the 2D axis is touched.
                    if (axis2D.Touch)
                    {
                        // Update the 2D axis value.
                        Vector2 mouse2DValue = new Vector2();
                        mouse2DValue.x = (mouse2D.x - axis2DRange.center.x) / (axis2DRange.width / 2.0f);
                        mouse2DValue.y = (axis2DRange.center.y - mouse2D.y) / (axis2DRange.height / 2.0f);
                        axis2D.TouchAxis(true, mouse2DValue);

                        // If the mouse is inside the 2D axis value.
                        if ((axis2DValue.x <= mouse2D.x && mouse2D.x <= axis2DValue.x + axis2DValue.width) &&
                            (axis2DValue.y <= mouse2D.y && mouse2D.y <= axis2DValue.y + axis2DValue.height))
                        {
                            // If the event is mouse down.
                            if (Event.current.type == EventType.MouseDown)
                            {
                                // Clicking the axis.
                                axis2D.ClickAxis(true, mouse2DValue);
                            }
                        }
                    }
                }
                // If the mouse is outside the horizontal 2D axis range and after the individual fields.
                else if (fieldWidth <= mouse2D.x)
                {
                    // If the event is mouse down.
                    if (Event.current.type == EventType.MouseDown)
                    {
                        // No longer touching the axis.
                        axis2D.TouchAxis(false);
                    }
                }
            }
        }
        else
        {
            // If touching.
            if (axis2D.Touch)
            {
                // Update the 2D axis.
                axis2D.TouchAxis(true, new Vector2(axisXValue, axisYValue));
            }
        }

        // If the touch is used.
        if (axis2D.TouchIncluded)
        {
            // Create the checkbox for the touch state.
            bool touchValue = EditorGUILayout.Toggle(touchLabel, axis2D.Touch, GUILayout.MaxWidth(fieldWidth));
            position.y += VerticalSpace;

            // Check for a change in the touch value.
            if (touchValue != axis2D.Touch)
            {
                // Update touching the axis.
                axis2D.TouchAxis(touchValue, axis2D.Axis);
            }
        }

        // If the click is used.
        if (axis2D.ClickIncluded)
        {
            // Create the checkbox for the click state.
            bool clickValue = EditorGUILayout.Toggle(clickLabel, axis2D.Click, GUILayout.MaxWidth(fieldWidth));
            position.y += VerticalSpace;

            // Check for a change in the click value.
            if (clickValue != axis2D.Click)
            {
                // Update clicking the axis.
                axis2D.ClickAxis(clickValue, axis2D.Axis);
            }
        }
    }