Beispiel #1
0
        public void Raise(KeyboardKey value)
        {
            for (int i = listeners.Count - 1; i >= 0; i--)
            {
                if (listeners[i] != null)
                {
                    listeners[i].OnEventRaised();
                }
            }

            for (int i = boolListeners.Count - 1; i >= 0; i--)
            {
                if (boolListeners[i] != null)
                {
                    boolListeners[i].OnEventRaised(value);
                }
            }

            for (int i = actions.Count - 1; i >= 0; i--)
            {
                if (actions[i] != null)
                {
                    actions[i].Invoke();
                }
            }

            for (int i = boolActions.Count - 1; i >= 0; i--)
            {
                if (boolActions[i] != null)
                {
                    boolActions[i].Invoke(value);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Occures when a key is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="key"></param>
        public void keyPressed(KeyboardKey key)
        {
            if (key.keyGlyph.code == KeyCode.CapsLock)
            {
                IsCapsLocked = !IsCapsLocked;
            }

            if (useShiftToggle &&
                (key.keyGlyph.code == KeyCode.LeftShift || key.keyGlyph.code == KeyCode.RightShift))
            {
                IsShifted = !IsShifted;
            }

            if (useAltGrToggle &&
                (key.keyGlyph.code == KeyCode.AltGr))
            {
                IsAltGr = !IsAltGr;
            }

            if (KeyboardGameEvent != null)
            {
                KeyboardGameEvent.Raise(key);
            }

            if (KeyboardKeyPressed != null)
            {
                KeyboardKeyPressed.Invoke(key);
            }
        }
        private void keyPressed(KeyboardKey key)
        {
            if (!enabled)
            {
                return;
            }

            if (!ValidateHost())
            {
                return;
            }

            switch (targetType)
            {
            case KeyboardOutputTargetType.Function:
                keyStrokeEvent.Invoke(key);
                break;

            default:
                bool selectionReplaced = false;
                if (linkedBehaviour != null && field != null)
                {
                    string initalValue = GetLinkedFieldValue();

                    if (selectionLength > 0)
                    {
                        selectionReplaced = true;
                        initalValue       = initalValue.Remove(insertPoint, selectionLength);
                    }

                    if ((key.keyType == KeyboardKeyType.Backspace || key.keyType == KeyboardKeyType.Delete) && selectionReplaced)
                    {
                        SetLinkedFieldValue(initalValue);
                        insertPoint++;
                    }
                    else
                    {
                        string resultingValue = key.ToString(initalValue, insertPoint);
                        SetLinkedFieldValue(resultingValue);
                        insertPoint += resultingValue.Length - initalValue.Length;
                    }

                    //insertPoint++;
                    selectionLength = 0;
                }

                if (linkedBehaviour.GetType() == typeof(UnityEngine.UI.InputField))
                {
                    lastInputField.selectionAnchorPosition = insertPoint;
                }
                else if (linkedBehaviour.GetType() == typeof(TMPro.TMP_InputField))
                {
                    LastTMProInputField.selectionAnchorPosition = insertPoint;
                }
                break;
            }
        }
Beispiel #4
0
 /// <summary>
 /// Activate the selected key and press it
 /// </summary>
 /// <param name="key"></param>
 public void PressKey(KeyboardKey key)
 {
     if (key != null)
     {
         EventSystem.current.SetSelectedGameObject(key.gameObject);
         ActiveKey = key;
         key.Press();
     }
 }
Beispiel #5
0
 /// <summary>
 /// Registeres a KeybaordKey to the keyboard allowing events to be tracked for the key
 /// </summary>
 /// <param name="key"></param>
 public void RegisterKey(KeyboardKey key)
 {
     if (!keys.Contains(key))
     {
         keys.Add(key);
         key.keyboard = this;
         key.pressed.AddListener(keyPressed);
         key.isDown.AddListener(keyIsDown);
         key.isUp.AddListener(keyIsUp);
     }
 }
        public void ApplyTo(Keyboard keyboard, KeyboardKey keyTemplate)
        {
            if (keyboard == null || keyTemplate == null)
            {
                return;
            }

            while (keyboard.keys.Count > 0)
            {
                var target = keyboard.keys[0];
                keyboard.keys.Remove(target);

                try
                {
#if UNITY_EDITOR
                    if (Application.IsPlaying(target.gameObject))
                    {
                        Destroy(target.gameObject);
                    }
                    else
                    {
                        DestroyImmediate(target.gameObject);
                    }
#else
                    Destroy(target.gameObject);
#endif
                }
                catch { }
            }

            keyboard.keys.Clear();

            var transform = keyboard.GetComponent <Transform>();

            int index = 0;
            foreach (var key in Keys)
            {
                var GO = Instantiate(keyTemplate.gameObject, transform);
                GO.name = "[" + index.ToString() + "] '" + key.keyGlyph.shifted + "' Key";
                var keyCom = GO.GetComponent <KeyboardKey>();
                keyCom.EditorParseKeyCode = key.parseFromKeyCode;
                keyCom.keyGlyph.Set(key.keyGlyph);
                keyCom.keyType                            = key.keyType;
                keyCom.selfRectTransform                  = keyCom.gameObject.GetComponent <RectTransform>();
                keyCom.selfRectTransform.sizeDelta        = key.Size;
                keyCom.selfRectTransform.localPosition    = key.Position;
                keyCom.selfRectTransform.localEulerAngles = key.Rotation;
                keyCom.selfRectTransform.localScale       = key.Scale;
                GO.SetActive(true);
                keyboard.RegisterKey(keyCom);
                index++;
            }
        }
Beispiel #7
0
        public KeyRecord(Transform root, KeyboardKey key)
        {
            parseFromKeyCode      = key.EditorParseKeyCode;
            keyType               = key.keyType;
            keyGlyph              = new KeyGlyphData(key.keyGlyph);
            key.selfRectTransform = key.gameObject.GetComponent <RectTransform>();
            Size     = key.selfRectTransform.sizeDelta;
            Position = root.InverseTransformPoint(key.selfRectTransform.position);
            var parent = key.selfRectTransform.parent;

            key.selfRectTransform.SetParent(root);
            Rotation = key.selfRectTransform.localEulerAngles;
            Scale    = key.selfRectTransform.localScale;
            key.selfRectTransform.SetParent(parent);
        }
Beispiel #8
0
 /// <summary>
 /// Occures when a key was down and is released 'up'
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="key"></param>
 private void keyIsUp(KeyboardKey key)
 {
     //For modifiers consider a down movement a press if the board is not in this state
     if (key.keyType == KeyboardKeyType.Modifier)
     {
         //If we arent in AltGr mode for the AltGr key then press AltGr
         if (key.keyGlyph.code == KeyCode.AltGr && AltGrKeysHeld.Contains(key))
         {
             AltGrKeysHeld.Remove(key);
         }
         else if ((key.keyGlyph.code == KeyCode.LeftShift || key.keyGlyph.code == KeyCode.RightShift) && ShiftKeysHeld.Contains(key))
         {
             ShiftKeysHeld.Remove(key);
         }
     }
 }
Beispiel #9
0
 public void OnEventRaised(KeyboardKey value)
 {
     KeyResponce.Invoke(value);
 }
        /// <summary>
        /// Refresh the Template with the current keyboard structure
        /// </summary>
        public void RefreshTemplate()
        {
            keyboard = GetComponent <Keyboard>();

            if (keyboard == null)
            {
                return;
            }

            if (workingTemplate == null)
            {
                workingTemplate = new Serialization.KeyboardTemplate()
                {
                    TemplateName = "New Template"
                }
            }
            ;

            if (headerRowTransform == null)
            {
                workingTemplate.HeaderRow = null;
            }
            else
            {
                if (workingTemplate.HeaderRow == null)
                {
                    workingTemplate.HeaderRow = new Serialization.KeyboardTemplateRow();
                }

                workingTemplate.HeaderRow.RowOffset   = headerRowTransform.anchoredPosition3D;
                workingTemplate.HeaderRow.RowRotation = headerRowTransform.localEulerAngles;

                List <KeyboardKeyTemplate> keyTemplates = new List <KeyboardKeyTemplate>();

                foreach (Transform trans in headerRowTransform)
                {
                    KeyboardKey key = trans.gameObject.GetComponent <KeyboardKey>();
                    if (key != null)
                    {
                        key.UpdateTemplate(ref key.template);
                        keyTemplates.Add(key.template);
                    }
                }

                workingTemplate.HeaderRow.Keys = keyTemplates.ToArray();
            }

            List <KeyboardTemplateRow> rowTemplates = new List <KeyboardTemplateRow>();

            if (rowTransforms == null)
            {
                rowTransforms = new List <RectTransform>();
            }

            foreach (RectTransform row in rowTransforms)
            {
                if (row == null)
                {
                    continue;
                }

                KeyboardTemplateRow nRow = new KeyboardTemplateRow();
                nRow.RowOffset   = row.anchoredPosition3D;
                nRow.RowRotation = row.localEulerAngles;

                List <KeyboardKeyTemplate> keyTemplates = new List <KeyboardKeyTemplate>();

                foreach (Transform trans in row)
                {
                    KeyboardKey key = trans.gameObject.GetComponent <KeyboardKey>();
                    if (key != null)
                    {
                        key.UpdateTemplate(ref key.template);
                        keyTemplates.Add(key.template);
                    }
                }

                nRow.Keys = keyTemplates.ToArray();
                rowTemplates.Add(nRow);
            }

            workingTemplate.PrimaryRows  = rowTemplates.ToArray();
            workingTemplate.TemplateName = name;
        }
        /// <summary>
        /// Refresh the keyboard with the current templates structure
        /// </summary>
        public void RefreshKeyboard()
        {
            keyboard = GetComponent <Keyboard>();

            if (keyboard == null)
            {
                return;
            }

            if (Prototype == null)
            {
                Debug.LogError("An attempt to refresh keyboard '" + name + "' failed with error: No Key Prototype present unable to generate keyboard.");
                return;
            }

            if (Container == null)
            {
                Debug.LogWarning("An attempt to refresh keyboard '" + name + "' logged a warning: No Key Container present generating new transform.");
                GameObject nContainer = new GameObject("Key Container", typeof(RectTransform));
                Container = nContainer.GetComponent <RectTransform>();
                Container.SetParent(transform);
                Container.localScale         = Vector3.one;
                Container.localEulerAngles   = Vector3.zero;
                Container.pivot              = new Vector2(0.5f, 0.5f);
                Container.anchoredPosition3D = Vector3.zero;
            }

            List <GameObject> killList = new List <GameObject>();

            for (int c = 0; c < Container.childCount; c++)
            {
                if (Container.GetChild(c).gameObject != Prototype.gameObject)
                {
                    killList.Add(Container.GetChild(c).gameObject);
                }
            }
            foreach (GameObject go in killList)
            {
                if (Application.isEditor)
                {
                    DestroyImmediate(go);
                }
                else
                {
                    Destroy(go);
                }
            }

            if (workingTemplate.HeaderRow != null)
            {
                GameObject    headerRow      = new GameObject("Header Row", typeof(RectTransform));
                RectTransform headerRowTrans = headerRow.GetComponent <RectTransform>();
                headerRowTransform = headerRowTrans;
                headerRowTrans.SetParent(Container);
                headerRowTrans.localScale         = Vector3.one;
                headerRowTrans.pivot              = new Vector2(0.5f, 0.5f);
                headerRowTrans.anchoredPosition3D = workingTemplate.HeaderRow.RowOffset;
                headerRowTrans.localEulerAngles   = workingTemplate.HeaderRow.RowRotation;

                foreach (Serialization.KeyboardKeyTemplate key in workingTemplate.HeaderRow.Keys)
                {
                    GameObject newKey = Instantiate <GameObject>(Prototype.gameObject);
                    newKey.SetActive(true);
                    newKey.name = (key.Code != KeyCode.None ? key.Code.ToString() : key.DisplayNormal) + " key";
                    KeyboardKey newKeyboardKey = newKey.GetComponent <KeyboardKey>();

                    newKeyboardKey.pressed.AddListener(keyboard.keyPressed);
                    newKeyboardKey.keyGlyph.ApplyTemplate(key);
                    if (newKeyboardKey.selfRectTransform == null)
                    {
                        newKeyboardKey.selfRectTransform = newKey.GetComponent <RectTransform>();
                    }

                    newKeyboardKey.selfRectTransform.SetParent(headerRowTrans);

                    newKeyboardKey.selfRectTransform.anchoredPosition3D = key.KeyOffset;
                    newKeyboardKey.selfRectTransform.localEulerAngles   = key.KeyRotation;
                    newKeyboardKey.selfRectTransform.sizeDelta          = key.KeySize;
                    newKeyboardKey.selfRectTransform.localScale         = Vector3.one;

                    newKeyboardKey.keyGlyph.altGrString        = key.AltGr;
                    newKeyboardKey.keyGlyph.shiftedString      = key.Shifted;
                    newKeyboardKey.keyGlyph.normalString       = key.Normal;
                    newKeyboardKey.keyGlyph.shiftedAltGrString = key.ShiftedAltGr;
                    newKeyboardKey.keyGlyph.code = key.Code;
                    newKeyboardKey.keyType       = key.KeyType;

                    if (newKeyboardKey.keyGlyph.altGrDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.altGrDisplay.text = key.DisplayAltGr;
                    }

                    if (newKeyboardKey.keyGlyph.shiftedDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.shiftedDisplay.text = key.DisplayShifted;
                    }

                    if (newKeyboardKey.keyGlyph.normalDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.normalDisplay.text = key.DisplayNormal;
                    }

                    if (newKeyboardKey.keyGlyph.shiftedAltGrDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.shiftedAltGrDisplay.text = key.DisplayShiftedAltGr;
                    }
                }
            }

            int i = 1;

            rowTransforms.Clear();
            foreach (Serialization.KeyboardTemplateRow row in workingTemplate.PrimaryRows)
            {
                GameObject    nRow      = new GameObject("Row " + i.ToString(), typeof(RectTransform));
                RectTransform nRowTrans = nRow.GetComponent <RectTransform>();
                rowTransforms.Add(nRowTrans);
                nRowTrans.SetParent(Container);
                nRowTrans.pivot = new Vector2(0.5f, 0.5f);
                nRowTrans.anchoredPosition3D = row.RowOffset;
                nRowTrans.localEulerAngles   = row.RowRotation;
                nRowTrans.localScale         = Vector3.one;

                foreach (Serialization.KeyboardKeyTemplate key in row.Keys)
                {
                    GameObject newKey = Instantiate <GameObject>(Prototype.gameObject);
                    newKey.SetActive(true);
                    newKey.name = (key.Code != KeyCode.None ? key.Code.ToString() : key.DisplayNormal) + " key";
                    KeyboardKey newKeyboardKey = newKey.GetComponent <KeyboardKey>();

                    newKeyboardKey.pressed.AddListener(keyboard.keyPressed);
                    newKeyboardKey.keyGlyph.ApplyTemplate(key);
                    if (newKeyboardKey.selfRectTransform == null)
                    {
                        newKeyboardKey.selfRectTransform = newKey.GetComponent <RectTransform>();
                    }

                    newKeyboardKey.selfRectTransform.SetParent(nRowTrans);

                    newKeyboardKey.selfRectTransform.anchoredPosition3D = key.KeyOffset;
                    newKeyboardKey.selfRectTransform.localEulerAngles   = key.KeyRotation;
                    newKeyboardKey.selfRectTransform.sizeDelta          = key.KeySize;
                    newKeyboardKey.selfRectTransform.localScale         = Vector3.one;
                    newKeyboardKey.keyType = key.KeyType;

                    newKeyboardKey.keyGlyph.altGrString        = key.AltGr;
                    newKeyboardKey.keyGlyph.shiftedString      = key.Shifted;
                    newKeyboardKey.keyGlyph.normalString       = key.Normal;
                    newKeyboardKey.keyGlyph.shiftedAltGrString = key.ShiftedAltGr;
                    newKeyboardKey.keyGlyph.code = key.Code;

                    if (newKeyboardKey.keyGlyph.altGrDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.altGrDisplay.text = key.DisplayAltGr;
                    }

                    if (newKeyboardKey.keyGlyph.shiftedDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.shiftedDisplay.text = key.DisplayShifted;
                    }

                    if (newKeyboardKey.keyGlyph.normalDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.normalDisplay.text = key.DisplayNormal;
                    }

                    if (newKeyboardKey.keyGlyph.shiftedAltGrDisplay != null)
                    {
                        newKeyboardKey.keyGlyph.shiftedAltGrDisplay.text = key.DisplayShiftedAltGr;
                    }
                }
                i++;
            }
        }
Beispiel #12
0
    public override void OnInspectorGUI()
    {
        HeathenEngineering.UIX.KeyboardKey key = target as HeathenEngineering.UIX.KeyboardKey;
        key.keyType = (HeathenEngineering.UIX.KeyboardKeyType)EditorGUILayout.EnumPopup("Type", key.keyType);

        if (key.keyType == HeathenEngineering.UIX.KeyboardKeyType.WhiteSpace)
        {
            if (!key.EditorParseKeyCode)
            {
                key.EditorParseKeyCode = true;
                key.keyType            = key.keyGlyph.DefaultFromCode(key.keyGlyph.code);
                EditorUtility.SetDirty(key);
            }
        }

        key.EditorParseKeyCode = EditorGUILayout.ToggleLeft("Parse return values from key code?", key.EditorParseKeyCode);
        KeyCode previousCode = (KeyCode)EditorGUILayout.EnumPopup("Code", key.keyGlyph.code);

        if (previousCode == KeyCode.Space || previousCode == KeyCode.Tab)
        {
            key.keyGlyph.code = previousCode;
            key.keyType       = key.keyGlyph.DefaultFromCode(key.keyGlyph.code);

            EditorGUILayout.HelpBox("White space key codes automatically parse the return value based on the provided code.", MessageType.Info);

            if (key.keyGlyph.normalDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.normalDisplay);
            }

            if (key.keyGlyph.shiftedDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.shiftedDisplay);
            }

            if (key.keyGlyph.altGrDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.altGrDisplay);
            }

            if (key.keyGlyph.shiftedAltGrDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.shiftedAltGrDisplay);
            }
        }
        else if (key.keyGlyph.code != previousCode && key.EditorParseKeyCode)
        {
            key.keyGlyph.code = previousCode;
            key.keyType       = key.keyGlyph.DefaultFromCode(key.keyGlyph.code);

            if (key.keyGlyph.normalDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.normalDisplay);
            }

            if (key.keyGlyph.shiftedDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.shiftedDisplay);
            }

            if (key.keyGlyph.altGrDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.altGrDisplay);
            }

            if (key.keyGlyph.shiftedAltGrDisplay != null)
            {
                EditorUtility.SetDirty(key.keyGlyph.shiftedAltGrDisplay);
            }
        }
        else
        {
            key.keyGlyph.code = previousCode;
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("State Glyphs", EditorStyles.boldLabel);
        key.keyGlyph.normal = EditorGUILayout.ObjectField("Normal", key.keyGlyph.normal, typeof(RectTransform), true) as RectTransform;
        if (key.keyGlyph.normal != null)
        {
            key.keyGlyph.normalDisplay = EditorGUILayout.ObjectField("Text", key.keyGlyph.normalDisplay, typeof(UnityEngine.UI.Text), true) as UnityEngine.UI.Text;
            if (!key.EditorParseKeyCode)
            {
                EditorGUILayout.LabelField("Return Value:");
                key.keyGlyph.normalString = EditorGUILayout.TextArea(key.keyGlyph.normalString);
            }
        }
        EditorGUILayout.Space();
        key.keyGlyph.shifted = EditorGUILayout.ObjectField("On Shift", key.keyGlyph.shifted, typeof(RectTransform), true) as RectTransform;
        if (key.keyGlyph.shifted != null)
        {
            key.keyGlyph.shiftedDisplay = EditorGUILayout.ObjectField("Text", key.keyGlyph.shiftedDisplay, typeof(UnityEngine.UI.Text), true) as UnityEngine.UI.Text;
            if (!key.EditorParseKeyCode)
            {
                EditorGUILayout.LabelField("Return Value:");
                key.keyGlyph.shiftedString = EditorGUILayout.TextArea(key.keyGlyph.shiftedString);
            }
        }
        EditorGUILayout.Space();
        key.keyGlyph.altGr = EditorGUILayout.ObjectField("On AltGr", key.keyGlyph.altGr, typeof(RectTransform), true) as RectTransform;
        if (key.keyGlyph.altGr != null)
        {
            key.keyGlyph.altGrDisplay = EditorGUILayout.ObjectField("Text", key.keyGlyph.altGrDisplay, typeof(UnityEngine.UI.Text), true) as UnityEngine.UI.Text;
            if (!key.EditorParseKeyCode)
            {
                EditorGUILayout.LabelField("Return Value:");
                key.keyGlyph.altGrString = EditorGUILayout.TextArea(key.keyGlyph.altGrString);
            }
        }
        EditorGUILayout.Space();
        key.keyGlyph.shiftedAltGr = EditorGUILayout.ObjectField("On Shift + AltGr", key.keyGlyph.shiftedAltGr, typeof(RectTransform), true) as RectTransform;
        if (key.keyGlyph.shiftedAltGr != null)
        {
            key.keyGlyph.shiftedAltGrDisplay = EditorGUILayout.ObjectField("Text", key.keyGlyph.shiftedAltGrDisplay, typeof(UnityEngine.UI.Text), true) as UnityEngine.UI.Text;
            if (!key.EditorParseKeyCode)
            {
                EditorGUILayout.LabelField("Return Value:");
                key.keyGlyph.shiftedAltGrString = EditorGUILayout.TextArea(key.keyGlyph.shiftedAltGrString);
            }
        }

        EditorUtility.SetDirty(key);

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Events");
        var p = serializedObject.FindProperty("pressed");

        EditorGUILayout.PropertyField(p);
        p = serializedObject.FindProperty("isDown");
        EditorGUILayout.PropertyField(p);
        p = serializedObject.FindProperty("isUp");
        EditorGUILayout.PropertyField(p);
        p = serializedObject.FindProperty("isClicked");
        EditorGUILayout.PropertyField(p);

        serializedObject.ApplyModifiedProperties();
    }