Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.65 KB | None | 0 0
  1.  
  2. local chatLineArr = [];
  3. local chatNameArr = [];
  4.  
  5. local curSwitch = 0;
  6.  
  7. local chatMaxLines = 15;
  8.  
  9. local chatShowed = true;
  10.  
  11. local g_chatX = 5;
  12. local g_chatY = 5;
  13. chatInputSetPosition(g_chatX,g_chatY);
  14.  
  15. function newChatLine(name,text,n_r,n_g,n_b,r,g,b)
  16. {
  17.     if (chatLineArr.len() == 60)
  18.     {
  19.         drawDestroy(chatNameArr[0]);
  20.         drawDestroy(chatLineArr[0]);
  21.         chatNameArr.remove(0);
  22.         chatLineArr.remove(0);
  23.     }
  24.     if (name != "")
  25.     {
  26.         chatNameArr.push(drawCreatePx(g_chatX,g_chatY,n_r,n_g,n_b,"Font_Old_10_White.TGA",name + ": "));
  27.     }
  28.     else
  29.     {
  30.         chatNameArr.push(drawCreatePx(g_chatX,g_chatY,n_r,n_g,n_b,"Font_Old_10_White.TGA",""));
  31.     }
  32.     chatLineArr.push(drawCreatePx(g_chatX + (drawGetWidthPx(chatNameArr[chatNameArr.len() - 1]) + 1),g_chatY,r,g,b,"Font_Old_10_White_Hi.TGA",text));
  33.     if (chatLineArr.len() >= chatMaxLines && (curSwitch - (chatLineArr.len() - chatMaxLines) == 1 || (chatLineArr.len() - chatMaxLines) - curSwitch == 1))
  34.     {
  35.         curSwitch = (chatLineArr.len() - chatMaxLines);
  36.     }
  37. }
  38.  
  39. function chatUpdate()
  40. {
  41.     local curline = 0;
  42.     if (chatShowed == true)
  43.     {
  44.         if (chatLineArr.len() != 0)
  45.         {
  46.             for (local i = 0; i < chatLineArr.len(); ++ i)
  47.             {
  48.                 drawSetVisible(chatLineArr[i],false);
  49.                 drawSetVisible(chatNameArr[i],false);
  50.             }
  51.             if (chatLineArr.len() > chatMaxLines)
  52.             {
  53.                 for (local j = curSwitch; j < (curSwitch + chatMaxLines); ++j)
  54.                 {
  55.                     drawSetVisible(chatLineArr[j],true);
  56.                     drawSetVisible(chatNameArr[j],true);
  57.                     drawSetPositionPx(chatNameArr[j],g_chatX,g_chatY + (drawGetHeightPx(chatNameArr[j]) * curline));
  58.                     local pos = drawGetPositionPx(chatLineArr[j]);
  59.                     drawSetPositionPx(chatLineArr[j],pos.x,g_chatY +(drawGetHeightPx(chatNameArr[j]) * curline));
  60.                     curline += 1;
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 for (local j = curSwitch; j < chatLineArr.len(); ++j)
  66.                 {
  67.                     drawSetVisible(chatLineArr[j],true);
  68.                     drawSetVisible(chatNameArr[j],true);
  69.                     drawSetPositionPx(chatNameArr[j],g_chatX,g_chatY + (drawGetHeightPx(chatNameArr[j]) * curline));
  70.                     local pos = drawGetPositionPx(chatLineArr[j]);
  71.                     drawSetPositionPx(chatLineArr[j],pos.x,g_chatY +(drawGetHeightPx(chatNameArr[j]) * curline));
  72.                     curline += 1;
  73.                 }
  74.             }
  75.             local position = drawGetPosition(chatNameArr[chatNameArr.len() - 1]);
  76.             chatInputSetPosition(g_chatX + 10,position.y + drawGetHeight(chatNameArr[chatNameArr.len() - 1]))
  77.             print("" + position.y + drawGetHeight(chatNameArr[chatNameArr.len() - 1]));
  78.         }
  79.     }
  80. }
  81.  
  82. function chatSwitch(side)
  83. {
  84.     if (side == 0)
  85.     {
  86.         if (chatLineArr.len() > chatMaxLines)
  87.         {
  88.             if ((curSwitch - 1) >= 0)
  89.             {
  90.                 curSwitch-=1;
  91.                 chatUpdate();
  92.             }
  93.         }
  94.     }
  95.     else if (side == 1)
  96.     {
  97.         if (chatLineArr.len() > chatMaxLines)
  98.         {
  99.             if (chatLineArr.len() < 60)
  100.             {
  101.                 if ((curSwitch + 1) <= (chatLineArr.len() - chatMaxLines))
  102.                 {
  103.                     curSwitch+=1;
  104.                     chatUpdate();
  105.                 }
  106.             }
  107.             else
  108.             {
  109.                 if ((curSwitch + 1) <= (60 - chatMaxLines))
  110.                 {
  111.                     curSwitch+=1;
  112.                     chatUpdate();
  113.                 }
  114.             };
  115.         }
  116.     }
  117.     else if (side == 2)
  118.     {
  119.         curSwitch = 0;
  120.         chatUpdate();
  121.     }
  122.     else if (side == 3)
  123.     {
  124.         if (chatLineArr.len() > chatMaxLines)
  125.         {
  126.             curSwitch = (chatLineArr.len() - chatMaxLines);
  127.             chatUpdate();
  128.         };
  129.     }
  130. }
  131.  
  132. function playerMsg(name,text,r,g,b)
  133. {
  134.     newChatLine(name,text,r,g,b,255,255,255);
  135.     chatUpdate();
  136. }
  137.  
  138. function serverMsg(text,r,g,b)
  139. {
  140.     newChatLine("",text,255,255,255,r,g,b);
  141.     chatUpdate();
  142. }
  143.  
  144. function sendPlayerMessage(id,sender_id,text)
  145. {
  146.     if (heroId == id)
  147.     {
  148.         local color = getPlayerColor(sender_id);
  149.         playerMsg(getPlayerName(sender_id),text,color.r,color.g,color.b);
  150.     }
  151. }
  152.  
  153. function clearChat()
  154. {
  155.     if (chatLineArr.len() != 0)
  156.     {
  157.         for (local i = 0; i < chatLineArr.len(); ++i)
  158.         {
  159.             drawDestroy(chatLineArr[i]);
  160.             drawDestroy(chatNameArr[i]);
  161.         };
  162.     };
  163.     chatLineArr.clear();
  164.     chatNameArr.clear();
  165.     curSwitch = 0;
  166.     chatInputSetPosition(g_chatX,g_chatY);
  167. }
  168.  
  169. function chatVisible(toggle)
  170. {
  171.     if (toggle == true)
  172.     {
  173.         if (chatShowed == false)
  174.         {
  175.             chatShowed = true;
  176.             if (chatLineArr.len() != 0)
  177.             {
  178.                 for (local i = 0; i < chatLineArr.len(); ++i)
  179.                 {
  180.                     drawSetVisible(chatLineArr[i],true);
  181.                     drawSetVisible(chatNameArr[i],true);
  182.                     chatUpdate();
  183.                 };
  184.             };
  185.         }
  186.     }
  187.     else if (toggle == false)
  188.     {
  189.         if (chatShowed == true)
  190.         {
  191.             chatShowed = false;
  192.             if (chatLineArr.len() != 0)
  193.             {
  194.                 for (local i = 0; i < chatLineArr.len(); ++i)
  195.                 {
  196.                     drawSetVisible(chatLineArr[i],false);
  197.                     drawSetVisible(chatNameArr[i],false);
  198.                 };
  199.             };
  200.         }
  201.     }
  202. }
  203.  
  204. function chatIsVisible()
  205. {
  206.     return chatShowed;
  207. }
  208.            
  209.  
  210. function sendMessage(id,text,r,g,b)
  211. {
  212.     if (heroId == id)
  213.     {
  214.         serverMsg(text,r,g,b);
  215.     }
  216. }
  217.  
  218. function setChatMaxLines(lines)
  219. {
  220.     if (lines >= 5 && lines <= 30)
  221.     {
  222.         chatMaxLines = lines;
  223.         chatUpdate();
  224.     }
  225. }
  226.  
  227. function getChatMaxLines()
  228. {
  229.     return chatMaxLines;
  230. }
  231.  
  232. function key(key)
  233. {
  234.     if (key == KEY_T)
  235.     {
  236.         if (!chatInputIsOpen())
  237.         {
  238.             chatInputOpen();
  239.         }
  240.     }
  241.     else if (key == KEY_RETURN)
  242.     {
  243.         if(chatInputIsOpen())
  244.         {
  245.             sendPlayerMessage(heroId,heroId,chatInputGetText());
  246.             chatInputSend();
  247.             chatInputClear();
  248.             chatInputClose();
  249.         }
  250.     }
  251.     else if (key == KEY_ESCAPE)
  252.     {
  253.         if (chatInputIsOpen())
  254.         {
  255.             chatInputClear();
  256.             chatInputClose();
  257.         }
  258.     }
  259.     else if (key == KEY_UP)
  260.     {
  261.         if (chatInputIsOpen())
  262.         {
  263.             chatSwitch(0);
  264.         }
  265.     }
  266.     else if (key == KEY_DOWN)
  267.     {
  268.         if (chatInputIsOpen())
  269.         {
  270.             chatSwitch(1);
  271.         }
  272.     }
  273.     else if (key == KEY_HOME)
  274.     {
  275.         if (chatInputIsOpen())
  276.         {
  277.             chatSwitch(2);
  278.         }
  279.     }
  280.     else if (key == KEY_END)
  281.     {
  282.         if (chatInputIsOpen())
  283.         {
  284.             chatSwitch(3);
  285.         }
  286.     }
  287. }
  288.  
  289. addEventHandler("onKey",key);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement