Guest User

uMessageBox 2,2 by Universal

a guest
Jul 15th, 2012
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.81 KB | None | 0 0
  1. /*
  2.                                 uMessageBox Include
  3.            
  4.             This is an include used for manipulating message boxes which
  5.             are created using textdraws. It is very useful for outputting
  6.             some useful information or data for players.
  7.             Its simple, but nice. I myself use it. You can show only one
  8.             messagebox for now, also you can update the message of the
  9.             messagebox while its displayed.
  10.                
  11.             Version : 2.2
  12.             Author  : Universal
  13.            
  14.             Official topic: http://forum.sa-mp.com/showthread.php?p=1642085
  15. */
  16.  
  17.  
  18. #if defined _uMessageBox_included
  19.     #endinput
  20. #endif
  21.  
  22. #define _uMessageBox_included
  23.  
  24. #define Timer: Timer_
  25.  
  26. /*
  27.     Hooking part
  28. */
  29. #define S_ALS(%0_%1(%2)) %0_%1(%2) <%0:y>
  30. #define chain%0_%1(%2)%3; {state %0:y;%0_%1(%2);}
  31. #define redirect%0_%1(%2)%3; forward%0_%1(%2);stock%0@%1(%2)<%0:y>{}public%0_%1(%2)<%0:n>%3;public%0_%1(%2)<>%3;
  32.  
  33. /*
  34.     Defines
  35. */
  36. #define TITLE_X         (10)
  37. #define TITLE_Y         (100)
  38. #define TITLE_W         (100)
  39. #define TITLE_H         (20)
  40. #define TITLE_COLOR     0x000000AA
  41.  
  42. #define CONTENT_X       (10)
  43. #define CONTENT_Y       (114)
  44. #define CONTENT_W       (100)
  45. #define CONTENT_H       (50)
  46. #define CONTENT_COLOR   0x00000066
  47.  
  48. #define DEFAULT_TIMEOUT (5000) // Equals to 5 seconds
  49.  
  50. /*
  51.     Global variables
  52. */
  53. new
  54.     Timer:uMsgBox_Hide      [ MAX_PLAYERS ],
  55.     bool: uMsgBox_Active    [ MAX_PLAYERS ],
  56.     bool: uMsgBox_Init      [ MAX_PLAYERS ],
  57.     Text: uMsgBox_Title     [ MAX_PLAYERS ],
  58.     Text: uMsgBox_Content   [ MAX_PLAYERS ];
  59.  
  60. /*
  61.     Stocks, Publics
  62. */
  63. stock
  64.     InitPlayerTextDraws( playerid )
  65. {
  66.     if( uMsgBox_Init[ playerid ] )
  67.     {
  68.         printf( "uMessageBox.inc: trying to init textdraws for player when already initialised.");
  69.         return 1;
  70.     }
  71.    
  72.     uMsgBox_Title[ playerid ]   = TextDrawCreate( TITLE_X, TITLE_Y, " " );
  73.     uMsgBox_Content[ playerid ] = TextDrawCreate( CONTENT_X, CONTENT_Y, " " );
  74.    
  75.     TextDrawFont            ( uMsgBox_Title[ playerid ], 2 );
  76.     TextDrawSetProportional ( uMsgBox_Title[ playerid ], true );
  77.     TextDrawUseBox          ( uMsgBox_Title[ playerid ], true );
  78.     TextDrawBoxColor        ( uMsgBox_Title[ playerid ], TITLE_COLOR );
  79.     TextDrawLetterSize      ( uMsgBox_Title[ playerid ], 0.3, 1.5 );
  80.     TextDrawSetShadow       ( uMsgBox_Title[ playerid ], false );
  81.     TextDrawSetOutline      ( uMsgBox_Title[ playerid ], true );
  82.     TextDrawTextSize        ( uMsgBox_Title[ playerid ], TITLE_X + TITLE_W, 1);
  83.    
  84.     TextDrawFont            ( uMsgBox_Content[ playerid ], 1 );
  85.     TextDrawSetProportional ( uMsgBox_Content[ playerid ], true );
  86.     TextDrawUseBox          ( uMsgBox_Content[ playerid ], true );
  87.     TextDrawBoxColor        ( uMsgBox_Content[ playerid ], CONTENT_COLOR );
  88.     TextDrawLetterSize      ( uMsgBox_Content[ playerid ], 0.2, 1.2 );
  89.     TextDrawSetShadow       ( uMsgBox_Content[ playerid ], false );
  90.     TextDrawSetOutline      ( uMsgBox_Content[ playerid ], true );
  91.     TextDrawTextSize        ( uMsgBox_Content[ playerid ], CONTENT_X + CONTENT_W, 1 );
  92.    
  93.     uMsgBox_Init[ playerid ] = true;
  94.    
  95.     return 1;
  96. }
  97.  
  98. stock
  99.     ShowPlayerMessageBox( playerid, title[], content[], sound = 0, timeout = DEFAULT_TIMEOUT )
  100. {
  101.     if( uMsgBox_Active[ playerid ] )
  102.         printf( "uMessageBox.inc: trying to show message box when already showing.");
  103.     else
  104.     if( !uMsgBox_Init[ playerid ] )
  105.         printf( "uMessageBox.inc: trying to show message box when not initialised.");
  106.     else
  107.     {
  108.         if( sound )
  109.             PlayerPlaySound( playerid, sound, 0, 0, 0 );
  110.            
  111.         TextDrawSetString( uMsgBox_Title[ playerid ], title );
  112.         TextDrawSetString( uMsgBox_Content[ playerid ], content );
  113.        
  114.         TextDrawShowForPlayer( playerid, uMsgBox_Title[ playerid ] );
  115.         TextDrawShowForPlayer( playerid, uMsgBox_Content[ playerid ] );
  116.        
  117.         uMsgBox_Active[ playerid ] = true;
  118.        
  119.         Timer:uMsgBox_Hide[ playerid ] = SetTimerEx( "OnMessageBoxHide", timeout, false, "d", playerid );
  120.     }
  121. }
  122.  
  123. stock
  124.     HidePlayerMessageBox( playerid )
  125. {
  126.     TextDrawHideForPlayer( playerid, uMsgBox_Title[ playerid ] );
  127.     TextDrawHideForPlayer( playerid, uMsgBox_Content[ playerid ] );
  128.  
  129.     uMsgBox_Active[ playerid ] = false;
  130. }
  131.  
  132. stock
  133.     UpdatePlayerMessageBox( playerid, title[] = "", content[] = "", timeout = 0 )
  134. {
  135.     if( uMsgBox_Active[ playerid ] == false )
  136.         printf( "uMessageBox.inc: trying to update an existing message box when none exists.");
  137.     else
  138.     {
  139.         if( strlen( title ) > 0 )
  140.             TextDrawSetString( uMsgBox_Title[ playerid ], title );
  141.            
  142.         if( strlen( content ) > 0 )
  143.             TextDrawSetString( uMsgBox_Content[ playerid ], content );
  144.    
  145.         if( timeout )
  146.         {
  147.             KillTimer( Timer:uMsgBox_Hide[ playerid ] );
  148.             Timer:uMsgBox_Hide[ playerid ] = SetTimerEx( "OnMessageBoxHide", timeout, false, "d", playerid );
  149.         }
  150.     }
  151. }
  152.  
  153. stock
  154.     KillPlayerMessageBox( playerid )
  155. {
  156.     TextDrawDestroy( uMsgBox_Title[ playerid ] );
  157.     TextDrawDestroy( uMsgBox_Content[ playerid ] );
  158.    
  159.     uMsgBox_Active[ playerid ]  = false;
  160.     uMsgBox_Init[ playerid ]    = false;
  161.    
  162.     return 1;
  163. }
  164.  
  165. forward
  166.     OnMessageBoxHide( playerid );
  167. public
  168.     OnMessageBoxHide( playerid )
  169. {
  170.     if( !IsPlayerConnected( playerid ) || uMsgBox_Active[ playerid ] == false )
  171.     {
  172.         KillTimer( Timer:uMsgBox_Hide[ playerid ] );
  173.         return 1;
  174.     }
  175.    
  176.     HidePlayerMessageBox( playerid );
  177.    
  178.     return 1;
  179. }
  180.  
  181. /*
  182.     OnPlayerConnect hook
  183. */
  184. public OnPlayerConnect( playerid )
  185.     return InitPlayerTextDraws( playerid );
  186.  
  187. #if defined _ALS_OnPlayerConnect
  188.     #undef OnPlayerConnect
  189. #else
  190.     #define _ALS_OnPlayerConnect
  191. #endif
  192. #define OnPlayerConnect uMessageBox_OnPlayerConnect
  193. forward uMessageBox_OnPlayerConnect( playerid );
  194.  
  195. /*
  196.     OnPlayerDisconnect hook
  197. */
  198. public OnPlayerDisconnect( playerid, reason )
  199.     return KillPlayerMessageBox( playerid );
  200.  
  201. #if defined _ALS_OnPlayerDisconnect
  202.     #undef OnPlayerDisconnect
  203. #else
  204.     #define _ALS_OnPlayerDisconnect
  205. #endif
  206. #define OnPlayerDisconnect uMessageBox_OnPlayerDisconnect
  207. forward uMessageBox_OnPlayerDisconnect( playerid, reason );
Advertisement
Add Comment
Please, Sign In to add comment