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