Don't like ads? PRO users don't see any ads ;-)

Southclaw's Button Script

By: Southclaw on Nov 3rd, 2011  |  syntax: PAWN  |  size: 6.81 KB  |  hits: 669  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #define MAX_BUTTON 64
  2.  
  3. #define YELLOW                                  0xFFFF00AA
  4. #define RED                                     0xAA3333AA
  5.  
  6. #define BUTTON_DATA_WORLD               (0)
  7. #define BUTTON_DATA_INTERIOR            (1)
  8. #define BUTTON_DATA_TYPE                (2)
  9.  
  10. #define BUTTON_TYPE_GEN                 (0)
  11. #define BUTTON_TYPE_TP                  (1)
  12. #define BUTTON_TYPE_NOTE                (2)
  13.  
  14. #define TogCon(%1,%2) TogglePlayerControllable(%2,false),SetTimerEx("UnFreezePlayer",%1,false,"d",%2)
  15. #define strcpy(%0,%1) strcat((%0[0] = '\0', %0), %1)
  16.  
  17. /*
  18. native CreateButton(Float:x, Float:y, Float:z, msg[], world=0, interior=0, type=0, label=0)
  19. native LinkTP(buttonid1, buttonid2)
  20. native UnLinkTP(buttonid1, buttonid2)
  21. native IsValidButtonID(buttonid)
  22. native IsButtonActive(buttonid)
  23. native SetButtonMessage(buttonid, msg[])
  24. native SetButtonUsable(buttonid, bool:toggle)
  25. native SetButtonLabelColour(buttonid, colour)
  26. */
  27.  
  28.  
  29. new
  30.         ButtonData[MAX_BUTTON][3],
  31.         ButtonArea[MAX_BUTTON],
  32.         Text3D:ButtonLabel[MAX_BUTTON],
  33.         ButtonString[MAX_BUTTON][128],
  34.         bool:ButtonActive[MAX_BUTTON],
  35.         bool:ButtonIdUsed[MAX_BUTTON],
  36.         LinkedButton[MAX_BUTTON];
  37.  
  38. forward OnButtonPress(playerid, buttonid);
  39. forward UnFreezePlayer(playerid);
  40. public UnFreezePlayer(playerid)TogglePlayerControllable(playerid, true);
  41. // Used so the player doesn't fall through custom maps
  42.  
  43.  
  44. // Message Box Script
  45. new
  46.         PlayerText:MsgBox,
  47.         bool:MsgShown[MAX_PLAYERS];
  48. forward HideMsgBox(playerid);
  49. ShowMsgBox(playerid, message[], time=0)
  50. {
  51.         PlayerTextDrawSetString(playerid, MsgBox, message);
  52.         PlayerTextDrawShow(playerid, MsgBox);
  53.         MsgShown[playerid]=true;
  54.         if(time!=0)SetTimerEx("HideMsgBox", time, false, "d", playerid);
  55. }
  56. public HideMsgBox(playerid)
  57. {
  58.         PlayerTextDrawHide(playerid, MsgBox);
  59.         MsgShown[playerid] = false;
  60. }
  61.  
  62.  
  63. stock CreateButton(Float:x, Float:y, Float:z, msg[], world=0, interior=0, type=0, label=0)
  64. {
  65.         new id;
  66.         while(ButtonActive[id])id++;
  67.         if(id>=MAX_BUTTON)return print("BUTTON ID LIMIT REACHED");
  68.  
  69.         ButtonArea[id]=CreateDynamicSphere(x, y, z, 1.0, world, interior);
  70.         if(label)ButtonLabel[id]=CreateDynamic3DTextLabel(msg, YELLOW, x, y, z, 10.0, _, _, 1, world, interior, _, 10.0);
  71.         LinkedButton[id]=-1;
  72.         strcpy(ButtonString[id], msg);
  73.         ButtonData[id][BUTTON_DATA_WORLD] = world;
  74.         ButtonData[id][BUTTON_DATA_INTERIOR] = interior;
  75.         ButtonData[id][BUTTON_DATA_TYPE] = type;
  76.  
  77.         ButtonActive[id]=true;
  78.         ButtonIdUsed[id]=true;
  79.  
  80.         return id;
  81. }
  82.  
  83. stock LinkTP(buttonid1, buttonid2)
  84. {
  85.         if(ButtonIdUsed[buttonid1] || ButtonIdUsed[buttonid2])return 0;
  86.         LinkedButton[buttonid1]=bID2;
  87.         LinkedButton[buttonid2]=bID1;
  88.         return 1;
  89. }
  90. stock UnLinkTP(buttonid1, buttonid2)
  91. {
  92.         if(ButtonIdUsed[buttonid1] || ButtonIdUsed[buttonid2])return 0;
  93.         LinkedButton[buttonid1]=-1;
  94.         LinkedButton[buttonid2]=-1;
  95.         return 1;
  96. }
  97.  
  98. Internal_OnButtonPress(playerid, buttonid)
  99. {
  100.         if(!ButtonActive[buttonid])return 0;
  101.         if(LinkedButton[buttonid] >= 0)
  102.         {
  103.                 new
  104.                         id = LinkedButton[buttonid],
  105.                         Float:x,
  106.                         Float:y,
  107.                         Float:z;
  108.  
  109.                 Streamer_GetFloatData(STREAMER_TYPE_AREA, ButtonArea[id], E_STREAMER_X, x);
  110.                 Streamer_GetFloatData(STREAMER_TYPE_AREA, ButtonArea[id], E_STREAMER_Y, y);
  111.                 Streamer_GetFloatData(STREAMER_TYPE_AREA, ButtonArea[id], E_STREAMER_Z, z);
  112.  
  113.         TogCon(1000, playerid);
  114.                 SetPlayerVirtualWorld(playerid, ButtonData[id]{BUTTON_DATA_WORLD});
  115.                 SetPlayerInterior(playerid, ButtonData[id]{BUTTON_DATA_INTERIOR});
  116.                 SetPlayerPos(playerid, x, y, z);
  117.                 Streamer_UpdateEx(playerid, x, y, z, ButtonData[id]{BUTTON_DATA_WORLD}, ButtonData[id]{BUTTON_DATA_INTERIOR});
  118.         }
  119.         else CallLocalFunction("OnButtonPress", "dd", playerid, buttonid);
  120.         return 1;
  121. }
  122.  
  123.  
  124.  
  125. // Extra functions for external use
  126.  
  127. stock IsValidButtonID(buttonid)
  128. {
  129.         if(ButtonIdUsed[buttonid])return 1;
  130.         return 0;
  131. }
  132. stock IsButtonActive(buttonid)
  133. {
  134.         if(ButtonActive[buttonid])return 1;
  135.         return 0;
  136. }
  137.  
  138. stock SetButtonMessage(buttonid, msg[])
  139. {
  140.         if(!IsValidButtonID(buttonid))return 0;
  141.         strcpy(ButtonString[buttonid], msg);
  142.         for(new i;i<MAX_PLAYERS;i++)if(MsgShown[i])PlayerTextDrawSetString(i, MsgBox, msg);
  143.         UpdateDynamic3DTextLabelText(ButtonLabel[buttonid], YELLOW, msg);
  144.         return 1;
  145. }
  146. stock SetButtonUsable(buttonid, bool:toggle)
  147. {
  148.         if(!IsValidButtonID(buttonid))return 0;
  149.         if(toggle)
  150.         {
  151.                 UpdateDynamic3DTextLabelText(ButtonLabel[buttonid], YELLOW, ButtonString[buttonid]);
  152.                 ButtonActive[buttonid] = true;
  153.         }
  154.         else
  155.         {
  156.                 UpdateDynamic3DTextLabelText(ButtonLabel[buttonid], RED, ButtonString[buttonid]);
  157.                 ButtonActive[buttonid] = false;
  158.         }
  159.         return 1;
  160. }
  161. stock SetButtonLabelColour(buttonid, colour)
  162. {
  163.         if(!IsValidButtonID(buttonid))return 0;
  164.         UpdateDynamic3DTextLabelText(ButtonLabel[buttonid], colour, ButtonString[buttonid]);
  165.         return 1;
  166. }
  167.  
  168.  
  169. //
  170.  
  171.  
  172. public OnPlayerEnterDynamicArea(playerid, areaid)
  173. {
  174.     for(new x;x<MAX_BUTTON;x++)
  175.         {
  176.                 if(areaid==ButtonArea[x])
  177.                 {
  178.                         ShowMsgBox(playerid, ButtonString[x]);
  179.                         break;
  180.                 }
  181.         }
  182.         CallLocalFunction("BTN_OnPlayerEnterDynamicArea", "dd", playerid, areaid);
  183. }
  184. #if defined _ALS_OnPlayerEnterDynamicArea
  185.         #undef OnPlayerEnterDynamicArea
  186. #else
  187.         #define _ALS_OnPlayerEnterDynamicArea
  188. #endif
  189. #define OnPlayerEnterDynamicArea BTN_OnPlayerEnterDynamicArea
  190. forward OnPlayerEnterDynamicArea(playerid, areaid);
  191.  
  192.  
  193. //
  194.  
  195.  
  196. public OnPlayerLeaveDynamicArea(playerid, areaid)
  197. {
  198.     for(new x;x<MAX_BUTTON;x++)
  199.         {
  200.                 if(areaid==ButtonArea[x])
  201.                 {
  202.                         HideMsgBox(playerid);
  203.                         break;
  204.                 }
  205.         }
  206.         CallLocalFunction("BTN_OnPlayerLeaveDynamicArea", "dd", playerid, areaid);
  207. }
  208. #if defined _ALS_OnPlayerLeaveDynamicArea
  209.         #undef OnPlayerLeaveDynamicArea
  210. #else
  211.         #define _ALS_OnPlayerLeaveDynamicArea
  212. #endif
  213. #define OnPlayerLeaveDynamicArea BTN_OnPlayerLeaveDynamicArea
  214. forward OnPlayerLeaveDynamicArea(playerid, areaid);
  215.  
  216.  
  217. //
  218.  
  219.  
  220. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  221. {
  222.     if(newkeys==16)
  223.         {
  224.                 for(new i;i<MAX_BUTTON;i++)
  225.                 {
  226.                         if(IsPlayerInDynamicArea(playerid, ButtonArea[i]))
  227.                         {
  228.                                 Internal_OnButtonPress(playerid, i);
  229.                                 break;
  230.                         }
  231.                 }
  232.         }
  233.         CallLocalFunction("BTN_OnPlayerKeyStateChange", "ddd", playerid, newkeys, oldkeys);
  234. }
  235. #if defined _ALS_OnPlayerKeyStateChange
  236.         #undef OnPlayerKeyStateChange
  237. #else
  238.         #define _ALS_OnPlayerKeyStateChange
  239. #endif
  240. #define OnPlayerKeyStateChange BTN_OnPlayerKeyStateChange
  241. forward OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  242.  
  243.  
  244. public OnGameModeInit()
  245. {
  246.         for(new id;id<MAX_PLAYERS;id++)
  247.         {
  248.                 MsgBox                                  =CreatePlayerTextDraw(id, 37.000000, 130.00000, "_");
  249.                 PlayerTextDrawUseBox    (id, MsgBox, 1);
  250.                 PlayerTextDrawBoxColor  (id, MsgBox, 0x00000033);
  251.                 PlayerTextDrawTextSize  (id, MsgBox, 300, 300);
  252.                 PlayerTextDrawFont              (id, MsgBox, 1);
  253.                 PlayerTextDrawLetterSize(id, MsgBox, 0.499999, 1.600000);
  254.                 PlayerTextDrawSetShadow (id, MsgBox, 0);
  255.         }
  256.         CallLocalFunction("BTN_OnGameModeInit", "");
  257. }
  258. #if defined _ALS_OnGameModeInit
  259.         #undef OnGameModeInit
  260. #else
  261.         #define _ALS_OnGameModeInit
  262. #endif
  263. #define OnGameModeInit BTN_OnGameModeInit
  264. forward OnGameModeInit();