Advertisement
AwDod

Untitled

Oct 24th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 19.21 KB | None | 0 0
  1. /*
  2.  
  3.     About: modern dialog system
  4.     Author: ziggi
  5.  
  6. */
  7.  
  8. #if defined _mdialog_included
  9.     #endinput
  10. #endif
  11.  
  12. #define _mdialog_included
  13.  
  14. /*
  15.     Defines
  16. */
  17.  
  18. // external defines
  19. #if !defined MDIALOG_DIALOG_ID
  20.     #define MDIALOG_DIALOG_ID 32700
  21. #endif
  22.  
  23. #if !defined MAX_FUNCTION_NAME
  24.     #define MAX_FUNCTION_NAME 32
  25. #endif
  26.  
  27. #if !defined MDIALOG_MAX_LINES
  28.     #define MDIALOG_MAX_LINES 100
  29. #endif
  30.  
  31. #if !defined MDIALOG_MAX_SPACES
  32.     #define MDIALOG_MAX_SPACES 200
  33. #endif
  34.  
  35. #if !defined MDIALOG_MIN_DIALOG_WIDTH
  36.     #define MDIALOG_MIN_DIALOG_WIDTH 200.0
  37. #endif
  38.  
  39. #if !defined MDIALOG_MAX_CAPTION_SIZE
  40.     #define MDIALOG_MAX_CAPTION_SIZE 64
  41. #endif
  42.  
  43. #if !defined MDIALOG_MAX_INFO_SIZE
  44.     #define MDIALOG_MAX_INFO_SIZE 4096
  45. #endif
  46.  
  47. #if defined MDIALOG_ZLANG_MODE
  48.     #if !defined MDIALOG_MAX_BUTTON_SIZE
  49.         #define MDIALOG_MAX_BUTTON_SIZE 16
  50.     #endif
  51.  
  52.     #if MDIALOG_MAX_CAPTION_SIZE > MAX_LANG_VALUE_STRING
  53.         #undef MDIALOG_MAX_CAPTION_SIZE
  54.         #define MDIALOG_MAX_CAPTION_SIZE MAX_LANG_VALUE_STRING
  55.     #endif
  56.  
  57.     #if MDIALOG_MAX_INFO_SIZE > MAX_LANG_MVALUE_STRING
  58.         #undef MDIALOG_MAX_INFO_SIZE
  59.         #define MDIALOG_MAX_INFO_SIZE MAX_LANG_MVALUE_STRING
  60.     #endif
  61.  
  62.     #if MDIALOG_MAX_BUTTON_SIZE > MAX_LANG_VALUE_STRING
  63.         #undef MDIALOG_MAX_BUTTON_SIZE
  64.         #define MDIALOG_MAX_BUTTON_SIZE MAX_LANG_VALUE_STRING
  65.     #endif
  66. #endif
  67.  
  68. #if !defined FIX_const
  69.     #define FIX_const 0
  70.     #define MDIALOG_FIX_CLEAR
  71. #endif
  72.  
  73. native MDialogFix_ShowPlayerDialog(playerid, dialogid, style, const caption[], const info[], const button1[], const button2[]) = ShowPlayerDialog;
  74.  
  75. #if !FIX_const
  76.     #if defined _ALS_ShowPlayerDialog
  77.         #undef ShowPlayerDialog
  78.     #else
  79.         #define _ALS_ShowPlayerDialog
  80.     #endif
  81.  
  82.     #define ShowPlayerDialog MDialogFix_ShowPlayerDialog
  83.  
  84.     #if defined MDIALOG_FIX_CLEAR
  85.         #undef MDIALOG_FIX_CLEAR
  86.         #undef FIX_const
  87.     #endif
  88. #endif
  89.  
  90. #if !defined isnull
  91.     #define isnull(%1) \
  92.                 ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  93. #endif
  94.  
  95. // internal defines
  96. #define DialogCreate:%0(%1) \
  97.     forward dcr_%0(%1); \
  98.     public dcr_%0(%1)
  99.  
  100. #define DialogResponse:%0(%1) \
  101.     forward dre_%0(%1); \
  102.     public dre_%0(%1)
  103.  
  104. #define DialogInterrupt:%0(%1) \
  105.     forward din_%0(%1); \
  106.     public din_%0(%1)
  107.  
  108. #define Dialog: #
  109.  
  110. /*
  111.     Enums
  112. */
  113.  
  114. enum E_MDIALOG_TAG {
  115.     E_MDIALOG_TAG_NONE = -1,
  116.     E_MDIALOG_TAG_CENTER,
  117.     E_MDIALOG_TAG_RIGHT,
  118. };
  119.  
  120. /*
  121.     Vars
  122. */
  123.  
  124. #if !defined MDIALOG_DISABLE_TAGS
  125. static
  126.     Float:gCharSize[256] = {
  127.         -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,
  128.         4.4453125, // space
  129.         5.328125, // !
  130.         7.5859375, // "
  131.         8.8984375, // #
  132.         8.8984375, // $
  133.         14.2265625, // %
  134.         11.5546875, // &
  135.         3.8046875, // '
  136.         5.328125, // (
  137.         5.328125, // )
  138.         6.2265625, // *
  139.         9.34375, // +
  140.         4.4453125, // ,
  141.         5.328125, // -
  142.         4.4453125, // .
  143.         4.4453125, // /
  144.         8.8984375, // 0
  145.         8.8984375, // 1
  146.         8.8984375, // 2
  147.         8.8984375, // 3
  148.         8.8984375, // 4
  149.         8.8984375, // 5
  150.         8.8984375, // 6
  151.         8.8984375, // 7
  152.         8.8984375, // 8
  153.         8.8984375, // 9
  154.         5.328125, // :
  155.         5.328125, // ;
  156.         9.34375, // <
  157.         9.34375, // =
  158.         9.34375, // >
  159.         9.7734375, // ?
  160.         15.6015625, // @
  161.         11.5546875, // A
  162.         11.5546875, // B
  163.         11.5546875, // C
  164.         11.5546875, // D
  165.         10.671875, // E
  166.         9.7734375, // F
  167.         12.4453125, // G
  168.         11.5546875, // H
  169.         4.4453125, // I
  170.         8.8984375, // J
  171.         11.5546875, // K
  172.         9.7734375, // L
  173.         13.328125, // M
  174.         11.5546875, // N
  175.         12.4453125, // O
  176.         10.671875, // P
  177.         12.4453125, // Q
  178.         11.5546875, // R
  179.         10.671875, // S
  180.         9.7734375, // T
  181.         11.5546875, // U
  182.         10.671875, // V
  183.         15.1015625, // W
  184.         10.671875, // X
  185.         10.671875, // Y
  186.         9.7734375, // Z
  187.         5.328125, // [
  188.         4.4453125, // backslash
  189.         5.328125, // ]
  190.         9.34375, // ^
  191.         8.8984375, // _
  192.         5.328125, // `
  193.         8.8984375, // a
  194.         9.7734375, // b
  195.         8.8984375, // c
  196.         9.7734375, // d
  197.         8.8984375, // e
  198.         5.328125, // f
  199.         9.7734375, // g
  200.         9.7734375, // h
  201.         4.4453125, // i
  202.         4.4453125, // j
  203.         8.8984375, // k
  204.         4.4453125, // l
  205.         14.2265625, // m
  206.         9.7734375, // n
  207.         9.7734375, // o
  208.         9.7734375, // p
  209.         9.7734375, // q
  210.         6.2265625, // r
  211.         8.8984375, // s
  212.         5.328125, // t
  213.         9.7734375, // u
  214.         8.8984375, // v
  215.         12.4453125, // w
  216.         8.8984375, // x
  217.         8.8984375, // y
  218.         8.0, // z
  219.         6.2265625, // {
  220.         4.4765625, // |
  221.         6.2265625, // }
  222.         9.34375, // ~
  223.         8.0, // 
  224.         14.1640625, // �
  225.         9.0703125, // �
  226.         4.4453125, // �
  227.         6.6640625, // �
  228.         8.0, // �
  229.         16.0, // �
  230.         8.8984375, // �
  231.         8.8984375, // �
  232.         8.8984375, // �
  233.         16.0, // �
  234.         17.5, // �
  235.         5.328125, // �
  236.         17.0, // �
  237.         9.765625, // �
  238.         14.0, // �
  239.         11.5, // �
  240.         9.7734375, // �
  241.         4.4453125, // �
  242.         4.4453125, // �
  243.         8.0, // �
  244.         8.0, // �
  245.         5.6015625, // �
  246.         8.8984375, // �
  247.         16.0, // �
  248.         16.0, // �
  249.         15.5, // �
  250.         5.328125, // �
  251.         14.5, // �
  252.         8.0078125, // �
  253.         9.7734375, // �
  254.         9.6640625, // �
  255.         4.4453125, //
  256.         9.953125, // �
  257.         8.8984375, // �
  258.         8.8984375, // �
  259.         8.8984375, // �
  260.         7.7890625, // �
  261.         4.4765625, // �
  262.         8.8984375, // �
  263.         -1.0,
  264.         10.703125, // �
  265.         11.7890625, // �
  266.         11.3828125, // �
  267.         8.8984375, // �
  268.         9.34375, // �
  269.         0.0, // �
  270.         11.7890625, // �
  271.         4.4140625, // �
  272.         6.3984375, // �
  273.         8.78125, // �
  274.         4.4453125, // �
  275.         4.4453125, // �
  276.         7.1484375, // �
  277.         9.21875, // �
  278.         8.8984375, // �
  279.         5.328125, // �
  280.         8.8984375, // �
  281.         17.8359375, // �
  282.         8.8359375, // �
  283.         8.8984375, // �
  284.         4.4453125, // �
  285.         10.671875, // �
  286.         8.8984375, // �
  287.         4.5, // �
  288.         11.5546875, // �
  289.         11.5, // �
  290.         11.5546875, // �
  291.         9.0703125, // �
  292.         11.3984375, // �
  293.         10.671875, // �
  294.         14.4609375, // �
  295.         10.0234375, // �
  296.         11.5, // �
  297.         11.5, // �
  298.         9.765625, // �
  299.         11.2265625, // �
  300.         13.328125, // �
  301.         11.5546875, // �
  302.         12.4453125, // �
  303.         11.5, // �
  304.         10.671875, // �
  305.         11.5546875, // �
  306.         9.7734375, // �
  307.         9.953125, // �
  308.         13.65625, // �
  309.         10.671875, // �
  310.         11.6875, // �
  311.         11.2421875, // �
  312.         16.078125, // �
  313.         16.3046875, // �
  314.         13.9140625, // �
  315.         15.6640625, // �
  316.         11.5, // �
  317.         11.3828125, // �
  318.         16.5, // �
  319.         11.5, // �
  320.         8.8984375, // �
  321.         9.8828125, // �
  322.         9.8359375, // �
  323.         6.6640625, // �
  324.         10.15625, // �
  325.         8.8984375, // �
  326.         11.34375, // �
  327.         7.953125, // �
  328.         9.8359375, // �
  329.         9.8359375, // �
  330.         8.0078125, // �
  331.         10.1640625, // �
  332.         11.8359375, // �
  333.         9.6640625, // �
  334.         9.7734375, // �
  335.         9.6640625, // �
  336.         9.7734375, // �
  337.         8.8984375, // �
  338.         7.8359375, // �
  339.         8.8984375, // �
  340.         14.0, // �
  341.         8.8984375, // �
  342.         9.8359375, // �
  343.         9.2890625, // �
  344.         13.3359375, // �
  345.         13.5, // �
  346.         11.6640625, // �
  347.         13.6640625, // �
  348.         9.8359375, // �
  349.         8.8359375, // �
  350.         13.6640625, // �
  351.         9.3359375 // �
  352.     };
  353.  
  354. static
  355.     gTag[E_MDIALOG_TAG][] = {
  356.         "\\c",
  357.         "\\r"
  358.     },
  359.     gTagLength[E_MDIALOG_TAG];
  360.  
  361. #endif
  362.  
  363. static
  364.     bool:gIsDialogOpen[MAX_PLAYERS char],
  365.     gDialogFunction[MAX_PLAYERS][MAX_FUNCTION_NAME];
  366.  
  367. /*
  368.     OnGameModeInit
  369. */
  370.  
  371. #if !defined MDIALOG_DISABLE_TAGS
  372.  
  373. public OnGameModeInit()
  374. {
  375.     for (new tag = 0; tag < sizeof(gTag); tag++) {
  376.         gTagLength[E_MDIALOG_TAG:tag] = strlen(gTag[E_MDIALOG_TAG:tag]);
  377.     }
  378.  
  379.     #if defined MDialog_OnGameModeInit
  380.         return MDialog_OnGameModeInit();
  381.     #else
  382.         return 1;
  383.     #endif
  384. }
  385. #if defined _ALS_OnGameModeInit
  386.     #undef OnGameModeInit
  387. #else
  388.     #define _ALS_OnGameModeInit
  389. #endif
  390.  
  391. #define OnGameModeInit MDialog_OnGameModeInit
  392. #if defined MDialog_OnGameModeInit
  393.     forward MDialog_OnGameModeInit();
  394. #endif
  395.  
  396. #endif
  397.  
  398. /*
  399.     ShowPlayerDialog
  400. */
  401.  
  402. stock MDialog_ShowPlayerDialog(playerid, dialogid, style, const caption[], const info[], const button1[], const button2[])
  403. {
  404.     if (dialogid != -1 && dialogid != MDIALOG_DIALOG_ID) {
  405.         if (Dialog_IsOpen(playerid)) {
  406.             Dialog_Close(playerid, gDialogFunction[playerid][4], false);
  407.         }
  408.     }
  409.  
  410.     return ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2);
  411. }
  412. #if defined _ALS_ShowPlayerDialog
  413.     #undef ShowPlayerDialog
  414. #else
  415.     #define _ALS_ShowPlayerDialog
  416. #endif
  417.  
  418. #define ShowPlayerDialog MDialog_ShowPlayerDialog
  419.  
  420. /*
  421.     Dialog_Open
  422. */
  423.  
  424. static stock _Dialog_Open(playerid, const function[], style, const caption[], const info[], const button1[], const button2[])
  425. {
  426.     if (Dialog_IsOpen(playerid)) {
  427.         Dialog_Interrupt(playerid, gDialogFunction[playerid][4]);
  428.     }
  429.  
  430.     gIsDialogOpen{playerid} = true;
  431.  
  432.     gDialogFunction[playerid] = "dre_";
  433.     strcat(gDialogFunction[playerid], function, sizeof(gDialogFunction[]));
  434.  
  435.     switch (style) {
  436.         case DIALOG_STYLE_MSGBOX, DIALOG_STYLE_INPUT, DIALOG_STYLE_PASSWORD: {
  437.             #if defined MDIALOG_DISABLE_TAGS
  438.                 return ShowPlayerDialog(playerid, MDIALOG_DIALOG_ID, style, caption, info, button1, button2);
  439.             #else
  440.                 static info_result[MDIALOG_MAX_INFO_SIZE];
  441.                 _MDialog_ProcessTags(info, info_result);
  442.                 return ShowPlayerDialog(playerid, MDIALOG_DIALOG_ID, style, caption, info_result, button1, button2);
  443.             #endif
  444.         }
  445.     }
  446.  
  447.     return ShowPlayerDialog(playerid, MDIALOG_DIALOG_ID, style, caption, info, button1, button2);
  448. }
  449.  
  450. #if defined MDIALOG_ZLANG_MODE
  451.  
  452. stock Dialog_Open(playerid, const function[], style, const caption[], const info[], const button1[], const button2[], lang_args<>)
  453. {
  454.     static
  455.         Lang:lang,
  456.         temp_caption[MDIALOG_MAX_CAPTION_SIZE],
  457.         temp_button1[MDIALOG_MAX_BUTTON_SIZE],
  458.         temp_button2[MDIALOG_MAX_BUTTON_SIZE],
  459.         temp_info[MDIALOG_MAX_INFO_SIZE];
  460.  
  461.     lang = Lang_GetPlayerLang(playerid);
  462.  
  463.     Lang_GetText(lang, caption, temp_caption);
  464.     Lang_GetText(lang, info, temp_info);
  465.     Lang_GetText(lang, button1, temp_button1);
  466.  
  467.     if (!isnull(button2)) {
  468.         Lang_GetText(lang, button2, temp_button2);
  469.     } else {
  470.         temp_button2[0] = '\0';
  471.     }
  472.  
  473.     if (numargs() > 7) {
  474.         Lang_format(temp_info, sizeof(temp_info), temp_info, lang_start<7>);
  475.     }
  476.  
  477.     return _Dialog_Open(playerid, function, style, temp_caption, temp_info, temp_button1, temp_button2);
  478. }
  479.  
  480. #else
  481.  
  482. stock Dialog_Open(playerid, const function[], style, const caption[], const info[], const button1[], const button2[])
  483. {
  484.     return _Dialog_Open(playerid, function, style, caption, info, button1, button2);
  485. }
  486.  
  487. #endif
  488.  
  489. /*
  490.     Dialog_Close
  491. */
  492.  
  493. stock Dialog_Close(playerid, const function[] = "", showDialog = true)
  494. {
  495.     gIsDialogOpen{playerid} = false;
  496.     gDialogFunction[playerid][0] = '\0';
  497.  
  498.     Dialog_Interrupt(playerid, function);
  499.  
  500.     if (showDialog) {
  501.         return MDialogFix_ShowPlayerDialog(playerid, -1, DIALOG_STYLE_MSGBOX, " ", " ", " ", "");
  502.     }
  503.  
  504.     return 1;
  505. }
  506.  
  507. stock Dialog_Interrupt(playerid, const function[])
  508. {
  509.     if (!isnull(function)) {
  510.         new call_func[MAX_FUNCTION_NAME char] = !"din_";
  511.         strcat(call_func, function);
  512.         return CallLocalFunction(call_func, "d", playerid);
  513.     }
  514.     return 0;
  515. }
  516.  
  517. /*
  518.     Dialog_IsOpen
  519. */
  520.  
  521. stock Dialog_IsOpen(playerid, const function[] = "")
  522. {
  523.     if (isnull(function)) {
  524.         return _:gIsDialogOpen{playerid};
  525.     }
  526.  
  527.     if (!isnull(gDialogFunction[playerid]) && gIsDialogOpen{playerid} && strcmp(gDialogFunction[playerid][4], function) == 0) {
  528.         return 1;
  529.     }
  530.  
  531.     return 0;
  532. }
  533.  
  534. /*
  535.     Dialog_Show
  536. */
  537.  
  538. stock Dialog_Show(playerid, const function[])
  539. {
  540.     new call_func[MAX_FUNCTION_NAME char] = !"dcr_";
  541.     strcat(call_func, function);
  542.     return CallLocalFunction(call_func, "d", playerid);
  543. }
  544.  
  545. /*
  546.     Dialog_GetCurrent
  547. */
  548.  
  549. stock Dialog_GetCurrent(playerid, function[], const size = sizeof(function))
  550. {
  551.     if (gDialogFunction[playerid][0] == '\0') {
  552.         function[0] = '\0';
  553.         return 0;
  554.     }
  555.  
  556.     return _MDialog_strcpy(function, gDialogFunction[playerid][4], size);
  557. }
  558.  
  559. /*
  560.     Dialog_Message
  561. */
  562.  
  563. #if defined MDIALOG_ZLANG_MODE
  564.  
  565. stock Dialog_Message(playerid, const caption[], const info[], const button1[], lang_args<>)
  566. {
  567.     static
  568.         Lang:lang,
  569.         temp_caption[MDIALOG_MAX_CAPTION_SIZE],
  570.         temp_button1[MDIALOG_MAX_BUTTON_SIZE],
  571.         temp_info[MDIALOG_MAX_INFO_SIZE];
  572.  
  573.     lang = Lang_GetPlayerLang(playerid);
  574.  
  575.     Lang_GetText(lang, caption, temp_caption);
  576.     Lang_GetText(lang, info, temp_info);
  577.     Lang_GetText(lang, button1, temp_button1);
  578.  
  579.     if (numargs() > 4) {
  580.         Lang_format(temp_info, sizeof(temp_info), temp_info, lang_start<4>);
  581.     }
  582.  
  583.     return _Dialog_Open(playerid, Dialog:Message, DIALOG_STYLE_MSGBOX, temp_caption, temp_info, temp_button1, "");
  584. }
  585.  
  586. #else
  587.  
  588. stock Dialog_Message(playerid, const caption[], const info[], const button1[])
  589. {
  590.     return _Dialog_Open(playerid, Dialog:Message, DIALOG_STYLE_MSGBOX, caption, info, button1, "");
  591. }
  592.  
  593. #endif
  594.  
  595. /*
  596.     Dialog_MessageEx
  597. */
  598.  
  599. #if defined MDIALOG_ZLANG_MODE
  600.  
  601. stock Dialog_MessageEx(playerid, const response[], const caption[], const info[], const button1[], const button2[], lang_args<>)
  602. {
  603.     static
  604.         Lang:lang,
  605.         temp_caption[MDIALOG_MAX_CAPTION_SIZE],
  606.         temp_button1[MDIALOG_MAX_BUTTON_SIZE],
  607.         temp_button2[MDIALOG_MAX_BUTTON_SIZE],
  608.         temp_info[MDIALOG_MAX_INFO_SIZE];
  609.  
  610.     lang = Lang_GetPlayerLang(playerid);
  611.  
  612.     Lang_GetText(lang, caption, temp_caption);
  613.     Lang_GetText(lang, info, temp_info);
  614.     Lang_GetText(lang, button1, temp_button1);
  615.  
  616.     if (!isnull(button2)) {
  617.         Lang_GetText(lang, button2, temp_button2);
  618.     } else {
  619.         temp_button2[0] = '\0';
  620.     }
  621.  
  622.     if (numargs() > 6) {
  623.         Lang_format(temp_info, sizeof(temp_info), temp_info, lang_start<6>);
  624.     }
  625.  
  626.     return _Dialog_Open(playerid, response, DIALOG_STYLE_MSGBOX, temp_caption, temp_info, temp_button1, temp_button2);
  627. }
  628.  
  629. #else
  630.  
  631. stock Dialog_MessageEx(playerid, const response[], const caption[], const info[], const button1[], const button2[])
  632. {
  633.     return _Dialog_Open(playerid, response, DIALOG_STYLE_MSGBOX, caption, info, button1, button2);
  634. }
  635.  
  636. #endif
  637.  
  638. /*
  639.     OnDialogResponse
  640. */
  641.  
  642. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  643. {
  644.     static temp[128];
  645.     _MDialog_strcpy(temp, inputtext);
  646.  
  647.     if (dialogid == MDIALOG_DIALOG_ID) {
  648.         if (temp[0] == '\0') {
  649.             if (Dialog_IsOpen(playerid)) {
  650.                 gIsDialogOpen{playerid} = false;
  651.                 return CallLocalFunction(gDialogFunction[playerid], "ddds", playerid, response, listitem, "\1");
  652.             }
  653.             return 0;
  654.         }
  655.  
  656.         for (new i = strlen(temp) - 1; i != -1; i--) {
  657.             if (temp[i] == '%') {
  658.                 temp[i] = '#';
  659.             }
  660.         }
  661.  
  662.         if (Dialog_IsOpen(playerid)) {
  663.             gIsDialogOpen{playerid} = false;
  664.             return CallLocalFunction(gDialogFunction[playerid], "ddds", playerid, response, listitem, temp);
  665.         }
  666.         return 0;
  667.     }
  668.  
  669.     #if defined MDialog_OnDialogResponse
  670.         return MDialog_OnDialogResponse(playerid, dialogid, response, listitem, temp);
  671.     #else
  672.         return 0;
  673.     #endif
  674. }
  675. #if defined _ALS_OnDialogResponse
  676.     #undef OnDialogResponse
  677. #else
  678.     #define _ALS_OnDialogResponse
  679. #endif
  680.  
  681. #define OnDialogResponse MDialog_OnDialogResponse
  682. #if defined MDialog_OnDialogResponse
  683.     forward MDialog_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  684. #endif
  685.  
  686. /*
  687.     _MDialog_ProcessTags
  688. */
  689. #if !defined MDIALOG_DISABLE_TAGS
  690.  
  691. static stock _MDialog_ProcessTags(const info[], result_info[], const size = sizeof(result_info))
  692. {
  693.     static
  694.         line_lengths[MDIALOG_MAX_LINES],
  695.         Float:line_sizes[MDIALOG_MAX_LINES],
  696.         line_pos[MDIALOG_MAX_LINES],
  697.         sep_pos,
  698.         prev_sep_pos,
  699.         E_MDIALOG_TAG:tag,
  700.         tag_pos,
  701.         tag_length,
  702.         E_MDIALOG_TAG:tag_line[MDIALOG_MAX_LINES],
  703.         lines_count,
  704.         length,
  705.         Float:max_size,
  706.         i,
  707.         Float:temp_size;
  708.  
  709.     // if string is packed
  710.     if (info{0} != 0) {
  711.         strunpack(result_info, info, size);
  712.     } else {
  713.         _MDialog_strcpy(result_info, info, size);
  714.     }
  715.  
  716.     // reset variables
  717.     lines_count = 0;
  718.     sep_pos = 0;
  719.     prev_sep_pos = 0;
  720.     max_size = 0.0;
  721.  
  722.     // get biggest line and save line sizes
  723.     while (sep_pos >= 0) {
  724.         // get line length
  725.         sep_pos = strfind(result_info, "\n", false, prev_sep_pos + 1);
  726.         if (sep_pos == -1) {
  727.             length = strlen(result_info) - prev_sep_pos - 1;
  728.         } else {
  729.             length = sep_pos - prev_sep_pos - _:(lines_count != 0);
  730.             line_pos[lines_count + 1] = sep_pos + 1;
  731.         }
  732.  
  733.         // check for tag
  734.         tag_line[lines_count] = E_MDIALOG_TAG_NONE;
  735.  
  736.         for (tag = E_MDIALOG_TAG:0; tag < E_MDIALOG_TAG; tag++) {
  737.             tag_pos = line_pos[lines_count] - 1;
  738.             if (tag_pos < 0) {
  739.                 tag_pos = 0;
  740.             }
  741.  
  742.             tag_pos = strfind(result_info, gTag[tag], false, tag_pos);
  743.  
  744.             // if tag exists
  745.             if (tag_pos != -1 && (sep_pos == -1 || tag_pos < line_pos[lines_count + 1])) {
  746.                 tag_line[lines_count] = tag;
  747.  
  748.                 // remove the tag
  749.                 tag_length = gTagLength[tag];
  750.  
  751.                 strdel(result_info, tag_pos, tag_pos + tag_length);
  752.                 length -= tag_length;
  753.                 sep_pos -= tag_length;
  754.                 line_pos[lines_count + 1] -= tag_length;
  755.  
  756.                 break;
  757.             }
  758.         }
  759.  
  760.         // remember length
  761.         line_lengths[lines_count] = length;
  762.  
  763.         // get line size
  764.         line_sizes[lines_count] = 0;
  765.  
  766.         for (i = prev_sep_pos; i <= prev_sep_pos + length; i++) {
  767.             if (result_info[i] == '{') {
  768.                 i += 7;
  769.                 continue;
  770.             }
  771.  
  772.             temp_size = gCharSize[ result_info[i] ];
  773.             if (temp_size != -1) {
  774.                 line_sizes[lines_count] += temp_size;
  775.             }
  776.         }
  777.  
  778.         // update max size
  779.         temp_size = line_sizes[lines_count];
  780.         if (temp_size > max_size) {
  781.             max_size = temp_size;
  782.         }
  783.  
  784.         // save separator position for the future
  785.         prev_sep_pos = sep_pos;
  786.  
  787.         // check for array borders
  788.         lines_count++;
  789.         if (lines_count >= MDIALOG_MAX_LINES) {
  790.             break;
  791.         }
  792.     }
  793.  
  794.     if (max_size < MDIALOG_MIN_DIALOG_WIDTH) {
  795.         max_size = MDIALOG_MIN_DIALOG_WIDTH;
  796.     }
  797.  
  798.     // add spaces to the string
  799.     static
  800.         spaces_string[MDIALOG_MAX_SPACES],
  801.         spaces,
  802.         j;
  803.  
  804.     for (i = 0; i < lines_count; i++) {
  805.         // get spaces count by tag
  806.         switch (tag_line[i]) {
  807.             case E_MDIALOG_TAG_NONE: {
  808.                 continue;
  809.             }
  810.             case E_MDIALOG_TAG_CENTER: {
  811.                 spaces = floatround((max_size - line_sizes[i]) / 2.0 / gCharSize[' ']);
  812.             }
  813.             case E_MDIALOG_TAG_RIGHT: {
  814.                 spaces = floatround((max_size - line_sizes[i]) / gCharSize[' ']);
  815.             }
  816.         }
  817.  
  818.         // is spaces are needed
  819.         if (spaces == 0) {
  820.             continue;
  821.         }
  822.  
  823.         // make spaces string
  824.         for (j = 0; j <= spaces; j++) {
  825.             spaces_string[j] = ' ';
  826.         }
  827.  
  828.         spaces_string[spaces + 1] = '\0';
  829.         line_sizes[i] += spaces * gCharSize[' '];
  830.  
  831.         // insert spaces into the string
  832.         format(result_info, size, "%.*s%s%s", line_pos[i], result_info, spaces_string, result_info[ line_pos[i] ]);
  833.  
  834.         // update line positions
  835.         for (j = i + 1; j < lines_count; j++) {
  836.             line_pos[j] += spaces + 1;
  837.         }
  838.     }
  839. }
  840.  
  841. #endif
  842.  
  843. /*
  844.     Other functions
  845. */
  846.  
  847. static stock _MDialog_strcpy(result[], const source[], const size = sizeof(result))
  848. {
  849.     result[0] = 0;
  850.     return strcat(result, source, size);
  851. }
  852.  
  853. /*
  854.     strlen
  855. */
  856.  
  857. stock MDialog_strlen(const string[])
  858. {
  859.     if (isnull(string)) {
  860.         return 0;
  861.     }
  862.  
  863.     return strlen(string);
  864. }
  865. #if defined _ALS_strlen
  866.     #undef strlen
  867. #else
  868.     #define _ALS_strlen
  869. #endif
  870.  
  871. #define strlen MDialog_strlen
  872.  
  873. exemple:
  874.  
  875. // generated by "sampctl package generate"
  876.  
  877. #define MDIALOG_ZLANG_MODE
  878. #include <zlang>
  879. #include "mdialog.inc"
  880.  
  881. DialogCreate:test(playerid)
  882. {
  883.     Dialog_Open(playerid, Dialog:test, DIALOG_STYLE_MSGBOX,
  884.                 "Hello",
  885.                 "LANG_ARE_YOU_OK",
  886.                 "Yes", "BUTTON_NO",
  887.                 playerid);
  888. }
  889.  
  890. DialogResponse:test(playerid, response, listitem, inputtext[])
  891. {
  892.     if (!response) {
  893.         SendClientMessage(playerid, -1, "This club only for OK guys!");
  894.         Dialog_Show(playerid, Dialog:test);
  895.         return 1;
  896.     }
  897.  
  898.     SendClientMessage(playerid, -1, "Welcome to the club");
  899.     return 1;
  900. }
  901.  
  902. main() {
  903.     Dialog_Show(0, Dialog:test);
  904. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement