Created
October 22, 2013 12:48
-
-
Save naruse/7100104 to your computer and use it in GitHub Desktop.
Basically draws a calculator and all the logic inside of it.
This file contains hidden or 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
/* | |
Basically draws a calculator and all the logic inside. | |
programmed by: Juan Sebastian Munoz Arango | |
[email protected] | |
October | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
using System.Globalization; | |
public class Calc : MonoBehaviour { | |
private Rect calcSize = new Rect(360, 20, 220, 250); | |
private float[] registers = new float[2];//2 registers | |
private string currentNumber = "0"; | |
private int calcScreenFontSize = 27; | |
private int operationFontSize = 15; | |
private string currentOperationToPerform = ""; | |
private int maxDigits = 11; | |
private bool isFirst = true; | |
private bool shouldClearScreen = false; | |
private string GetCalcInternalsInfo() { | |
string info = ""; | |
info += "Screen: " + currentNumber + "\n"; | |
info += "Clear Screen?: " + shouldClearScreen + "\n"; | |
for(int i = 0; i < registers.Length; i++ ) { | |
info += "Reg[" + i + "] <= " + registers[i] + "\n"; | |
} | |
info += "Current op: " + currentOperationToPerform + "\n"; | |
info += "Register to use: " + (isFirst?"0":"1"); | |
return info; | |
} | |
void Update() { | |
GetInputFromCalc(); | |
} | |
void OnGUI() { | |
//GUILayout.Label(GetCalcInternalsInfo());//uncomment this if you want to see some info internals. | |
calcSize = GUI.Window(0, calcSize, DoMyWindow, "Calculator"); | |
} | |
void DoMyWindow(int windowID) { | |
GUI.Box(new Rect(20,20, calcSize.width-40, 43),""); | |
int tmpSize = GUI.skin.GetStyle("Label").fontSize; | |
GUI.skin.GetStyle("Label").fontSize = calcScreenFontSize; | |
GUI.Label(new Rect(23,18,calcSize.width-40, 37), currentNumber); | |
GUI.skin.GetStyle("Label").fontSize = tmpSize; | |
tmpSize = GUI.skin.GetStyle("Label").fontSize; | |
GUI.skin.GetStyle("Label").fontSize = operationFontSize; | |
GUI.Label(new Rect(130,42,calcSize.width-42, 37), currentOperationToPerform); | |
GUI.skin.GetStyle("Label").fontSize = tmpSize; | |
if(GUI.Button(new Rect(8, 70, 47, 30), "C")) { | |
ClearCalcData(); | |
} | |
if(GUI.Button(new Rect(61, 70, 47, 30), "+/-")) { | |
if(currentNumber != "0") { | |
if(currentNumber[0] != '-') | |
currentNumber = currentNumber.Insert(0,"-"); | |
else | |
currentNumber = currentNumber.Remove(0,1); | |
} | |
} | |
if(GUI.Button(new Rect(165, 141, 47, 30), "+")) | |
OperatorPressed("+"); | |
if(GUI.Button(new Rect(165, 106, 47, 30), "-")) | |
OperatorPressed("-"); | |
if(GUI.Button(new Rect(113, 70, 47, 30), "/")) | |
OperatorPressed("/"); | |
if(GUI.Button(new Rect(165, 70, 47, 30), "x")) | |
OperatorPressed("x"); | |
if(GUI.Button(new Rect(8, 176, 47, 30), "1")) | |
AppendNumber("1"); | |
if(GUI.Button(new Rect(61, 176, 47, 30), "2")) | |
AppendNumber("2"); | |
if(GUI.Button(new Rect(113, 176, 47, 30), "3")) | |
AppendNumber("3"); | |
if(GUI.Button(new Rect(8, 141, 47, 30), "4")) | |
AppendNumber("4"); | |
if(GUI.Button(new Rect(61, 141, 47, 30), "5")) | |
AppendNumber("5"); | |
if(GUI.Button(new Rect(113, 141, 47, 30), "6")) | |
AppendNumber("6"); | |
if(GUI.Button(new Rect(8, 106, 47, 30), "7")) | |
AppendNumber("7"); | |
if(GUI.Button(new Rect(61, 106, 47, 30), "8")) | |
AppendNumber("8"); | |
if(GUI.Button(new Rect(113, 106, 47, 30), "9")) | |
AppendNumber("9"); | |
if(GUI.Button(new Rect(8, 212, 100, 30), "0")) | |
AppendNumber("0"); | |
if(GUI.Button(new Rect(113, 212, 47, 30), ".")) { | |
if(!currentNumber.Contains(".") || shouldClearScreen) | |
AppendNumber("."); | |
} | |
if(GUI.Button(new Rect(165, 176, 47, 66), "=")) { | |
PerformOperation(); | |
} | |
GUI.DragWindow(); | |
} | |
private void OperatorPressed(string op) { | |
StoreCurrentNumberInReg(0); | |
isFirst = false; | |
shouldClearScreen = true; | |
currentOperationToPerform = op; | |
} | |
private void ClearCalcData() { | |
isFirst = true; | |
shouldClearScreen = true; | |
currentOperationToPerform = ""; | |
currentNumber = "0"; | |
for(int i = 0; i < registers.Length; i++) | |
registers[i] = 0; | |
} | |
private void PerformOperation() { | |
switch(currentOperationToPerform) { | |
case "+": | |
if(currentNumber != "NaN") | |
currentNumber = (registers[0] + registers[1]).ToString(); | |
break; | |
case "-": | |
if(currentNumber != "NaN") | |
currentNumber = (registers[0] - registers[1]).ToString(); | |
break; | |
case "x": | |
if(currentNumber != "NaN") | |
currentNumber = (registers[0] * registers[1]).ToString(); | |
break; | |
case "/": | |
if(currentNumber != "NaN") | |
currentNumber = (registers[1] != 0) ? (registers[0] / registers[1]).ToString() : "NaN"; | |
break; | |
case "": | |
break; | |
default: | |
Debug.LogError("Unknown operation: " + currentOperationToPerform); | |
break; | |
} | |
StoreCurrentNumberInReg(0); | |
isFirst = true; | |
shouldClearScreen = true; | |
} | |
private void StoreCurrentNumberInReg(int regNumber) { | |
registers[regNumber] = float.Parse(currentNumber, CultureInfo.InvariantCulture.NumberFormat); | |
} | |
private void AppendNumber(string s) { | |
if((currentNumber == "0") || shouldClearScreen) | |
currentNumber = (s == ".") ? "0." : s; | |
else | |
if(currentNumber.Length < maxDigits) | |
currentNumber += s; | |
if(shouldClearScreen) | |
shouldClearScreen = false; | |
StoreCurrentNumberInReg(isFirst ? 0 : 1); | |
} | |
private void GetInputFromCalc() { | |
if(Input.GetKeyDown(KeyCode.Keypad0) || Input.GetKeyDown(KeyCode.Alpha0)) | |
AppendNumber("0"); | |
if(Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Alpha1)) | |
AppendNumber("1"); | |
if(Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) | |
AppendNumber("2"); | |
if(Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) | |
AppendNumber("3"); | |
if(Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) | |
AppendNumber("4"); | |
if(Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) | |
AppendNumber("5"); | |
if(Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6)) | |
AppendNumber("6"); | |
if(Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7)) | |
AppendNumber("7"); | |
if(Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8)) | |
AppendNumber("8"); | |
if(Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9)) | |
AppendNumber("9"); | |
if(Input.GetKeyDown(KeyCode.C)) | |
ClearCalcData(); | |
if(Input.GetKeyDown(KeyCode.KeypadPeriod) || Input.GetKeyDown(KeyCode.Period)) | |
if(!currentNumber.Contains(".") || shouldClearScreen) | |
AppendNumber("."); | |
if(Input.GetKeyDown(KeyCode.KeypadDivide) || Input.GetKeyDown(KeyCode.Slash)) | |
OperatorPressed("/"); | |
if(Input.GetKeyDown(KeyCode.KeypadMultiply) || Input.GetKeyDown(KeyCode.Asterisk)) | |
OperatorPressed("*"); | |
if(Input.GetKeyDown(KeyCode.KeypadPlus) || Input.GetKeyDown(KeyCode.Plus)) | |
OperatorPressed("+"); | |
if(Input.GetKeyDown(KeyCode.KeypadMinus) || Input.GetKeyDown(KeyCode.Minus)) | |
OperatorPressed("-"); | |
if(Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) | |
PerformOperation(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment