public/IMenuManager.h
author David Anderson <dvander@alliedmods.net>
Sun May 13 04:43:37 2007 +0000 (2007-05-13)
changeset 777 9d7b77df8649
parent 776 c031ed44193c
child 780 570fa34723bf
permissions -rw-r--r--
fixed MENU_NO_PAGINATION and a typo
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
    20 #define _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
    21 
    22 #include <IShareSys.h>
    23 
    24 #define SMINTERFACE_MENUMANAGER_NAME		"IMenuManager"
    25 #define SMINTERFACE_MENUMANAGER_VERSION		1
    26 
    27 /**
    28  * @file IMenuManager.h
    29  * @brief Abstracts on-screen menus for clients.
    30  */
    31 
    32 namespace SourceMod
    33 {
    34 	/**
    35 	 * @brief Used to determine how an item selection is interpreted.
    36 	 */
    37 	enum ItemSelection
    38 	{
    39 		ItemSel_None,					/**< Invalid selection */
    40 		ItemSel_Back,					/**< Go back one page */
    41 		ItemSel_Next,					/**< Go forward one page */
    42 		ItemSel_Exit,					/**< Menu was exited */
    43 		ItemSel_Item,					/**< Valid item selection */
    44 	};
    45 
    46 	/**
    47 	 * @brief Used to determine which order to search for items in.
    48 	 */
    49 	enum ItemOrder
    50 	{
    51 		ItemOrder_Ascending,			/**< Items should be drawn ascendingly */
    52 		ItemOrder_Descending,			/**< Items should be drawn descendingly */
    53 	};
    54 
    55 	/**
    56 	 * @brief Pairs an item type with an item menu position.
    57 	 */
    58 	struct menu_slots_t
    59 	{
    60 		ItemSelection type;				/**< Item selection type */
    61 		unsigned int item;				/**< Item position, if applicable */
    62 	};
    63 
    64 	class IBaseMenu;
    65 	class IMenuDisplay;
    66 	class IMenuHandler;
    67 
    68 	/**
    69 	 * @brief Describes menu display information.
    70 	 */
    71 	struct menu_states_t
    72 	{
    73 		unsigned int apiVers;			/**< Must be filled with the API version */
    74 		IBaseMenu *menu;				/**< Menu pointer, or NULL if there is only a display */
    75 		IMenuHandler *mh;				/**< Menu callbacks handler */
    76 		unsigned int firstItem;			/**< MENU ONLY: First item displayed on the last page */
    77 		unsigned int lastItem;			/**< MENU ONLY: Last item displayed on the last page */
    78 		menu_slots_t slots[11];			/**< MENU ONLY: Item selection table (first index is 1) */
    79 	};
    80 
    81 	#define ITEMDRAW_DEFAULT		(0)		/**< Item should be drawn normally */
    82 	#define ITEMDRAW_DISABLED		(1<<0)	/**< Item is drawn but not selectable */
    83 	#define ITEMDRAW_RAWLINE		(1<<1)	/**< Item should be a raw line, without a slot */
    84 	#define ITEMDRAW_NOTEXT			(1<<2)	/**< No text should be drawn */
    85 	#define ITEMDRAW_SPACER			(1<<3)	/**< Item should be drawn as a spacer, if possible */
    86 	#define ITEMDRAW_IGNORE	((1<<1)|(1<<2))	/**< Item should be completely ignored (rawline + notext) */
    87 
    88 	/**
    89 	 * @brief Information about item drawing.
    90 	 */
    91 	struct ItemDrawInfo
    92 	{
    93 		ItemDrawInfo()
    94 		{
    95 			style = 0;
    96 			display = NULL;
    97 		}
    98 		ItemDrawInfo(const char *DISPLAY, unsigned int STYLE=ITEMDRAW_DEFAULT) 
    99 			: display(DISPLAY), style(STYLE)
   100 		{
   101 		}
   102 		const char *display;			/**< Display text (NULL for none) */
   103 		unsigned int style;				/**< ITEMDRAW style flags */
   104 	};
   105 
   106 	/**
   107 	 * @brief Reasons for a menu dying.
   108 	 */
   109 	enum MenuCancelReason
   110 	{
   111 		MenuCancel_Disconnect = -1,	/** Client dropped from the server */
   112 		MenuCancel_Interrupt = -2,	/** Client was interrupted with another menu */
   113 		MenuCancel_Exit = -3,		/** Client selected "exit" on a paginated menu */
   114 		MenuCancel_NoDisplay = -4,	/** Menu could not be displayed to the client */
   115 	};
   116 
   117 	#define MENU_NO_PAGINATION			0		/**< Menu should not be paginated (10 items max) */
   118 	#define MENU_TIME_FOREVER			0		/**< Menu should be displayed as long as possible */
   119 
   120 	/**
   121 	 * @brief Extended menu options.
   122 	 */
   123 	enum MenuOption
   124 	{
   125 		MenuOption_IntroMessage,				/**< CONST CHAR *: Valve menus only; defaults to:
   126 													 "You have a menu, hit ESC"
   127 													 */
   128 		MenuOption_IntroColor,					/**< INT[4]: Valve menus only; specifies the intro message colour
   129 													 using R,G,B,A (defaults to 255,0,0,255)
   130 													 */
   131 		MenuOption_Priority,					/**< INT *: Valve menus only; priority (less is higher) */
   132 	};
   133 
   134 	/**
   135 	 * @brief Describes the menu a player is viewing.
   136 	 */
   137 	enum MenuSource
   138 	{
   139 		MenuSource_None = 0,					/**< No menu is being displayed */
   140 		MenuSource_External = 1,				/**< External menu, no pointer */
   141 		MenuSource_BaseMenu = 2,				/**< An IBaseMenu pointer. */
   142 		MenuSource_Display = 3,					/**< IMenuDisplay source, no pointer */
   143 	};
   144 
   145 	class IMenuStyle;
   146 
   147 	/**
   148 	 * @brief Sets how a raw menu should be drawn.
   149 	 */
   150 	class IMenuDisplay
   151 	{
   152 	public:
   153 		/**
   154 		 * @brief Returns the parent IMenuStyle pointer.
   155 		 *
   156 		 * @return				IMenuStyle pointer which created
   157 		 *						this object.
   158 		 */
   159 		virtual IMenuStyle *GetParentStyle() =0;
   160 
   161 		/**
   162 		 * @brief Resets/clears the cached display text.
   163 		 */
   164 		virtual void Reset() =0;
   165 
   166 		/**
   167 		 * @brief Sets how the title should be drawn.
   168 		 *
   169 		 * @param text			Text string to display for the title.
   170 		 * @param onlyIfEmpty	Only sets the title if one does not already 
   171 		 *						exist.
   172 		 */
   173 		virtual void DrawTitle(const char *text, bool onlyIfEmpty=false) =0;
   174 
   175 		/**
   176 		 * @brief Adds an item to the menu and returns the position (1-10).
   177 		 *
   178 		 * Note: Item will fail to draw if there are too many items,
   179 		 * or the item is not drawable (for example, invisible).	
   180 		 * 
   181 		 * @return				Item draw position, or 0 on failure.
   182 		 */
   183 		virtual unsigned int DrawItem(const ItemDrawInfo &item) =0;
   184 
   185 		/**
   186 		 * @brief Draws a raw line of text, if supported.  The line does not 
   187 		 * need to be newline terminated.
   188 		 *
   189 		 * @return				True on success, false if not supported.
   190 		 */
   191 		virtual bool DrawRawLine(const char *rawline) =0;
   192 
   193 		/**
   194 		 * @brief Sets an extended menu option.
   195 		 *
   196 		 * @param option		Option type.
   197 		 * @param valuePtr		Pointer of the type expected by the option.
   198 		 * @return				True on success, false if option or value is not supported.
   199 		 */
   200 		virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
   201 
   202 		/**
   203 		 * @brief Returns whether the display is capable of rendering an item
   204 		 * with the given flags.
   205 		 *
   206 		 * @param flags			ITEMDRAW flags.
   207 		 * @return				True if renderable, false otherwise.
   208 		 */
   209 		virtual bool CanDrawItem(unsigned int drawFlags) =0;
   210 
   211 		/**
   212 		 * @brief Sends the menu display to a client.
   213 		 *
   214 		 * @param client		Client index to display to.
   215 		 * @param handler		Menu handler to use.
   216 		 * @param time			Time to hold menu for.
   217 		 * @return				True on success, false otherwise.
   218 		 */
   219 		virtual bool SendDisplay(int client, IMenuHandler *handler, unsigned int time) =0;
   220 
   221 		/**
   222 		 * @brief Destroys the display object.
   223 		 */
   224 		virtual void DeleteThis() =0;
   225 	};
   226 
   227 	/**
   228 	 * @brief Describes a "MenuStyle" system which manages
   229 	 * menu drawing and construction.
   230 	 */
   231 	class IMenuStyle
   232 	{
   233 	public:
   234 		/**
   235 		 * @brief Returns the style API version.
   236 		 *
   237 		 * @return				API version.
   238 		 */
   239 		virtual unsigned int GetStyleAPIVersion()
   240 		{
   241 			return SMINTERFACE_MENUMANAGER_VERSION;
   242 		}
   243 
   244 		/**
   245 		 * @brief Returns the name of the menu style.
   246 		 *
   247 		 * @return				String containing the style name.
   248 		 */
   249 		virtual const char *GetStyleName() =0;
   250 
   251 		/**
   252 		 * @brief Creates an IMenuDisplay object.
   253 		 *
   254 		 * Note: the object should be freed using ::DeleteThis.
   255 		 *
   256 		 * @return				IMenuDisplay object.
   257 		 */
   258 		virtual IMenuDisplay *CreateDisplay() =0;
   259 
   260 		/**
   261 		 * @brief Creates an IBaseMenu object of this style.
   262 		 *
   263 		 * Note: the object should be freed using IBaseMenu::Destroy.
   264 		 *
   265 		 * @return				An IBaseMenu pointer.
   266 		 */
   267 		virtual IBaseMenu *CreateMenu() =0;
   268 
   269 		/**
   270 		 * @brief Returns the maximum number of items per page.
   271 		 *
   272 		 * @return				Number of items per page.
   273 		 */
   274 		virtual unsigned int GetMaxPageItems() =0;
   275 
   276 		/**
   277 		 * @brief Returns whether or not a client is viewing a menu.
   278 		 *
   279 		 * @param client		Client index.
   280 		 * @param object		Optional pointer to retrieve menu object,
   281 		 *						if any.
   282 		 * @return				MenuSource value.
   283 		 */
   284 		virtual MenuSource GetClientMenu(int client, void **object) =0;
   285 
   286 		/**
   287 		 * @brief Cancels a client's menu.
   288 		 *
   289 		 * @param client		Client index.
   290 		 * @param autoIgnore	If true, no menus can be created during 
   291 		 *						the cancellation process.
   292 		 * @return				True if a menu was cancelled, false otherwise.
   293 		 */
   294 		virtual bool CancelClientMenu(int client, bool autoIgnore=false) =0;
   295 	};
   296 
   297 	/**
   298 	 * @brief High-level interface for building menus.
   299 	 */
   300 	class IBaseMenu
   301 	{
   302 	public:
   303 		/**
   304 		 * @brief Appends an item to the end of a menu.
   305 		 *
   306 		 * @param info			Item information string.
   307 		 * @param draw			Default drawing information.
   308 		 * @return				True on success, false on item limit reached.
   309 		 */
   310 		virtual bool AppendItem(const char *info, const ItemDrawInfo &draw) =0;
   311 
   312 		/**
   313 		 * @brief Inserts an item into the menu before a certain position; 
   314 		 * the new item will be at the given position and all next items 
   315 		 * pushed forward.
   316 		 *
   317 		 * @param position		Position, starting from 0.
   318 		 * @param info			Item information string.
   319 		 * @param draw			Default item draw info.
   320 		 * @return				True on success, false on invalid menu position
   321 		 */
   322 		virtual bool InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw) =0;
   323 
   324 		/**
   325 		 * @brief Removes an item from the menu.
   326 		 *
   327 		 * @param position		Position, starting from 0.
   328 		 * @return				True on success, false on invalid menu position.
   329 		 */
   330 		virtual bool RemoveItem(unsigned int position) =0;
   331 
   332 		/**
   333 		 * @brief Removes all items from the menu.
   334 		 */
   335 		virtual void RemoveAllItems() =0;
   336 
   337 		/**
   338 		 * @brief Returns an item's info.
   339 		 *
   340 		 * @param position		Position, starting from 0.
   341 		 * @param draw			Optional pointer to store a draw information.
   342 		 * @return				Info string pointer, or NULL if position was invalid.
   343 		 */
   344 		virtual const char *GetItemInfo(unsigned int position, ItemDrawInfo *draw) =0;
   345 
   346 		/**
   347 		 * @brief Returns the number of items.
   348 		 *
   349 		 * @return				Number of items in the menu.
   350 		 */
   351 		virtual unsigned int GetItemCount() =0;
   352 
   353 		/** 
   354 		 * @brief Sets the menu's pagination,.
   355 		 *
   356 		 * @param itemsPerPage	Number of items per page, or MENU_NO_PAGINATION.
   357 		 * @return				True on success, false if itemsPerPage is too large.
   358 		 */
   359 		virtual bool SetPagination(unsigned int itemsPerPage) =0;
   360 
   361 		/**
   362 		 * @brief Returns an item's pagination.
   363 		 *
   364 		 * @return				Pagination setting.
   365 		 */
   366 		virtual unsigned int GetPagination() =0;
   367 
   368 		/**
   369 		 * @brief Returns the menu style.
   370 		 * 
   371 		 * @return				Menu style.
   372 		 */
   373 		virtual IMenuStyle *GetDrawStyle() =0;
   374 
   375 		/**
   376 		 * @brief Sets the menu's display title/message.
   377 		 *
   378 		 * @param message		Message (format options allowed).
   379 		 */
   380 		virtual void SetDefaultTitle(const char *message) =0;
   381 
   382 		/**
   383 		 * @brief Returns the menu's display/title message.
   384 		 *
   385 		 * @return				Message string.
   386 		 */
   387 		virtual const char *GetDefaultTitle() =0;
   388 
   389 		/**
   390 		 * @brief Sets an extended menu option.
   391 		 *
   392 		 * @param option		Option type.
   393 		 * @param valuePtr		Pointer of the type expected by the option.
   394 		 * @return				True on success, false if option or value is not supported.
   395 		 */
   396 		virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
   397 
   398 		/**
   399 		 * @brief Creates a new IMenuDisplay object using extended options specific
   400 		 * to the IMenuStyle parent.  Titles, items, etc, are not copied.
   401 		 *
   402 		 * Note: The object should be freed with IMenuDisplay::DeleteThis.
   403 		 *
   404 		 * @return				IMenuDisplay pointer.
   405 		 */
   406 		virtual IMenuDisplay *CreateDisplay() =0;
   407 
   408 		/**
   409 		 * @brief Returns whether or not the menu should have an "Exit" button for
   410 		 * paginated menus.
   411 		 *
   412 		 * @return				True to have an exit button, false otherwise.
   413 		 */
   414 		virtual bool GetExitButton() =0;
   415 		
   416 		/**
   417 		 * @brief Sets whether or not the menu should have an "Exit" button for
   418 		 * paginated menus.
   419 		 *
   420 		 * @param set			True to enable, false to disable the exit button.
   421 		 * @return				True on success, false if the exit button is 
   422 		 *						non-optional.
   423 		 */
   424 		virtual bool SetExitButton(bool set) =0;
   425 
   426 		/**
   427 		 * @brief Sends the menu to a client.
   428 		 *
   429 		 * @param client		Client index to display to.
   430 		 * @param handler		Menu handler to use.
   431 		 * @param time			Time to hold menu for.
   432 		 * @return				True on success, false otherwise.
   433 		 */
   434 		virtual bool Display(int client, IMenuHandler *handler, unsigned int time) =0;
   435 
   436 		/**
   437 		 * @brief Destroys the menu and frees all associated resources.1
   438 		 */
   439 		virtual void Destroy() =0;
   440 
   441 		/**
   442 		 * @brief Cancels the menu on all client's displays.  While the menu is
   443 		 * being cancelled, the menu may not be re-displayed to any clients.
   444 		 *
   445 		 * @return				Number of menus cancelled.
   446 		 */
   447 		virtual void Cancel() =0;
   448 	};
   449 
   450 	/** 
   451 	 * @brief Contains callbacks for menu actions.
   452 	 */
   453 	class IMenuHandler
   454 	{
   455 	public:
   456 		/**
   457 		 * @brief Returns the menu api verison.
   458 		 *
   459 		 * @return				Menu API version.
   460 		 */
   461 		virtual unsigned int GetMenuAPIVersion2()
   462 		{
   463 			return SMINTERFACE_MENUMANAGER_VERSION;
   464 		}
   465 
   466 		/** 
   467 		 * @brief A display/selection cycle has started.
   468 		 *
   469 		 * @param menu			Menu pointer.
   470 		 */
   471 		virtual void OnMenuStart(IBaseMenu *menu)
   472 		{
   473 		}
   474 
   475 		/**
   476 		 * @brief Called before a menu is being displayed.  This is where
   477 		 * you can set an alternate title on the menu.
   478 		 *
   479 		 * @param menu			Menu pointer.
   480 		 * @param client		Client index.
   481 		 * @param display		IMenuDisplay pointer.
   482 		 */
   483 		virtual void OnMenuDisplay(IBaseMenu *menu, int client, IMenuDisplay *display)
   484 		{
   485 		}
   486 
   487 		/**
   488 		 * @brief Called when an item is selected.
   489 		 *
   490 		 * @param menu			Menu pointer.
   491 		 * @param client		Client that selected the item.
   492 		 * @param item			Item number.
   493 		 */
   494 		virtual void OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
   495 		{
   496 		}
   497 
   498 		/**
   499 		 * @brief An active menu display was dropped from a client.
   500 		 *
   501 		 * @param menu			Menu pointer.
   502 		 * @param client		Client that had the menu.
   503 		 * @param reason		Menu cancellation reason.
   504 		 */
   505 		virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
   506 		{
   507 		}
   508 
   509 		/**
   510 		 * @brief A display/selection cycle has ended.
   511 		 *
   512 		 * @param menu			Menu pointer.
   513 		 */
   514 		virtual void OnMenuEnd(IBaseMenu *menu)
   515 		{
   516 		}
   517 
   518 		/**
   519 		 * @brief Called when requesting how to render an item.
   520 		 *
   521 		 * @param menu			Menu pointer.
   522 		 * @param client		Client index receiving the menu.
   523 		 * @param item			Item number in the menu.
   524 		 * @param style			ITEMSTYLE flags, by reference for modification.
   525 		 */
   526 		virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
   527 		{
   528 		}
   529 
   530 		/**
   531 		 * @brief Called when requesting how to draw an item's text.
   532 		 *
   533 		 * @param menu			Menu pointer.
   534 		 * @param client		Client index receiving the menu.
   535 		 * @param item			Item number in the menu.
   536 		 * @param display		Pointer to the display text string (changeable).
   537 		 */
   538 		virtual void OnMenuDisplayItem(IBaseMenu *menu, int client, unsigned int item, const char **display)
   539 		{
   540 		}
   541 	};
   542 
   543 	/** 
   544 	 * @brief Handles a vote menu.
   545 	 */
   546 	class IMenuVoteHandler : public IMenuHandler
   547 	{
   548 	public:
   549 		/**
   550 		 * @brief Called when a vote ends.
   551 		 *
   552 		 * @param menu			Menu pointer.
   553 		 * @param item			Item position that was chosen by a majority.
   554 		 */
   555 		virtual void OnMenuVoteEnd(IBaseMenu *menu, unsigned int item) =0;
   556 	};
   557 
   558 	/**
   559 	 * @brief Manages menu creation and displaying.
   560 	 */
   561 	class IMenuManager : public SMInterface
   562 	{
   563 	public:
   564 		virtual const char *GetInterfaceName()
   565 		{
   566 			return SMINTERFACE_MENUMANAGER_NAME;
   567 		}
   568 		virtual unsigned int GetInterfaceVersion()
   569 		{
   570 			return SMINTERFACE_MENUMANAGER_VERSION;
   571 		}
   572 	public:
   573 		/**
   574 		 * @brief Finds a style by name.
   575 		 *
   576 		 * @param name			Name of the style (case insensitive).
   577 		 * @return				IMenuStyle pointer, or NULL if not found.
   578 		 */
   579 		virtual IMenuStyle *FindStyleByName(const char *name) =0;
   580 
   581 		/**
   582 		 * @brief Broadcasts a menu to a number of clients.
   583 		 *
   584 		 * @param menu			Menu pointer.
   585 		 * @param handler		IMenuHandler pointer.
   586 		 * @param clients		Array of client indexes.
   587 		 * @param numClients	Number of clients in the array.
   588 		 * @param time			Time to hold the menu.
   589 		 * @return				Number of clients the menu will be waiting on.
   590 		 */
   591 		virtual unsigned int BroadcastMenu(IBaseMenu *menu, 
   592 									IMenuHandler *handler,
   593 									int clients[], 
   594 									unsigned int numClients,
   595 									unsigned int time) =0;
   596 
   597 		/**
   598 		 * @brief Broadcasts a menu to a number of clients as a vote menu.
   599 		 *
   600 		 * @param menu			Menu pointer.
   601 		 * @param handler		IMenuHandler pointer.
   602 		 * @param clients		Array of client indexes.
   603 		 * @param numClients	Number of clients in the array.
   604 		 * @param time			Time to hold the menu.
   605 		 * @return				Number of clients the menu will be waiting on.
   606 		 */
   607 		virtual unsigned int VoteMenu(IBaseMenu *menu, 
   608 									IMenuVoteHandler *handler,
   609 									int clients[], 
   610 									unsigned int numClients,
   611 									unsigned int time) =0;
   612 
   613 		/**
   614 		 * @brief Returns the default draw style Core is using.
   615 		 *
   616 		 * @return				Menu style pointer.
   617 		 */
   618 		virtual IMenuStyle *GetDefaultStyle() =0;
   619 
   620 		/** 
   621 		 * @brief Sets the default draw style Core uses.
   622 		 *
   623 		 * @param style			Menu style.
   624 		 * @return				True on success, false on failure.
   625 		 */
   626 		virtual bool SetDefaultStyle(IMenuStyle *style) =0;
   627 
   628 		/**
   629 		 * @brief Given a set of menu states, converts it to an IDisplay object.
   630 		 *
   631 		 * The state parameter is both INPUT and OUTPUT.
   632 		 * INPUT: menu, mh, firstItem, lastItem
   633 		 * OUTPUT: display, firstItem, lastItem, slots
   634 		 *
   635 		 * @param client		Client index.
   636 		 * @param states		Menu states.
   637 		 * @return				IDisplay pointer, or NULL if no items could be 
   638 		 *						found in the IBaseMenu pointer, or NULL if any
   639 		 *						other error occurred.  Any valid pointer must
   640 		 *						be freed using IMenuDisplay::DeleteThis.
   641 		 */
   642 		virtual IMenuDisplay *RenderMenu(int client, menu_states_t &states, ItemOrder order) =0;
   643 	};
   644 }
   645 
   646 #endif //_INCLUDE_SOURCEMOD_MENU_SYSTEM_H_