Last active
April 23, 2016 19:52
-
-
Save tuti107/7e6babd903b2492c47bac7e68f74dd00 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class Board : MonoBehaviour { | |
public GameObject keyPrefab; | |
public Display display; | |
private static string[] keyboardStr = { | |
"1,!,1, 2,\",1, 3,#,1, 4,$,1, 5,%,1, 6,&,1, 7,\',1, 8,(,1, 9,),1, 0,0,1", | |
"q,Q,1, w,W,1, e,E,1, r,R,1, t,T,1, y,Y,1, u,U,1, i,I,1, o,O,1, p,P,1", | |
"a,A,1, s,S,1, d,D,1, f,F,1, g,G,1, h,H,1, j,J,1, k,K,1, l,L,1", | |
"sft,SFT,1.5, z,Z,1, x,X,1, c,C,1, v,V,1, b,B,1, n,N,1, m,M,1, BS,BS,1.5" , | |
"space,space,4", | |
}; | |
List<GameObject[]> keyList = null; | |
// Use this for initialization | |
void Start () { | |
Quaternion r = transform.rotation; | |
transform.rotation = Quaternion.Euler (0, 0, 0); | |
float maxLineWidth = 0; | |
float[] lineWidth = new float[ keyboardStr.Length ]; | |
keyList = new List<GameObject[]> (); | |
int lines = keyboardStr.Length; | |
for (int i = 0; i < lines; i++) { | |
string[] keyInfoArray = keyboardStr [i].Split (','); | |
lineWidth [i] = 0.2f; | |
int arraySize = keyInfoArray.Length / 3; | |
GameObject[] keys = new GameObject[arraySize]; | |
for (int j = 0; j < arraySize; j++) { | |
string a = keyInfoArray [j * 3 + 0].Trim(); | |
string A = keyInfoArray [j * 3 + 1].Trim(); | |
float kw = float.Parse (keyInfoArray [j * 3 + 2]); | |
keys[j] = GameObject.Instantiate (keyPrefab); | |
keys [j].transform.parent = transform; | |
keys [j].name = "key-" + a; | |
Key key = keys [j].GetComponent<Key> (); | |
key.Init (this, a, A, kw); | |
if (j > 0) | |
lineWidth[i] += 0.1f; | |
lineWidth [i] += kw; | |
} | |
keyList.Add (keys); | |
if (lineWidth [i] > maxLineWidth) { | |
maxLineWidth = lineWidth [i]; | |
} | |
} | |
float keyHeight = 1 / (lines * 1.1f + 0.1f); | |
float keyWidth = 1 / maxLineWidth; | |
float y = 0.5f - 0.6f * keyHeight; | |
for (int i = 0; i < lines; i++) { | |
GameObject[] keys = keyList [i]; | |
float x = -0.5f + 0.1f * keyWidth + (1-lineWidth[i]/maxLineWidth)/2; | |
for (int j = 0; j < keys.Length; j++) { | |
Key key = keys [j].GetComponent<Key> (); | |
x += (key.width / 2)* keyWidth; | |
keys [j].transform.localPosition = new Vector3 (x, 0.1f, y); | |
keys [j].transform.localScale = new Vector3 (keyWidth*key.width, 1, keyHeight); | |
x += (key.width / 2 + 0.1f)* keyWidth; | |
} | |
y -= 1.1f * keyHeight; | |
} | |
transform.rotation = r; | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
public void keyPressed(Key key) { | |
if (display != null) { | |
if (key.keyString == "sft") { | |
foreach (GameObject[] keys in keyList) { | |
for (int i = 0; i < keys.Length; i++) { | |
Key k = keys [i].GetComponent<Key> (); | |
k.shift = !k.shift; | |
} | |
} | |
} else if (key.keyString == "BS") { | |
display.Delete (); | |
} else if (key.keyString == "space") { | |
display.Append (" "); | |
} else { | |
display.Append (key.shift ? key.keyStringShift : key.keyString); | |
} | |
} | |
} | |
} |
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
using UnityEngine; | |
using System.Collections; | |
public class Display : MonoBehaviour { | |
private string _text; | |
public string text { | |
get { | |
return _text; | |
} | |
set { | |
_text = value; | |
if (textMesh != null) { | |
textMesh.text = _text; | |
} | |
} | |
} | |
public TextMesh textMesh; | |
// Use this for initialization | |
void Start () { | |
if (textMesh != null) { | |
if (text != null && text != "") { | |
textMesh.text = text; | |
} else { | |
text = textMesh.text; | |
} | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
public void Append(string a) { | |
text += a; | |
} | |
public void Delete() { | |
text = text.Substring (0, text.Length - 1); | |
} | |
} |
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
using UnityEngine; | |
using System.Collections; | |
public class Key : MonoBehaviour { | |
public Board board; | |
public TextMesh textMesh; | |
public string keyString; | |
public string keyStringShift; | |
public float width; | |
private Vector3 origPos; | |
private bool isShift; | |
public bool shift { | |
get { | |
return isShift; | |
} | |
set { | |
isShift = value; | |
if (textMesh != null) { | |
textMesh.text = isShift ? keyStringShift : keyString; | |
} | |
} | |
} | |
private float pushedTime; | |
public void Init(Board board, string a, string A, float width) { | |
this.board = board; | |
keyString = a; | |
keyStringShift = A; | |
this.width = width; | |
shift = false; | |
if (textMesh != null) { | |
Vector3 orig = textMesh.transform.localScale; | |
textMesh.transform.localScale = new Vector3 (orig.x * (1 / width), orig.y, orig.z); | |
} | |
} | |
// Use this for initialization | |
void Start () { | |
pushedTime = 0; | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
public void Push() { | |
if (pushedTime == 0f) { | |
pushedTime = Time.time; | |
origPos = transform.localPosition; | |
Vector3 newPos = new Vector3 (origPos.x, origPos.y-0.1f, origPos.z); | |
iTween.MoveTo(gameObject, iTween.Hash("position", newPos, "islocal", true, "oncomplete", "PushAnimCompleted", "time", 0.25f)); | |
if (board != null) { | |
board.keyPressed (this); | |
} | |
} | |
} | |
public void PushAnimCompleted() { | |
iTween.MoveTo(gameObject, iTween.Hash("position", origPos, "islocal", true, "oncomplete", "ReturnAnimCompleted", "time", 0.25f)); | |
} | |
public void ReturnAnimCompleted() { | |
pushedTime = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment