Guest User

**Accessory System - With 20 Accessories**

a guest
May 23rd, 2013
2,638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <a_samp>
  2. #include "../include/gl_common.inc"
  3.  
  4. #define TOTAL_ACCESSORIES 20
  5. #define SELECTION_ACCESSORIES 20
  6. #define ACCESSORIES_PER_LINE 7
  7.  
  8. #define MESSAGE_COLOR 0xFF444499
  9.  
  10. #define HEADER_TEXT "Accessories"
  11. #define NEXT_TEXT "Next"
  12. #define PREV_TEXT "Prev"
  13.  
  14. #define DIALOG_BASE_X 75.0
  15. #define DIALOG_BASE_Y 130.0
  16. #define DIALOG_WIDTH 550.0
  17. #define DIALOG_HEIGHT 180.0
  18. #define SPRITE_DIM_X 60.0
  19. #define SPRITE_DIM_Y 70.0
  20.  
  21. new gTotalACCESSORIES = TOTAL_ACCESSORIES;
  22. new PlayerText:gCurrentPageTextDrawId[MAX_PLAYERS];
  23. new PlayerText:gHeaderTextDrawId[MAX_PLAYERS];
  24. new PlayerText:gBackgroundTextDrawId[MAX_PLAYERS];
  25. new PlayerText:gNextButtonTextDrawId[MAX_PLAYERS];
  26. new PlayerText:gPrevButtonTextDrawId[MAX_PLAYERS];
  27. new PlayerText:gSelectionItems[MAX_PLAYERS][SELECTION_ACCESSORIES];
  28. new gSelectionItemsTag[MAX_PLAYERS][SELECTION_ACCESSORIES];
  29. new gItemAt[MAX_PLAYERS];
  30.  
  31. new gItemList[TOTAL_ACCESSORIES] = {
  32. 19488,19352,19330,19331,19098,19006,19007,19008,19009,19010,
  33. 18640,18975,19077,19136,19274,19039,19040,19041,19042,18729
  34. };
  35.  
  36. GetNumberOfPages()
  37. {
  38. if((gTotalACCESSORIES >= SELECTION_ACCESSORIES) && (gTotalACCESSORIES % SELECTION_ACCESSORIES) == 0)
  39. {
  40. return (gTotalACCESSORIES / SELECTION_ACCESSORIES);
  41. }
  42. else return (gTotalACCESSORIES / SELECTION_ACCESSORIES) + 1;
  43. }
  44.  
  45. PlayerText:CreateCurrentPageTextDraw(playerid, Float:Xpos, Float:Ypos)
  46. {
  47. new PlayerText:txtInit;
  48. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, "0/0");
  49. PlayerTextDrawUseBox(playerid, txtInit, 0);
  50. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  51. PlayerTextDrawFont(playerid, txtInit, 1);
  52. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  53. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  54. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  55. PlayerTextDrawShow(playerid, txtInit);
  56. return txtInit;
  57. }
  58.  
  59. PlayerText:CreatePlayerDialogButton(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height, button_text[])
  60. {
  61. new PlayerText:txtInit;
  62. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, button_text);
  63. PlayerTextDrawUseBox(playerid, txtInit, 1);
  64. PlayerTextDrawBoxColor(playerid, txtInit, 0x000000FF);
  65. PlayerTextDrawBackgroundColor(playerid, txtInit, 0x000000FF);
  66. PlayerTextDrawLetterSize(playerid, txtInit, 0.4, 1.1);
  67. PlayerTextDrawFont(playerid, txtInit, 1);
  68. PlayerTextDrawSetShadow(playerid, txtInit, 0); // no shadow
  69. PlayerTextDrawSetOutline(playerid, txtInit, 0);
  70. PlayerTextDrawColor(playerid, txtInit, 0x4A5A6BFF);
  71. PlayerTextDrawSetSelectable(playerid, txtInit, 1);
  72. PlayerTextDrawAlignment(playerid, txtInit, 2);
  73. PlayerTextDrawTextSize(playerid, txtInit, Height, Width); // The width and height are reversed for centering.. something the game does <g>
  74. PlayerTextDrawShow(playerid, txtInit);
  75. return txtInit;
  76. }
  77.  
  78. PlayerText:CreatePlayerHeaderTextDraw(playerid, Float:Xpos, Float:Ypos, header_text[])
  79. {
  80. new PlayerText:txtInit;
  81. txtInit = CreatePlayerTextDraw(playerid, Xpos, Ypos, header_text);
  82. PlayerTextDrawUseBox(playerid, txtInit, 0);
  83. PlayerTextDrawLetterSize(playerid, txtInit, 1.25, 3.0);
  84. PlayerTextDrawFont(playerid, txtInit, 0);
  85. PlayerTextDrawSetShadow(playerid, txtInit, 0);
  86. PlayerTextDrawSetOutline(playerid, txtInit, 1);
  87. PlayerTextDrawColor(playerid, txtInit, 0xACCBF1FF);
  88. PlayerTextDrawShow(playerid, txtInit);
  89. return txtInit;
  90. }
  91.  
  92. PlayerText:CreatePlayerBackgroundTextDraw(playerid, Float:Xpos, Float:Ypos, Float:Width, Float:Height)
  93. {
  94. new PlayerText:txtBackground = CreatePlayerTextDraw(playerid, Xpos, Ypos," ~n~"); // enough space for everyone
  95. PlayerTextDrawUseBox(playerid, txtBackground, 1);
  96. PlayerTextDrawBoxColor(playerid, txtBackground, 0x4A5A6BBB);
  97. PlayerTextDrawLetterSize(playerid, txtBackground, 5.0, 5.0);
  98. PlayerTextDrawFont(playerid, txtBackground, 0);
  99. PlayerTextDrawSetShadow(playerid, txtBackground, 0);
  100. PlayerTextDrawSetOutline(playerid, txtBackground, 0);
  101. PlayerTextDrawColor(playerid, txtBackground,0x000000FF);
  102. PlayerTextDrawTextSize(playerid, txtBackground, Width, Height);
  103. PlayerTextDrawBackgroundColor(playerid, txtBackground, 0x4A5A6BBB);
  104. PlayerTextDrawShow(playerid, txtBackground);
  105. return txtBackground;
  106. }
  107.  
  108. PlayerText:CreateModelPreviewTextDraw(playerid, modelindex, Float:Xpos, Float:Ypos, Float:width, Float:height)
  109. {
  110. new PlayerText:txtPlayerSprite = CreatePlayerTextDraw(playerid, Xpos, Ypos, ""); // it has to be set with SetText later
  111. PlayerTextDrawFont(playerid, txtPlayerSprite, TEXT_DRAW_FONT_MODEL_PREVIEW);
  112. PlayerTextDrawColor(playerid, txtPlayerSprite, 0xFFFFFFFF);
  113. PlayerTextDrawBackgroundColor(playerid, txtPlayerSprite, 0x88888899);
  114. PlayerTextDrawTextSize(playerid, txtPlayerSprite, width, height); // Text size is the Width:Height
  115. PlayerTextDrawSetPreviewModel(playerid, txtPlayerSprite, modelindex);
  116. PlayerTextDrawSetSelectable(playerid, txtPlayerSprite, 1);
  117. PlayerTextDrawShow(playerid,txtPlayerSprite);
  118. return txtPlayerSprite;
  119. }
  120.  
  121. DestroyPlayerModelPreviews(playerid)
  122. {
  123. new x=0;
  124. while(x != SELECTION_ACCESSORIES) {
  125. if(gSelectionItems[playerid][x] != PlayerText:INVALID_TEXT_DRAW) {
  126. PlayerTextDrawDestroy(playerid, gSelectionItems[playerid][x]);
  127. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  128. }
  129. x++;
  130. }
  131. }
  132.  
  133. ShowPlayerModelPreviews(playerid)
  134. {
  135. new x=0;
  136. new Float:BaseX = DIALOG_BASE_X;
  137. new Float:BaseY = DIALOG_BASE_Y - (SPRITE_DIM_Y * 0.33); // down a bit
  138. new linetracker = 0;
  139.  
  140. new itemat = GetPVarInt(playerid, "items_page") * SELECTION_ACCESSORIES;
  141.  
  142. // Destroy any previous ones created
  143. DestroyPlayerModelPreviews(playerid);
  144.  
  145. while(x != SELECTION_ACCESSORIES && itemat < gTotalACCESSORIES) {
  146. if(linetracker == 0) {
  147. BaseX = DIALOG_BASE_X + 25.0; // in a bit from the box
  148. BaseY += SPRITE_DIM_Y + 1.0; // move on the Y for the next line
  149. }
  150. gSelectionItems[playerid][x] = CreateModelPreviewTextDraw(playerid, gItemList[itemat], BaseX, BaseY, SPRITE_DIM_X, SPRITE_DIM_Y);
  151. gSelectionItemsTag[playerid][x] = gItemList[itemat];
  152. BaseX += SPRITE_DIM_X + 1.0; // move on the X for the next sprite
  153. linetracker++;
  154. if(linetracker == ACCESSORIES_PER_LINE) linetracker = 0;
  155. itemat++;
  156. x++;
  157. }
  158. }
  159.  
  160. UpdatePageTextDraw(playerid)
  161. {
  162. new PageText[64+1];
  163. format(PageText, 64, "%d/%d", GetPVarInt(playerid,"items_page") + 1, GetNumberOfPages());
  164. PlayerTextDrawSetString(playerid, gCurrentPageTextDrawId[playerid], PageText);
  165. }
  166.  
  167. CreateSelectionMenu(playerid)
  168. {
  169. gBackgroundTextDrawId[playerid] = CreatePlayerBackgroundTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y + 20.0, DIALOG_WIDTH, DIALOG_HEIGHT);
  170. gHeaderTextDrawId[playerid] = CreatePlayerHeaderTextDraw(playerid, DIALOG_BASE_X, DIALOG_BASE_Y, HEADER_TEXT);
  171. gCurrentPageTextDrawId[playerid] = CreateCurrentPageTextDraw(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y + 15.0);
  172. gNextButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 30.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, NEXT_TEXT);
  173. gPrevButtonTextDrawId[playerid] = CreatePlayerDialogButton(playerid, DIALOG_WIDTH - 90.0, DIALOG_BASE_Y+DIALOG_HEIGHT+100.0, 50.0, 16.0, PREV_TEXT);
  174.  
  175. ShowPlayerModelPreviews(playerid);
  176. UpdatePageTextDraw(playerid);
  177. }
  178.  
  179. DestroySelectionMenu(playerid)
  180. {
  181. DestroyPlayerModelPreviews(playerid);
  182.  
  183. PlayerTextDrawDestroy(playerid, gHeaderTextDrawId[playerid]);
  184. PlayerTextDrawDestroy(playerid, gBackgroundTextDrawId[playerid]);
  185. PlayerTextDrawDestroy(playerid, gCurrentPageTextDrawId[playerid]);
  186. PlayerTextDrawDestroy(playerid, gNextButtonTextDrawId[playerid]);
  187. PlayerTextDrawDestroy(playerid, gPrevButtonTextDrawId[playerid]);
  188.  
  189. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  190. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  191. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  192. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  193. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  194. }
  195.  
  196. HandlePlayerItemSelection(playerid, selecteditem)
  197. {
  198. if(gSelectionItemsTag[playerid][selecteditem] == 19488 || gSelectionItemsTag[playerid][selecteditem] == 19352 || gSelectionItemsTag[playerid][selecteditem] == 19330 || gSelectionItemsTag[playerid][selecteditem] == 19331 || gSelectionItemsTag[playerid][selecteditem] == 19098)
  199. {
  200. SetPlayerAttachedObject(playerid, 0, gSelectionItemsTag[playerid][selecteditem], 2);
  201. SendClientMessage(playerid,MESSAGE_COLOR,"Use /edittoy to Edit Toy and /removetoy to Remove it.");
  202. return 1;
  203. }
  204. if(gSelectionItemsTag[playerid][selecteditem] >= 19006 && gSelectionItemsTag[playerid][selecteditem] <= 19010)
  205. {
  206. SetPlayerAttachedObject(playerid, 1, gSelectionItemsTag[playerid][selecteditem], 18);
  207. SendClientMessage(playerid,MESSAGE_COLOR,"Use /edittoy to Edit Toy and /removetoy to Remove it.");
  208. return 1;
  209. }
  210. if(gSelectionItemsTag[playerid][selecteditem] == 18640 || gSelectionItemsTag[playerid][selecteditem] == 18975 || gSelectionItemsTag[playerid][selecteditem] == 19077 || gSelectionItemsTag[playerid][selecteditem] == 19136 || gSelectionItemsTag[playerid][selecteditem] == 19274)
  211. {
  212. SetPlayerAttachedObject(playerid, 2, gSelectionItemsTag[playerid][selecteditem], 2);
  213. SendClientMessage(playerid,MESSAGE_COLOR,"Use /edittoy to Edit Toy and /removetoy to Remove it.");
  214. return 1;
  215. }
  216. if(gSelectionItemsTag[playerid][selecteditem] == 18729)
  217. {
  218. SetPlayerAttachedObject(playerid, 3, gSelectionItemsTag[playerid][selecteditem], 5);
  219. SendClientMessage(playerid,MESSAGE_COLOR,"Use /edittoy to Edit Toy and /removetoy to Remove it.");
  220. return 1;
  221. }
  222. return 0;
  223. }
  224.  
  225. public OnPlayerConnect(playerid)
  226. {
  227. // Init all of the textdraw related globals
  228. gHeaderTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  229. gBackgroundTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  230. gCurrentPageTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  231. gNextButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  232. gPrevButtonTextDrawId[playerid] = PlayerText:INVALID_TEXT_DRAW;
  233.  
  234. for(new x=0; x < SELECTION_ACCESSORIES; x++) {
  235. gSelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
  236. }
  237.  
  238. gItemAt[playerid] = 0;
  239.  
  240. return 1; // Allow other scripts to keep processing OnPlayerConnect
  241. }
  242.  
  243. public OnPlayerClickTextDraw(playerid, Text:clickedid)
  244. {
  245. if(GetPVarInt(playerid, "items_active") == 0) return 0;
  246.  
  247. // Handle: They cancelled (with ESC)
  248. if(clickedid == Text:INVALID_TEXT_DRAW) {
  249. DestroySelectionMenu(playerid);
  250. SetPVarInt(playerid, "items_active", 0);
  251. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  252. return 1;
  253. }
  254.  
  255. return 0;
  256. }
  257.  
  258. public OnPlayerClickPlayerTextDraw(playerid, PlayerText:playertextid)
  259. {
  260. if(GetPVarInt(playerid, "items_active") == 0) return 0;
  261.  
  262. new curpage = GetPVarInt(playerid, "items_page");
  263.  
  264. // Handle: next button
  265. if(playertextid == gNextButtonTextDrawId[playerid]) {
  266. if(curpage < (GetNumberOfPages() - 1)) {
  267. SetPVarInt(playerid, "items_page", curpage + 1);
  268. ShowPlayerModelPreviews(playerid);
  269. UpdatePageTextDraw(playerid);
  270. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  271. } else {
  272. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  273. }
  274. return 1;
  275. }
  276.  
  277. // Handle: previous button
  278. if(playertextid == gPrevButtonTextDrawId[playerid]) {
  279. if(curpage > 0) {
  280. SetPVarInt(playerid, "items_page", curpage - 1);
  281. ShowPlayerModelPreviews(playerid);
  282. UpdatePageTextDraw(playerid);
  283. PlayerPlaySound(playerid, 1084, 0.0, 0.0, 0.0);
  284. } else {
  285. PlayerPlaySound(playerid, 1085, 0.0, 0.0, 0.0);
  286. }
  287. return 1;
  288. }
  289.  
  290. // Search in the array of textdraws used for the items
  291. new x=0;
  292. while(x != SELECTION_ACCESSORIES) {
  293. if(playertextid == gSelectionItems[playerid][x]) {
  294. HandlePlayerItemSelection(playerid, x);
  295. PlayerPlaySound(playerid, 1083, 0.0, 0.0, 0.0);
  296. DestroySelectionMenu(playerid);
  297. CancelSelectTextDraw(playerid);
  298. SetPVarInt(playerid, "items_active", 0);
  299. return 1;
  300. }
  301. x++;
  302. }
  303.  
  304. return 0;
  305. }
  306.  
  307. public OnPlayerCommandText(playerid, cmdtext[])
  308. {
  309. new cmd[256+1];
  310. new idx;
  311.  
  312. cmd = strtok(cmdtext, idx);
  313.  
  314. if(strcmp("/accessories", cmd, true) == 0)
  315. {
  316. DestroySelectionMenu(playerid);
  317. SetPVarInt(playerid, "items_active", 1);
  318. CreateSelectionMenu(playerid);
  319. SelectTextDraw(playerid, 0xACCBF1FF);
  320. return 1;
  321. }
  322. if(strcmp("/removeaccessories", cmd, true) == 0)
  323. {
  324. for(new i=0; i<MAX_PLAYER_ATTACHED_OBJECTS; i++)
  325. {
  326. if(IsPlayerAttachedObjectSlotUsed(playerid, i)) RemovePlayerAttachedObject(playerid, i);
  327. }
  328. SendClientMessage(playerid,MESSAGE_COLOR,"Accessory Removed.");
  329. return 1;
  330. }
  331. if(strcmp("/editaccessories", cmd, true) == 0)
  332. {
  333. for(new i=0; i<MAX_PLAYER_ATTACHED_OBJECTS; i++)
  334. {
  335. if(IsPlayerAttachedObjectSlotUsed(playerid, i)) EditAttachedObject(playerid, i);
  336. }
  337. SendClientMessage(playerid,MESSAGE_COLOR,"Use Right Shift to Look Around.");
  338. return 1;
  339. }
  340. return 0;
  341. }
RAW Paste Data