Advertisement
kasru

Basic Chat

Feb 7th, 2013
6,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. import Debug;
  5.  
  6. private var log:Array = new Array();
  7. var maxLogMessages:int = 200;
  8. var visible:boolean = true;
  9. var stringToEdit : String = "";
  10. var selectTextfield : boolean = true;
  11.  
  12. function Start() {
  13.  
  14.     Input.eatKeyPressOnTextFieldFocus = false;
  15.         log.Add("Alpha Build 1.0");
  16. }
  17.  
  18. function print(string:String)
  19.  
  20.     {
  21.  
  22.         log.push(string);
  23.         if(log.length > maxLogMessages)
  24.             log.RemoveAt(0);
  25.     }
  26.  
  27.     private var scrollPos:Vector2 = Vector2(0, 0);
  28.     private var lastLogLen:int = 0;
  29.     var printGUIStyle:GUIStyle;
  30.     var maxLogLabelHeight:float = 100.0f;
  31.  
  32. function OnGUI() {
  33.  
  34.         if(visible) {
  35.         GUI.SetNextControlName ("chatWindow");
  36.         stringToEdit = GUI.TextField (Rect (0.0, Screen.height - 50, 200, 20), stringToEdit, 25);
  37.  
  38.     if (!selectTextfield) {
  39.             GUI.FocusControl ("chatWindow");    
  40.  
  41.         }
  42.    
  43.     var logBoxWidth:float = 180.0;
  44.     var logBoxHeights:float[] = new float[log.length];
  45.     var totalHeight:float = 0.0;
  46.     var i:int = 0;
  47.  
  48.         for(var string:String in log) {
  49.  
  50.                 var logBoxHeight = Mathf.Min(maxLogLabelHeight, printGUIStyle.CalcHeight(GUIContent(string), logBoxWidth));
  51.                 logBoxHeights[i++] = logBoxHeight;
  52.             totalHeight += logBoxHeight+10.0;
  53.         }
  54.  
  55.     var innerScrollHeight:float = totalHeight;
  56.  
  57.     // if there's a new message, automatically scroll to bottom
  58.     if(lastLogLen != log.length) {
  59.         scrollPos = Vector2(0.0, innerScrollHeight);
  60.                 lastLogLen = log.length;
  61.         }
  62.  
  63.     scrollPos = GUI.BeginScrollView(Rect(0.0, Screen.height-150.0-50.0, 200, 150), scrollPos, Rect(0.0, 0.0, 180, innerScrollHeight));
  64.  
  65.             var currY:float = 0.0;
  66.             i = 0;
  67.  
  68.             for(var string:String in log) {
  69.  
  70.                 logBoxHeight = logBoxHeights[i++];
  71.                 GUI.Label(Rect(10, currY, logBoxWidth, logBoxHeight), string, printGUIStyle);
  72.                 currY += logBoxHeight+10.0;
  73.             }
  74.  
  75.             GUI.EndScrollView();
  76.         }
  77.  
  78. }
  79.  
  80. function Update () {
  81.  
  82.     if(Input.GetKeyDown("return")) {
  83.         if(selectTextfield) {
  84.             selectTextfield = !selectTextfield;
  85.         }
  86.         if(stringToEdit != "") {
  87.             log.Add("" + stringToEdit );
  88.             stringToEdit = "";
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement