Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chatLineArr = [];
- local chatNameArr = [];
- local curSwitch = 0;
- local chatMaxLines = 15;
- local chatShowed = true;
- local g_chatX = 5;
- local g_chatY = 5;
- chatInputSetPosition(g_chatX,g_chatY);
- function newChatLine(name,text,n_r,n_g,n_b,r,g,b)
- {
- if (chatLineArr.len() == 60)
- {
- drawDestroy(chatNameArr[0]);
- drawDestroy(chatLineArr[0]);
- chatNameArr.remove(0);
- chatLineArr.remove(0);
- }
- if (name != "")
- {
- chatNameArr.push(drawCreatePx(g_chatX,g_chatY,n_r,n_g,n_b,"Font_Old_10_White.TGA",name + ": "));
- }
- else
- {
- chatNameArr.push(drawCreatePx(g_chatX,g_chatY,n_r,n_g,n_b,"Font_Old_10_White.TGA",""));
- }
- chatLineArr.push(drawCreatePx(g_chatX + (drawGetWidthPx(chatNameArr[chatNameArr.len() - 1]) + 1),g_chatY,r,g,b,"Font_Old_10_White_Hi.TGA",text));
- if (chatLineArr.len() >= chatMaxLines && (curSwitch - (chatLineArr.len() - chatMaxLines) == 1 || (chatLineArr.len() - chatMaxLines) - curSwitch == 1))
- {
- curSwitch = (chatLineArr.len() - chatMaxLines);
- }
- }
- function chatUpdate()
- {
- local curline = 0;
- if (chatShowed == true)
- {
- if (chatLineArr.len() != 0)
- {
- for (local i = 0; i < chatLineArr.len(); ++ i)
- {
- drawSetVisible(chatLineArr[i],false);
- drawSetVisible(chatNameArr[i],false);
- }
- if (chatLineArr.len() > chatMaxLines)
- {
- for (local j = curSwitch; j < (curSwitch + chatMaxLines); ++j)
- {
- drawSetVisible(chatLineArr[j],true);
- drawSetVisible(chatNameArr[j],true);
- drawSetPositionPx(chatNameArr[j],g_chatX,g_chatY + (drawGetHeightPx(chatNameArr[j]) * curline));
- local pos = drawGetPositionPx(chatLineArr[j]);
- drawSetPositionPx(chatLineArr[j],pos.x,g_chatY +(drawGetHeightPx(chatNameArr[j]) * curline));
- curline += 1;
- }
- }
- else
- {
- for (local j = curSwitch; j < chatLineArr.len(); ++j)
- {
- drawSetVisible(chatLineArr[j],true);
- drawSetVisible(chatNameArr[j],true);
- drawSetPositionPx(chatNameArr[j],g_chatX,g_chatY + (drawGetHeightPx(chatNameArr[j]) * curline));
- local pos = drawGetPositionPx(chatLineArr[j]);
- drawSetPositionPx(chatLineArr[j],pos.x,g_chatY +(drawGetHeightPx(chatNameArr[j]) * curline));
- curline += 1;
- }
- }
- local position = drawGetPosition(chatNameArr[chatNameArr.len() - 1]);
- chatInputSetPosition(g_chatX + 10,position.y + drawGetHeight(chatNameArr[chatNameArr.len() - 1]))
- print("" + position.y + drawGetHeight(chatNameArr[chatNameArr.len() - 1]));
- }
- }
- }
- function chatSwitch(side)
- {
- if (side == 0)
- {
- if (chatLineArr.len() > chatMaxLines)
- {
- if ((curSwitch - 1) >= 0)
- {
- curSwitch-=1;
- chatUpdate();
- }
- }
- }
- else if (side == 1)
- {
- if (chatLineArr.len() > chatMaxLines)
- {
- if (chatLineArr.len() < 60)
- {
- if ((curSwitch + 1) <= (chatLineArr.len() - chatMaxLines))
- {
- curSwitch+=1;
- chatUpdate();
- }
- }
- else
- {
- if ((curSwitch + 1) <= (60 - chatMaxLines))
- {
- curSwitch+=1;
- chatUpdate();
- }
- };
- }
- }
- else if (side == 2)
- {
- curSwitch = 0;
- chatUpdate();
- }
- else if (side == 3)
- {
- if (chatLineArr.len() > chatMaxLines)
- {
- curSwitch = (chatLineArr.len() - chatMaxLines);
- chatUpdate();
- };
- }
- }
- function playerMsg(name,text,r,g,b)
- {
- newChatLine(name,text,r,g,b,255,255,255);
- chatUpdate();
- }
- function serverMsg(text,r,g,b)
- {
- newChatLine("",text,255,255,255,r,g,b);
- chatUpdate();
- }
- function sendPlayerMessage(id,sender_id,text)
- {
- if (heroId == id)
- {
- local color = getPlayerColor(sender_id);
- playerMsg(getPlayerName(sender_id),text,color.r,color.g,color.b);
- }
- }
- function clearChat()
- {
- if (chatLineArr.len() != 0)
- {
- for (local i = 0; i < chatLineArr.len(); ++i)
- {
- drawDestroy(chatLineArr[i]);
- drawDestroy(chatNameArr[i]);
- };
- };
- chatLineArr.clear();
- chatNameArr.clear();
- curSwitch = 0;
- chatInputSetPosition(g_chatX,g_chatY);
- }
- function chatVisible(toggle)
- {
- if (toggle == true)
- {
- if (chatShowed == false)
- {
- chatShowed = true;
- if (chatLineArr.len() != 0)
- {
- for (local i = 0; i < chatLineArr.len(); ++i)
- {
- drawSetVisible(chatLineArr[i],true);
- drawSetVisible(chatNameArr[i],true);
- chatUpdate();
- };
- };
- }
- }
- else if (toggle == false)
- {
- if (chatShowed == true)
- {
- chatShowed = false;
- if (chatLineArr.len() != 0)
- {
- for (local i = 0; i < chatLineArr.len(); ++i)
- {
- drawSetVisible(chatLineArr[i],false);
- drawSetVisible(chatNameArr[i],false);
- };
- };
- }
- }
- }
- function chatIsVisible()
- {
- return chatShowed;
- }
- function sendMessage(id,text,r,g,b)
- {
- if (heroId == id)
- {
- serverMsg(text,r,g,b);
- }
- }
- function setChatMaxLines(lines)
- {
- if (lines >= 5 && lines <= 30)
- {
- chatMaxLines = lines;
- chatUpdate();
- }
- }
- function getChatMaxLines()
- {
- return chatMaxLines;
- }
- function key(key)
- {
- if (key == KEY_T)
- {
- if (!chatInputIsOpen())
- {
- chatInputOpen();
- }
- }
- else if (key == KEY_RETURN)
- {
- if(chatInputIsOpen())
- {
- sendPlayerMessage(heroId,heroId,chatInputGetText());
- chatInputSend();
- chatInputClear();
- chatInputClose();
- }
- }
- else if (key == KEY_ESCAPE)
- {
- if (chatInputIsOpen())
- {
- chatInputClear();
- chatInputClose();
- }
- }
- else if (key == KEY_UP)
- {
- if (chatInputIsOpen())
- {
- chatSwitch(0);
- }
- }
- else if (key == KEY_DOWN)
- {
- if (chatInputIsOpen())
- {
- chatSwitch(1);
- }
- }
- else if (key == KEY_HOME)
- {
- if (chatInputIsOpen())
- {
- chatSwitch(2);
- }
- }
- else if (key == KEY_END)
- {
- if (chatInputIsOpen())
- {
- chatSwitch(3);
- }
- }
- }
- addEventHandler("onKey",key);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement