plugins/include/menus.inc
author David Anderson <dvander@alliedmods.net>
Wed Jun 06 06:33:57 2007 +0000 (2007-06-06)
changeset 885 7767bebc6262
parent 878 967a6613eb7e
child 891 30343d215489
permissions -rw-r--r--
exposed new voting features to plugins (MenuAction_VoteCancel, IsVoteInProgress)
     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's display/selection cycle is complete (nothing passed) */
    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 };
    45 
    46 /** Default menu actions */
    47 #define MENU_ACTIONS_DEFAULT	MenuAction_Select|MenuAction_Cancel|MenuAction_End
    48 /** All menu actions */
    49 #define MENU_ACTIONS_ALL		-1
    50 
    51 #define MENU_NO_PAGINATION		0		/**< Menu should not be paginated (10 items max) */
    52 #define MENU_TIME_FOREVER		0		/**< Menu should be displayed as long as possible */
    53 
    54 #define ITEMDRAW_DEFAULT		(0)		/**< Item should be drawn normally */
    55 #define ITEMDRAW_DISABLED		(1<<0)	/**< Item is drawn but not selectable */
    56 #define ITEMDRAW_RAWLINE		(1<<1)	/**< Item should be a raw line, without a slot */
    57 #define ITEMDRAW_NOTEXT			(1<<2)	/**< No text should be drawn */
    58 #define ITEMDRAW_SPACER			(1<<3)	/**< Item should be drawn as a spacer, if possible */
    59 #define ITEMDRAW_IGNORE	((1<<1)|(1<<2))	/**< Item should be completely ignored (rawline + notext) */
    60 #define ITEMDRAW_CONTROL		(1<<4)	/**< Item is control text (back/next/exit) */
    61 
    62 /**
    63  * Reasons a menu can be cancelled.
    64  */
    65 enum
    66 {
    67 	MenuCancel_Disconnect = -1,			/**< Client dropped from the server */
    68 	MenuCancel_Interrupt = -2,			/**< Client was interrupted with another menu */
    69 	MenuCancel_Exit = -3,				/**< Client selected "exit" on a paginated menu */
    70 	MenuCancel_NoDisplay = -4,			/**< Menu could not be displayed to the client */
    71 };
    72 
    73 /**
    74  * Describes a menu's source
    75  */
    76 enum MenuSource
    77 {
    78 	MenuSource_None = 0,			/**< No menu is being displayed */	
    79 	MenuSource_External = 1,		/**< External menu */
    80 	MenuSource_Normal = 2,			/**< A basic menu is being displayed */
    81 	MenuSource_RawPanel = 3,		/**< A display is active, but it is not tied to a menu */
    82 };
    83 
    84 /**
    85  * Called when a menu action is completed.
    86  *
    87  * @param menu				The menu being acted upon.
    88  * @param action			The action of the menu.
    89  * @param param1			First action parameter (usually the client).
    90  * @param param2			Second action parameter (usually the item).
    91  * @noreturn
    92  */
    93 functag MenuHandler public(Handle:menu, MenuAction:action, param1, param2);
    94 
    95 /**
    96  * Creates a new, empty menu using the default style.
    97  * 
    98  * @param handler			Function which will receive menu actions.
    99  * @param actions			Optionally set which actions to receive.  Select, 
   100  *							Cancel, and End will always be received regardless
   101  *							of whether they are set or not.  They are also
   102  *							the only default actions.
   103  * @return					A new menu Handle.
   104  */
   105 native Handle:CreateMenu(MenuHandler:handler, MenuAction:actions=MENU_ACTIONS_DEFAULT);
   106 
   107 /**
   108  * Displays a menu to a client.
   109  *
   110  * @param menu				Menu Handle.
   111  * @param client			Client index.
   112  * @param time				Maximum time to leave menu on the screen.
   113  * @return					True on success, false on failure.
   114  * @error					Invalid Handle or client not in game.
   115  */
   116 native bool:DisplayMenu(Handle:menu, client, time);
   117 
   118 /**
   119  * Appends a new item to the end of a menu.
   120  *
   121  * @param menu				Menu Handle.
   122  * @param info				Item information string.
   123  * @param display			Default item display string.
   124  * @param style				Drawing style flags.
   125  * @return					True on success, false on failure.
   126  * @error					Invalid Handle or item limit reached.
   127  */
   128 native AddMenuItem(Handle:menu, 
   129 					const String:info[], 
   130 					const String:display[], 
   131 					style=ITEMDRAW_DEFAULT);
   132 
   133 /**
   134  * Inserts an item into the menu before a certain position; the new item will
   135  * be at the given position and all next items pushed forward.
   136  *
   137  * @param menu				Menu Handle.
   138  * @param position			Position, starting from 0.
   139  * @param info				Item information string.
   140  * @param display			Default item display string.
   141  * @param style				Drawing style flags.
   142  * @return					True on success, false on failure.
   143  * @error					Invalid Handle or menu position.
   144  */
   145 native bool:InsertMenuItem(Handle:menu,
   146 						position, 
   147 						const String:info[], 
   148 						const String:display[], 
   149 						style=ITEMDRAW_DEFAULT);
   150 
   151 /**
   152  * Removes an item from the menu.
   153  *
   154  * @param menu				Menu Handle.
   155  * @param position			Position, starting from 0.
   156  * @return					True on success, false on failure.
   157  * @error					Invalid Handle or menu position.
   158  */
   159 native bool:RemoveMenuItem(Handle:menu, position);
   160 
   161 /**
   162  * Removes all items from a menu.
   163  *
   164  * @param menu				Menu Handle.
   165  * @noreturn
   166  * @error					Invalid Handle or menu position.
   167  */
   168 native RemoveAllMenuItems(Handle:menu);
   169 
   170 /**
   171  * Retrieves information about a menu item.
   172  *
   173  * @param menu				Menu Handle.
   174  * @param position			Position, starting from 0.
   175  * @param infoBuf			Info buffer.
   176  * @param infoBufLen		Maximum length of the info buffer.
   177  * @param style				By-reference variable to store drawing flags.
   178  * @param dispBuf			Display buffer.
   179  * @param dispBufLen		Maximum length of the display buffer.
   180  * @return					True on success, false if position is invalid.
   181  * @error					Invalid Handle.
   182  */
   183 native bool:GetMenuItem(Handle:menu, position, String:infoBuf[], infoBufLen, &style=0, String:dispBuf[]="", dispBufLen=0);
   184 
   185 /**
   186  * Returns the number of items in a menu.
   187  *
   188  * @param menu				Menu Handle.
   189  * @return					Number of items in the menu.
   190  * @error					Invalid Handle.
   191  */
   192 native GetMenuItemCount(Handle:menu);
   193 
   194 /**
   195  * Sets whether the menu should be paginated or not.
   196  *
   197  * @param menu				Handle to the menu.
   198  * @param itemsPerPage		Number of items per page, or MENU_NO_PAGINATION.
   199  * @return					True on success, false if pagination is too high or low.
   200  * @error					Invalid Handle.
   201  */
   202 native bool:SetMenuPagination(Handle:menu, itemsPerPage);
   203 
   204 /**
   205  * Returns a menu's pagination setting.
   206  *
   207  * @param menu				Handle to the menu.
   208  * @return					Pagination setting.
   209  * @error					Invalid Handle.
   210  */
   211 native GetMenuPagination(Handle:menu);
   212 
   213 /**
   214  * Returns a menu's MenuStyle Handle.  The Handle
   215  * is global and cannot be freed.
   216  *
   217  * @param menu				Handle to the menu.
   218  * @return 					Handle to the menu's draw style.
   219  * @error					Invalid Handle.
   220  */
   221 native Handle:GetMenuStyle(Handle:menu);
   222 
   223 /**
   224  * Sets the menu's default title/instruction message.
   225  *
   226  * @param menu				Menu Handle.
   227  * @param fmt				Message string format
   228  * @param ...				Message string arguments.
   229  * @noreturn
   230  * @error					Invalid Handle.
   231  */
   232 native SetMenuTitle(Handle:menu, const String:fmt[], any:...);
   233 
   234 /**
   235  * Creates a raw MenuPanel based off the menu's style.
   236  * The Handle must be freed with CloseHandle().
   237  *
   238  * @return					A new MenuPanel Handle.
   239  * @error					Invalid Handle.
   240  */
   241 native Handle:CreatePanelFromMenu(Handle:menu);
   242 
   243 /**
   244  * Returns whether or not the menu has an exit button.
   245  * By default, menus have an exit button.
   246  *
   247  * @param menu				Menu Handle.
   248  * @return 					True if the menu has an exit button; false otherwise.
   249  * @error					Invalid Handle.
   250  */
   251 native bool:GetMenuExitButton(Handle:menu);
   252 
   253 /**
   254  * Sets whether or not the menu has an exit button.
   255  * By default, menus have an exit button.
   256  *
   257  * @param menu				Menu Handle.
   258  * @param button			True to enable the button, false to remove it.
   259  * @return 					True if allowed; false on failure.
   260  * @error					Invalid Handle.
   261  */
   262 native bool:SetMenuExitButton(Handle:menu, bool:button);
   263 
   264 /**
   265  * Cancels a menu from displaying on all clients.  While the
   266  * cancellation is in progress, this menu cannot be re-displayed
   267  * to any clients.
   268  *
   269  * The menu may still exist on the client's screen after this command.
   270  * This simply verifies that the menu is not being used anywhere.
   271  *
   272  * If any vote is in progress on a menu, it will be cancelled.
   273  *
   274  * @param menu				Menu Handle.
   275  * @noreturn
   276  * @error					Invalid Handle.
   277  */
   278 native CancelMenu(Handle:menu);
   279 
   280 /**
   281  * Returns whether a vote is in progress on the given menu.
   282  *
   283  * @param menu				Menu Handle.
   284  * @return					True if a vote is in progress, false otherwise.
   285  */
   286 native bool:IsVoteInProgress(Handle:menu);
   287 
   288 /**
   289  * Broadcasts a menu to a list of clients.  The most selected item will be 
   290  * returned through MenuAction_End.  On a tie, a random item will be returned 
   291  * from a list of the tied items.
   292  *
   293  * Note that MenuAction_VoteEnd and MenuAction_VoteStart are both
   294  * default callbacks and do not need to be enabled.
   295  *
   296  * @param menu				Menu Handle.
   297  * @param clients			Array of clients to broadcast to.
   298  * @param numClients		Number of clients in the array.
   299  * @param time				Maximum time to leave menu on the screen.
   300  * @return					True on success, false if this menu already has a vote session
   301  *							in progress.
   302  * @error					Invalid Handle.
   303  */
   304 native bool:VoteMenu(Handle:menu, clients[], numClients, time);
   305 
   306 /**
   307  * Sends a vote menu to all clients.  See VoteMenu() for more information.
   308  *
   309  * @param menu				Menu Handle.
   310  * @param time				Maximum time to leave menu on the screen.
   311  * @return					True on success, false if this menu already has a vote session
   312  *							in progress.
   313  * @error					Invalid Handle.
   314  */
   315 stock VoteMenuToAll(Handle:menu, time)
   316 {
   317 	new num = GetMaxClients();
   318 	new total;
   319 	decl players[num];
   320 	
   321 	for (new i=1; i<=num; i++)
   322 	{
   323 		if (!IsClientConnected(i))
   324 		{
   325 			continue;
   326 		}
   327 		players[total++] = i;
   328 	}
   329 	
   330 	return VoteMenu(menu, players, total, time);
   331 }
   332 
   333 /**
   334  * Returns a style's global Handle.
   335  *
   336  * @param style			Menu Style.
   337  * @return				A Handle, or INVALID_HANDLE if not found or unusable.
   338  */
   339 native Handle:GetMenuStyleHandle(MenuStyle:style);
   340 
   341 /**
   342  * Creates a MenuPanel from a MenuStyle.  Panels are used for drawing raw 
   343  * menus without any extra helper functions.  The Handle must be closed
   344  * with CloseHandle().
   345  *
   346  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   347  * @return				A new MenuPanel Handle.
   348  * @error				Invalid Handle other than INVALID_HANDLE.
   349  */
   350 native Handle:CreatePanel(Handle:hStyle=INVALID_HANDLE);
   351 
   352 /** 
   353  * Creates a Menu from a MenuStyle.  The Handle must be closed with 
   354  * CloseHandle().
   355  *
   356  * @parma hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   357  * @param handler		Function which will receive menu actions.
   358  * @param actions		Optionally set which actions to receive.  Select, 
   359  *						Cancel, and End will always be received regardless
   360  *						of whether they are set or not.  They are also
   361  *						the only default actions.
   362  * @return				A new menu Handle.
   363  * @error				Invalid Handle other than INVALID_HANDLE.
   364  */
   365 native Handle:CreateMenuEx(Handle:hStyle=INVALID_HANDLE, MenuHandler:handler, MenuAction:actions=MENU_ACTIONS_DEFAULT);
   366 
   367 /**
   368  * Returns whether a client is viewing a menu.  If the menu source 
   369  * is MenuSource_Normal, a menu Handle will also be returned.
   370  *
   371  * @param client		Client index.
   372  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   373  * @return				A MenuSource value.
   374  * @error				Invalid Handle other than INVALID_HANDLE.
   375  */
   376 native MenuSource:GetClientMenu(client, Handle:hStyle=INVALID_HANDLE);
   377 
   378 /** 
   379  * Cancels a menu on a client.  This will only affect non-external menus.
   380  *
   381  * @param hstyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   382  * @param client		Client index.
   383  * @param autoIgnore	If true, no menus can be re-drawn on the client during
   384  *						the cancellation process.
   385  * @return				True if a menu was cancelled, false otherwise.
   386  */
   387 native bool:CancelClientMenu(client, bool:autoIgnore=false, Handle:hStyle=INVALID_HANDLE);
   388 
   389 /**
   390  * Returns a style's maximum items per page.
   391  *
   392  * @param hStyle		MenuStyle Handle, or INVALID_HANDLE to use the default style.
   393  * @return				Maximum items per page.
   394  * @error				Invalid Handle other than INVALID_HANDLE.
   395  */
   396 native GetMaxPageItems(Handle:hStyle=INVALID_HANDLE);
   397 
   398 /**
   399  * Returns a MenuPanel's parent style.
   400  *
   401  * @param panel			A MenuPanel Handle.
   402  * @return				The MenuStyle Handle that created the panel.
   403  * @error				Invalid Handle.
   404  */
   405 native Handle:GetPanelStyle(Handle:panel);
   406 
   407 /**
   408  * Sets the panel's title.
   409  *
   410  * @param panel			A MenuPanel Handle.
   411  * @param title			Text to set as the title.
   412  * @param onlyIfEmpty	If true, the title will only be set if no title is set.
   413  * @noreturn
   414  * @error				Invalid Handle.
   415  */
   416 native Handle:SetPanelTitle(Handle:panel, const String:text[], bool:onlyIfEmpty=false);
   417 
   418 /**
   419  * Draws an item on a panel.  If the item takes up a slot, the position
   420  * is returned.
   421  *
   422  * @param panel			A MenuPanel Handle.
   423  * @param text			Display text to use.  If not a raw line, 
   424  *						the style may automatically add color markup.
   425  *						No numbering or newlines are needed.
   426  * @param style			ITEMDRAW style flags.
   427  * @return				A slot position, or 0 if item was a rawline or could not be drawn.
   428  * @error				Invalid Handle.
   429  */
   430 native DrawPanelItem(Handle:panel, const String:text[], style=ITEMDRAW_DEFAULT);
   431 
   432 /**
   433  * Draws a raw line of text on a panel, without any markup other than a newline.
   434  *
   435  * @param panel			A MenuPanel Handle.
   436  * @param text			Display text to use.
   437  * @return				True on success, false if raw lines are not supported.
   438  * @error				Invalid Handle.
   439  */
   440 native DrawPanelText(Handle:panel, const String:text[]);
   441 
   442 /**
   443  * Returns whether or not the given drawing flags are supported by
   444  * the menu style.
   445  *
   446  * @param panel			A MenuPanel Handle.
   447  * @param style			ITEMDRAW style flags.
   448  * @return				True if item is drawable, false otherwise.
   449  * @error				Invalid Handle.
   450  */
   451 native CanPanelDrawFlags(Handle:panel, style);
   452 
   453 /**
   454  * Sets the selectable key map of a panel.  This is not supported by
   455  * all styles (only by Radio, as of this writing).  
   456  *
   457  * @param keys			An integer where each bit N allows key
   458  *						N+1 to be selected.  If no keys are selectable,
   459  *						then key 0 (bit 9) is automatically set.
   460  * @return 				True if supported, false otherwise.
   461  */
   462 native bool:SetPanelKeys(Handle:panel, keys);
   463 
   464 /**
   465  * Sends a panel to a client.  Unlike full menus, the handler
   466  * function will only receive the following actions, both of 
   467  * which will have INVALID_HANDLE for a menu, and the client
   468  * as param1.
   469  *
   470  * MenuAction_Select (param2 will be the key pressed)
   471  * MenuAction_Cancel (param2 will be the reason)
   472  *
   473  * Also, if the menu fails to display, no callbacks will be called.
   474  *
   475  * @param panel			A MenuPanel Handle.
   476  * @param client		A client to draw to.
   477  * @param handler		The MenuHandler function to catch actions with.
   478  * @param time			Time to hold the menu for.
   479  * @return				True on success, false on failure.
   480  * @error				Invalid Handle.
   481  */
   482 native bool:SendPanelToClient(Handle:panel, client, MenuHandler:handler, time);
   483 
   484 /**
   485  * @brief Returns the current key position.
   486  *
   487  * @param panel			A MenuPanel Handle.
   488  * @return				Current key position starting at 1.
   489  * @error				Invalid Handle.
   490  */
   491 native GetPanelCurrentKey(Handle:panel);
   492 
   493 /**
   494  * @brief Sets the next key position.  This cannot be used
   495  * to traverse backwards.
   496  *
   497  * @param panel			A MenuPanel Handle.
   498  * @param key			Key that is greater or equal to
   499  *						GetPanelCurrentKey().
   500  * @return				True on success, false otherwise.
   501  * @error				Invalid Handle.
   502  */
   503 native bool:SetPanelCurrentKey(Handle:panel, key);