Advertisement
Guest User

buttons.inc

a guest
Mar 30th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.65 KB | None | 0 0
  1. /*
  2.         Script Type: Include
  3.         Script Name: Buttons Include
  4.         Script Version: V1.0 (Initial Release)
  5.         Script Author: SpiritEvil
  6.         Script Description: Create Buttons using only ONE line of code!
  7.  
  8.  
  9.         ++++++++ | Cahgnge Log | +++++++++
  10.         +                                +
  11.         +  *V1.0 Initial Release         +
  12.         +                                +
  13.         +                                +
  14.         +                                +
  15.         +                                +
  16.         +                                +
  17.         +                                +
  18.         ++++++++++++++++++++++++++++++++++
  19. */
  20.  
  21. /*  Natives List:
  22.  
  23. native Button:CreateButtonForPlayer(playerid, text[], Float:x, Float:y, Float:width = 72.0, Float:height = 23.0);
  24. native ShowButtonForPlayer(playerid, Button:btnid);
  25. native DestroyButton(Button:btnid);
  26. native DestroyButtonForPlayer(playerid, Button:btnid);
  27. native HideButtonForPlayer(playerid, Button:btnid);
  28. native SetButtonTextForPlayer(playerid, Button:btnid, text[]);
  29. native ChangeButtonColorsForPlayer(playerid, Button:btnid, strokeCol, buttonCol, textCol);
  30. native IsValidButton(Button:btnid);
  31. native UpdateButtonForPlayer(playerid, Button:btnid);
  32.  
  33. */
  34.  
  35. forward OnPlayerClickButton(playerid, Button:btnid);
  36.  
  37. #include <a_samp>
  38.  
  39. #define MAX_BUTTONS 100
  40. #define INVALID_BUTTON_ID (Button:-1)
  41.  
  42. #define HOVER_COLOR 0x1696CCFF //Change this to the color you like! (default: 0x1696CCFF)
  43.  
  44. enum buttonInfo
  45. {
  46.     Float:btn_x,
  47.     Float:btn_y,
  48.     Float:btn_w,
  49.     Float:btn_h,
  50.     btn_txt[200],
  51.     btn_col_stroke,
  52.     btn_col_bg,
  53.     btn_col_txt,
  54.     bool:btn_created,
  55. }
  56.  
  57. new Buttons[MAX_BUTTONS][buttonInfo];
  58. new PlayerText:btn_td1[MAX_BUTTONS][MAX_PLAYERS],
  59.     PlayerText:btn_td2[MAX_BUTTONS][MAX_PLAYERS],
  60.     PlayerText:btn_td3[MAX_BUTTONS][MAX_PLAYERS];
  61.  
  62. stock Button:CreateButtonForPlayer(playerid, text[], Float:x, Float:y, Float:width = 72.0, Float:height = 23.0)
  63. {
  64.     new btnid;
  65.     for(btnid = 0; btnid <= sizeof(Buttons); btnid++)
  66.         if(!Buttons[btnid][btn_created]) break;
  67.     if(Buttons[btnid][btn_created] || btnid == sizeof Buttons)
  68.         return INVALID_BUTTON_ID;
  69.    
  70.     new PlayerText:textdraw;
  71.     textdraw = btn_td1[btnid][playerid] = CreatePlayerTextDraw(playerid, x, y, "_");
  72.     PlayerTextDrawBackgroundColor(playerid, textdraw, 255);
  73.     PlayerTextDrawFont(playerid, textdraw, 1);
  74.     PlayerTextDrawLetterSize(playerid, textdraw, 0.500000, height / 10);
  75.     PlayerTextDrawColor(playerid, textdraw, -1);
  76.     PlayerTextDrawSetOutline(playerid, textdraw, 0);
  77.     PlayerTextDrawSetProportional(playerid, textdraw, 1);
  78.     PlayerTextDrawSetShadow(playerid, textdraw, 1);
  79.     PlayerTextDrawUseBox(playerid, textdraw, 1);
  80.     PlayerTextDrawBoxColor(playerid, textdraw, 255);
  81.     PlayerTextDrawTextSize(playerid, textdraw, x + width, 0.000000);
  82.    
  83.     textdraw = btn_td2[btnid][playerid] = CreatePlayerTextDraw(playerid, x+2, y+3, "_");
  84.     PlayerTextDrawBackgroundColor(playerid, textdraw, 255);
  85.     PlayerTextDrawFont(playerid, textdraw, 1);
  86.     PlayerTextDrawLetterSize(playerid, textdraw, 0.500000, height / 10 - 0.5);
  87.     PlayerTextDrawColor(playerid, textdraw, -1);
  88.     PlayerTextDrawSetOutline(playerid, textdraw, 0);
  89.     PlayerTextDrawSetProportional(playerid, textdraw, 1);
  90.     PlayerTextDrawSetShadow(playerid, textdraw, 1);
  91.     PlayerTextDrawUseBox(playerid, textdraw, 1);
  92.     PlayerTextDrawBoxColor(playerid, textdraw, -16776961);
  93.     PlayerTextDrawTextSize(playerid, textdraw, x + width - 2, 0.000000);
  94.    
  95.     textdraw = btn_td3[btnid][playerid] = CreatePlayerTextDraw(playerid, x+8, y+3, text);
  96.     PlayerTextDrawBackgroundColor(playerid, textdraw, 255);
  97.     PlayerTextDrawFont(playerid, textdraw, 1);
  98.     PlayerTextDrawLetterSize(playerid, textdraw, 0.5, height / 10 - 0.8);
  99.     PlayerTextDrawColor(playerid, textdraw, -1);
  100.     PlayerTextDrawSetOutline(playerid, textdraw, 1);
  101.     PlayerTextDrawSetProportional(playerid, textdraw, 1);
  102.     PlayerTextDrawUseBox(playerid, textdraw, 1);
  103.     PlayerTextDrawBoxColor(playerid, textdraw, 0);
  104.     PlayerTextDrawTextSize(playerid, textdraw, x + width - 1, height);
  105.    
  106.     PlayerTextDrawSetSelectable(playerid, textdraw, 1);
  107.    
  108.     Buttons[btnid][btn_x] = x;
  109.     Buttons[btnid][btn_y] = y;
  110.     Buttons[btnid][btn_h] = height;
  111.     Buttons[btnid][btn_w] = width;
  112.     format(Buttons[_:btnid][btn_txt], 200, "%s", text);
  113.     Buttons[btnid][btn_col_stroke] = 255;
  114.     Buttons[btnid][btn_col_bg] = -16776961;
  115.     Buttons[btnid][btn_col_txt] = -1;
  116.     Buttons[btnid][btn_created] = true;
  117.     //printf("%i", _:btnid);
  118.     return Button:btnid;
  119.    
  120. }
  121.  
  122. stock ShowButtonForPlayer(playerid, Button:btnid)
  123. {
  124.     if(IsPlayerConnected(playerid))
  125.     {
  126.         if(!Buttons[_:btnid][btn_created]) return 0;
  127.         PlayerTextDrawShow(playerid, btn_td1[_:btnid][playerid]);
  128.         PlayerTextDrawShow(playerid, btn_td2[_:btnid][playerid]);
  129.         PlayerTextDrawShow(playerid, btn_td3[_:btnid][playerid]);
  130.         SelectTextDraw(playerid, HOVER_COLOR);
  131.         return 1;
  132.     }
  133.     return 0;
  134. }
  135.  
  136. stock DestroyButton(Button:btnid)
  137. {
  138.     for(new playerid = 0; playerid <= GetMaxPlayers(); playerid++)
  139.     {
  140.         if(IsPlayerConnected(playerid))
  141.         {
  142.             if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  143.             {
  144.                 if(!Buttons[_:btnid][btn_created]) return 0;
  145.                
  146.                 PlayerTextDrawDestroy(playerid, btn_td1[btnid][playerid]);
  147.                 PlayerTextDrawDestroy(playerid, btn_td2[btnid][playerid]);
  148.                 PlayerTextDrawDestroy(playerid, btn_td3[btnid][playerid]);
  149.                
  150.                 Buttons[btnid][btn_created] = false;
  151.                 Buttons[btnid][btn_x] = -1;
  152.                 Buttons[btnid][btn_y] = -1;
  153.                 Buttons[btnid][btn_w] = -1;
  154.                 Buttons[btnid][btn_h] = -1;
  155.                 btn_td1[btnid][playerid] = INVALID_TEXT_DRAW;
  156.                 btn_td2[btnid][playerid] = INVALID_TEXT_DRAW;
  157.                 btn_td3[btnid][playerid] = INVALID_TEXT_DRAW;
  158.                 Buttons[btnid][btn_col_stroke] = -1;
  159.                 Buttons[btnid][btn_col_bg] = -1;
  160.                 Buttons[btnid][btn_col_txt] = -1;
  161.                 return 1;
  162.             }
  163.         }
  164.     }      
  165.    
  166.     return 0;
  167. }
  168.  
  169. stock DestroyButtonForPlayer(playerid, Button:btnid)
  170. {
  171.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  172.     {
  173.         if(!Buttons[_:btnid][btn_created]) return 0;
  174.                
  175.         PlayerTextDrawDestroy(playerid, btn_td1[_:btnid][playerid]);
  176.         PlayerTextDrawDestroy(playerid, btn_td2[_:btnid][playerid]);
  177.         PlayerTextDrawDestroy(playerid, btn_td3[_:btnid][playerid]);
  178.                
  179.         Buttons[_:btnid][btn_created] = false;
  180.         Buttons[_:btnid][btn_x] = -1;
  181.         Buttons[_:btnid][btn_y] = -1;              
  182.         Buttons[_:btnid][btn_w] = -1;
  183.         Buttons[_:btnid][btn_h] = -1;
  184.         btn_td1[_:btnid][playerid] = PlayerText:-1;
  185.         btn_td2[_:btnid][playerid] = PlayerText:-1;
  186.         btn_td3[_:btnid][playerid] = PlayerText:-1;
  187.         Buttons[_:btnid][btn_col_stroke] = -1;
  188.         Buttons[_:btnid][btn_col_bg] = -1;
  189.         Buttons[_:btnid][btn_col_txt] = -1;
  190.         //Buttons[btnid][btn_txt] = "_";
  191.         return 1;
  192.     }
  193.     return 0;
  194. }
  195.  
  196. stock HideButtonForPlayer(playerid, Button:btnid)
  197. {
  198.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  199.     {
  200.         if(!Buttons[_:btnid][btn_created]) return 0;
  201.         PlayerTextDrawHide(playerid, btn_td1[_:btnid][playerid]);
  202.         PlayerTextDrawHide(playerid, btn_td2[_:btnid][playerid]);
  203.         PlayerTextDrawHide(playerid, btn_td3[_:btnid][playerid]);
  204.         return 1;
  205.     }  
  206.     return 0;
  207. }
  208.  
  209. stock SetButtonTextForPlayer(playerid, Button:btnid, text[])
  210. {
  211.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  212.     {
  213.         if(!Buttons[_:btnid][btn_created]) return 0;
  214.         PlayerTextDrawSetString(playerid, btn_td3[_:btnid][playerid], text);
  215.         format(Buttons[_:btnid][btn_txt], 200, "%s", text);
  216.         return 1;
  217.     }  
  218.     return 0;
  219. }
  220.  
  221. stock ChangeButtonColorsForPlayer(playerid, Button:btnid, strokeCol, buttonCol, textCol)
  222. {
  223.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  224.     {
  225.         if(!Buttons[_:btnid][btn_created]) return 0;
  226.         Buttons[_:btnid][btn_col_stroke] = strokeCol;
  227.         Buttons[_:btnid][btn_col_bg] = buttonCol;
  228.         Buttons[_:btnid][btn_col_txt] = textCol;
  229.         PlayerTextDrawBoxColor(playerid, btn_td1[_:btnid][playerid], strokeCol);
  230.         PlayerTextDrawBoxColor(playerid, btn_td2[_:btnid][playerid], buttonCol);
  231.         PlayerTextDrawColor(playerid, btn_td3[_:btnid][playerid], textCol);
  232.         return 1;
  233.     }
  234.     return 0;
  235. }
  236.  
  237. stock IsValidButton(Button:btnid)
  238. {
  239.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  240.     {
  241.         if(!Buttons[_:btnid][btn_created]) return false;
  242.         else return true;
  243.     }  
  244.     return false;
  245. }
  246.  
  247. stock UpdateButtonForPlayer(playerid, Button:btnid)
  248. {
  249.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  250.     {
  251.         if(!Buttons[_:btnid][btn_created]) return 0;
  252.         HideButtonForPlayer(playerid, btnid);
  253.         ShowButtonForPlayer(playerid, btnid);
  254.         return 1;
  255.     }  
  256.     return 0;
  257. }
  258.  
  259. stock B_OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  260. {
  261.     for(new i; i <= MAX_BUTTONS; i++)
  262.     {
  263.         if(Buttons[i][btn_created])
  264.         {
  265.             if(playertextid == btn_td3[i][playerid])
  266.             {
  267.                 OnPlayerClickButton(playerid, Button:i);
  268.             }
  269.         }  
  270.     }  
  271.     return 1;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement