plugins/include/menus.inc
author David Anderson <dvander@alliedmods.net>
Wed Jul 25 01:18:11 2007 +0000 (2007-07-25)
changeset 1163 0bb03cb6277b
parent 1136 022c6dfdb3c0
child 1259 472c5e61cdc8
permissions -rw-r--r--
- added MenuAction_DisplayItem callback for plugins
- fixed various bugs in MenuAction_Display
- added an "access" parameter for IMenuManager. better to do this now than later, even though it's not used yet
- hard bumped menu api to change the OnMenuDisplayItem callback. sorry!
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _menus_included
    17  #endinput
    18 #endif
    19 #define _menus_included
    20 
    21 /**
    22  * Low-level drawing style of the menu.
    23  */
    24 enum MenuStyle
    25 {
    26 	MenuStyle_Default = 0,		/**< The "default" menu style for the mod */
    27 	MenuStyle_Valve = 1,		/**< The Valve provided menu style (Used on HL2DM) */
    28 	MenuStyle_Radio = 2,		/**< The simpler menu style commonly used on CS:S */
    29 };
    30 
    31 /**
    32  * Different actions for the menu "pump" callback
    33  */
    34 enum MenuAction
    35 {
    36 	MenuAction_Start = (1<<0),		/**< A menu has been started (nothing passed) */
    37 	MenuAction_Display = (1<<1),	/**< A menu is about to be displayed (param1=client, param2=MenuPanel Handle) */
    38 	MenuAction_Select = (1<<2),		/**< An item was selected (param1=client, param2=item) */
    39 	MenuAction_Cancel = (1<<3),		/**< The menu was cancelled (param1=client, param2=reason) */
    40 	MenuAction_End = (1<<4),		/**< A menu display has fully ended (param1=reason, param2=cancel reason) */
    41 	MenuAction_VoteEnd = (1<<5),	/**< (VOTE ONLY): A vote sequence has succeeded (param1=chosen item) */
    42 	MenuAction_VoteStart = (1<<6), 	/**< (VOTE ONLY): A vote sequence has started (nothing passed) */
    43 	MenuAction_VoteCancel = (1<<7),	/**< (VOTE ONLY): A vote sequence has been cancelled (nothing passed) */
    44 	MenuAction_DrawItem = (1<<8),	/**< An item is being drawn; return the new style (param1=client, param2=item) */
    45 	MenuAction_DisplayItem = (1<<9) /**< Item text is being drawn to the display (param1=client, param2=item)
    46 										  To change the text, use RedrawMenuItem().
    47 										  If you do so, return its return value.  Otherwise, return 0.
    48 										  */
    49 };
    50 
    51 /** Default menu actions */
    52 #define MENU_ACTIONS_DEFAULT	MenuAction_Select|MenuAction_Cancel|MenuAction_End
    53 /** All menu actions */
    54 #define MENU_ACTIONS_ALL		-1
    55 
    56 #define MENU_NO_PAGINATION		0		/**< Menu should not be paginated (10 items max) */
    57 #define MENU_TIME_FOREVER		0		/**< Menu should be displayed as long as possible */
    58 
    59 #define ITEMDRAW_DEFAULT		(0)		/**< Item should be drawn normally */
    60 #define ITEMDRAW_DISABLED		(1<<0)	/**< Item is drawn but not selectable */
    61 #define ITEMDRAW_RAWLINE		(1<<1)	/**< Item should be a raw line, without a slot */
    62 #define ITEMDRAW_NOTEXT			(1<<2)	/**< No text should be drawn */
    63 #define ITEMDRAW_SPACER			(1<<3)	/**< Item should be drawn as a spacer, if possible */
    64 #define ITEMDRAW_IGNORE	((1<<1)|(1<<2))	/**< Item should be completely ignored (rawline + notext) */
    65 #define ITEMDRAW_CONTROL		(1<<4)	/**< Item is control text (back/next/exit) */
    66 
    67 /**
    68  * Reasons a menu can be cancelled.
    69  */
    70 enum
    71 {
    72 	MenuCancel_Disconnected = -1,	/**< Client dropped from the server */
    73 	MenuCancel_Interrupted = -2,	/**< Client was interrupted with another menu */
    74 	MenuCancel_Exit = -3,			/**< Client exited via "exit" */
    75 	MenuCancel_NoDisplay = -4,		/**< Menu could not be displayed to the client */
    76 	MenuCancel_Timeout = -5,		/**< Menu timed out */
    77 	MenuCancel_ExitBack = -6,		/**< Client selected "exit back" on a paginated menu */
    78 };
    79 
    80 /**
    81  * Reasons a menu ended.
    82  */
    83 enum
    84 {
    85 	MenuEnd_Selected = 0,			/**< Menu item was selected */
    86 	MenuEnd_VotingDone = -1,		/**< Voting finished */
    87 	MenuEnd_VotingCancelled = -2,	/**< Voting was cancelled */
    88 	MenuEnd_Cancelled = -3,			/**< Menu was cancelled (reason in param2) */
    89 	MenuEnd_Exit = -4,				/**< Menu was cleanly exited via "exit" */
    90 	MenuEnd_ExitBack = -5,			/**< Menu was cleanly exited via "back" */
    91 };
    92 
    93 /**
    94  * Describes a menu's source
    95  */
    96 enum MenuSource
    97 {
    98 	MenuSource_None = 0,			/**< No menu is being displayed */	
    99 	MenuSource_External = 1,		/**< External menu */
   100 	MenuSource_Normal = 2,			/**< A basic menu is being displayed */
   101 	MenuSource_RawPanel = 3,		/**< A display is active, but it is not tied to a menu */
   102 };
   103 
   104 /**
   105  * Called when a menu action is completed.
   106  *
   107  * @param menu				The menu being acted upon.
   108  * @param action			The action of the menu.
   109  * @param param1			First action parameter (usually the client).
   110  * @param param2			Second action parameter (usually the item).
   111  * @noreturn
   112  */
   113 functag MenuHandler public(Handle:menu, MenuAction:action, param1, param2);
   114 
   115 /**
   116  * Creates a new, empty menu using the default style.
   117  * 
   118  * @param handler			Function which will receive menu actions.
   119  * @param actions			Optionally set which actions to receive.  Select, 
   120  *							Cancel, and End will always be received regardless
   121  *							of whether they are set or not.  They are also
   122  *							the only default actions.
   123  * @return					A new menu Handle.
   124  */
   125 native Handle:CreateMenu(MenuHandler:handler, MenuAction:actions=MENU_ACTIONS_DEFAULT);
   126 
   127 /**
   128  * Displays a menu to a client.
   129  *
   130  * @param menu				Menu Handle.
   131  * @param client			Client index.
   132  * @param time				Maximum time to leave menu on the screen.
   133  * @return					True on success, false on failure.
   134  * @error					Invalid Handle or client not in game.
   135  */
   136 native bool:DisplayMenu(Handle:menu, client, time);
   137 
   138 /**
   139  * Appends a new item to the end of a menu.
   140  *
   141  * @param menu				Menu Handle.
   142  * @param info				Item information string.
   143  * @param display			Default item display string.
   144  * @param style				Drawing style flags.  Anything other than DEFAULT or 
   145  *							DISABLED will be completely ignored when paginating.
   146  * @return					True on success, false on failure.
   147  * @error					Invalid Handle or item limit reached.
   148  */
   149 native AddMenuItem(Handle:menu, 
   150 					const String:info[], 
   151 					const String:display[], 
   152 					style=ITEMDRAW_DEFAULT);
   153 
   154 /**
   155  * Inserts an item into the menu before a certain position; the new item will
   156  * be at the given position and all next items pushed forward.
   157  *
   158  * @param menu				Menu Handle.
   159  * @param position			Position, starting from 0.
   160  * @param info				Item information string.
   161  * @param display			Default item display string.
   162  * @param style				Drawing style flags.  Anything other than DEFAULT or 
   163  *							DISABLED will be completely ignored when paginating.
   164  * @return					True on success, false on failure.
   165  * @error					Invalid Handle or menu position.
   166  */
   167 native bool:InsertMenuItem(Handle:menu,
   168 						position, 
   169 						const String:info[], 
   170 						const String:display[], 
   171 						style=ITEMDRAW_DEFAULT);
   172 
   173 /**
   174  * Removes an item from the menu.
   175  *
   176  * @param menu				Menu Handle.
   177  * @param position			Position, starting from 0.
   178  * @return					True on success, false on failure.
   179  * @error					Invalid Handle or menu position.
   180  */
   181 native bool:RemoveMenuItem(Handle:menu, position);
   182 
   183 /**
   184  * Removes all items from a menu.
   185  *
   186  * @param menu				Menu Handle.
   187  * @noreturn
   188  * @error					Invalid Handle or menu position.
   189  */
   190 native RemoveAllMenuItems(Handle:menu);
   191 
   192 /**
   193  * Retrieves information about a menu item.
   194  *
   195  * @param menu				Menu Handle.
   196  * @param position			Position, starting from 0.
   197  * @param infoBuf			Info buffer.
   198  * @param infoBufLen		Maximum length of the info buffer.
   199  * @param style				By-reference variable to store drawing flags.
   200  * @param dispBuf			Display buffer.
   201  * @param dispBufLen		Maximum length of the display buffer.
   202  * @return					True on success, false if position is invalid.
   203  * @error					Invalid Handle.
   204  */
   205 native bool:GetMenuItem(Handle:menu, position, String:infoBuf[], infoBufLen, &style=0, String:dispBuf[]="", dispBufLen=0);
   206 
   207 /**
   208  * Returns the number of items in a menu.
   209  *
   210  * @param menu				Menu Handle.
   211  * @return					Number of items in the menu.
   212  * @error					Invalid Handle.
   213  */
   214 native GetMenuItemCount(Handle:menu);
   215 
   216 /**
   217  * Sets whether the menu should be paginated or not.
   218  *
   219  * @param menu				Handle to the menu.
   220  * @param itemsPerPage		Number of items per page, or MENU_NO_PAGINATION.
   221  * @return					True on success, false if pagination is too high or low.
   222  * @error					Invalid Handle.
   223  */
   224 native bool:SetMenuPagination(Handle:menu, itemsPerPage);
   225 
   226 /**
   227  * Returns a menu's pagination setting.
   228  *
   229  * @param menu				Handle to the menu.
   230  * @return					Pagination setting.
   231  * @error					Invalid Handle.
   232  */
   233 native GetMenuPagination(Handle:menu);
   234 
   235 /**
   236  * Returns a menu's MenuStyle Handle.  The Handle
   237  * is global and cannot be freed.
   238  *
   239  * @param menu				Handle to the menu.
   240  * @return 					Handle to the menu's draw style.
   241  * @error					Invalid Handle.
   242  */
   243 native Handle:GetMenuStyle(Handle:menu);
   244 
   245 /**
   246  * Sets the menu's default title/instruction message.
   247  *
   248  * @param menu				Menu Handle.
   249  * @param fmt				Message string format
   250  * @param ...				Message string arguments.
   251  * @noreturn
   252  * @error					Invalid Handle.
   253  */
   254 native SetMenuTitle(Handle:menu, const String:fmt[], any:...);
   255 
   256 /**
   257  * Creates a raw MenuPanel based off the menu's style.
   258  * The Handle must be freed with CloseHandle().
   259  *
   260  * @return					A new MenuPanel Handle.
   261  * @error					Invalid Handle.
   262  */
   263 native Handle:CreatePanelFromMenu(Handle:menu);
   264 
   265 /**
   266  * Returns whether or not the menu has an exit button.
   267  * By default, menus have an exit button.
   268  *
   269  * @param menu				Menu Handle.
   270  * @return 					True if the menu has an exit button; false otherwise.
   271  * @error					Invalid Handle.
   272  */
   273 native bool:GetMenuExitButton(Handle:menu);
   274 
   275 /**
   276  * Sets whether or not the menu has an exit button.
   277  * By default, menus have an exit button.
   278  *
   279  * @param menu				Menu Handle.
   280  * @param button			True to enable the button, false to remove it.
   281  * @return 					True if allowed; false on failure.
   282  * @error					Invalid Handle.
   283  */
   284 native bool:SetMenuExitButton(Handle:menu, bool:button);
   285 
   286 /**
   287  * Returns whether or not the menu has an "exit back" button.
   288  * By default, menus do not have an exit back button.
   289  * Exit Back buttons appear as "Back" on page 1 of paginated menus and
   290  * have functionality defined by the user in MenuEnd_ExitBack.
   291  *
   292  * @param menu				Menu Handle.
   293  * @return 					True if the menu has an exit back button; false otherwise.
   294  * @error					Invalid Handle.
   295  */
   296 native bool:GetMenuExitBackButton(Handle:menu);
   297 
   298 /**
   299  * Sets whether or not the menu has an "exit back" button.
   300  * By default, menus do not have an exit back button.
   301  * Exit Back buttons appear as "Back" on page 1 of paginated menus and
   302  * have functionality defined by the user in MenuEnd_ExitBack.
   303  *
   304  * @param menu				Menu Handle.
   305  * @param button			True to enable the button, false to remove it.
   306  * @error					Invalid Handle.
   307  */
   308 native SetMenuExitBackButton(Handle:menu, bool:button);
   309 
   310 
   311 /**
   312  * Cancels a menu from displaying on all clients.  While the
   313  * cancellation is in progress, this menu cannot be re-displayed
   314  * to any clients.
   315  *
   316  * The menu may still exist on the client's screen after this command.
   317  * This simply verifies that the menu is not being used anywhere.
   318  *
   319  * If any vote is in progress on a menu, it will be cancelled.
   320  *
   321  * @param menu				Menu Handle.
   322  * @noreturn
   323  * @error					Invalid Handle.
   324  */
   325 native CancelMenu(Handle:menu);
   326 
   327 /**
   328  * Returns whether a vote is in progress on the given menu.
   329  *
   330  * @param menu				Menu Handle.
   331  * @return					True if a vote is in progress, false otherwise.
   332  */
   333 native bool:IsVoteInProgress(Handle:menu);
   334 
   335 /**
   336  * Broadcasts a menu to a list of clients.  The most selected item will be 
   337  * returned through MenuAction_End.  On a tie, a random item will be returned 
   338  * from a list of the tied items.
   339  *
   340  * Note that MenuAction_VoteEnd and MenuAction_VoteStart are both
   341  * default callbacks and do not need to be enabled.
   342  *
   343  * @param menu				Menu Handle.
   344  * @param clients			Array of clients to broadcast to.
   345  * @param numClients		Number of clients in the array.
   346  * @param time				Maximum time to leave menu on the screen.
   347  * @return					True on success, false if this menu already has a vote session
   348  *							in progress.
   349  * @error					Invalid Handle.
   350  */
   351 native bool:VoteMenu(Handle:menu, clients[], numClients, time);
   352 
   353 /**
   354  * Sends a vote menu to all clients.  See VoteMenu() for more information.
   355  *
   356  * @param menu				Menu Handle.
   357  * @param time				Maximum time to leave menu on the screen.
   358  * @return					True on success, false if this menu already has a vote session
   359  *							in progress.
   360  * @error					Invalid Handle.
   361  */
   362 stock VoteMenuToAll(Handle:menu, time)
   363 {
   364 	new num = GetMaxClients();
   365 	new total;
   366 	decl players[num];
   367 	
   368 	for (new i=1; i<=num; i++)
   369 	{
   370 		if (!IsClientInGame(i))
   371 		{
   372 			continue;
   373 		}
   374 		players[total++] = i;
   375 	}
   376 	
   377 	return VoteMenu(menu, players, total, time);
   378 }
   379 
   380 /**
   381  * Returns a style's global Handle.
   382  *
   383  * @param style			Menu Style.
   384  * @return				A Handle, or INVALID_HANDLE if not found or unusable.
   385  */
   386 native Handle:GetMenuStyleHandle(MenuStyle:style);
   387 
   388 /**
   389  * Creates a MenuPanel from a MenuStyle.  Panels are used for drawing raw 
   390  * menus without any extra helper functions.  The Handle must be closed
   391  * with CloseHandle().
   392  *
   393  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   394  * @return				A new MenuPanel Handle.
   395  * @error				Invalid Handle other than INVALID_HANDLE.
   396  */
   397 native Handle:CreatePanel(Handle:hStyle=INVALID_HANDLE);
   398 
   399 /** 
   400  * Creates a Menu from a MenuStyle.  The Handle must be closed with 
   401  * CloseHandle().
   402  *
   403  * @parma hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   404  * @param handler		Function which will receive menu actions.
   405  * @param actions		Optionally set which actions to receive.  Select, 
   406  *						Cancel, and End will always be received regardless
   407  *						of whether they are set or not.  They are also
   408  *						the only default actions.
   409  * @return				A new menu Handle.
   410  * @error				Invalid Handle other than INVALID_HANDLE.
   411  */
   412 native Handle:CreateMenuEx(Handle:hStyle=INVALID_HANDLE, MenuHandler:handler, MenuAction:actions=MENU_ACTIONS_DEFAULT);
   413 
   414 /**
   415  * Returns whether a client is viewing a menu.  If the menu source 
   416  * is MenuSource_Normal, a menu Handle will also be returned.
   417  *
   418  * @param client		Client index.
   419  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   420  * @return				A MenuSource value.
   421  * @error				Invalid Handle other than INVALID_HANDLE.
   422  */
   423 native MenuSource:GetClientMenu(client, Handle:hStyle=INVALID_HANDLE);
   424 
   425 /** 
   426  * Cancels a menu on a client.  This will only affect non-external menus.
   427  *
   428  * @param hstyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   429  * @param client		Client index.
   430  * @param autoIgnore	If true, no menus can be re-drawn on the client during
   431  *						the cancellation process.
   432  * @return				True if a menu was cancelled, false otherwise.
   433  */
   434 native bool:CancelClientMenu(client, bool:autoIgnore=false, Handle:hStyle=INVALID_HANDLE);
   435 
   436 /**
   437  * Returns a style's maximum items per page.
   438  *
   439  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   440  * @return				Maximum items per page.
   441  * @error				Invalid Handle other than INVALID_HANDLE.
   442  */
   443 native GetMaxPageItems(Handle:hStyle=INVALID_HANDLE);
   444 
   445 /**
   446  * Returns a MenuPanel's parent style.
   447  *
   448  * @param panel			A MenuPanel Handle.
   449  * @return				The MenuStyle Handle that created the panel.
   450  * @error				Invalid Handle.
   451  */
   452 native Handle:GetPanelStyle(Handle:panel);
   453 
   454 /**
   455  * Sets the panel's title.
   456  *
   457  * @param panel			A MenuPanel Handle.
   458  * @param title			Text to set as the title.
   459  * @param onlyIfEmpty	If true, the title will only be set if no title is set.
   460  * @noreturn
   461  * @error				Invalid Handle.
   462  */
   463 native Handle:SetPanelTitle(Handle:panel, const String:text[], bool:onlyIfEmpty=false);
   464 
   465 /**
   466  * Draws an item on a panel.  If the item takes up a slot, the position
   467  * is returned.
   468  *
   469  * @param panel			A MenuPanel Handle.
   470  * @param text			Display text to use.  If not a raw line, 
   471  *						the style may automatically add color markup.
   472  *						No numbering or newlines are needed.
   473  * @param style			ITEMDRAW style flags.
   474  * @return				A slot position, or 0 if item was a rawline or could not be drawn.
   475  * @error				Invalid Handle.
   476  */
   477 native DrawPanelItem(Handle:panel, const String:text[], style=ITEMDRAW_DEFAULT);
   478 
   479 /**
   480  * Draws a raw line of text on a panel, without any markup other than a newline.
   481  *
   482  * @param panel			A MenuPanel Handle, or INVALID_HANDLE if inside a 
   483  *						MenuAction_DisplayItem callback.
   484  * @param text			Display text to use.
   485  * @return				True on success, false if raw lines are not supported.
   486  * @error				Invalid Handle.
   487  */
   488 native DrawPanelText(Handle:panel, const String:text[]);
   489 
   490 /**
   491  * Returns whether or not the given drawing flags are supported by
   492  * the menu style.
   493  *
   494  * @param panel			A MenuPanel Handle.
   495  * @param style			ITEMDRAW style flags.
   496  * @return				True if item is drawable, false otherwise.
   497  * @error				Invalid Handle.
   498  */
   499 native CanPanelDrawFlags(Handle:panel, style);
   500 
   501 /**
   502  * Sets the selectable key map of a panel.  This is not supported by
   503  * all styles (only by Radio, as of this writing).  
   504  *
   505  * @param keys			An integer where each bit N allows key
   506  *						N+1 to be selected.  If no keys are selectable,
   507  *						then key 0 (bit 9) is automatically set.
   508  * @return 				True if supported, false otherwise.
   509  */
   510 native bool:SetPanelKeys(Handle:panel, keys);
   511 
   512 /**
   513  * Sends a panel to a client.  Unlike full menus, the handler
   514  * function will only receive the following actions, both of 
   515  * which will have INVALID_HANDLE for a menu, and the client
   516  * as param1.
   517  *
   518  * MenuAction_Select (param2 will be the key pressed)
   519  * MenuAction_Cancel (param2 will be the reason)
   520  *
   521  * Also, if the menu fails to display, no callbacks will be called.
   522  *
   523  * @param panel			A MenuPanel Handle.
   524  * @param client		A client to draw to.
   525  * @param handler		The MenuHandler function to catch actions with.
   526  * @param time			Time to hold the menu for.
   527  * @return				True on success, false on failure.
   528  * @error				Invalid Handle.
   529  */
   530 native bool:SendPanelToClient(Handle:panel, client, MenuHandler:handler, time);
   531 
   532 /**
   533  * @brief Returns the current key position.
   534  *
   535  * @param panel			A MenuPanel Handle.
   536  * @return				Current key position starting at 1.
   537  * @error				Invalid Handle.
   538  */
   539 native GetPanelCurrentKey(Handle:panel);
   540 
   541 /**
   542  * @brief Sets the next key position.  This cannot be used
   543  * to traverse backwards.
   544  *
   545  * @param panel			A MenuPanel Handle.
   546  * @param key			Key that is greater or equal to
   547  *						GetPanelCurrentKey().
   548  * @return				True on success, false otherwise.
   549  * @error				Invalid Handle.
   550  */
   551 native bool:SetPanelCurrentKey(Handle:panel, key);
   552 
   553 /**
   554  * @brief Redraws menu text from inside a MenuAction_DisplayItem callback.
   555  * 
   556  * @param text			Menu text to draw.
   557  * @return				Item position; must be returned via the callback.
   558  */
   559 native RedrawMenuItem(const String:text[]);
   560 
   561 /**
   562  * Retrieves voting information from MenuAction_VoteEnd.
   563  *
   564  * @param param2		Second parameter of MenuAction_VoteEnd.
   565  * @param winningVotes	Number of votes received by the winning option.
   566  * @param totalVotes	Number of total votes received.
   567  * @noreturn
   568  */
   569 stock GetMenuVoteInfo(param2, &winningVotes, &totalVotes)
   570 {
   571 	winningVotes = param2 & 0xFFFF;
   572 	totalVotes = param2 >> 16;
   573 }