//------------------------------------------
// Tasharen Network
// Copyright © 2012 Tasharen Entertainment
//------------------------------------------
import System.Collections.Generic;
import UnityEngine;
import TNet;
/// <summary>
/// This example script shows how to create a chat window powered by the Tasharen Network framework.
/// You can see it used in Example Chat.
/// </summary>
class ChatEntry
{
var text:String;
var color:Color;
}
//[ExecuteInEditMode]
var mRect:Rect;
var mName:String = "Guest";
var mInput:String = "";
var ChatStyle:GUISkin ;
var mChatEntries
: List
.<ChatEntry
> = new List
.<ChatEntry
>();
/// <summary>
/// Add a new chat entry.
/// </summary>
function AddToChat (text:String, color:Color)
{
var ent
:ChatEntry
= new ChatEntry
(); ent.text = text;
ent.color = color;
mChatEntries.Add(ent);
}
/// <summary>
/// The list of players in the channel is immediately available upon joining a room.
/// </summary>
function OnNetworkJoinChannel (success:boolean, error:String)
{
mName = TNManager.playerName;
var text:String = "Players here: ";
var players:List.<Player> = TNManager.players;
for (var i = 0; i < players.size; ++i)
{
if (i > 0) text += ", ";
text += players[i].name;
if (players[i].id == TNManager.playerID) text += " (you)";
}
AddToChat(text, Color.black);
}
/// <summary>
/// Notification of a new player joining the channel.
/// </summary>
function OnNetworkPlayerJoin (p:Player)
{
AddToChat(p.name + " has joined the channel.", Color.black);
}
/// <summary>
/// Notification of another player leaving the channel.
/// </summary>
function OnNetworkPlayerLeave (p:Player)
{
AddToChat(p.name + " has left the channel.", Color.black);
}
/// <summary>
/// Notification of a player changing their name.
/// </summary>
function OnNetworkPlayerRenamed ( p:Player, previous:String)
{
AddToChat(previous + " is now known as " + p.name, Color.black);
}
/// <summary>
/// This is our chat callback. As messages arrive, they simply get added to the list.
/// </summary>
@RFC
function OnChat (playerID:int, text:String)
{
// Figure out who sent the message and add their name to the text
var player:Player = TNManager.GetPlayer(playerID);
var color:Color = (player.id == TNManager.playerID) ? Color.green : Color.white;
AddToChat("[" + player.name + "]: " + text, color);
}
/// <summary>
/// Send the typed message to the server and clear the text.
/// </summary>
function Send ()
{
if (!String.IsNullOrEmpty(mInput))
{
tno.Send("OnChat", Target.All, TNManager.playerID, mInput);
mInput = "";
}
}
/// <summary>
/// This function draws the chat window.
/// </summary>
function OnGUI ()
{
if(ChatStyle)
{
GUI.skin=ChatStyle;
}
//float cx = Screen.width * 0.5f;
//float cy = Screen.height * 0.5f;
// GUI.Box(new Rect(Screen.width * 0.5f - 270f, Screen.height * 0.5f - 200f, 540f, 410f), "");
/* GUI.Label(new Rect(cx - 140f, cy - 170f, 80f, 24f), "Nickname");
GUI.SetNextControlName("Nickname");
mName = GUI.TextField(new Rect(cx - 70f, cy - 170f, 120f, 24f), mName);
if (GUI.Button(new Rect(cx + 60f, cy - 170f, 80f, 24f), "Change"))
{
// Change the player's name when the button gets clicked.
TNManager.playerName = mName;
}*/
GUI.SetNextControlName("Chat Window");
mRect
= new Rect
(0, Screen
.height-400, 400f, 300f
); GUI.Window(0, mRect, OnGUIWindow, "");
if (Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyUp)
{
var ctrl:String = GUI.GetNameOfFocusedControl();
if (ctrl == "Nickname")
{
// Enter key pressed on the input field for the player's nickname -- change the player's name.
TNManager.playerName = mName;
GUI.FocusControl("Chat Window");
}
else if (ctrl == "Chat Input")
{
Send();
GUI.FocusControl("Chat Window");
}
else
{
// Enter key pressed -- give focus to the chat input
GUI.FocusControl("Chat Input");
}
}
}
/// <summary>
/// This function draws the chat window and the chat messages.
/// </summary>
function OnGUIWindow (id:int)
{
GUI.SetNextControlName("Chat Input");
mInput
= GUI
.TextField(new Rect
(6, mRect
.height - 30,
328,
24), mInput
);
if (GUI
.Button(new Rect
(334, mRect
.height - 31,
60,
26),
"Send")) {
Send();
GUI.FocusControl("Chat Window");
}
GUI
.BeginGroup(new Rect
(2,
20,
382,
254)); //{
var rect
:Rect
= new Rect
(4,
244,
382,
300);
for (var i = mChatEntries.size; i > 0; )
{
var ent:ChatEntry = mChatEntries[--i];
rect
.y -= GUI
.skin.label.CalcHeight(new GUIContent
(ent
.text),
382); GUI.color = ent.color;
GUI.Label(rect, ent.text, GUI.skin.label);
if (rect.y < 0) break;
}
GUI.color = Color.white;
//}
GUI.EndGroup();
}