uMessageBox.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. uMessageBox Include
  3. */
  4. #if defined _uMessageBox_included
  5. #endinput
  6. #endif
  7. #define _uMessageBox_included
  8. #define Timer: Timer_
  9. /*
  10. Hooking part
  11. */
  12. #define S_ALS(%0_%1(%2)) %0_%1(%2) <%0:y>
  13. #define chain%0_%1(%2)%3; {state %0:y;%0_%1(%2);}
  14. #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;
  15. /*
  16. Defines
  17. */
  18. #define TITLE_X (10)
  19. #define TITLE_Y (100)
  20. #define TITLE_W (100)
  21. #define TITLE_H (20)
  22. #define TITLE_COLOR 0x000000AA
  23. #define CONTENT_X (10)
  24. #define CONTENT_Y (114)
  25. #define CONTENT_W (100)
  26. #define CONTENT_H (50)
  27. #define CONTENT_COLOR 0x00000066
  28. #define DEFAULT_TIMEOUT (5000) // Equals to 5 seconds
  29. /*
  30. Global variables
  31. */
  32. new
  33. Timer:uMsgBox_Hide [ MAX_PLAYERS ],
  34. bool: uMsgBox_Active [ MAX_PLAYERS ],
  35. bool: uMsgBox_Init [ MAX_PLAYERS ],
  36. Text: uMsgBox_Title [ MAX_PLAYERS ],
  37. Text: uMsgBox_Content [ MAX_PLAYERS ];
  38. /*
  39. Stocks, Publics
  40. */
  41. stock
  42. InitPlayerTextDraws( playerid )
  43. {
  44. if( uMsgBox_Init[ playerid ] )
  45. {
  46. printf( "uMessageBox.inc: trying to init textdraws for player when already initialised.");
  47. return 1;
  48. }
  49. uMsgBox_Title[ playerid ] = TextDrawCreate( TITLE_X, TITLE_Y, " " );
  50. uMsgBox_Content[ playerid ] = TextDrawCreate( CONTENT_X, CONTENT_Y, " " );
  51. TextDrawFont ( uMsgBox_Title[ playerid ], 2 );
  52. TextDrawSetProportional ( uMsgBox_Title[ playerid ], true );
  53. TextDrawUseBox ( uMsgBox_Title[ playerid ], true );
  54. TextDrawBoxColor ( uMsgBox_Title[ playerid ], TITLE_COLOR );
  55. TextDrawLetterSize ( uMsgBox_Title[ playerid ], 0.3, 1.5 );
  56. TextDrawSetShadow ( uMsgBox_Title[ playerid ], false );
  57. TextDrawSetOutline ( uMsgBox_Title[ playerid ], true );
  58. TextDrawTextSize ( uMsgBox_Title[ playerid ], TITLE_X + TITLE_W, 1);
  59. TextDrawFont ( uMsgBox_Content[ playerid ], 1 );
  60. TextDrawSetProportional ( uMsgBox_Content[ playerid ], true );
  61. TextDrawUseBox ( uMsgBox_Content[ playerid ], true );
  62. TextDrawBoxColor ( uMsgBox_Content[ playerid ], CONTENT_COLOR );
  63. TextDrawLetterSize ( uMsgBox_Content[ playerid ], 0.2, 1.2 );
  64. TextDrawSetShadow ( uMsgBox_Content[ playerid ], false );
  65. TextDrawSetOutline ( uMsgBox_Content[ playerid ], true );
  66. TextDrawTextSize ( uMsgBox_Content[ playerid ], CONTENT_X + CONTENT_W, 1 );
  67. uMsgBox_Init[ playerid ] = true;
  68. return 1;
  69. }
  70. stock
  71. ShowPlayerMessageBox( playerid, title[], content[], sound = 0, timeout = DEFAULT_TIMEOUT )
  72. {
  73. if( uMsgBox_Active[ playerid ] )
  74. printf( "uMessageBox.inc: trying to show message box when already showing.");
  75. else
  76. if( !uMsgBox_Init[ playerid ] )
  77. printf( "uMessageBox.inc: trying to show message box when not initialised.");
  78. else
  79. {
  80. if( sound )
  81. PlayerPlaySound( playerid, sound, 0, 0, 0 );
  82. TextDrawSetString( uMsgBox_Title[ playerid ], title );
  83. TextDrawSetString( uMsgBox_Content[ playerid ], content );
  84. TextDrawShowForPlayer( playerid, uMsgBox_Title[ playerid ] );
  85. TextDrawShowForPlayer( playerid, uMsgBox_Content[ playerid ] );
  86. uMsgBox_Active[ playerid ] = true;
  87. Timer:uMsgBox_Hide[ playerid ] = SetTimerEx( "OnMessageBoxHide", timeout, false, "d", playerid );
  88. }
  89. }
  90. stock
  91. HidePlayerMessageBox( playerid )
  92. {
  93. TextDrawHideForPlayer( playerid, uMsgBox_Title[ playerid ] );
  94. TextDrawHideForPlayer( playerid, uMsgBox_Content[ playerid ] );
  95. uMsgBox_Active[ playerid ] = false;
  96. }
  97. stock
  98. UpdatePlayerMessageBox( playerid, title[] = "", content[] = "", timeout = 0 )
  99. {
  100. if( uMsgBox_Active[ playerid ] == false )
  101. printf( "uMessageBox.inc: trying to update an existing message box when none exists.");
  102. else
  103. {
  104. if( strlen( title ) > 0 )
  105. TextDrawSetString( uMsgBox_Title[ playerid ], title );
  106. if( strlen( content ) > 0 )
  107. TextDrawSetString( uMsgBox_Content[ playerid ], content );
  108. if( timeout )
  109. {
  110. KillTimer( Timer:uMsgBox_Hide[ playerid ] );
  111. Timer:uMsgBox_Hide[ playerid ] = SetTimerEx( "OnMessageBoxHide", timeout, false, "d", playerid );
  112. }
  113. }
  114. }
  115. stock
  116. KillPlayerMessageBox( playerid )
  117. {
  118. TextDrawDestroy( uMsgBox_Title[ playerid ] );
  119. TextDrawDestroy( uMsgBox_Content[ playerid ] );
  120. uMsgBox_Active[ playerid ] = false;
  121. uMsgBox_Init[ playerid ] = false;
  122. return 1;
  123. }
  124. forward
  125. OnMessageBoxHide( playerid );
  126. public
  127. OnMessageBoxHide( playerid )
  128. {
  129. if( !IsPlayerConnected( playerid ) || uMsgBox_Active[ playerid ] == false )
  130. {
  131. KillTimer( Timer:uMsgBox_Hide[ playerid ] );
  132. return 1;
  133. }
  134. HidePlayerMessageBox( playerid );
  135. return 1;
  136. }
  137. /*
  138. OnPlayerConnect hook
  139. */
  140. public OnPlayerConnect( playerid )
  141. return InitPlayerTextDraws( playerid );
  142. #if defined _ALS_OnPlayerConnect
  143. #undef OnPlayerConnect
  144. #else
  145. #define _ALS_OnPlayerConnect
  146. #endif
  147. #define OnPlayerConnect uMessageBox_OnPlayerConnect
  148. forward uMessageBox_OnPlayerConnect( playerid );
  149. /*
  150. OnPlayerDisconnect hook
  151. */
  152. public OnPlayerDisconnect( playerid, reason )
  153. return KillPlayerMessageBox( playerid );
  154. #if defined _ALS_OnPlayerDisconnect
  155. #undef OnPlayerDisconnect
  156. #else
  157. #define _ALS_OnPlayerDisconnect
  158. #endif
  159. #define OnPlayerDisconnect uMessageBox_OnPlayerDisconnect
  160. forward uMessageBox_OnPlayerDisconnect( playerid, reason );