Skip to content

Commit 2386070

Browse files
committed
Initial release
1 parent 8fdef36 commit 2386070

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

dialogs.inc

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#if defined dialog_included
2+
#endinput
3+
#endif
4+
#define dialog_included
5+
6+
#if !defined DIALOG_PROTECT_INPUT
7+
#define DIALOG_PROTECT_INPUT (1)
8+
#endif
9+
10+
#if !defined DIALOG_MAX_CAPTION
11+
#define DIALOG_MAX_CAPTION (64)
12+
#endif
13+
#if !defined DIALOG_MAX_INFO
14+
#define DIALOG_MAX_INFO (4096)
15+
#endif
16+
#if !defined DIALOG_MAX_BUTTON
17+
#define DIALOG_MAX_BUTTON (128)
18+
#endif
19+
20+
#define INVALID_DIALOG_STYLE (-1)
21+
#define DIALOG_DEFAULT_ID (25635)
22+
23+
#if !defined isnull
24+
#define isnull(%1) \
25+
((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
26+
#endif
27+
28+
#define Dialog:%0(%1) \
29+
forward dialog_%0(%1); public dialog_%0(%1)
30+
31+
#define CreateDialog:%0(%1,%2[],%3[],%4[],%5[]) \
32+
forward DL_F@%0(%1); public DL_F@%0(%1) { DL_f@%0(%1,DL_@style,DL_@caption,DL_@info,DL_@button1,DL_@button2); } \
33+
forward DL_f@%0(%1,%2[DIALOG_MAX_CAPTION],%3[DIALOG_MAX_INFO],%4[DIALOG_MAX_BUTTON],%5[DIALOG_MAX_BUTTON]); \
34+
public DL_f@%0(%1,%2[DIALOG_MAX_CAPTION],%3[DIALOG_MAX_INFO],%4[DIALOG_MAX_BUTTON],%5[DIALOG_MAX_BUTTON])
35+
36+
#define ShowDialog(%0,%1) ShowDialog@(%0,#%1)
37+
38+
static
39+
DL_@Name[MAX_PLAYERS][32 char]
40+
;
41+
new
42+
DL_@style,
43+
DL_@caption[DIALOG_MAX_CAPTION],
44+
DL_@info[DIALOG_MAX_INFO],
45+
DL_@button1[DIALOG_MAX_BUTTON],
46+
DL_@button2[DIALOG_MAX_BUTTON]
47+
;
48+
#if defined OnDialogPerformed
49+
forward OnDialogPerformed(playerid, dialog[], response, success);
50+
#endif
51+
forward _@_DL_funcinc_@_();
52+
public _@_DL_funcinc_@_()
53+
{
54+
format("", 0, "");
55+
}
56+
stock ShowDialog@(playerid, function[], style=INVALID_DIALOG_STYLE, caption[]="", info[]="", button1[]="", button2[]="", {Float,_}:...)
57+
{
58+
static args;
59+
if(style==INVALID_DIALOG_STYLE)
60+
{
61+
new
62+
funcname[32]
63+
;
64+
funcname = "DL_F@";
65+
strcat(funcname, function);
66+
if(funcidx(funcname) == -1)
67+
{
68+
printf("[DIALOG ERROR]: None dialog created with name \"%s\"", function);
69+
return 0;
70+
}
71+
strpack(DL_@Name[playerid], function, sizeof(DL_@Name[]));
72+
CallLocalFunction(funcname, "d", playerid);
73+
ShowPlayerDialog(playerid, DIALOG_DEFAULT_ID, DL_@style, DL_@caption, DL_@info, DL_@button1, DL_@button2);
74+
}
75+
else
76+
{
77+
if ((args = numargs()) > 7)
78+
{
79+
while (--args >= 7)
80+
{
81+
#emit LCTRL 5
82+
#emit LOAD.alt args
83+
#emit SHL.C.alt 2
84+
#emit ADD.C 12
85+
#emit ADD
86+
#emit LOAD.I
87+
#emit PUSH.pri
88+
}
89+
#emit PUSH.S info
90+
#emit PUSH.C 4096
91+
#emit PUSH.C DL_@info
92+
93+
#emit LOAD.S.pri 8
94+
#emit CONST.alt 16
95+
#emit SUB
96+
#emit PUSH.pri
97+
98+
#emit SYSREQ.C format
99+
#emit LCTRL 5
100+
#emit SCTRL 4
101+
ShowPlayerDialog(playerid, DIALOG_DEFAULT_ID, style, caption, DL_@info, button1, button2);
102+
}
103+
else
104+
ShowPlayerDialog(playerid, DIALOG_DEFAULT_ID, style, caption, info, button1, button2);
105+
106+
strpack(DL_@Name[playerid], function, sizeof(DL_@Name[]));
107+
}
108+
return 1;
109+
}
110+
111+
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
112+
{
113+
#if defined DIALOG_PROTECT_INPUT && DIALOG_PROTECT_INPUT == 1
114+
for(new i, j = strlen(inputtext); i < j; ++i)
115+
inputtext[i] = '#';
116+
#endif
117+
if(dialogid == DIALOG_DEFAULT_ID)
118+
{
119+
new funcname[32] = "dialog_";
120+
strcat(funcname, DL_@Name[playerid]);
121+
122+
DL_@Name[playerid]{0} = 0;
123+
124+
#if defined OnDialogPerformed
125+
if(OnDialogPerformed(playerid, funcname[7], response, (funcidx(funcname) != -1)))
126+
CallLocalFunction(funcname, "ddds", playerid, response, listitem, (!inputtext[0]) ? ("\1") : (inputtext));
127+
#else
128+
CallLocalFunction(funcname, "ddds", playerid, response, listitem, (!inputtext[0]) ? ("\1") : (inputtext));
129+
#endif
130+
}
131+
#if defined DL_@OnDialogResponse
132+
return DL_@OnDialogResponse(playerid, dialogid, response, listitem, inputtext);
133+
#else
134+
return 0;
135+
#endif
136+
}
137+
138+
#if defined _ALS_OnDialogResponse
139+
#undef OnDialogResponse
140+
#else
141+
#define _ALS_OnDialogResponse
142+
#endif
143+
144+
#define OnDialogResponse DL_@OnDialogResponse
145+
146+
#if defined DL_@OnDialogResponse
147+
forward DL_@OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
148+
#endif

tests.pwn

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <a_samp>
2+
#include <core>
3+
#include <float>
4+
#include <dialogs>
5+
6+
#pragma tabsize 0
7+
8+
main()
9+
{
10+
print("\n----------------------------------");
11+
print(" Bare Script\n");
12+
print("----------------------------------\n");
13+
}
14+
15+
public OnPlayerConnect(playerid)
16+
{
17+
GameTextForPlayer(playerid,"~w~SA-MP: ~r~Bare Script",5000,5);
18+
return 1;
19+
}
20+
const Dialog_3 = 1;
21+
public OnPlayerCommandText(playerid, cmdtext[])
22+
{
23+
new idx;
24+
new cmd[256];
25+
26+
cmd = strtok(cmdtext, idx);
27+
28+
if(strcmp(cmd, "/dialog-1", true) == 0) {
29+
ShowDialog(playerid, Dialog_1);
30+
return 1;
31+
}
32+
else if(strcmp(cmd, "/dialog-2", true) == 0) {
33+
new name[MAX_PLAYER_NAME];
34+
GetPlayerName(playerid, name, sizeof(name));
35+
ShowDialog(playerid, Dialog_2, DIALOG_STYLE_MSGBOX, "Dialog_2", "Hi %s!\nHow are you?", "button1", "button2", name);
36+
return 1;
37+
}
38+
else if(strcmp(cmd, "/dialog-3", true) == 0) {
39+
new name[MAX_PLAYER_NAME], info[21 + MAX_PLAYER_NAME];
40+
GetPlayerName(playerid, name, sizeof(name));
41+
format(info, sizeof(info), "Hi %s!\nHow are you?", name);
42+
ShowPlayerDialog(playerid, Dialog_3, DIALOG_STYLE_MSGBOX, "Dialog_3", info, "button1", "button2");
43+
return 1;
44+
}
45+
46+
return 0;
47+
}
48+
CreateDialog:Dialog_1(playerid, style, caption[], info[], button1[], button2[])
49+
{
50+
style = DIALOG_STYLE_MSGBOX;
51+
caption = "Dialog_1";
52+
button1 = "button1";
53+
button2 = "button2";
54+
new name[MAX_PLAYER_NAME];
55+
GetPlayerName(playerid, name, sizeof(name));
56+
format(info, sizeof(info), "Hi %s!\nHow are you?", name);
57+
}
58+
Dialog:Dialog_1(playerid, dialogid, response, listitem, inputtext[])
59+
{
60+
SendClientMessage(playerid, -1, response ? "Dialog_1 response: You clicked at button 1" : "Dialog_1 response: You clicked at button 0");
61+
return true;
62+
}
63+
Dialog:Dialog_2(playerid, dialogid, response, listitem, inputtext[])
64+
{
65+
SendClientMessage(playerid, -1, response ? "Dialog_2 response: You clicked at button 1" : "Dialog_2 response: You clicked at button 0");
66+
return true;
67+
}
68+
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
69+
{
70+
if(dialogid == Dialog_3)
71+
{
72+
SendClientMessage(playerid, -1, response ? "Dialog_3 OnDialogResponse: You clicked at button 1" : "Dialog_3 OnDialogResponse: You clicked at button 0");
73+
return true;
74+
}
75+
return true;
76+
}
77+
public OnPlayerSpawn(playerid)
78+
{
79+
SetPlayerInterior(playerid,0);
80+
TogglePlayerClock(playerid,0);
81+
SendClientMessage(playerid, -1, "To test this include use:");
82+
SendClientMessage(playerid, -1, "{0000FF}/dialog-1{FFFFFF} to test the pre-created dialog.");
83+
SendClientMessage(playerid, -1, "{0000FF}/dialog-2{FFFFFF} to test the normal dialog, using ShowDialog.");
84+
SendClientMessage(playerid, -1, "{0000FF}/dialog-3{FFFFFF} to test the normal dialog, using ShowPlayerDialog.");
85+
return 1;
86+
}
87+
88+
public OnPlayerDeath(playerid, killerid, reason)
89+
{
90+
return 1;
91+
}
92+
93+
SetupPlayerForClassSelection(playerid)
94+
{
95+
SetPlayerInterior(playerid,14);
96+
SetPlayerPos(playerid,258.4893,-41.4008,1002.0234);
97+
SetPlayerFacingAngle(playerid, 270.0);
98+
SetPlayerCameraPos(playerid,256.0815,-43.0475,1004.0234);
99+
SetPlayerCameraLookAt(playerid,258.4893,-41.4008,1002.0234);
100+
}
101+
102+
public OnPlayerRequestClass(playerid, classid)
103+
{
104+
SetupPlayerForClassSelection(playerid);
105+
return 1;
106+
}
107+
108+
public OnGameModeInit()
109+
{
110+
SetGameModeText("Bare Script");
111+
ShowPlayerMarkers(1);
112+
ShowNameTags(1);
113+
AllowAdminTeleport(1);
114+
115+
AddPlayerClass(265,1958.3783,1343.1572,15.3746,270.1425,0,0,0,0,-1,-1);
116+
117+
return 1;
118+
}
119+
120+
strtok(const string[], &index)
121+
{
122+
new length = strlen(string);
123+
while ((index < length) && (string[index] <= ' '))
124+
{
125+
index++;
126+
}
127+
128+
new offset = index;
129+
new result[20];
130+
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
131+
{
132+
result[index - offset] = string[index];
133+
index++;
134+
}
135+
result[index - offset] = EOS;
136+
return result;
137+
}

0 commit comments

Comments
 (0)