Advertisement
toribio

toribio

Apr 5th, 2011
16,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.48 KB | None | 0 0
  1. /**
  2.  *  Progress Bar Creator
  3.  *  Copyright 2007-2010 Infernus' Group,
  4.  *  Flávio Toribio (flavio_toribio@hotmail.com)
  5.  *
  6.  *  This is an add-on for the include Progress Bar
  7.  *  http://forum.sa-mp.com/index.php?topic=138556
  8.  */
  9.  
  10. #include <a_samp>
  11. #include <progress>
  12.  
  13. #if !defined SetPVarInt
  14.     #error Version 0.3 R7 or higher of SA:MP Server requiered
  15. #endif
  16.  
  17. #if _progress_version < 0x1310
  18.     #error Version 1.3.1 or higher of progress.inc required
  19. #endif
  20.  
  21. #define DIALOG_BAR      1834
  22. #define DIALOG_COLOR    1835
  23. #define DIALOG_DONE     1836
  24.  
  25. #define ITEM_MOVE       0
  26. #define ITEM_RESIZE     1
  27. #define ITEM_COLOR      2
  28. #define ITEM_DONE       3
  29.  
  30. #define MOVE_NONE       0
  31. #define MOVE_POSITION   1
  32. #define MOVE_SIZE       2
  33.  
  34. static Bars[MAX_BARS][e_bar];
  35.  
  36. public OnFilterScriptInit()
  37. {
  38.     for(new i = 0; i < MAX_PLAYERS; ++i) if(IsPlayerConnected(i))
  39.     {
  40.         OnPlayerSpawn(i);
  41.     }
  42.     print("Progress Bar Creator by Flavio Toribio loaded");
  43.     return 1;
  44. }
  45.  
  46. public OnFilterScriptExit()
  47. {
  48.     for(new i = 0; i < MAX_PLAYERS; ++i) if(IsPlayerConnected(i))
  49.     {
  50.         OnPlayerDisconnect(i, 0);
  51.         if(GetPVarInt(i, "MovingBar"))
  52.         {
  53.             TogglePlayerControllable(i, true);
  54.         }
  55.         if(GetPVarInt(i, "CreatingBar"))
  56.         {
  57.             DeletePVar(i, "BarID");
  58.             DeletePVar(i, "MovingBar");
  59.             DeletePVar(i, "CreatingBar");
  60.         }
  61.     }
  62.     print("Progress Bar Creator by Flavio Toribio unloaded");
  63.     return 1;
  64. }
  65.  
  66. public OnPlayerSpawn(playerid)
  67. {
  68.     SendClientMessage(playerid, 0xFFF000AA, "Use /bar to start making a progress bar!");
  69.     return 1;
  70. }
  71.  
  72. GetVars(index, &Float:x, &Float:y, &Float:w, &Float:h, &color)
  73. {
  74.     x = Bars[index][pb_x];
  75.     y = Bars[index][pb_y];
  76.     w = Bars[index][pb_w];
  77.     h = Bars[index][pb_h];
  78.     color = Bars[index][pb_color];
  79. }
  80.  
  81. UpdateVars(index, Float:x, Float:y, Float:w, Float:h, color)
  82. {
  83.     Bars[index][pb_x] = x;
  84.     Bars[index][pb_y] = y;
  85.     Bars[index][pb_w] = w;
  86.     Bars[index][pb_h] = h;
  87.     Bars[index][pb_color] = color;
  88. }
  89.  
  90. DeleteVars(index)
  91. {
  92.     Bars[index][pb_x] = 0.0;
  93.     Bars[index][pb_y] = 0.0;
  94.     Bars[index][pb_w] = 0.0;
  95.     Bars[index][pb_h] = 0.0;
  96.     Bars[index][pb_color] = 0;
  97. }
  98.  
  99. public OnPlayerUpdate(playerid)
  100. {
  101.     if(GetPVarInt(playerid, "MovingBar"))
  102.     {
  103.         new keys, ud, lr;
  104.         GetPlayerKeys(playerid, keys, ud, lr);
  105.  
  106.         new Bar:barid = Bar:GetPVarInt(playerid, "BarID");
  107.         new Float:x, Float:y, Float:w, Float:h, color;
  108.  
  109.         if(ud || lr)
  110.         {
  111.             DestroyProgressBar(barid);
  112.             GetVars(_:barid, x, y, w, h, color);
  113.             DeleteVars(_:barid);
  114.             if(ud == KEY_UP)
  115.             {
  116.                 if(GetPVarInt(playerid, "MovingBar") == MOVE_POSITION)
  117.                 {
  118.                     y -= keys & KEY_SPRINT ? 2.0 : 1.0;
  119.                     if(y < 0.0) y = 0.0;
  120.                 }
  121.                 else if(GetPVarInt(playerid, "MovingBar") == MOVE_SIZE)
  122.                 {
  123.                     h -= keys & KEY_SPRINT ? 2.0 : 1.0;
  124.                     if(h < 1.5) h = 1.5;
  125.                 }
  126.             }
  127.             else if(ud == KEY_DOWN)
  128.             {
  129.                 if(GetPVarInt(playerid, "MovingBar") == MOVE_POSITION)
  130.                 {
  131.                     y += keys & KEY_SPRINT ? 2.0 : 1.0;
  132.                     if(y > 480.0) y = 480.0;
  133.                 }
  134.                 else if(GetPVarInt(playerid, "MovingBar") == MOVE_SIZE)
  135.                 {
  136.                     h += keys & KEY_SPRINT ? 2.0 : 1.0;
  137.                     if(h > 480.0) h = 480.0;
  138.                 }
  139.             }
  140.             if(lr == KEY_LEFT)
  141.             {
  142.                 if(GetPVarInt(playerid, "MovingBar") == MOVE_POSITION)
  143.                 {
  144.                     x -= keys & KEY_SPRINT ? 2.0 : 1.0;
  145.                     if(x < 0.0) x = 0.0;
  146.                 }
  147.                 else if(GetPVarInt(playerid, "MovingBar") == MOVE_SIZE)
  148.                 {
  149.                     w -= keys & KEY_SPRINT ? 2.0 : 1.0;
  150.                     if(w < 1.5) w = 1.5;
  151.                 }
  152.             }
  153.             else if(lr == KEY_RIGHT)
  154.             {
  155.                 if(GetPVarInt(playerid, "MovingBar") == MOVE_POSITION)
  156.                 {
  157.                     x += keys & KEY_SPRINT ? 2.0 : 1.0;
  158.                     if(x > 640.0) x = 640.0;
  159.                 }
  160.                 else if(GetPVarInt(playerid, "MovingBar") == MOVE_SIZE)
  161.                 {
  162.                     w += keys & KEY_SPRINT ? 2.0 : 1.0;
  163.                     if(w > 640.0) w = 640.0;
  164.                 }
  165.             }
  166.             barid = CreateProgressBar(x, y, w, h, color, 100.0);
  167.             SetProgressBarValue(barid, 50.0);
  168.             ShowProgressBarForPlayer(playerid, barid);
  169.             UpdateVars(_:barid, x, y, w, h, color);
  170.             SetPVarInt(playerid, "BarID", _:barid);
  171.         }
  172.     }
  173.     return 1;
  174. }
  175.  
  176. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  177. {
  178.     if(newkeys & KEY_SECONDARY_ATTACK)
  179.     {
  180.         if(GetPVarInt(playerid, "MovingBar"))
  181.         {
  182.             TogglePlayerControllable(playerid, true);
  183.             SetPVarInt(playerid, "MovingBar", MOVE_NONE);
  184.             SendClientMessage(playerid, 0xFFF000AA, "Now you can use /bar again to modify or finalize it.");
  185.         }
  186.     }
  187.     return 1;
  188. }
  189.  
  190. public OnPlayerDeath(playerid, killerid, reason)
  191. {
  192.     if(GetPVarInt(playerid, "MovingBar"))
  193.     {
  194.         TogglePlayerControllable(playerid, true);
  195.         SetPVarInt(playerid, "MovingBar", MOVE_NONE);
  196.     }
  197.     return 1;
  198. }
  199.  
  200. public OnPlayerDisconnect(playerid, reason)
  201. {
  202.     if(GetPVarInt(playerid, "CreatingBar") == 1)
  203.     {
  204.         DestroyProgressBar(Bar:GetPVarInt(playerid, "BarID"));
  205.         DeleteVars(GetPVarInt(playerid, "BarID"));
  206.     }
  207.     return 1;
  208. }
  209.  
  210. public OnPlayerCommandText(playerid, cmdtext[])
  211. {
  212.     if(!strcmp(cmdtext, "/bar", true))
  213.     {
  214.         if(GetPVarInt(playerid, "CreatingBar") == 0)
  215.         {
  216.             new Bar:barid;
  217.             if((barid = CreateProgressBar(320.0, 240.0, 56.50, 3.39, 0xFF0000FF, 100.0)) == INVALID_BAR_ID)
  218.             {
  219.                 SendClientMessage(playerid, 0xFF0000AA, "Internal error occurred when creating progress bar.");
  220.                 return 1;
  221.             }
  222.             SetProgressBarValue(barid, 50.0);
  223.             ShowProgressBarForPlayer(playerid, barid);
  224.             TogglePlayerControllable(playerid, false);
  225.             UpdateVars(_:barid, 320.0, 240.0, 55.5, 3.2, 0xFF0000FF);
  226.  
  227.             SetPVarInt(playerid, "MovingBar", MOVE_POSITION);
  228.             SetPVarInt(playerid, "CreatingBar", 1);
  229.             SetPVarInt(playerid, "BarID", _:barid);
  230.  
  231.             SendClientMessage(playerid, 0xFFF000AA, "Use the arrow keys to move the bar arround the screen.");
  232.             SendClientMessage(playerid, 0xFFF000AA, "Keep pressing 'Sprint' key to move faster.");
  233.             SendClientMessage(playerid, 0xFFF000AA, "Press the 'Enter car' key when done.");
  234.         }
  235.         else
  236.         {
  237.             ShowPlayerDialog(playerid, DIALOG_BAR, DIALOG_STYLE_LIST, "Progress Bar", "Change Position\nChange Size\nChange Color\nI'm done, save bar!", "OK", "Cancel");
  238.         }
  239.         return 1;
  240.     }
  241.     return 0;
  242. }
  243.  
  244. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  245. {
  246.     if(dialogid == DIALOG_BAR)
  247.     {
  248.         if(response)
  249.         {
  250.             if(listitem == ITEM_MOVE)
  251.             {
  252.                 SetPVarInt(playerid, "MovingBar", MOVE_POSITION);
  253.                 TogglePlayerControllable(playerid, false);
  254.                 SendClientMessage(playerid, 0xFFF000AA, "Use the arrow keys to move the bar arround the screen.");
  255.                 SendClientMessage(playerid, 0xFFF000AA, "Keep pressing 'Sprint' key to move faster.");
  256.                 SendClientMessage(playerid, 0xFFF000AA, "Press the 'Enter car' key when done.");
  257.             }
  258.             else if(listitem == ITEM_RESIZE)
  259.             {
  260.                 SetPVarInt(playerid, "MovingBar", MOVE_SIZE);
  261.                 TogglePlayerControllable(playerid, false);
  262.                 SendClientMessage(playerid, 0xFFF000AA, "Use the arrow keys to resize the bar.");
  263.                 SendClientMessage(playerid, 0xFFF000AA, "Keep pressing 'Sprint' key to resize faster.");
  264.                 SendClientMessage(playerid, 0xFFF000AA, "Press the 'Enter car' key when done.");
  265.             }
  266.             else if(listitem == ITEM_COLOR)
  267.             {
  268.                 ShowPlayerDialog(playerid, DIALOG_COLOR, DIALOG_STYLE_INPUT, "Change Color", "Type the color in hexadecimal format.\nExample: 0xFFF000FF\nRemember the alpha parameter (the last 2 numbers),\nthey define the transparency.\nIf you have doubts, use an external Color Picker.", "OK", "Cancel");
  269.             }
  270.             else if(listitem == ITEM_DONE)
  271.             {
  272.                 ShowPlayerDialog(playerid, DIALOG_DONE, DIALOG_STYLE_INPUT, "Saving Bar", "Type the file name which you want to save the bar;\nYou don't need to specify the extension;\nThe default one is .txt;\nThe file will be created in the scriptfiles folder;\nAny file with the same name will be replaced automatically.", "Save", "Cancel");
  273.             }
  274.         }
  275.     }
  276.     else if(dialogid == DIALOG_COLOR)
  277.     {
  278.         SetProgressBarColor(Bar:GetPVarInt(playerid, "BarID"), hexstr(inputtext));
  279.         UpdateProgressBar(Bar:GetPVarInt(playerid, "BarID"), playerid);
  280.         Bars[GetPVarInt(playerid, "BarID")][pb_color] = hexstr(inputtext);
  281.     }
  282.     else if(dialogid == DIALOG_DONE)
  283.     {
  284.         new File:file, name[32], line[128], barid;
  285.  
  286.         if(strlen(inputtext) > 32 - 4)
  287.         {
  288.             strdel(inputtext, 32 - 4, strlen(inputtext));
  289.         }
  290.         format(name, sizeof name, "%s.txt", inputtext);
  291.  
  292.         if(!(file = fopen(name, io_write)))
  293.         {
  294.             SendClientMessage(playerid, 0xFF0000AA, "There was an error on file writing, try again.");
  295.             ShowPlayerDialog(playerid, DIALOG_DONE, DIALOG_STYLE_INPUT, "Saving Bar", "Type the file name which you want to save the bar;\nYou don't need to specify the extension;\nThe default one is .txt;\nThe file will be created in the scriptfiles folder;\nAny file with the same name will be replaced automatically.", "Save", "Cancel");
  296.             return 1;
  297.         }
  298.         barid = GetPVarInt(playerid, "BarID");
  299.         format(line, sizeof line, "new Bar:bar = CreateProgressBar(%.2f, %.2f, %.2f, %.2f, %d, 100.0);\r\n",
  300.             Bars[barid][pb_x], Bars[barid][pb_y], Bars[barid][pb_w], Bars[barid][pb_h], Bars[barid][pb_color]);
  301.  
  302.         fwrite(file, line);
  303.         fwrite(file, "ShowProgressBarForAll(bar);\r\n");
  304.         fwrite(file, "\r\nNow, take a look at the official SA:MP forum topic to know how to use this:\r\n\r\nhttp://forum.sa-mp.com/index.php?topic=138556\r\n");
  305.         fclose(file);
  306.  
  307.         DeleteVars(barid);
  308.         DestroyProgressBar(Bar:barid);
  309.         DeletePVar(playerid, "BarID");
  310.         DeletePVar(playerid, "MovingBar");
  311.         DeletePVar(playerid, "CreatingBar");
  312.  
  313.         SendClientMessage(playerid, 0xFFFF00AA, "All done! Now take a look at your file to see the result!");
  314.         SendClientMessage(playerid, 0xFFFFFFAA, "You can create another progress bar now.");
  315.     }
  316.     return 1;
  317. }
  318.  
  319. stock hexstr(string[])
  320. {
  321.     new ret, val, i;
  322.     if(string[0] == '0' && (string[1] == 'x' || string[1] == 'X')) i = 2;
  323.     while(string[i])
  324.     {
  325.         ret <<= 4;
  326.         val = string[i++] - '0';
  327.         if(val > 0x09) val -= 0x07;
  328.         if(val > 0x0F) val -= 0x20;
  329.         if(val < 0x01) continue;
  330.         if(val < 0x10) ret += val;
  331.     }
  332.     return ret;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement