Ejemplo n.º 1
0
        public void DrawAxis(Rect windowRect)
        {
            // Graph rect
            Rect graphRect = GUILayoutUtility.GetAspectRect((float)Screen.width / (float)Screen.height);

            // Axis value
            GUI.Label(new Rect(graphRect.x, graphRect.y, graphRect.width, 20),
                      "  <color=#808080><b>" + Axis.OutputValue.ToString("0.00") + "</b></color>",
                      new GUIStyle(Elements.Labels.Default)
            {
                richText = true, alignment = TextAnchor.MiddleLeft
            });

            // Screen size
            GUI.Label(new Rect(graphRect.x, graphRect.y + graphRect.height - 20, graphRect.width, 20),
                      "  <color=#808080><b>" + Screen.width + " / " + Screen.height + "</b></color>",
                      new GUIStyle(Elements.Labels.Default)
            {
                richText = true, alignment = TextAnchor.MiddleCenter
            });

            // Screen frame
            Util.DrawRect(graphRect, Color.gray);

            float mouse_pos   = Axis.Axis == global::Axis.X ? UnityEngine.Input.mousePosition.x : UnityEngine.Input.mousePosition.y;
            float screen_size = Axis.Axis == global::Axis.X ? Screen.width : Screen.height;
            float range_size  = Axis.Range == 0 ? 1 : screen_size * Axis.Range / 2f;
            float center      = screen_size / 2f + screen_size / 2f * Axis.Center;

            if (Axis.Axis == global::Axis.X)
            {
                // Draw range frame
                float frame_left  = Mathf.Clamp(graphRect.width * ((center - range_size) / screen_size), 0, graphRect.width);
                float frame_right = Mathf.Clamp(graphRect.width * ((center + range_size) / screen_size), 0, graphRect.width);
                Util.DrawRect(new Rect(
                                  graphRect.x + frame_left,
                                  graphRect.y,
                                  frame_right - frame_left,
                                  graphRect.height),
                              Color.white);

                // Draw center line
                Util.FillRect(new Rect(
                                  graphRect.x + graphRect.width * (center / screen_size),
                                  graphRect.y,
                                  1,
                                  graphRect.height),
                              Color.white);

                // Draw mouse position
                float line_pos = Mathf.Clamp((mouse_pos / screen_size), 0, 1);
                Util.FillRect(new Rect(
                                  graphRect.x + graphRect.width * line_pos,
                                  graphRect.y,
                                  1,
                                  graphRect.height),
                              Color.yellow);
            }
            else
            {
                // Draw range frame
                float frame_top    = Mathf.Clamp(graphRect.height * ((screen_size - center - range_size) / screen_size), 0, graphRect.height);
                float frame_bottom = Mathf.Clamp(graphRect.height * ((screen_size - center + range_size) / screen_size), 0, graphRect.height);
                Util.DrawRect(new Rect(
                                  graphRect.x,
                                  graphRect.y + frame_top,
                                  graphRect.width,
                                  frame_bottom - frame_top),
                              Color.white);

                // Draw center line
                Util.FillRect(new Rect(
                                  graphRect.x,
                                  graphRect.y + graphRect.height * ((screen_size - center) / screen_size),
                                  graphRect.width,
                                  1),
                              Color.white);

                // Draw mouse position
                float line_pos = Mathf.Clamp(((screen_size - mouse_pos) / screen_size), 0, 1);
                Util.FillRect(new Rect(
                                  graphRect.x,
                                  graphRect.y + graphRect.height * line_pos,
                                  graphRect.width,
                                  1),
                              Color.yellow);
            }

            // Draw axis toggles
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("X", Axis.Axis == global::Axis.X ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(80)))
            {
                Axis.Axis = global::Axis.X;
            }
            if (GUILayout.Button("Y", Axis.Axis == global::Axis.Y ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(80)))
            {
                Axis.Axis = global::Axis.Y;
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            // Draw sliders
            Axis.Center = Util.DrawSlider("Center", Axis.Center, -1, 1, center_string, out center_string);
            Axis.Range  = Util.DrawSlider("Range", Axis.Range, 0, 1, range_string, out range_string);
        }
Ejemplo n.º 2
0
        public void DrawAxis(Rect windowRect)
        {
            // Draw graph
            Rect graphRect = new Rect(
                GUI.skin.window.padding.left,
                GUI.skin.window.padding.top + 36,
                windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right,
                20);

            GUILayout.Label("  <color=#808080><b>" + Axis.OutputValue.ToString("0.00") + "</b></color>",
                            new GUIStyle(Elements.Labels.Default)
            {
                richText = true, alignment = TextAnchor.MiddleLeft
            },
                            GUILayout.Height(20));

            Util.DrawRect(graphRect, Color.gray);
            Util.FillRect(new Rect(
                              graphRect.x + graphRect.width / 2,
                              graphRect.y,
                              1,
                              graphRect.height),
                          Color.gray);

            Util.FillRect(new Rect(
                              graphRect.x + graphRect.width / 2 + graphRect.width / 2 * Axis.OutputValue,
                              graphRect.y,
                              1,
                              graphRect.height),
                          Color.yellow);

            // Draw key mappers
            GUILayout.BeginHorizontal();

            GUILayout.Button(new GUIContent(Axis.NegativeBind != null ? Axis.NegativeBind.Name : "None", "Key Mapper Negative"), Elements.Buttons.Red);
            if (GUI.tooltip == "Key Mapper Negative")
            {
                foreach (KeyValuePair <Guid, Controller> entry in Controller.Devices)
                {
                    foreach (Button b in entry.Value.Buttons)
                    {
                        if (b.IsDown)
                        {
                            Axis.NegativeBind = b;
                        }
                    }
                }
                foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
                {
                    if (key >= KeyCode.JoystickButton0 && key <= KeyCode.Joystick8Button19)
                    {
                        continue;
                    }
                    if (UnityEngine.Input.GetKey(key))
                    {
                        Axis.NegativeBind = key == KeyCode.Backspace ? null : new Key(key);
                        break;
                    }
                }
            }
            GUILayout.Button(new GUIContent(Axis.PositiveBind != null ? Axis.PositiveBind.Name : "None", "Key Mapper Positive"), Elements.Buttons.Red);
            if (GUI.tooltip == "Key Mapper Positive")
            {
                foreach (KeyValuePair <Guid, Controller> entry in Controller.Devices)
                {
                    foreach (Button b in entry.Value.Buttons)
                    {
                        if (b.IsDown)
                        {
                            Axis.PositiveBind = b;
                        }
                    }
                }
                foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
                {
                    if (key >= KeyCode.JoystickButton0 && key <= KeyCode.Joystick8Button19)
                    {
                        continue;
                    }
                    if (UnityEngine.Input.GetKey(key))
                    {
                        Axis.PositiveBind = key == KeyCode.Backspace ? null : new Key(key);
                        break;
                    }
                }
            }

            GUILayout.EndHorizontal();

            // Draw Sensitivity slider
            Axis.Sensitivity = Util.DrawSlider("Sensitivity", Axis.Sensitivity, 0, 10, sens_string, out sens_string);

            // Draw Gravity slider
            Axis.Gravity = Util.DrawSlider("Gravity", Axis.Gravity, 0, 10, grav_string, out grav_string);

            // Draw Momentum slider
            Axis.Momentum = Util.DrawSlider("Momentum", Axis.Momentum, 0, 10, momn_string, out momn_string);

            // Draw toggles
            GUILayout.BeginHorizontal();

            Axis.Raw = GUILayout.Toggle(Axis.Raw, "",
                                        Util.ToggleStyle,
                                        GUILayout.Width(20),
                                        GUILayout.Height(20));

            GUILayout.Label("Raw ",
                            new GUIStyle(Elements.Labels.Default)
            {
                margin = new RectOffset(0, 0, 14, 0)
            });

            Axis.Snap = GUILayout.Toggle(Axis.Snap, "",
                                         Util.ToggleStyle,
                                         GUILayout.Width(20),
                                         GUILayout.Height(20));

            GUILayout.Label("Snap ",
                            new GUIStyle(Elements.Labels.Default)
            {
                margin = new RectOffset(0, 0, 14, 0)
            });

            GUILayout.EndHorizontal();

            // Set notes
            note = "<color=#FFFF00><b>Device disconnected</b></color>";
            var disconnected = false;

            if (Axis.PositiveBind != null && !Axis.PositiveBind.Connected)
            {
                disconnected = true;
                note        += "\n'" + Axis.PositiveBind.Name + "' is not connected.";
            }
            if (Axis.NegativeBind != null && !Axis.NegativeBind.Connected)
            {
                disconnected = true;
                note        += "\n'" + Axis.NegativeBind.Name + "' is not connected.";
            }
            if (!disconnected)
            {
                note = null;
            }
        }
        public void DrawAxis(Rect windowRect)
        {
            if (!DeviceManager.SDL_Initialized)
            {
#if windows
                GUILayout.Label("<b>Additional library needed</b>\n" +
                                "Controller axis requires SDL2 library to work.\n" +
                                "Press download to install it automatically.\n\n"+
                                "<b>Platform</b>\n" +
                                "You are using Windows version of ACM.\n"+
                                "If you are using some other operating system,\n"+
                                "download the correct version of the mod.");
                if (GUILayout.Button(download_button_text) && !downloading_in_progress && !DeviceManager.SDL_Installed)
                    DeviceManager.InstallSDL();
#elif linux
                GUILayout.Label("<b>Additional library needed</b>\n" +
                                "Controller axis requires SDL2 library to work.\n" +
                                "Run the command below to install it.\n\n"+
                                "<b>Platform</b>\n" +
                                "You are using Linux version of ACM.\n" +
                                "If you are using some other operating system,\n" +
                                "download the correct version of the mod.");
                GUILayout.TextField("sudo apt-get install libsdl2-2.0-0");
#elif osx
                GUILayout.Label("<b>Additional library needed</b>\n" +
                                "Controller axis requires SDL2 library to work.\n" +
                                "Download it at the link below.\n\n"+
                                "<b>Platform</b>\n" +
                                "You are using OSX version of ACM.\n" +
                                "If you are using some other operating system,\n" +
                                "download the correct version of the mod.");
                if (GUILayout.Button("www.libsdl.org/download-2.0.php"))
                    Application.OpenURL("www.libsdl.org/download-2.0.php");
#endif
            }
            else if (Controller.NumDevices == 0)
            {
                note =  "<color=#FFFF00><b>No controllers connected.</b></color>\n"+
                        "Connect a joystick or controller to use this axis.";
            }
            else if (controller_index < 0)
            {
                note = "<color=#FFFF00><b>Associated controller not connected.</b></color>\n" +
                        "The device this axis is bound to is not found.\n"+
                        "\n<b>Device GUID</b>\n" + Axis.GUID;
                if (GUILayout.Button("Use another controller"))
                {
                    controller_index = 0;
                    Axis.GUID = Controller.DeviceList[controller_index];
                }
            }
            else
            {
                error = null;
                note = null;

                controller = Controller.Get(Axis.GUID);
                Axis.Axis %= controller.NumAxes;

                // Graph rect
                graphRect = new Rect(
                    GUI.skin.window.padding.left,
                    GUI.skin.window.padding.top + 36,
                    windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right,
                    windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right);

                // Axis value
                GUI.Label(new Rect(graphRect.x, graphRect.y, graphRect.width, 20),
                        "  <color=#808080><b>"+ Axis.OutputValue.ToString("0.00")+"</b></color>",
                        new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft });

                // Draw drag controls
                if (Axis.OffsetX == 0 && Axis.OffsetY == 0)
                {
                    GUI.Label(new Rect(graphRect.x, graphRect.y + graphRect.height - 20, graphRect.width, 20),
                        "<color=#808080><b>DRAG TO SET OFFSET</b></color>",
                        new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleCenter });
                }
                else
                {
                    GUI.Label(new Rect(graphRect.x, graphRect.y + graphRect.height - 20, (graphRect.width - 16) / 2, 20),
                        "  <color=#808080><b>X: " + Axis.OffsetX.ToString("0.00") + "\tY:" + Axis.OffsetY.ToString("0.00") + "</b></color>",
                        new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft });
                    if (GUI.Button(new Rect(graphRect.x + (graphRect.width - 16) / 2, graphRect.y + graphRect.height - 20, (graphRect.width - 16) / 2, 20),
                            "<color=#808080><b>RESET OFFSET</b></color>",
                            new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleRight }))
                    {
                        Axis.OffsetX = 0;
                        Axis.OffsetY = 0;
                    }
                }

                // Draw graph
                DrawGraph();

                // Listen for drag
                var mousePos = UnityEngine.Input.mousePosition;
                mousePos.y = Screen.height - mousePos.y;
                var drag_handle = new Rect(windowRect.x + graphRect.x, windowRect.y + graphRect.y, graphRect.width, graphRect.height);
                var drag_range = (graphRect.width) / 2f;

                if (!dragging && UnityEngine.Input.GetMouseButtonDown(0) && drag_handle.Contains(mousePos))
                {
                    dragging = true;
                    click_position = mousePos;
                    click_position.x += Axis.OffsetX * drag_range;
                    click_position.y += Axis.OffsetY * drag_range;
                }

                if (dragging)
                {
                    Axis.OffsetX = Mathf.Clamp((click_position.x - mousePos.x) / drag_range, -1f, 1f);
                    Axis.OffsetY = Mathf.Clamp((click_position.y - mousePos.y) / drag_range, -1f, 1f);
                    if (UnityEngine.Input.GetMouseButtonUp(0))
                    {
                        dragging = false;
                    }
                }

                // Draw graph input and frame
                Util.DrawRect(graphRect, Color.gray);
                Util.FillRect(new Rect(
                        graphRect.x + graphRect.width / 2,
                        graphRect.y,
                        1,
                        graphRect.height),
                    Color.gray);
                Util.FillRect(new Rect(
                        graphRect.x,
                        graphRect.y + graphRect.height / 2,
                        graphRect.width,
                        1),
                    Color.gray);

                Util.FillRect(new Rect(
                                  graphRect.x + graphRect.width / 2 + graphRect.width / 2 * Axis.InputValue,
                                  graphRect.y,
                                  1,
                                  graphRect.height),
                         Color.yellow);

                // Draw controller selection
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("<", controller_index > 0 ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(30)) 
                    && controller_index > 0)
                {
                    controller_index--;
                    Axis.GUID = Controller.DeviceList[controller_index];
                }

                GUILayout.Label(controller != null ? controller.Name : "<color=#FF0000>Disconnected controller</color>",
                    new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter });

                if (GUILayout.Button(">", controller_index < Controller.NumDevices - 1 ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(30)) 
                    && controller_index < Controller.NumDevices - 1)
                {
                    controller_index++;
                    Axis.GUID = Controller.DeviceList[controller_index];
                }

                if (controller == null) return;

                GUILayout.EndHorizontal();

                // Draw axis selection
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("<", Axis.Axis > 0 ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(30)) 
                    && Axis.Axis > 0)
                    Axis.Axis--;

                GUILayout.Label(controller.AxisNames[Axis.Axis], new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter });

                if (GUILayout.Button(">", Axis.Axis < Controller.Get(Axis.GUID).NumAxes - 1 ? Elements.Buttons.Default : Elements.Buttons.Disabled, GUILayout.Width(30)) 
                    && Axis.Axis < Controller.Get(Axis.GUID).NumAxes - 1)
                    Axis.Axis++;

                GUILayout.EndHorizontal();

                // Draw Sensitivity slider
                Axis.Sensitivity = Util.DrawSlider("Sensitivity", Axis.Sensitivity, 0, 5, sens_string, out sens_string);

                // Draw Curvature slider
                Axis.Curvature = Util.DrawSlider("Curvaure", Axis.Curvature, 0, 3, curv_string, out curv_string);

                // Draw Deadzone slider
                Axis.Deadzone = Util.DrawSlider("Deadzone", Axis.Deadzone, 0, 0.5f, dead_string, out dead_string);

                GUILayout.BeginHorizontal();

                // Draw Invert toggle
                Axis.Invert = GUILayout.Toggle(Axis.Invert, "",
                    Util.ToggleStyle,
                    GUILayout.Width(20),
                    GUILayout.Height(20));

                GUILayout.Label("Invert",
                    new GUIStyle(Elements.Labels.Default) { margin = new RectOffset(0, 0, 14, 0) });

                // Draw Raw toggle
                Axis.Smooth = GUILayout.Toggle(Axis.Smooth, "",
                    Util.ToggleStyle,
                    GUILayout.Width(20),
                    GUILayout.Height(20));

                GUILayout.Label("Smooth",
                    new GUIStyle(Elements.Labels.Default) { margin = new RectOffset(0, 0, 14, 0) });

                GUILayout.EndHorizontal();
            }
        }