3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #ifndef _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
33 #define _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
35 #include <IShareSys.h>
36 #include <IHandleSys.h>
38 #define SMINTERFACE_MENUMANAGER_NAME "IMenuManager"
39 #define SMINTERFACE_MENUMANAGER_VERSION 15
42 * @file IMenuManager.h
43 * @brief Abstracts on-screen menus for clients.
49 * @brief Used to determine how an item selection is interpreted.
53 ItemSel_None, /**< Invalid selection */
54 ItemSel_Back, /**< Go back one page (really "Previous") */
55 ItemSel_Next, /**< Go forward one page */
56 ItemSel_Exit, /**< Menu was exited */
57 ItemSel_Item, /**< Valid item selection */
58 ItemSel_ExitBack, /**< Sends MenuEnd_ExitBack */
62 * @brief Used to determine which order to search for items in.
66 ItemOrder_Ascending, /**< Items should be drawn ascendingly */
67 ItemOrder_Descending, /**< Items should be drawn descendingly */
71 * @brief Pairs an item type with an item menu position.
75 ItemSelection type; /**< Item selection type */
76 unsigned int item; /**< Item position, if applicable */
84 * @brief Describes menu display information.
88 unsigned int apiVers; /**< Must be filled with the API version */
89 IBaseMenu *menu; /**< Menu pointer, or NULL if there is only a display */
90 IMenuHandler *mh; /**< Menu callbacks handler */
91 unsigned int firstItem; /**< MENU ONLY: First item displayed on the last page */
92 unsigned int lastItem; /**< MENU ONLY: Last item displayed on the last page */
93 unsigned int item_on_page; /**< MENU ONLY: First item on page */
94 menu_slots_t slots[11]; /**< MENU ONLY: Item selection table (first index is 1) */
97 #define ITEMDRAW_DEFAULT (0) /**< Item should be drawn normally */
98 #define ITEMDRAW_DISABLED (1<<0) /**< Item is drawn but not selectable */
99 #define ITEMDRAW_RAWLINE (1<<1) /**< Item should be a raw line, without a slot */
100 #define ITEMDRAW_NOTEXT (1<<2) /**< No text should be drawn */
101 #define ITEMDRAW_SPACER (1<<3) /**< Item should be drawn as a spacer, if possible */
102 #define ITEMDRAW_IGNORE ((1<<1)|(1<<2)) /**< Item should be completely ignored (rawline + notext) */
103 #define ITEMDRAW_CONTROL (1<<4) /**< Item is control text (back/next/exit) */
106 * @brief Information about item drawing.
110 ItemDrawInfo(const char *DISPLAY=NULL, unsigned int STYLE=ITEMDRAW_DEFAULT,
111 unsigned int FLAGS=0, const char *HELPTEXT=NULL)
112 : display(DISPLAY), style(STYLE)
115 const char *display; /**< Display text (NULL for none) */
116 unsigned int style; /**< ITEMDRAW style flags */
120 * @brief Contains information about a vote result.
122 struct menu_vote_result_t
124 unsigned int num_clients; /**< Number of clients the menu was displayed to */
125 unsigned int num_votes; /**< Number of votes received */
126 struct menu_client_vote_t
128 int client; /**< Client index */
129 int item; /**< Item # (or -1 for none) */
130 } *client_list; /**< Array of size num_clients */
131 unsigned int num_items; /**< Number of items voted for */
132 struct menu_item_vote_t
134 unsigned int item; /**< Item index */
135 unsigned int count; /**< Number of votes */
136 } *item_list; /**< Array of size num_items, sorted by count,
141 * @brief Reasons for a menu dying.
143 enum MenuCancelReason
145 MenuCancel_Disconnected = -1, /**< Client dropped from the server */
146 MenuCancel_Interrupted = -2, /**< Client was interrupted with another menu */
147 MenuCancel_Exit = -3, /**< Client selected "exit" on a paginated menu */
148 MenuCancel_NoDisplay = -4, /**< Menu could not be displayed to the client */
149 MenuCancel_Timeout = -5, /**< Menu timed out */
150 MenuCancel_ExitBack = -6, /**< Client selected "exit back" on a paginated menu */
154 * @brief Reasons a menu ended.
158 MenuEnd_Selected = 0, /**< Menu item was selected */
159 MenuEnd_VotingDone = -1, /**< Voting finished */
160 MenuEnd_VotingCancelled = -2, /**< Voting was cancelled */
161 MenuEnd_Cancelled = -3, /**< Menu was uncleanly cancelled */
162 MenuEnd_Exit = -4, /**< Menu was cleanly exited via "exit" */
163 MenuEnd_ExitBack = -5, /**< Menu was cleanly exited via "back" */
167 * @brief Reasons a vote can be cancelled.
169 enum VoteCancelReason
171 VoteCancel_Generic = -1, /**< Vote was generically cancelled. */
172 VoteCancel_NoVotes = -2, /**< Vote did not receive any votes. */
175 #define MENU_NO_PAGINATION 0 /**< Menu should not be paginated (10 items max) */
176 #define MENU_TIME_FOREVER 0 /**< Menu should be displayed as long as possible */
178 #define MENUFLAG_BUTTON_EXIT (1<<0) /**< Menu has an "exit" button */
179 #define MENUFLAG_BUTTON_EXITBACK (1<<1) /**< Menu has an "exit back" button */
180 #define MENUFLAG_NO_SOUND (1<<2) /**< Menu will not have any select sounds */
183 * @brief Extended menu options.
187 MenuOption_IntroMessage, /**< CONST CHAR *: Valve menus only; defaults to:
188 "You have a menu, hit ESC"
190 MenuOption_IntroColor, /**< INT[4]: Valve menus only; specifies the intro message colour
191 using R,G,B,A (defaults to 255,0,0,255)
193 MenuOption_Priority, /**< INT *: Valve menus only; priority (less is higher) */
197 * @brief Describes the menu a player is viewing.
201 MenuSource_None = 0, /**< No menu is being displayed */
202 MenuSource_External = 1, /**< External menu, no pointer */
203 MenuSource_BaseMenu = 2, /**< An IBaseMenu pointer. */
204 MenuSource_Display = 3, /**< IMenuPanel source, no pointer */
210 * @brief Sets how a raw menu should be drawn.
216 * @brief Returns the parent IMenuStyle pointer.
218 * @return IMenuStyle pointer which created
221 virtual IMenuStyle *GetParentStyle() =0;
224 * @brief Resets/clears the cached display text.
226 virtual void Reset() =0;
229 * @brief Sets how the title should be drawn.
231 * @param text Text string to display for the title.
232 * @param onlyIfEmpty Only sets the title if one does not already
235 virtual void DrawTitle(const char *text, bool onlyIfEmpty=false) =0;
238 * @brief Adds an item to the menu and returns the position (1-10).
240 * Note: Item will fail to draw if there are too many items,
241 * or the item is not drawable (for example, invisible).
243 * @return Item draw position, or 0 on failure.
245 virtual unsigned int DrawItem(const ItemDrawInfo &item) =0;
248 * @brief Draws a raw line of text, if supported. The line does not
249 * need to be newline terminated.
251 * @return True on success, false if not supported.
253 virtual bool DrawRawLine(const char *rawline) =0;
256 * @brief Sets an extended menu option.
258 * @param option Option type.
259 * @param valuePtr Pointer of the type expected by the option.
260 * @return True on success, false if option or value is not supported.
262 virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
265 * @brief Returns whether the display is capable of rendering an item
266 * with the given flags.
268 * @param drawFlags ITEMDRAW flags.
269 * @return True if renderable, false otherwise.
271 virtual bool CanDrawItem(unsigned int drawFlags) =0;
274 * @brief Sends the menu display to a client.
276 * @param client Client index to display to.
277 * @param handler Menu handler to use.
278 * @param time Time to hold menu for.
279 * @return True on success, false otherwise.
281 virtual bool SendDisplay(int client, IMenuHandler *handler, unsigned int time) =0;
284 * @brief Destroys the display object.
286 virtual void DeleteThis() =0;
289 * @brief Sets the selectable key map. Returns false if the function
292 * @param keymap A bit string where each bit N-1 specifies
293 * that key N is selectable (key 0 is bit 9).
294 * If the selectable key map is 0, it will be
295 * automatically set to allow 0.
296 * @return True on success, false if not supported.
298 virtual bool SetSelectableKeys(unsigned int keymap) =0;
301 * @brief Returns the current key position.
303 * @return Current key position starting at 1.
305 virtual unsigned int GetCurrentKey() =0;
308 * @brief Sets the next key position. This cannot be used
309 * to traverse backwards.
311 * @param key Key that is greater or equal to
313 * @return True on success, false otherwise.
315 virtual bool SetCurrentKey(unsigned int key) =0;
318 * @brief Returns the number of characters that can be added to the
319 * menu. The internal buffer is truncated if overflowed; this is for
320 * manual truncation/wrapping purposes.
322 * @return Number of bytes available. If the result is
323 * -1, then the panel has no text limit.
325 virtual int GetAmountRemaining() =0;
328 * @brief For the Handle system, returns approximate memory usage.
330 * @return Approximate number of bytes being used.
332 virtual unsigned int GetApproxMemUsage() =0;
336 * @brief Describes a "MenuStyle" system which manages
337 * menu drawing and construction.
343 * @brief Returns the style API version.
345 * @return API version.
347 virtual unsigned int GetStyleAPIVersion()
349 return SMINTERFACE_MENUMANAGER_VERSION;
353 * @brief Returns the name of the menu style.
355 * @return String containing the style name.
357 virtual const char *GetStyleName() =0;
360 * @brief Creates an IMenuPanel object.
362 * Note: the object should be freed using ::DeleteThis.
364 * @return IMenuPanel object.
366 virtual IMenuPanel *CreatePanel() =0;
369 * @brief Creates an IBaseMenu object of this style.
371 * Note: the object should be freed using IBaseMenu::Destroy.
373 * @param handler IMenuHandler pointer.
374 * @param pOwner Optional IdentityToken_t owner for handle
376 * @return An IBaseMenu pointer.
378 virtual IBaseMenu *CreateMenu(IMenuHandler *handler, IdentityToken_t *pOwner=NULL) =0;
381 * @brief Returns the maximum number of items per page.
383 * Menu implementations must return >= 2. Styles with only 1 or 0
384 * items per page are not valid.
386 * @return Number of items per page.
388 virtual unsigned int GetMaxPageItems() =0;
391 * @brief Returns whether or not a client is viewing a menu.
393 * @param client Client index.
394 * @param object Optional pointer to retrieve menu object,
396 * @return MenuSource value.
398 virtual MenuSource GetClientMenu(int client, void **object) =0;
401 * @brief Cancels a client's menu.
403 * @param client Client index.
404 * @param autoIgnore If true, no menus can be created during
405 * the cancellation process.
406 * @return True if a menu was cancelled, false otherwise.
408 virtual bool CancelClientMenu(int client, bool autoIgnore=false) =0;
411 * @brief Returns a Handle the IMenuStyle object.
413 * @return Handle_t pointing to this object.
415 virtual Handle_t GetHandle() =0;
418 * @brief For the Handle system, returns approximate memory usage.
420 * @return Approximate number of bytes being used.
422 virtual unsigned int GetApproxMemUsage() =0;
426 * @brief High-level interface for building menus.
432 * @brief Appends an item to the end of a menu.
434 * @param info Item information string.
435 * @param draw Default drawing information.
436 * @return True on success, false on item limit reached.
438 virtual bool AppendItem(const char *info, const ItemDrawInfo &draw) =0;
441 * @brief Inserts an item into the menu before a certain position;
442 * the new item will be at the given position and all next items
445 * @param position Position, starting from 0.
446 * @param info Item information string.
447 * @param draw Default item draw info.
448 * @return True on success, false on invalid menu position
450 virtual bool InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw) =0;
453 * @brief Removes an item from the menu.
455 * @param position Position, starting from 0.
456 * @return True on success, false on invalid menu position.
458 virtual bool RemoveItem(unsigned int position) =0;
461 * @brief Removes all items from the menu.
463 virtual void RemoveAllItems() =0;
466 * @brief Returns an item's info.
468 * @param position Position, starting from 0.
469 * @param draw Optional pointer to store a draw information.
470 * @return Info string pointer, or NULL if position was invalid.
472 virtual const char *GetItemInfo(unsigned int position, ItemDrawInfo *draw) =0;
475 * @brief Returns the number of items.
477 * @return Number of items in the menu.
479 virtual unsigned int GetItemCount() =0;
482 * @brief Sets the menu's pagination,.
484 * If pagination is set to MENU_NO_PAGINATION, and the previous
485 * pagination was not MENU_NO_PAGINATION, then the MENUFLAG_BUTTON_EXIT
486 * is unset. It can be re-applied if desired.
488 * @param itemsPerPage Number of items per page, or MENU_NO_PAGINATION.
489 * @return True on success, false if itemsPerPage is too
492 virtual bool SetPagination(unsigned int itemsPerPage) =0;
495 * @brief Returns an item's pagination.
497 * @return Pagination setting.
499 virtual unsigned int GetPagination() =0;
502 * @brief Returns the menu style.
504 * @return Menu style.
506 virtual IMenuStyle *GetDrawStyle() =0;
509 * @brief Sets the menu's display title/message.
511 * @param message Message (format options allowed).
513 virtual void SetDefaultTitle(const char *message) =0;
516 * @brief Returns the menu's display/title message.
518 * @return Message string.
520 virtual const char *GetDefaultTitle() =0;
523 * @brief Sets an extended menu option.
525 * @param option Option type.
526 * @param valuePtr Pointer of the type expected by the option.
527 * @return True on success, false if option or value is not supported.
529 virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
532 * @brief Creates a new IMenuPanel object using extended options specific
533 * to the IMenuStyle parent. Titles, items, etc, are not copied.
535 * Note: The object should be freed with IMenuPanel::DeleteThis.
537 * @return IMenuPanel pointer.
539 virtual IMenuPanel *CreatePanel() =0;
542 * @brief Sends the menu to a client.
544 * @param client Client index to display to.
545 * @param time Time to hold menu for.
546 * @param alt_handler Alternate IMenuHandler.
547 * @return True on success, false otherwise.
549 virtual bool Display(int client, unsigned int time, IMenuHandler *alt_handler=NULL) =0;
552 * @brief Destroys the menu and frees all associated resources.
554 * @param releaseHandle If true, the Handle will be released
555 * in the destructor. This should be set
556 * to true except for IHandleTypeDispatch
559 virtual void Destroy(bool releaseHandle=true) =0;
562 * @brief Cancels the menu on all client's displays. While the menu is
563 * being cancelled, the menu may not be re-displayed to any clients.
564 * If a vote menu is currently active, it will be cancelled as well.
566 * @return Number of menus cancelled.
568 virtual void Cancel() =0;
571 * @brief Returns the menu's Handle. The Handle is automatically
572 * removed when the menu is destroyed.
574 * @return Handle_t handle value.
576 virtual Handle_t GetHandle() =0;
579 * @brief Returns menu option flags.
581 * @return Menu option flags.
583 virtual unsigned int GetMenuOptionFlags() =0;
586 * @brief Sets menu option flags.
588 * @param flags Menu option flags.
590 virtual void SetMenuOptionFlags(unsigned int flags) =0;
593 * @brief Returns the menu's handler.
595 * @return IMenuHandler of the menu.
597 virtual IMenuHandler *GetHandler() =0;
600 * @brief Sends the menu to a client, starting from the given item number.
602 * Note: this API call was added in v13.
604 * @param client Client index to display to.
605 * @param time Time to hold menu for.
606 * @param start_item Starting item to draw.
607 * @param alt_handler Alternate IMenuHandler.
608 * @return True on success, false otherwise.
610 virtual bool DisplayAtItem(int client,
612 unsigned int start_item,
613 IMenuHandler *alt_handler=NULL) =0;
616 * @brief For the Handle system, returns approximate memory usage.
618 * @return Approximate number of bytes being used.
620 virtual unsigned int GetApproxMemUsage() =0;
624 * @brief Contains callbacks for menu actions.
630 * @brief Returns the menu api verison.
632 * @return Menu API version.
634 virtual unsigned int GetMenuAPIVersion2()
636 return SMINTERFACE_MENUMANAGER_VERSION;
640 * @brief A display/selection cycle has started.
642 * @param menu Menu pointer.
644 virtual void OnMenuStart(IBaseMenu *menu)
649 * @brief Called before a menu is being displayed. This is where
650 * you can set an alternate title on the menu.
652 * @param menu Menu pointer.
653 * @param client Client index.
654 * @param display IMenuPanel pointer.
656 virtual void OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
661 * @brief Called when an item is selected.
663 * @param menu Menu pointer.
664 * @param client Client that selected the item.
665 * @param item Item number.
667 virtual void OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
672 * @brief An active menu display was dropped from a client.
674 * @param menu Menu pointer.
675 * @param client Client that had the menu.
676 * @param reason Menu cancellation reason.
678 virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
683 * @brief A display/selection cycle has ended.
685 * @param menu Menu pointer.
686 * @param reason MenuEndReason reason.
688 virtual void OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
693 * @brief Called when the menu object is destroyed.
695 * @param menu Menu pointer.
697 virtual void OnMenuDestroy(IBaseMenu *menu)
702 * @brief Called when requesting how to render an item.
704 * @param menu Menu pointer.
705 * @param client Client index receiving the menu.
706 * @param item Item number in the menu.
707 * @param style ITEMSTYLE flags, by reference for modification.
709 virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
714 * @brief Called when drawing item text.
716 * @param menu Menu pointer.
717 * @param client Client index receiving the menu.
718 * @param panel Panel being used to draw the menu.
719 * @param item Item number in the menu.
720 * @param dr Item draw information.
721 * @return 0 to let the render algorithm decide how to draw, otherwise,
722 * the return value from panel->DrawItem should be returned.
724 virtual unsigned int OnMenuDisplayItem(IBaseMenu *menu,
728 const ItemDrawInfo &dr)
734 * @brief Called when a vote has been started and displayed to
735 * clients. This is called after OnMenuStart() and OnMenuDisplay(),
736 * but before OnMenuSelect().
738 * @param menu Menu pointer.
740 virtual void OnMenuVoteStart(IBaseMenu *menu)
745 * @brief Called when a vote ends. This is automatically called by the
746 * wrapper, and never needs to called from a style implementation.
748 * This function does not replace OnMenuEnd(), nor does it have the
749 * same meaning as OnMenuEnd(), meaning you should not destroy a menu
750 * while it is in this function.
752 * @param menu Menu pointer.
753 * @param results Menu vote results.
755 virtual void OnMenuVoteResults(IBaseMenu *menu, const menu_vote_result_t *results)
760 * @brief Called when a vote is cancelled. If this is called, then
761 * OnMenuVoteResults() will not be called. In both cases, OnMenuEnd will
764 * @param menu Menu pointer.
765 * @param reason VoteCancelReason reason.
767 virtual void OnMenuVoteCancel(IBaseMenu *menu, VoteCancelReason reason)
772 * @brief Call to set private handler stuff.
774 * @param option Option name.
775 * @param data Private data.
776 * @return True if set, false if invalid or unrecognized.
778 virtual bool OnSetHandlerOption(const char *option, const void *data)
784 * @brief Called when an item is selected.
786 * Note: This callback was added in v13. It is called after OnMenuSelect().
788 * @param menu Menu pointer.
789 * @param client Client that selected the item.
790 * @param item Item number.
791 * @param item_on_page The first item on the page the player was last
794 virtual void OnMenuSelect2(IBaseMenu *menu,
797 unsigned int item_on_page)
803 * @brief Manages menu creation and displaying.
805 class IMenuManager : public SMInterface
808 virtual const char *GetInterfaceName()
810 return SMINTERFACE_MENUMANAGER_NAME;
812 virtual unsigned int GetInterfaceVersion()
814 return SMINTERFACE_MENUMANAGER_VERSION;
816 virtual bool IsVersionCompatible(unsigned int version)
818 if (version < 11 || version > GetInterfaceVersion())
826 * @brief Finds a style by name.
828 * @param name Name of the style (case insensitive).
829 * @return IMenuStyle pointer, or NULL if not found.
831 virtual IMenuStyle *FindStyleByName(const char *name) =0;
834 * @brief Returns the default draw style Core is using.
836 * @return Menu style pointer.
838 virtual IMenuStyle *GetDefaultStyle() =0;
841 * @brief Given a set of menu states, converts it to an IMenuPanel object.
843 * The state parameter is both INPUT and OUTPUT.
844 * INPUT: menu, mh, firstItem, lastItem
845 * OUTPUT: display, firstItem, lastItem, slots
847 * @param client Client index.
848 * @param states Menu states.
849 * @param order Order to search for items.
850 * @return IMenuPanel pointer, or NULL if no items could be
851 * found in the IBaseMenu pointer, or NULL if any
852 * other error occurred. Any valid pointer must
853 * be freed using IMenuPanel::DeleteThis.
855 virtual IMenuPanel *RenderMenu(int client, menu_states_t &states, ItemOrder order) =0;
858 * @brief Cancels a menu. Calls IBaseMenu::Cancel() after doing some preparatory
859 * work. This should always be used instead of directly calling Cancel().
861 * @param menu IBaseMenu pointer.
863 virtual void CancelMenu(IBaseMenu *menu) =0;
866 * @brief Displays a menu as a vote.
868 * @param menu IBaseMenu pointer.
869 * @param num_clients Number of clients to display to.
870 * @param clients Client index array.
871 * @param max_time Maximum time to hold menu for.
872 * @param flags Vote flags (currently unused).
873 * @return True on success, false if a vote is in progress.
875 virtual bool StartVote(IBaseMenu *menu,
876 unsigned int num_clients,
878 unsigned int max_time,
879 unsigned int flags=0) =0;
882 * @brief Returns whether or not a vote is in progress.
884 * @return True if a vote is in progress, false otherwise.
886 virtual bool IsVoteInProgress() =0;
889 * @brief Cancels the vote in progress. This calls IBaseMenu::Cancel().
891 virtual void CancelVoting() =0;
894 * @brief Returns the remaining vote delay from the last menu. This delay is
895 * a suggestion for all public votes, and is not enforced.
897 * @return Number of seconds to wait.
899 virtual unsigned int GetRemainingVoteDelay() =0;
902 * @brief Returns whether a client is in the "allowed to vote" pool determined
903 * by the initial call to StartVote().
905 * @param client Client index.
906 * @return True if client is allowed to vote, false on failure.
908 virtual bool IsClientInVotePool(int client) =0;
911 * @brief Redraws the current vote menu to a client in the voting pool.
913 * @param client Client index.
914 * @return True on success, false if client is not allowed to vote.
916 virtual bool RedrawClientVoteMenu(int client) =0;
920 #endif //_INCLUDE_SOURCEMOD_MENU_SYSTEM_H_