-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
443 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,198 @@ | ||
using System.Collections; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
public class Key : MonoBehaviour | ||
{ | ||
public delegate void OnKeyPressed(); | ||
public static OnKeyPressed keyPressed; | ||
public string KeyCapChar; | ||
public string AlterateKeyCapChar; | ||
public Rigidbody Rigidbody; | ||
public bool KeyPressed = false; | ||
public Color PressedKeycapColor; | ||
public Color KeycapColor; | ||
public Color InitialKeycapColor; | ||
|
||
protected Transform initialPosition; | ||
private KeycodeAdder keycodeAdder; | ||
private Text keyCapText; | ||
private Vector3 initialLocalPosition; | ||
private Quaternion initialLocalRotation; | ||
private Vector3 constrainedPosition; | ||
private Quaternion constrainedRotation; | ||
private bool uppercaseSwitch = true; | ||
private bool symbolSwitch = false; | ||
private bool checkForButton = true; | ||
private const float DistanceToBePressed = 0.01f; | ||
private const float KeyBounceBackMultiplier = 1500f; | ||
private KeySoundController keySoundController; | ||
private float currentDistance = -1; | ||
|
||
void Start() | ||
{ | ||
keycodeAdder = this.gameObject.GetComponent<KeycodeAdder> (); | ||
|
||
keyCapText = this.gameObject.GetComponentInChildren<Text> (); | ||
KeycapColor = this.gameObject.GetComponent<Renderer> ().material.color; | ||
InitialKeycapColor = KeycapColor; | ||
|
||
initialPosition = new GameObject(string.Format("[{0}] initialPosition", this.gameObject.name)).transform; | ||
initialPosition.parent = this.transform.parent; | ||
initialPosition.localPosition = Vector3.zero; | ||
initialPosition.localRotation = Quaternion.identity; | ||
|
||
if(Rigidbody == null) | ||
{ | ||
Rigidbody = GetComponent<Rigidbody>(); | ||
} | ||
|
||
initialLocalPosition = this.transform.localPosition; | ||
initialLocalRotation = this.transform.localRotation; | ||
|
||
constrainedPosition = initialLocalPosition; | ||
constrainedRotation = initialLocalRotation; | ||
|
||
keySoundController = transform.parent.parent.parent.parent.parent | ||
.gameObject.GetComponent<KeySoundController> (); | ||
|
||
SwitchKeycapCharCase (); | ||
} | ||
|
||
void FixedUpdate() | ||
{ | ||
ConstrainPosition (); | ||
currentDistance = Vector3.Distance(this.transform.position, initialPosition.position); | ||
|
||
Vector3 PositionDelta = initialPosition.position - this.transform.position; | ||
this.Rigidbody.velocity = PositionDelta * KeyBounceBackMultiplier * Time.deltaTime; | ||
} | ||
|
||
void Update() | ||
{ | ||
if (checkForButton) | ||
{ | ||
if (currentDistance > DistanceToBePressed) | ||
{ | ||
KeyPressed = true; | ||
keyPressed (); | ||
if (symbolSwitch) | ||
{ | ||
keycodeAdder.SimulateAlternateKeyPress (); | ||
} | ||
else | ||
{ | ||
keycodeAdder.SimulateKeyPress (); | ||
} | ||
keySoundController.StartKeySound (this.gameObject.transform); | ||
checkForButton = false; | ||
} | ||
} else if (!checkForButton) | ||
{ | ||
if (currentDistance < DistanceToBePressed) | ||
{ | ||
KeyPressed = false; | ||
checkForButton = true; | ||
} | ||
} | ||
|
||
ChangeKeyColorOnPress (); | ||
} | ||
|
||
void LateUpdate() | ||
{ | ||
ConstrainPosition (); | ||
} | ||
|
||
void ChangeKeyColorOnPress() | ||
{ | ||
if (KeyPressed) | ||
{ | ||
gameObject.GetComponent<Renderer> ().material.color = PressedKeycapColor; | ||
} | ||
else | ||
{ | ||
gameObject.GetComponent<Renderer> ().material.color = KeycapColor; | ||
} | ||
} | ||
|
||
void ConstrainPosition() | ||
{ | ||
constrainedPosition.y = this.transform.localPosition.y; | ||
if (this.transform.localPosition.y > initialLocalPosition.y) | ||
{ | ||
constrainedPosition.y = initialLocalPosition.y; | ||
} | ||
this.transform.localPosition = constrainedPosition; | ||
this.transform.localRotation = constrainedRotation; | ||
} | ||
|
||
public void SwitchKeycapCharCase() | ||
{ | ||
if (uppercaseSwitch) | ||
{ | ||
keyCapText.text = KeyCapChar.ToLower (); | ||
uppercaseSwitch = false; | ||
} | ||
else | ||
{ | ||
keyCapText.text = KeyCapChar.ToUpper (); | ||
uppercaseSwitch = true; | ||
} | ||
} | ||
|
||
public void SwitchToSymbols() | ||
{ | ||
if (!symbolSwitch) | ||
{ | ||
keyCapText.text = AlterateKeyCapChar; | ||
symbolSwitch = true; | ||
} | ||
else | ||
{ | ||
keyCapText.text = KeyCapChar; | ||
keyCapText.text = KeyCapChar.ToLower (); | ||
symbolSwitch = false; | ||
} | ||
} | ||
using System; | ||
using System.Collections; | ||
using System.Runtime.InteropServices; | ||
using System.Text.RegularExpressions; | ||
using System.Windows.Forms; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using WindowsInput; | ||
|
||
public class Key : MonoBehaviour | ||
{ | ||
public delegate void OnKeyPressed(); | ||
public static OnKeyPressed keyPressed; | ||
public string KeyCapChar; | ||
public string AlterateKeyCapChar; | ||
public Rigidbody Rigidbody; | ||
public bool KeyPressed = false; | ||
public Color PressedKeycapColor; | ||
public Color KeycapColor; | ||
public Color InitialKeycapColor; | ||
|
||
protected Transform initialPosition; | ||
private KeycodeAdder keycodeAdder; | ||
private Text keyCapText; | ||
private Vector3 initialLocalPosition; | ||
private Quaternion initialLocalRotation; | ||
private Vector3 constrainedPosition; | ||
private Quaternion constrainedRotation; | ||
private bool uppercaseSwitch = true; | ||
private bool symbolSwitch = false; | ||
private bool checkForButton = true; | ||
private const float DistanceToBePressed = 0.01f; | ||
private const float KeyBounceBackMultiplier = 1500f; | ||
private KeySoundController keySoundController; | ||
private float currentDistance = -1; | ||
|
||
void Start() | ||
{ | ||
keycodeAdder = this.gameObject.GetComponent<KeycodeAdder> (); | ||
|
||
keyCapText = this.gameObject.GetComponentInChildren<Text> (); | ||
KeycapColor = this.gameObject.GetComponent<Renderer> ().material.color; | ||
InitialKeycapColor = KeycapColor; | ||
|
||
initialPosition = new GameObject(string.Format("[{0}] initialPosition", this.gameObject.name)).transform; | ||
initialPosition.parent = this.transform.parent; | ||
initialPosition.localPosition = Vector3.zero; | ||
initialPosition.localRotation = Quaternion.identity; | ||
|
||
if(Rigidbody == null) | ||
{ | ||
Rigidbody = GetComponent<Rigidbody>(); | ||
} | ||
|
||
initialLocalPosition = this.transform.localPosition; | ||
initialLocalRotation = this.transform.localRotation; | ||
|
||
constrainedPosition = initialLocalPosition; | ||
constrainedRotation = initialLocalRotation; | ||
|
||
keySoundController = transform.root.Find("Keyboards/Punchkeyboard") | ||
.gameObject.GetComponent<KeySoundController> (); | ||
|
||
SwitchKeycapCharCase (); | ||
} | ||
|
||
void FixedUpdate() | ||
{ | ||
ConstrainPosition (); | ||
currentDistance = Vector3.Distance(this.transform.position, initialPosition.position); | ||
|
||
Vector3 PositionDelta = initialPosition.position - this.transform.position; | ||
this.Rigidbody.velocity = PositionDelta * KeyBounceBackMultiplier * Time.deltaTime; | ||
} | ||
|
||
void Update() | ||
{ | ||
if (checkForButton) | ||
{ | ||
if (currentDistance > DistanceToBePressed) | ||
{ | ||
KeyPressed = true; | ||
keyPressed (); | ||
if (symbolSwitch) | ||
{ | ||
if (name.Contains("Space") || | ||
name.Contains("Backspace") || | ||
name.Contains("Return") || | ||
name.Contains("Shift") || | ||
name.Contains("Symbol")) | ||
|
||
keycodeAdder.SimulateAlternateKeyPress(); | ||
|
||
else | ||
|
||
WriteStringToTarget(); | ||
} | ||
else | ||
{ | ||
if (name.Contains("Space") || | ||
name.Contains("Backspace") || | ||
name.Contains("Return") || | ||
name.Contains("Shift") || | ||
name.Contains("Symbol")) | ||
|
||
keycodeAdder.SimulateKeyPress(); | ||
|
||
else | ||
|
||
WriteStringToTarget(); | ||
|
||
} | ||
keySoundController.StartKeySound (this.gameObject.transform); | ||
checkForButton = false; | ||
} | ||
} else if (!checkForButton) | ||
{ | ||
if (currentDistance < DistanceToBePressed) | ||
{ | ||
KeyPressed = false; | ||
checkForButton = true; | ||
} | ||
} | ||
|
||
ChangeKeyColorOnPress (); | ||
} | ||
|
||
void LateUpdate() | ||
{ | ||
ConstrainPosition (); | ||
} | ||
|
||
void ChangeKeyColorOnPress() | ||
{ | ||
if (KeyPressed) | ||
{ | ||
gameObject.GetComponent<Renderer> ().material.color = PressedKeycapColor; | ||
} | ||
else | ||
{ | ||
gameObject.GetComponent<Renderer> ().material.color = KeycapColor; | ||
} | ||
} | ||
|
||
void ConstrainPosition() | ||
{ | ||
constrainedPosition.y = this.transform.localPosition.y; | ||
if (this.transform.localPosition.y > initialLocalPosition.y) | ||
{ | ||
constrainedPosition.y = initialLocalPosition.y; | ||
} | ||
this.transform.localPosition = constrainedPosition; | ||
this.transform.localRotation = constrainedRotation; | ||
} | ||
|
||
public void SwitchKeycapCharCase() | ||
{ | ||
if (uppercaseSwitch) | ||
{ | ||
keyCapText.text = KeyCapChar.ToLower (); | ||
uppercaseSwitch = false; | ||
} | ||
else | ||
{ | ||
keyCapText.text = KeyCapChar.ToUpper (); | ||
uppercaseSwitch = true; | ||
} | ||
} | ||
|
||
public void SwitchToSymbols() | ||
{ | ||
if (!symbolSwitch) | ||
{ | ||
keyCapText.text = AlterateKeyCapChar; | ||
symbolSwitch = true; | ||
} | ||
else | ||
{ | ||
keyCapText.text = KeyCapChar; | ||
keyCapText.text = KeyCapChar.ToLower (); | ||
symbolSwitch = false; | ||
} | ||
} | ||
|
||
private void WriteStringToTarget() | ||
{ | ||
InitializeBehaviour window = | ||
transform.root.gameObject.GetComponent<InitializeBehaviour>(); | ||
if (!window.loadingWheel.activeSelf) | ||
{ | ||
int lcp = window.lastCaretPosition; | ||
window.keyboardTarget.ActivateInputField(); | ||
window.keyboardTarget.text = | ||
window.keyboardTarget.text.Insert(lcp, keyCapText.text); | ||
window.keyboardTarget.caretPosition = lcp + 1; | ||
} | ||
} | ||
} |
Oops, something went wrong.