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