Advertisement
Lemonadio

Dynamic Dialogs

Apr 15th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.99 KB | None | 0 0
  1. /*  Dynamic Dialogs BETA
  2.  *
  3.  *  (c) Copyright 2013, GameMan aka Lemonadio aka MeowCola
  4.  *
  5.  */
  6.  
  7. /* NATIVES for "Pawno"
  8. native CreateDialog(caption[], button1[], button2[] = "");
  9. native DestroyDialog(dialogid);
  10. native AddDialogItem(dialogid, itemid, itemtext[]);
  11. native UpdateDialogItem(dialogid, itemid, newitemtext[]);
  12. native GetDialogItemText(dialogid, itemid, dest[]);
  13. native DeleteDialogItem(dialogid, itemid);
  14. native UpdateDialogCaption(dialogid, newcaption[]);
  15. native GetDialogCaption(dialogid, dest[]);
  16. native UpdateDialogButton1(dialogid, newbutton[]);
  17. native UpdateDialogButton2(dialogid, newbutton[]);
  18. native GetDialogButton1(dialogid, dest[]);
  19. native GetDialogButton2(dialogid, dest[]);
  20. native ShowDialogForPlayer(playerid, dialogid);
  21. native HideDialogForPlayer(playerid);
  22. native GetPlayerDIalog(playerid);
  23. */
  24.  
  25. #if !defined _samp_included
  26.     #error "Please include 'a_samp.inc' before 'dyndial.inc'"
  27. #endif
  28.  
  29. #if defined _dynamic_dialogs_included
  30.     #endinput
  31. #endif
  32. #define _dynamic_dialogs_included
  33.  
  34. #define PrintERROR(%1)                      printf("DynamicDialogs ERROR: %s!", %1)
  35.  
  36. #define DIALOGID_OFFSET                 25000
  37. #define MAX_DIALOGS                         (MAX_PLAYERS*2)
  38. #define MAX_DIALOG_ITEMS                    (32)
  39. #define MAX_ITEM_LENGTH                 (64)
  40. #define MAX_CAPTION_LENGTH          (64)
  41. #define MAX_BUTTON_LENGTH               (8)
  42.  
  43. forward OnDynamicDialogResponse(playerid, dialogid, response, itemid, itemtext[]);
  44.  
  45. enum DD_Enum {
  46.     bool:DialogExist,
  47.     DialogCaption[MAX_CAPTION_LENGTH],
  48.     Button1[MAX_BUTTON_LENGTH],
  49.     Button2[MAX_BUTTON_LENGTH]
  50. }
  51.  
  52. new DialogInfo[MAX_DIALOGS][DD_Enum];
  53.  
  54. enum DDI_Enum {
  55.     ItemID,
  56.     ItemText[MAX_ITEM_LENGTH]
  57. }
  58.  
  59. new DialogItems[MAX_DIALOGS][MAX_DIALOG_ITEMS][DDI_Enum];
  60. new CurDialog[MAX_PLAYERS];
  61.  
  62. #if defined FILTERSCRIPT
  63. public OnFilterScriptInit() {
  64.     for(new i = 0; i < MAX_DIALOGS; i++) {
  65.         for(new di = 0; di < MAX_DIALOG_ITEMS; di++) {
  66.             DialogItems[i][di][ItemID] = -1;
  67.         }
  68.     }
  69.     foreach(new i : Players) {
  70.         CurDialog[i] = -1;
  71.     }
  72.     if(funcidx("dd_OnFilterScriptInit") != -1) return CallLocalFunction("dd_OnFilterScriptInit", "");
  73.     return 1;
  74. }
  75. #if defined _ALS_OnFilterScriptInit
  76.     #undef OnFilterScriptInit
  77. #else
  78.     #define _ALS_OnFilterScriptInit
  79. #endif
  80. #define OnFilterScriptInit dd_OnFilterScriptInit
  81. forward dd_OnFilterScriptInit();
  82.  
  83. public OnFilterScriptExit() {
  84.     for(new d = 0; d < MAX_DIALOGS; d++) {
  85.         if(DialogInfo[d][DialogExist]) DestroyDialog(d);
  86.     }
  87.     if(funcidx("dd_OnFilterScriptExit") != -1) return CallLocalFunction("dd_OnFilterScriptExit", "");
  88.     return 1;
  89. }
  90. #if defined _ALS_OnFilterScriptExit
  91.     #undef OnFilterScriptExit
  92. #else
  93.     #define _ALS_OnFilterScriptExit
  94. #endif
  95. #define OnFilterScriptExit dd_OnFilterScriptExit
  96. forward dd_OnFilterScriptExit();
  97.  
  98. #else
  99. public OnGameModeInit() {
  100.     for(new i = 0; i < MAX_DIALOGS; i++) {
  101.         for(new di = 0; di < MAX_DIALOG_ITEMS; di++) {
  102.             DialogItems[i][di][ItemID] = -1;
  103.         }
  104.     }
  105.     if(funcidx("dd_OnGameModeInit") != -1) return CallLocalFunction("dd_OnGameModeInit", "");
  106.     return 1;
  107. }
  108. #if defined _ALS_OnGameModeInit
  109.     #undef OnGameModeInit
  110. #else
  111.     #define _ALS_OnGameModeInit
  112. #endif
  113. #define OnGameModeInit dd_OnGameModeInit
  114. forward dd_OnGameModeInit();
  115.  
  116. public OnGameModeExit() {
  117.     for(new d = 0; d < MAX_DIALOGS; d++) {
  118.         if(DialogInfo[d][DialogExist]) DestroyDialog(d);
  119.     }
  120.     if(funcidx("dd_OnGameModeExit") != -1) return CallLocalFunction("dd_OnGameModeExit", "");
  121.     return 1;
  122. }
  123. #if defined _ALS_OnGameModeExit
  124.     #undef OnGameModeExit
  125. #else
  126.     #define _ALS_OnGameModeExit
  127. #endif
  128. #define OnGameModeExit dd_OnGameModeExit
  129. forward dd_OnGameModeExit();
  130.  
  131. #endif
  132.  
  133. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  134.     if(CurDialog[playerid] != -1) {
  135.         new dialog_id = CurDialog[playerid];
  136.         CurDialog[playerid] = -1;
  137.         for(new di = 0, x = 0; di < MAX_DIALOG_ITEMS; di++) {
  138.             if(DialogItems[dialog_id][di][ItemID] == -1) continue;
  139.             if(x == listitem) {
  140.                 if(funcidx("OnDynamicDialogResponse") != -1) CallLocalFunction("OnDynamicDialogResponse", "dddds", playerid, dialog_id, response, DialogItems[dialog_id][di][ItemID], inputtext);
  141.                 break;
  142.             }
  143.             x++;
  144.         }
  145.     }
  146.     if(funcidx("dd_OnDialogResponse") != -1) return CallLocalFunction("dd_OnDialogResponse", "dddds", playerid, dialogid, response, listitem, inputtext);
  147.     return 0;
  148. }
  149.  
  150. #if defined _ALS_OnDialogResponse
  151.     #undef OnDialogResponse
  152. #else
  153.     #define _ALS_OnDialogResponse
  154. #endif
  155. #define OnDialogResponse dd_OnDialogResponse
  156. forward dd_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  157.  
  158. public OnPlayerConnect(playerid)
  159. {
  160.     CurDialog[playerid] = -1;
  161.     if(funcidx("dd_OnPlayerConnect") != -1) return CallLocalFunction("dd_OnPlayerConnect", "d",playerid);
  162.     return 1;
  163. }
  164. #if defined _ALS_OnPlayerConnect
  165.     #undef OnPlayerConnect
  166. #else
  167.     #define _ALS_OnPlayerConnect
  168. #endif
  169. #define OnPlayerConnect dd_OnPlayerConnect
  170. forward dd_OnPlayerConnect(playerid);
  171.  
  172. stock CreateDialog(caption[], button1[], button2[] = "") {
  173.     if(caption[0] == EOS || strlen(caption) > MAX_CAPTION_LENGTH) return -1;
  174.     if(button1[0] == EOS || strlen(button1) > MAX_BUTTON_LENGTH || strlen(button2) > MAX_BUTTON_LENGTH) return -1;
  175.     for(new i = 0; i < MAX_DIALOGS; i++) {
  176.         if(DialogInfo[i][DialogExist]) continue;
  177.         DialogInfo[i][DialogExist] = true;
  178.         strcat(DialogInfo[i][DialogCaption], caption, MAX_CAPTION_LENGTH);
  179.         strcat(DialogInfo[i][Button1], button1, MAX_BUTTON_LENGTH);
  180.         strcat(DialogInfo[i][Button2], button2, MAX_BUTTON_LENGTH);
  181.         return i;
  182.     }
  183.     return -1;
  184. }
  185.  
  186. stock DestroyDialog(dialogid) {
  187.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  188.         PrintERROR("Dialog ID out of bounds array");
  189.         return 0;
  190.     }
  191.     if(!DialogInfo[dialogid][DialogExist]) {
  192.         PrintERROR("Dialog doesn't exist");
  193.         return 0;
  194.     }
  195.     foreach(new i : Player) {
  196.         if(CurDialog[i] != dialogid) continue;
  197.         HideDialogForPlayer(i);
  198.     }
  199.     DialogInfo[dialogid][DialogExist] = false;
  200.     strdel(DialogInfo[dialogid][DialogCaption], 0, MAX_CAPTION_LENGTH);
  201.     strdel(DialogInfo[dialogid][Button1], 0, MAX_BUTTON_LENGTH);
  202.     strdel(DialogInfo[dialogid][Button2], 0, MAX_BUTTON_LENGTH);
  203.     for(new di; di < MAX_DIALOG_ITEMS; di++) {
  204.         if(DialogItems[dialogid][di][ItemID] == -1) continue;
  205.         DialogItems[dialogid][di][ItemID] = -1;
  206.         strdel(DialogItems[dialogid][di][ItemText], 0, MAX_ITEM_LENGTH);
  207.     }
  208.     return 1;
  209. }
  210.  
  211. stock AddDialogItem(dialogid, itemid, itemtext[]) {
  212.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  213.         PrintERROR("Dialog ID out of bounds array");
  214.         return 0;
  215.     }
  216.     if(!DialogInfo[dialogid][DialogExist]) {
  217.         PrintERROR("Dialog doesn't exist");
  218.         return 0;
  219.     }
  220.     if(itemid < 0 || itemtext[0] == EOS || strlen(itemtext) > MAX_ITEM_LENGTH) return 0;
  221.     for(new di = 0; di < MAX_DIALOG_ITEMS; di++) {
  222.         if(DialogItems[dialogid][di][ItemID] != -1) continue;
  223.         DialogItems[dialogid][di][ItemID] = itemid;
  224.         strcat(DialogItems[dialogid][di][ItemText], itemtext, MAX_ITEM_LENGTH);
  225.         return 1;
  226.     }
  227.     return 0;
  228. }
  229.  
  230. stock GetDialogItemText(dialogid, itemid, dest[]) {
  231.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  232.         PrintERROR("Dialog ID out of bounds array");
  233.         return 0;
  234.     }
  235.     if(!DialogInfo[dialogid][DialogExist]) {
  236.         PrintERROR("Dialog doesn't exist");
  237.         return 0;
  238.     }
  239.     if(itemid < 0) return 0;
  240.     for(new di; di < MAX_DIALOG_ITEMS; di++) {
  241.         if(DialogItems[dialogid][di][ItemID] != itemid) continue;
  242.         strcat(dest, DialogItems[dialogid][di][ItemText], MAX_ITEM_LENGTH);
  243.         return 1;
  244.     }
  245.     return 0;
  246. }
  247.  
  248. stock DeleteDialogItem(dialogid, itemid) {
  249.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  250.         PrintERROR("Dialog ID out of bounds array");
  251.         return 0;
  252.     }
  253.     if(!DialogInfo[dialogid][DialogExist]) {
  254.         PrintERROR("Dialog doesn't exist");
  255.         return 0;
  256.     }
  257.     if(itemid < 0 || itemid >= sizeof(DialogItems[]) || DialogItems[dialogid][itemid][ItemID] == -1) return 0;
  258.     DialogItems[dialogid][itemid][ItemID] = -1;
  259.     strdel(DialogItems[dialogid][itemid][ItemText], 0, MAX_ITEM_LENGTH);
  260.     return 1;
  261. }
  262.  
  263. stock UpdateDialogItem(dialogid, itemid, newitemtext[]) {
  264.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  265.         PrintERROR("Dialog ID out of bounds array");
  266.         return 0;
  267.     }
  268.     if(!DialogInfo[dialogid][DialogExist]) {
  269.         PrintERROR("Dialog doesn't exist");
  270.         return 0;
  271.     }
  272.     if(itemid < 0) return 0;
  273.     for(new di; di < MAX_DIALOG_ITEMS; di++) {
  274.         if(DialogItems[dialogid][di][ItemID] != itemid) continue;
  275.         format(DialogItems[dialogid][di][ItemText], MAX_ITEM_LENGTH, newitemtext);
  276.         return 1;
  277.     }
  278.     return 0;
  279. }
  280.  
  281. stock UpdateDialogCaption(dialogid, newcaption[]) {
  282.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  283.         PrintERROR("Dialog ID out of bounds array");
  284.         return 0;
  285.     }
  286.     if(!DialogInfo[dialogid][DialogExist]) {
  287.         PrintERROR("Dialog doesn't exist");
  288.         return 0;
  289.     }
  290.     if(newcaption[0] == EOS || strlen(newcaption) > MAX_CAPTION_LENGTH) return 0;
  291.     format(DialogInfo[dialogid][DialogCaption], MAX_CAPTION_LENGTH, newcaption);
  292.     return 1;
  293. }
  294.  
  295. stock GetDialogCaption(dialogid, dest[]) {
  296.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  297.         PrintERROR("Dialog ID out of bounds array");
  298.         return 0;
  299.     }
  300.     if(!DialogInfo[dialogid][DialogExist]) {
  301.         PrintERROR("Dialog doesn't exist");
  302.         return 0;
  303.     }
  304.     if(caption[0] == EOS || strlen(caption) > MAX_CAPTION_LENGTH) return 0;
  305.     strcat(dest, DialogInfo[dialogid][DialogCaption], MAX_CAPTION_LENGTH);
  306.     return 1;
  307. }
  308.  
  309. stock UpdateDialogButton1(dialogid, newbutton[]) {
  310.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  311.         PrintERROR("Dialog ID out of bounds array");
  312.         return 0;
  313.     }
  314.     if(!DialogInfo[dialogid][DialogExist]) {
  315.         PrintERROR("Dialog doesn't exist");
  316.         return 0;
  317.     }
  318.     if(newbutton[0] == EOS || strlen(newbutton) > MAX_BUTTON_LENGTH) return 0;
  319.     format(DialogInfo[dialogid][Button1], MAX_BUTTON_LENGTH, newbutton);
  320.     return 1;
  321. }
  322.  
  323. stock GetDialogButton1(dialogid, dest[]) {
  324.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  325.         PrintERROR("Dialog ID out of bounds array");
  326.         return 0;
  327.     }
  328.     if(!DialogInfo[dialogid][DialogExist]) {
  329.         PrintERROR("Dialog doesn't exist");
  330.         return 0;
  331.     }
  332.     if(newbutton[0] == EOS || strlen(newbutton) > MAX_BUTTON_LENGTH) return 0;
  333.     strcat(dest, DialogInfo[dialogid][Button1],  MAX_BUTTON_LENGTH);
  334.     return 1;
  335. }
  336.  
  337. stock UpdateDialogButton2(dialogid, newbutton[]) {
  338.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  339.         PrintERROR("Dialog ID out of bounds array");
  340.         return 0;
  341.     }
  342.     if(!DialogInfo[dialogid][DialogExist]) {
  343.         PrintERROR("Dialog doesn't exist");
  344.         return 0;
  345.     }
  346.     if(strlen(newbutton) > MAX_BUTTON_LENGTH) return 0;
  347.     format(DialogInfo[dialogid][Button2], MAX_BUTTON_LENGTH, newbutton);
  348.     return 1;
  349. }
  350.  
  351. stock GetDialogButton2(dialogid, dest[]) {
  352.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  353.         PrintERROR("Dialog ID out of bounds array");
  354.         return 0;
  355.     }
  356.     if(!DialogInfo[dialogid][DialogExist]) {
  357.         PrintERROR("Dialog doesn't exist");
  358.         return 0;
  359.     }
  360.     if(newbutton[0] == EOS || strlen(newbutton) > MAX_BUTTON_LENGTH) return 0;
  361.     strcat(dest, DialogInfo[dialogid][Button2],  MAX_BUTTON_LENGTH);
  362.     return 1;
  363. }
  364.  
  365. stock ShowDialogForPlayer(playerid, dialogid) {
  366.     if(!IsPlayerConnected(playerid)) return 0;
  367.     if(dialogid < 0 || dialogid >= sizeof(DialogInfo)) {
  368.         PrintERROR("Dialog ID out of bounds array");
  369.         return 0;
  370.     }
  371.     if(!DialogInfo[dialogid][DialogExist]) {
  372.         PrintERROR("Dialog doesn't exist");
  373.         return 0;
  374.     }
  375.     CurDialog[playerid] = dialogid;
  376.     new msg[2048];
  377.     for(new di = 0; di < MAX_DIALOG_ITEMS; di++) {
  378.         if(DialogItems[dialogid][di][ItemID] == -1) continue;
  379.         new tmp[MAX_ITEM_LENGTH+2];
  380.         format(tmp, sizeof(tmp), "%s\n", DialogItems[dialogid][di][ItemText]);
  381.         strcat(msg, tmp);
  382.     }
  383.     ShowPlayerDialog(playerid, dialogid+DIALOGID_OFFSET, DIALOG_STYLE_LIST, DialogInfo[dialogid][DialogCaption], msg, DialogInfo[dialogid][Button1], DialogInfo[dialogid][Button2]);
  384.     return 1;
  385. }
  386.  
  387. stock HideDialogForPlayer(playerid) {
  388.     if(!IsPlayerConnected(playerid)) return 0;
  389.     CurDialog[playerid] = -1;
  390.     ShowPlayerDialog(playerid, -1, DIALOG_STYLE_MSGBOX, " ", " ", " ", " ");
  391.     return 1;
  392. }
  393.  
  394. stock GetPlayerDIalog(playerid) {
  395.     if(!IsPlayerConnected(playerid)) return -1;
  396.     return CurDialog[playerid];
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement