Advertisement
Guest User

Setup CInput - Cinput For UFPS

a guest
Jul 5th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class setupcInput : MonoBehaviour {
  5.  
  6.     public GUISkin guiSkin;
  7.     public Color menuColor;
  8.     public bool MenuIsOpen;
  9.     public static bool guiPanelOpen;
  10.  
  11.     Rect windowRect;
  12.     float _wHeight = 5000;
  13.     float _wWidth = 5000;
  14.     bool showPopUp;
  15.     private Vector2 _scrollPosition;
  16.     private float _clickDelay = 0f;
  17.  
  18.     private string label2, box2, popwindow, smallbutton;
  19.  
  20.     void Awake() {
  21.         // cInput setup
  22.         // initialize cInput
  23.         cInput.Init();
  24.         // cInput default keys
  25.         cInput.SetKey ("Interact", Keys.E);
  26.         cInput.SetKey("Move Left", Keys.A, Keys.LeftArrow);
  27.         cInput.SetKey("Move Right", Keys.D, Keys.RightArrow);
  28.         cInput.SetKey("Move Forward", Keys.W, Keys.UpArrow);
  29.         cInput.SetKey("Move Back", Keys.S, Keys.DownArrow);
  30.  
  31.         cInput.SetKey("Look Left", Keys.MouseLeft, Keys.Keypad4);
  32.         cInput.SetKey("Look Right", Keys.MouseRight, Keys.Keypad6);
  33.         cInput.SetKey("Look Up", Keys.MouseUp, Keys.Keypad8);
  34.         cInput.SetKey("Look Down", Keys.MouseDown, Keys.Keypad2);
  35.  
  36.         cInput.SetKey("Attack", Keys.Mouse0, Keys.None);
  37.         cInput.SetKey("Next Weapon", Keys.E, Keys.None);
  38.         cInput.SetKey("Previous Weapon", Keys.Q, Keys.None);
  39.         cInput.SetKey("Clear Weapon", Keys.Backspace, Keys.None);
  40.         cInput.SetKey("Zoom", Keys.Mouse1, Keys.None);
  41.         cInput.SetKey("Reload", Keys.R, Keys.None);
  42.         cInput.SetKey("Jump", Keys.Space, Keys.None);
  43.  
  44.         cInput.SetKey("Crouch", Keys.C, Keys.None);
  45.         cInput.SetKey("Run", Keys.LeftShift, Keys.RightShift);
  46.         cInput.SetKey("Accept 1", Keys.Return, Keys.None);
  47.         cInput.SetKey("Accept 2", Keys.KeypadEnter, Keys.None);
  48.         cInput.SetKey("Pause", Keys.P, Keys.None);
  49.         cInput.SetKey("Menu", Keys.Escape, Keys.None);
  50.         cInput.SetKey("Toggle3rdPerson", Keys.V, Keys.None);
  51.  
  52.         // comment these out (or remove them) if you don't want these to show
  53.         cInput.SetKey("Weapon 1", Keys.Alpha1, Keys.None);
  54.         cInput.SetKey("Weapon 2", Keys.Alpha2, Keys.None);
  55.         cInput.SetKey("Weapon 3", Keys.Alpha3, Keys.None);
  56.         cInput.SetKey("Weapon 4", Keys.Alpha4, Keys.None);
  57.         cInput.SetKey("Weapon 5", Keys.Alpha5, Keys.None);
  58.         cInput.SetKey("Weapon 6", Keys.Alpha6, Keys.None);
  59.         cInput.SetKey("Weapon 7", Keys.Alpha7, Keys.None);
  60.         cInput.SetKey("Weapon 8", Keys.Alpha8, Keys.None);
  61.         cInput.SetKey("Weapon 9", Keys.Alpha9, Keys.None);
  62.         cInput.SetKey("Weapon 0", Keys.Alpha0, Keys.None);
  63.  
  64.  
  65.         // here we make the horizontal and vertical axis out of the
  66.         // "Move Left"/"Move Right" inputs and the "Move Back"/"Move Forward" inputs
  67.         cInput.SetAxis("Horizontal", "Move Left", "Move Right");
  68.         cInput.SetAxis("Vertical", "Move Back", "Move Forward");
  69.         // and here we make the look axis
  70.         cInput.SetAxis("HorizontalLook", "Look Left", "Look Right");
  71.         cInput.SetAxis("VerticalLook", "Look Down", "Look Up");
  72.  
  73.     }
  74.  
  75.  
  76.     void Update() {
  77.         // show menu with escape
  78.         if (Input.GetKeyDown(KeyCode.I) && !cInput.scanning) {
  79.             MenuIsOpen = !MenuIsOpen;
  80.             setupcInput.guiPanelOpen = MenuIsOpen;
  81.         }
  82.  
  83.     }
  84.  
  85.     void OnGUI() {
  86.         if (MenuIsOpen) {
  87.  
  88.             if (cInput.scanning) {
  89.                 _clickDelay = Time.realtimeSinceStartup + 0.15f;
  90.             }
  91.  
  92.             GUI.skin = guiSkin;
  93.             UpdateGUIColors();
  94.  
  95.             if (Screen.height - cGUI.windowMaxSize.y < 0) { _wHeight = Screen.height; } else { _wHeight = cGUI.windowMaxSize.y; }
  96.             if (Screen.width - cGUI.windowMaxSize.x < 0) { _wWidth = Screen.width; } else { _wWidth = cGUI.windowMaxSize.x; }
  97.             windowRect = new Rect((Screen.width - _wWidth) / 2, (Screen.height - _wHeight) / 2, _wWidth, _wHeight);
  98.             //windowRect = GUILayout.Window(0, windowRect, MenuWindow, "");
  99.             GUI.Window(0, windowRect, MenuWindow, "");
  100.             if (showPopUp) {
  101.                 GUI.Window(1, new Rect((Screen.width - 512) / 2, (Screen.height - 350) / 2, 512, 350), popUp, "", popwindow);
  102.                 GUI.BringWindowToFront(1);
  103.                 GUI.FocusWindow(1);
  104.             }
  105.         }
  106.     }
  107.  
  108.     void popUp(int windowID) {
  109.         GUI.FocusWindow(1);
  110.         GUI.TextField(new Rect(40, 100, 450, 95), "Please leave all analog inputs in their neutral positions.\n\nClick on OK when ready.");
  111.  
  112.         Rect _buttonRect = new Rect(150, 284, 200, 35);
  113.         GUI.Button(_buttonRect, "OK", smallbutton);
  114.         if (_buttonRect.Contains(Event.current.mousePosition) && Input.GetMouseButtonUp(0) && showPopUp) {
  115.             cInput.Calibrate();
  116.             showPopUp = false;
  117.         }
  118.     }
  119.  
  120.     void MenuWindow(int windowID) {
  121.         if (!showPopUp) { GUI.FocusWindow(0); }
  122.         GUI.backgroundColor = menuColor;
  123.         #region left menu ---------------------------------------------
  124.  
  125.         float _buttonWidth = 128;
  126.         float _buttonHeight = 35;
  127.         float _lbuttonStartH = _wWidth / 26.5f;
  128.         int _strvert = 7;
  129.         if (cGUI.cSkin && cGUI.cSkin.name == "cGUISkin Dark") { _strvert = 6; }
  130.         float _lbuttonStartV = _wHeight / _strvert;
  131.  
  132.         float _lbuttonEnd = _wHeight - (_wHeight / 6);
  133.         float _lbuttonSpace = 50;
  134.         int _showInt = 0;
  135.  
  136.         GUI.SetNextControlName("textarea"); // set button to active mode
  137.         if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonStartV + (_lbuttonSpace * _showInt++), _buttonWidth, _buttonHeight), "  INPUTS")) {
  138.             // don't actually do anything because this one is already activated
  139.         }
  140.  
  141.         GUI.FocusControl("textarea");
  142.  
  143.         if (cGUI.cAudioExists) {
  144.             if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonStartV + (_lbuttonSpace * _showInt++), _buttonWidth, _buttonHeight), "  AUDIO")) {
  145.                 cGUI.ShowAudioGUI();
  146.             }
  147.         }
  148.  
  149.         if (cGUI.cVideoExists) {
  150.             if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonStartV + (_lbuttonSpace * _showInt++), _buttonWidth, _buttonHeight), "  VIDEO")) {
  151.                 cGUI.ShowVideoGUI();
  152.             }
  153.         }
  154.  
  155.  
  156.         if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonEnd - (_lbuttonSpace * 2), _buttonWidth, _buttonHeight), "  CALIBRATE")) {
  157.             showPopUp = true;
  158.         }
  159.  
  160.         if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonEnd - (_lbuttonSpace), _buttonWidth, _buttonHeight), "  DEFAULTS")) {
  161.             cInput.ResetInputs(); // reset cInput to defaults
  162.         }
  163.  
  164.         if (GUI.Button(new Rect(_lbuttonStartH, _lbuttonEnd, _buttonWidth, _buttonHeight), "  EXIT")) {
  165.             MenuIsOpen = !MenuIsOpen;
  166.             setupcInput.guiPanelOpen = MenuIsOpen;
  167.         }
  168.  
  169.         #endregion // left menu ---------------------------------------
  170.  
  171.         #region right menu (cInput) -----------------------------------
  172.  
  173.         float v_widthStartH1 = windowRect.width / 3;
  174.         float v_widthStartH3 = windowRect.width / 1.8f;
  175.         float v_widthStartV2 = windowRect.height / 9f;
  176.         float v_space = 60;
  177.  
  178.         if (cGUI.cSkin) {
  179.             label2 = "label";
  180.             box2 = "box2";
  181.             popwindow = "popwindow";
  182.             smallbutton = "smallbutton";
  183.         }
  184.         else {
  185.             label2 = "label";
  186.             box2 = "box";
  187.             popwindow = "window";
  188.             smallbutton = "button";
  189.         }
  190.  
  191.         Rect rightSideRect = new Rect(windowRect.width / 3.6f, v_widthStartV2 * 2.2f, windowRect.width * 0.67f, windowRect.height);
  192.  
  193.         // input settings
  194.         GUI.Label(new Rect(rightSideRect.x + (rightSideRect.width / 2) - (_buttonWidth / 2), windowRect.height / 6.5f, _buttonWidth, _buttonHeight), "INPUT SETTINGS", label2);
  195.         // GUI.Label(new Rect(windowRect.width * 0.57f, v_widthStartV1, _buttonWidth, _buttonHeight), "INPUT SETTINGS", label2);
  196.  
  197.         GUI.Label(new Rect(rightSideRect.x + (rightSideRect.width * 0.2f) - (_buttonWidth * 1.2f / 2), rightSideRect.y, _buttonWidth * 1.2f, _buttonHeight), "ACTION", label2);
  198.         GUI.Label(new Rect(rightSideRect.x + (rightSideRect.width * 0.5f) - (_buttonWidth * 1.2f / 2), rightSideRect.y, _buttonWidth * 1.2f, _buttonHeight), "PRIMARY", label2);
  199.         GUI.Label(new Rect(rightSideRect.x + (rightSideRect.width * 0.8f) - (_buttonWidth * 1.2f / 2), rightSideRect.y, _buttonWidth * 1.2f, _buttonHeight), "SECONDARY", label2);
  200.  
  201.         // scroll
  202.         _scrollPosition = GUI.BeginScrollView(new Rect(v_widthStartH1, v_widthStartV2 * 3.0f, v_widthStartH3 * 1.1f, windowRect.height * 0.58f), _scrollPosition, new Rect(v_widthStartH1, 0, v_widthStartH3, v_space * (cInput.length - 13)));
  203.  
  204.         for (int n = 0; n < cInput.length; n++) {
  205.             GUI.Label(new Rect(rightSideRect.x + (rightSideRect.width * 0.2f) - (_buttonWidth * 1.2f / 2), 0 + (_buttonHeight * n), _buttonWidth * 1.2f, _buttonHeight), cInput.GetText(n, 0), "label"); // name of input
  206.             if (GUI.Button(new Rect(rightSideRect.x + (rightSideRect.width * 0.5f) - (_buttonWidth * 1.2f / 2), 0 + (_buttonHeight * n), _buttonWidth * 1.2f, _buttonHeight), cInput.GetText(n, 1), box2) && Input.GetMouseButtonUp(0)) {
  207.                 if (Time.realtimeSinceStartup > _clickDelay) {
  208.                     // only allow mouse axis on look axis
  209.                     if (n < 4 || n > 7) {
  210.                         cInput.ChangeKey(n, 1, false, true, true, true);
  211.                     }
  212.                     else { cInput.ChangeKey(n, 1, true, true, true, true, true); }
  213.                     print(cInput.GetText(n));
  214.                 }
  215.             }
  216.  
  217.             if (GUI.Button(new Rect(rightSideRect.x + (rightSideRect.width * 0.8f) - (_buttonWidth * 1.2f / 2), 0 + (_buttonHeight * n), _buttonWidth * 1.2f, _buttonHeight), cInput.GetText(n, 2), box2)) {
  218.                 if (Time.realtimeSinceStartup > _clickDelay) {
  219.                     // only allow mouse axis on look axis
  220.                     if (n < 4 || n > 7) {
  221.                         cInput.ChangeKey(n, 2, false, true, true, true);
  222.                     }
  223.                     else { cInput.ChangeKey(n, 2, true, true, true, true, true); }
  224.                     print(cInput.GetText(n));
  225.                 }
  226.             }
  227.         }
  228.  
  229.         GUI.EndScrollView();
  230.  
  231.         #endregion //right menu ---------------------------------------
  232.     }
  233.  
  234.     public void UpdateGUIColors() {
  235.         GUI.backgroundColor = menuColor;
  236.         Color _c = menuColor * 0.75f;
  237.         if (guiSkin) {
  238.             guiSkin.button.normal.textColor = _c;
  239.             guiSkin.label.normal.textColor = _c;
  240.             guiSkin.textField.normal.textColor = _c;
  241.             guiSkin.textArea.normal.textColor = _c;
  242.  
  243.             for (int n = 0; n < 6; n++) { guiSkin.customStyles[n].normal.textColor = _c; }
  244.             guiSkin.customStyles[1].normal.textColor = Color.white;
  245.             guiSkin.customStyles[7].normal.textColor = Color.white;
  246.             guiSkin.customStyles[6].normal.textColor = Color.white;
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement