Advertisement
Guest User

buttons.inc v1.1

a guest
Apr 7th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.05 KB | None | 0 0
  1. /*
  2.         Script Type: Include
  3.         Script Name: Buttons Include
  4.         Script Version: V1.1
  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.         + *V1.1 Added GetButtonpos                                    +
  14.         + *V1.1 Added GetButtonSize                                   +
  15.         + *V1.1 Added GetButtonColors                                 +
  16.         + *V1.1 Added GetButtonText                                   +
  17.         + *V1.1 Hooked OnPlayerClickTextdraw                          +
  18.         + (You will no longer need to add anything on your script)    +
  19.         + *V1.1 Makes usage of Global Textdraws instead of Player TDs +
  20.         + *V1.1 Small bug fixes                                       +
  21.         + =========================================================== +
  22.         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  23. */
  24.  
  25. /*  Natives List:
  26.  
  27. native Button:CreateButtonForPlayer(playerid, text[], Float:x, Float:y, Float:width = 72.0, Float:height = 23.0);
  28. native ShowButtonForPlayer(playerid, Button:btnid);
  29. native DestroyButton(Button:btnid);
  30. native DestroyButtonForPlayer(playerid, Button:btnid);
  31. native HideButtonForPlayer(playerid, Button:btnid);
  32. native SetButtonTextForPlayer(playerid, Button:btnid, text[]);
  33. native ChangeButtonColorsForPlayer(playerid, Button:btnid, strokeCol, buttonCol, textCol);
  34. native IsValidButton(Button:btnid);
  35. native UpdateButtonForPlayer(playerid, Button:btnid);
  36. native GetButtonPos(Button:btnid, &Float:x, &Float:y);
  37. native GetButtonSize(Button:btnid, &Float:width, &Float:height);
  38. native GetButtonColors(Button:btnid, &strokeCol, &buttonCol, &textCol);
  39. native GetButtonText(Button:btnid);
  40.  
  41. */
  42.  
  43. forward OnPlayerClickButton(playerid, Button:btnid);
  44.  
  45. #include <a_samp>
  46.  
  47. #define MAX_BUTTONS 100
  48. #define INVALID_BUTTON_ID (Button:-1)
  49.  
  50. #define HOVER_COLOR 0x1696CCFF //Change this to the color you like! (default: 0x1696CCFF)
  51.  
  52.  
  53. enum buttonInfo
  54. {
  55.     Float:btn_x,
  56.     Float:btn_y,
  57.     Float:btn_w,
  58.     Float:btn_h,
  59.     btn_txt[200],
  60.     btn_col_stroke,
  61.     btn_col_bg,
  62.     btn_col_txt,
  63.     bool:btn_created,
  64. }
  65.  
  66. new Buttons[MAX_BUTTONS][buttonInfo];
  67. new Text:btn_td1[MAX_BUTTONS][MAX_PLAYERS],
  68.     Text:btn_td2[MAX_BUTTONS][MAX_PLAYERS],
  69.     Text:btn_td3[MAX_BUTTONS][MAX_PLAYERS];
  70.  
  71. stock Button:CreateButtonForPlayer(playerid, text[], Float:x, Float:y, Float:width = 72.0, Float:height = 23.0)
  72. {
  73.     new btnid;
  74.     for(btnid = 0; btnid <= sizeof(Buttons); btnid++)
  75.         if(!Buttons[btnid][btn_created]) break;
  76.     if(Buttons[btnid][btn_created] || btnid == sizeof Buttons)
  77.         return INVALID_BUTTON_ID;
  78.    
  79.     new Text:textdraw;
  80.     textdraw = btn_td1[btnid][playerid] = TextDrawCreate(x, y, "_");
  81.     TextDrawBackgroundColor(textdraw, 255);
  82.     TextDrawFont(textdraw, 1);
  83.     TextDrawLetterSize(textdraw, 0.500000, height / 10);
  84.     TextDrawColor(textdraw, -1);
  85.     TextDrawSetOutline(textdraw, 0);
  86.     TextDrawSetProportional(textdraw, 1);
  87.     TextDrawSetShadow(textdraw, 1);
  88.     TextDrawUseBox(textdraw, 1);
  89.     TextDrawBoxColor(textdraw, 255);
  90.     TextDrawTextSize(textdraw, x + width, 0.000000);
  91.    
  92.     textdraw = btn_td2[btnid][playerid] = TextDrawCreate(x+2, y+3, "_");
  93.     TextDrawBackgroundColor(textdraw, 255);
  94.     TextDrawFont(textdraw, 1);
  95.     TextDrawLetterSize(textdraw, 0.500000, height / 10 - 0.5);
  96.     TextDrawColor(textdraw, -1);
  97.     TextDrawSetOutline(textdraw, 0);
  98.     TextDrawSetProportional(textdraw, 1);
  99.     TextDrawSetShadow(textdraw, 1);
  100.     TextDrawUseBox(textdraw, 1);
  101.     TextDrawBoxColor(textdraw, -16776961);
  102.     TextDrawTextSize(textdraw, x + width - 2, 0.000000);
  103.    
  104.     textdraw = btn_td3[btnid][playerid] = TextDrawCreate(x+8, y+3, text);
  105.     TextDrawBackgroundColor(textdraw, 255);
  106.     TextDrawFont(textdraw, 1);
  107.     TextDrawLetterSize(textdraw, 0.5, height / 10 - 0.8);
  108.     TextDrawColor(textdraw, -1);
  109.     TextDrawSetOutline(textdraw, 1);
  110.     TextDrawSetProportional(textdraw, 1);
  111.     TextDrawUseBox(textdraw, 1);
  112.     TextDrawBoxColor(textdraw, 0);
  113.     TextDrawTextSize(textdraw, x + width - 1, height);
  114.    
  115.     TextDrawSetSelectable(textdraw, 1);
  116.    
  117.     Buttons[btnid][btn_x] = x;
  118.     Buttons[btnid][btn_y] = y;
  119.     Buttons[btnid][btn_h] = height;
  120.     Buttons[btnid][btn_w] = width;
  121.     format(Buttons[_:btnid][btn_txt], 200, "%s", text);
  122.     Buttons[btnid][btn_col_stroke] = 255;
  123.     Buttons[btnid][btn_col_bg] = -16776961;
  124.     Buttons[btnid][btn_col_txt] = -1;
  125.     Buttons[btnid][btn_created] = true;
  126.     //printf("%i", _:btnid);
  127.     return Button:btnid;
  128.    
  129. }
  130.  
  131. stock ShowButtonForPlayer(playerid, Button:btnid)
  132. {
  133.     if(IsPlayerConnected(playerid))
  134.     {
  135.         if(!Buttons[_:btnid][btn_created]) return 0;
  136.         TextDrawShowForPlayer(playerid, btn_td1[_:btnid][playerid]);
  137.         TextDrawShowForPlayer(playerid, btn_td2[_:btnid][playerid]);
  138.         TextDrawShowForPlayer(playerid, btn_td3[_:btnid][playerid]);
  139.         SelectTextDraw(playerid, HOVER_COLOR);
  140.         return 1;
  141.     }
  142.     return 0;
  143. }
  144.  
  145. stock DestroyButton(Button:btnid)
  146. {
  147.     for(new playerid = 0; playerid <= GetMaxPlayers(); playerid++)
  148.     {
  149.         if(IsPlayerConnected(playerid))
  150.         {
  151.             if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  152.             {
  153.                 if(!Buttons[_:btnid][btn_created]) return 0;
  154.                
  155.                 TextDrawDestroy(btn_td1[_:btnid][playerid]);
  156.                 TextDrawDestroy(btn_td2[_:btnid][playerid]);
  157.                 TextDrawDestroy(btn_td3[_:btnid][playerid]);
  158.                
  159.                 Buttons[_:btnid][btn_created] = false;
  160.                 Buttons[_:btnid][btn_x] = -1;
  161.                 Buttons[_:btnid][btn_y] = -1;
  162.                 Buttons[_:btnid][btn_w] = -1;
  163.                 Buttons[_:btnid][btn_h] = -1;
  164.                 btn_td1[_:btnid][playerid] = Text:INVALID_TEXT_DRAW;
  165.                 btn_td2[_:btnid][playerid] = Text:INVALID_TEXT_DRAW;
  166.                 btn_td3[_:btnid][playerid] = Text:INVALID_TEXT_DRAW;
  167.                 Buttons[_:btnid][btn_col_stroke] = -1;
  168.                 Buttons[_:btnid][btn_col_bg] = -1;
  169.                 Buttons[_:btnid][btn_col_txt] = -1;
  170.                 return 1;
  171.             }
  172.         }
  173.     }      
  174.    
  175.     return 0;
  176. }
  177.  
  178. stock DestroyButtonForPlayer(playerid, Button:btnid)
  179. {
  180.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  181.     {
  182.         if(!Buttons[_:btnid][btn_created]) return 0;
  183.                
  184.         TextDrawDestroy(btn_td1[_:btnid][playerid]);
  185.         TextDrawDestroy(btn_td2[_:btnid][playerid]);
  186.         TextDrawDestroy(btn_td3[_:btnid][playerid]);
  187.                
  188.         Buttons[_:btnid][btn_created] = false;
  189.         Buttons[_:btnid][btn_x] = -1;
  190.         Buttons[_:btnid][btn_y] = -1;              
  191.         Buttons[_:btnid][btn_w] = -1;
  192.         Buttons[_:btnid][btn_h] = -1;
  193.         btn_td1[_:btnid][playerid] = Text:-1;
  194.         btn_td2[_:btnid][playerid] = Text:-1;
  195.         btn_td3[_:btnid][playerid] = Text:-1;
  196.         Buttons[_:btnid][btn_col_stroke] = -1;
  197.         Buttons[_:btnid][btn_col_bg] = -1;
  198.         Buttons[_:btnid][btn_col_txt] = -1;
  199.         //Buttons[btnid][btn_txt] = "_";
  200.         return 1;
  201.     }
  202.     return 0;
  203. }
  204.  
  205. stock HideButtonForPlayer(playerid, Button:btnid)
  206. {
  207.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  208.     {
  209.         if(!Buttons[_:btnid][btn_created]) return 0;
  210.         TextDrawHideForPlayer(playerid, btn_td1[_:btnid][playerid]);
  211.         TextDrawHideForPlayer(playerid, btn_td2[_:btnid][playerid]);
  212.         TextDrawHideForPlayer(playerid, btn_td3[_:btnid][playerid]);
  213.         return 1;
  214.     }  
  215.     return 0;
  216. }
  217.  
  218. stock SetButtonTextForPlayer(playerid, Button:btnid, text[])
  219. {
  220.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  221.     {
  222.         if(!Buttons[_:btnid][btn_created]) return 0;
  223.         TextDrawSetString(btn_td3[_:btnid][playerid], text);
  224.         format(Buttons[_:btnid][btn_txt], 200, "%s", text);
  225.         return 1;
  226.     }  
  227.     return 0;
  228. }
  229.  
  230. stock ChangeButtonColorsForPlayer(playerid, Button:btnid, strokeCol, buttonCol, textCol)
  231. {
  232.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  233.     {
  234.         if(!Buttons[_:btnid][btn_created]) return 0;
  235.         Buttons[_:btnid][btn_col_stroke] = strokeCol;
  236.         Buttons[_:btnid][btn_col_bg] = buttonCol;
  237.         Buttons[_:btnid][btn_col_txt] = textCol;
  238.         TextDrawBoxColor(btn_td1[_:btnid][playerid], strokeCol);
  239.         TextDrawBoxColor(btn_td2[_:btnid][playerid], buttonCol);
  240.         TextDrawColor(btn_td3[_:btnid][playerid], textCol);
  241.         return 1;
  242.     }
  243.     return 0;
  244. }
  245.  
  246. stock IsValidButton(Button:btnid)
  247. {
  248.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  249.     {
  250.         if(!Buttons[_:btnid][btn_created]) return false;
  251.         else return true;
  252.     }  
  253.     return false;
  254. }
  255.  
  256. stock UpdateButtonForPlayer(playerid, Button:btnid)
  257. {
  258.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  259.     {
  260.         if(!Buttons[_:btnid][btn_created]) return 0;
  261.         HideButtonForPlayer(playerid, btnid);
  262.         ShowButtonForPlayer(playerid, btnid);
  263.         return 1;
  264.     }  
  265.     return 0;
  266. }
  267.  
  268. stock GetButtonPos(Button:btnid, &Float:x, &Float:y)
  269. {
  270.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  271.     {
  272.         if(!Buttons[_:btnid][btn_created]) return 0;
  273.         x = Buttons[_:btnid][btn_x];
  274.         y = Buttons[_:btnid][btn_x];
  275.         return 1;
  276.     }
  277.     return 0;
  278. }
  279.  
  280. stock GetButtonSize(Button:btnid, &Float:width, &Float:height)
  281. {
  282.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  283.     {
  284.         if(!Buttons[_:btnid][btn_created]) return 0;
  285.         width = Buttons[_:btnid][btn_w];
  286.         height = Buttons[_:btnid][btn_h];
  287.         return 1;
  288.     }
  289.     return 0;
  290. }
  291.  
  292. stock GetButtonColors(Button:btnid, &strokeCol, &buttonCol, &textCol)
  293. {
  294.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  295.     {
  296.         if(!Buttons[_:btnid][btn_created]) return 0;
  297.         strokeCol = Buttons[_:btnid][btn_col_stroke];
  298.         buttonCol = Buttons[_:btnid][btn_col_bg];
  299.         textCol = Buttons[_:btnid][btn_col_txt];
  300.         return 1;
  301.     }  
  302.     return 0;
  303. }
  304.  
  305. stock GetButtonText(Button:btnid)
  306. {
  307.     if(btnid != INVALID_BUTTON_ID && Button:-1 < btnid < Button:MAX_BUTTONS)
  308.     {
  309.         if(Buttons[_:btnid][btn_created])
  310.             return Buttons[_:btnid][btn_txt];
  311.     }
  312.     return 0;
  313. }
  314.  
  315. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  316. {
  317.     for(new i; i <= MAX_BUTTONS; i++)
  318.     {
  319.         if(Buttons[i][btn_created])
  320.         {
  321.             if(clickedid == btn_td3[i][playerid])
  322.             {
  323.                 OnPlayerClickButton(playerid, Button:i);
  324.             }
  325.         }  
  326.     }  
  327.     return CallLocalFunction("myinc_OnPlayerClickTextDraw", "ii", playerid, _:clickedid);
  328. }
  329.  
  330. #if defined _ALS_OnPlayerClickTextDraw
  331.     #undef OnPlayerClickTextDraw
  332. #else
  333.     #define _ALS_OnPlayerClickTextDraw
  334. #endif
  335. #define OnPlayerClickTextDraw myinc_OnPlayerClickTextDraw
  336. forward myinc_OnPlayerClickTextDraw(playerid, Text:clickedid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement