3 * ===============================================================
4 * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC.
6 * ===============================================================
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.
14 * To view the latest information, see: http://www.sourcemod.net/license.php
19 #ifndef _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
20 #define _INCLUDE_SOURCEMOD_MENU_SYSTEM_H_
22 #include <IShareSys.h>
23 #include <IHandleSys.h>
25 #define SMINTERFACE_MENUMANAGER_NAME "IMenuManager"
26 #define SMINTERFACE_MENUMANAGER_VERSION 6
29 * @file IMenuManager.h
30 * @brief Abstracts on-screen menus for clients.
36 * @brief Used to determine how an item selection is interpreted.
40 ItemSel_None, /**< Invalid selection */
41 ItemSel_Back, /**< Go back one page (really "Previous") */
42 ItemSel_Next, /**< Go forward one page */
43 ItemSel_Exit, /**< Menu was exited */
44 ItemSel_Item, /**< Valid item selection */
45 ItemSel_ExitBack, /**< Sends MenuEnd_ExitBack */
49 * @brief Used to determine which order to search for items in.
53 ItemOrder_Ascending, /**< Items should be drawn ascendingly */
54 ItemOrder_Descending, /**< Items should be drawn descendingly */
58 * @brief Pairs an item type with an item menu position.
62 ItemSelection type; /**< Item selection type */
63 unsigned int item; /**< Item position, if applicable */
71 * @brief Describes menu display information.
75 unsigned int apiVers; /**< Must be filled with the API version */
76 IBaseMenu *menu; /**< Menu pointer, or NULL if there is only a display */
77 IMenuHandler *mh; /**< Menu callbacks handler */
78 unsigned int firstItem; /**< MENU ONLY: First item displayed on the last page */
79 unsigned int lastItem; /**< MENU ONLY: Last item displayed on the last page */
80 menu_slots_t slots[11]; /**< MENU ONLY: Item selection table (first index is 1) */
83 #define ITEMDRAW_DEFAULT (0) /**< Item should be drawn normally */
84 #define ITEMDRAW_DISABLED (1<<0) /**< Item is drawn but not selectable */
85 #define ITEMDRAW_RAWLINE (1<<1) /**< Item should be a raw line, without a slot */
86 #define ITEMDRAW_NOTEXT (1<<2) /**< No text should be drawn */
87 #define ITEMDRAW_SPACER (1<<3) /**< Item should be drawn as a spacer, if possible */
88 #define ITEMDRAW_IGNORE ((1<<1)|(1<<2)) /**< Item should be completely ignored (rawline + notext) */
89 #define ITEMDRAW_CONTROL (1<<4) /**< Item is control text (back/next/exit) */
92 * @brief Information about item drawing.
101 ItemDrawInfo(const char *DISPLAY, unsigned int STYLE=ITEMDRAW_DEFAULT)
102 : display(DISPLAY), style(STYLE)
105 const char *display; /**< Display text (NULL for none) */
106 unsigned int style; /**< ITEMDRAW style flags */
110 * @brief Reasons for a menu dying.
112 enum MenuCancelReason
114 MenuCancel_Disconnected = -1, /**< Client dropped from the server */
115 MenuCancel_Interrupted = -2, /**< Client was interrupted with another menu */
116 MenuCancel_Exit = -3, /**< Client selected "exit" on a paginated menu */
117 MenuCancel_NoDisplay = -4, /**< Menu could not be displayed to the client */
118 MenuCancel_Timeout = -5, /**< Menu timed out */
119 MenuCancel_ExitBack = -6, /**< Client selected "exit back" on a paginated menu */
123 * @brief Reasons a menu ended.
127 MenuEnd_Selected = 0, /**< Menu item was selected */
128 MenuEnd_VotingDone = -1, /**< Voting finished */
129 MenuEnd_VotingCancelled = -2, /**< Voting was cancelled */
130 MenuEnd_Cancelled = -3, /**< Menu was uncleanly cancelled */
131 MenuEnd_Exit = -4, /**< Menu was cleanly exited via "exit" */
132 MenuEnd_ExitBack = -5, /**< Menu was cleanly exited via "back" */
136 #define MENU_NO_PAGINATION 0 /**< Menu should not be paginated (10 items max) */
137 #define MENU_TIME_FOREVER 0 /**< Menu should be displayed as long as possible */
140 * @brief Extended menu options.
144 MenuOption_IntroMessage, /**< CONST CHAR *: Valve menus only; defaults to:
145 "You have a menu, hit ESC"
147 MenuOption_IntroColor, /**< INT[4]: Valve menus only; specifies the intro message colour
148 using R,G,B,A (defaults to 255,0,0,255)
150 MenuOption_Priority, /**< INT *: Valve menus only; priority (less is higher) */
154 * @brief Describes the menu a player is viewing.
158 MenuSource_None = 0, /**< No menu is being displayed */
159 MenuSource_External = 1, /**< External menu, no pointer */
160 MenuSource_BaseMenu = 2, /**< An IBaseMenu pointer. */
161 MenuSource_Display = 3, /**< IMenuPanel source, no pointer */
167 * @brief Sets how a raw menu should be drawn.
173 * @brief Returns the parent IMenuStyle pointer.
175 * @return IMenuStyle pointer which created
178 virtual IMenuStyle *GetParentStyle() =0;
181 * @brief Resets/clears the cached display text.
183 virtual void Reset() =0;
186 * @brief Sets how the title should be drawn.
188 * @param text Text string to display for the title.
189 * @param onlyIfEmpty Only sets the title if one does not already
192 virtual void DrawTitle(const char *text, bool onlyIfEmpty=false) =0;
195 * @brief Adds an item to the menu and returns the position (1-10).
197 * Note: Item will fail to draw if there are too many items,
198 * or the item is not drawable (for example, invisible).
200 * @return Item draw position, or 0 on failure.
202 virtual unsigned int DrawItem(const ItemDrawInfo &item) =0;
205 * @brief Draws a raw line of text, if supported. The line does not
206 * need to be newline terminated.
208 * @return True on success, false if not supported.
210 virtual bool DrawRawLine(const char *rawline) =0;
213 * @brief Sets an extended menu option.
215 * @param option Option type.
216 * @param valuePtr Pointer of the type expected by the option.
217 * @return True on success, false if option or value is not supported.
219 virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
222 * @brief Returns whether the display is capable of rendering an item
223 * with the given flags.
225 * @param flags ITEMDRAW flags.
226 * @return True if renderable, false otherwise.
228 virtual bool CanDrawItem(unsigned int drawFlags) =0;
231 * @brief Sends the menu display to a client.
233 * @param client Client index to display to.
234 * @param handler Menu handler to use.
235 * @param time Time to hold menu for.
236 * @return True on success, false otherwise.
238 virtual bool SendDisplay(int client, IMenuHandler *handler, unsigned int time) =0;
241 * @brief Destroys the display object.
243 virtual void DeleteThis() =0;
246 * @brief Sets the selectable key map. Returns false if the function
249 * @param keys A bit string where each bit N-1 specifies
250 * that key N is selectable (key 0 is bit 9).
251 * If the selectable key map is 0, it will be
252 * automatically set to allow 0.
253 * @return True on success, false if not supported.
255 virtual bool SetSelectableKeys(unsigned int keymap) =0;
258 * @brief Returns the current key position.
260 * @return Current key position starting at 1.
262 virtual unsigned int GetCurrentKey() =0;
265 * @brief Sets the next key position. This cannot be used
266 * to traverse backwards.
268 * @param key Key that is greater or equal to
270 * @return True on success, false otherwise.
272 virtual bool SetCurrentKey(unsigned int key) =0;
276 * @brief Describes a "MenuStyle" system which manages
277 * menu drawing and construction.
283 * @brief Returns the style API version.
285 * @return API version.
287 virtual unsigned int GetStyleAPIVersion()
289 return SMINTERFACE_MENUMANAGER_VERSION;
293 * @brief Returns the name of the menu style.
295 * @return String containing the style name.
297 virtual const char *GetStyleName() =0;
300 * @brief Creates an IMenuPanel object.
302 * Note: the object should be freed using ::DeleteThis.
304 * @return IMenuPanel object.
306 virtual IMenuPanel *CreatePanel() =0;
309 * @brief Creates an IBaseMenu object of this style.
311 * Note: the object should be freed using IBaseMenu::Destroy.
313 * @param handler IMenuHandler pointer.
314 * @param pOwner Optional IdentityToken_t owner for handle
316 * @return An IBaseMenu pointer.
318 virtual IBaseMenu *CreateMenu(IMenuHandler *handler, IdentityToken_t *pOwner=NULL) =0;
321 * @brief Returns the maximum number of items per page.
323 * @return Number of items per page.
325 virtual unsigned int GetMaxPageItems() =0;
328 * @brief Returns whether or not a client is viewing a menu.
330 * @param client Client index.
331 * @param object Optional pointer to retrieve menu object,
333 * @return MenuSource value.
335 virtual MenuSource GetClientMenu(int client, void **object) =0;
338 * @brief Cancels a client's menu.
340 * @param client Client index.
341 * @param autoIgnore If true, no menus can be created during
342 * the cancellation process.
343 * @return True if a menu was cancelled, false otherwise.
345 virtual bool CancelClientMenu(int client, bool autoIgnore=false) =0;
348 * @brief Returns a Handle the IMenuStyle object.
350 * @return Handle_t pointing to this object.
352 virtual Handle_t GetHandle() =0;
356 * @brief High-level interface for building menus.
362 * @brief Appends an item to the end of a menu.
364 * @param info Item information string.
365 * @param draw Default drawing information.
366 * @return True on success, false on item limit reached.
368 virtual bool AppendItem(const char *info, const ItemDrawInfo &draw) =0;
371 * @brief Inserts an item into the menu before a certain position;
372 * the new item will be at the given position and all next items
375 * @param position Position, starting from 0.
376 * @param info Item information string.
377 * @param draw Default item draw info.
378 * @return True on success, false on invalid menu position
380 virtual bool InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw) =0;
383 * @brief Removes an item from the menu.
385 * @param position Position, starting from 0.
386 * @return True on success, false on invalid menu position.
388 virtual bool RemoveItem(unsigned int position) =0;
391 * @brief Removes all items from the menu.
393 virtual void RemoveAllItems() =0;
396 * @brief Returns an item's info.
398 * @param position Position, starting from 0.
399 * @param draw Optional pointer to store a draw information.
400 * @return Info string pointer, or NULL if position was invalid.
402 virtual const char *GetItemInfo(unsigned int position, ItemDrawInfo *draw) =0;
405 * @brief Returns the number of items.
407 * @return Number of items in the menu.
409 virtual unsigned int GetItemCount() =0;
412 * @brief Sets the menu's pagination,.
414 * @param itemsPerPage Number of items per page, or MENU_NO_PAGINATION.
415 * @return True on success, false if itemsPerPage is too large.
417 virtual bool SetPagination(unsigned int itemsPerPage) =0;
420 * @brief Returns an item's pagination.
422 * @return Pagination setting.
424 virtual unsigned int GetPagination() =0;
427 * @brief Returns the menu style.
429 * @return Menu style.
431 virtual IMenuStyle *GetDrawStyle() =0;
434 * @brief Sets the menu's display title/message.
436 * @param message Message (format options allowed).
438 virtual void SetDefaultTitle(const char *message) =0;
441 * @brief Returns the menu's display/title message.
443 * @return Message string.
445 virtual const char *GetDefaultTitle() =0;
448 * @brief Sets an extended menu option.
450 * @param option Option type.
451 * @param valuePtr Pointer of the type expected by the option.
452 * @return True on success, false if option or value is not supported.
454 virtual bool SetExtOption(MenuOption option, const void *valuePtr) =0;
457 * @brief Creates a new IMenuPanel object using extended options specific
458 * to the IMenuStyle parent. Titles, items, etc, are not copied.
460 * Note: The object should be freed with IMenuPanel::DeleteThis.
462 * @return IMenuPanel pointer.
464 virtual IMenuPanel *CreatePanel() =0;
467 * @brief Returns whether or not the menu should have an "Exit" button for
470 * @return True to have an exit button, false otherwise.
472 virtual bool GetExitButton() =0;
475 * @brief Sets whether or not the menu should have an "Exit" button for
478 * @param set True to enable, false to disable the exit button.
479 * @return True on success, false if the exit button is
482 virtual bool SetExitButton(bool set) =0;
485 * @brief Sends the menu to a client.
487 * @param client Client index to display to.
488 * @param time Time to hold menu for.
489 * @return True on success, false otherwise.
491 virtual bool Display(int client, unsigned int time) =0;
494 * @brief Destroys the menu and frees all associated resources.
496 * @param releaseHandle If true, the Handle will be released
497 * in the destructor. This should be set
498 * to true except for IHandleTypeDispatch
501 virtual void Destroy(bool releaseHandle=true) =0;
504 * @brief Cancels the menu on all client's displays. While the menu is
505 * being cancelled, the menu may not be re-displayed to any clients.
506 * If a vote menu is currently active, it will be cancelled as well.
508 * @return Number of menus cancelled.
510 virtual void Cancel() =0;
513 * @brief Returns the menu's Handle. The Handle is automatically
514 * removed when the menu is destroyed.
516 * @return Handle_t handle value.
518 virtual Handle_t GetHandle() =0;
521 * @brief Sends a menu to multiple clients as a vote menu. All callbacks
522 * will be sent as normal, except two extras, OnMenuVoteStart and
523 * OnMenuVoteEnd, will be called.
525 * @param clients Array of client indexes.
526 * @param numClients Number of client indexes in the array.
527 * @param maxTime Maximum amount of time to hold the vote.
528 * @param flags Optional voting flags (currently unused).
529 * @return True on success, false if a vote is already in
530 * progress (the menu must be cancelled first).
532 virtual bool BroadcastVote(int clients[],
533 unsigned int numClients,
534 unsigned int maxTime,
535 unsigned int flags=0) =0;
538 * @brief Returns whether a vote menu is active.
540 * @return True if a vote menu is active, false otherwise.
542 virtual bool IsVoteInProgress() =0;
545 * @brief Returns whether to draw a "Back" button on the first page.
546 * ExitBack buttons are disabled by default.
548 * @return True if enabled, false otherwise.
550 virtual bool GetExitBackButton() =0;
553 * @brief Sets whether to draw a "Back" button on the first page.
554 * ExitBack buttons are disabled by default.
556 * @param set True to enable, false to disable.
558 virtual void SetExitBackButton(bool set) =0;
562 * @brief Contains callbacks for menu actions.
568 * @brief Returns the menu api verison.
570 * @return Menu API version.
572 virtual unsigned int GetMenuAPIVersion2()
574 return SMINTERFACE_MENUMANAGER_VERSION;
578 * @brief A display/selection cycle has started.
580 * @param menu Menu pointer.
582 virtual void OnMenuStart(IBaseMenu *menu)
587 * @brief Called before a menu is being displayed. This is where
588 * you can set an alternate title on the menu.
590 * @param menu Menu pointer.
591 * @param client Client index.
592 * @param display IMenuPanel pointer.
594 virtual void OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
599 * @brief Called when an item is selected.
601 * @param menu Menu pointer.
602 * @param client Client that selected the item.
603 * @param item Item number.
605 virtual void OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
610 * @brief An active menu display was dropped from a client.
612 * @param menu Menu pointer.
613 * @param client Client that had the menu.
614 * @param reason Menu cancellation reason.
616 virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
621 * @brief A display/selection cycle has ended.
623 * @param menu Menu pointer.
625 virtual void OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
630 * @brief Called when the menu object is destroyed.
632 * @param menu Menu pointer.
634 virtual void OnMenuDestroy(IBaseMenu *menu)
639 * @brief Called when requesting how to render an item.
641 * @param menu Menu pointer.
642 * @param client Client index receiving the menu.
643 * @param item Item number in the menu.
644 * @param style ITEMSTYLE flags, by reference for modification.
646 virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
651 * @brief Called when requesting how to draw an item's text.
653 * @param menu Menu pointer.
654 * @param client Client index receiving the menu.
655 * @param item Item number in the menu.
656 * @param display Pointer to the display text string (changeable).
658 virtual void OnMenuDisplayItem(IBaseMenu *menu, int client, unsigned int item, const char **display)
663 * @brief Called when a vote has been started and displayed to
664 * clients. This is called after OnMenuStart() and OnMenuDisplay(),
665 * but before OnMenuSelect().
667 * @param menu Menu pointer.
669 virtual void OnMenuVoteStart(IBaseMenu *menu)
674 * @brief Called when a vote ends. This is automatically called by the
675 * wrapper, and never needs to called from a style implementation.
677 * This function does not replace OnMenuEnd(), nor does it have the
678 * same meaning as OnMenuEnd(), meaning you should not destroy a menu
679 * while it is in this function.
681 * @param menu Menu pointer.
682 * @param item Item position that was chosen by a majority.
683 * @param winningVotes Number of votes from the winning item.
684 * @param totalVotes Number of votes total.
686 virtual void OnMenuVoteEnd(IBaseMenu *menu,
688 unsigned int winningVotes,
689 unsigned int totalVotes)
694 * @brief Called when a vote is cancelled. If this is called, then
695 * OnMenuVoteEnd() will not be called. In both cases, OnMenuEnd will
698 * @param menu Menu pointer.
700 virtual void OnMenuVoteCancel(IBaseMenu *menu)
706 * @brief Contains functions for managing a vote handler.
708 class IVoteMenuHandler : public IMenuHandler
712 * @brief Returns whether or not a vote is in progress.
714 * @return True if a vote is in progress, false otherwise.
716 virtual bool IsVoteInProgress() =0;
719 * @brief Use this to mark the vote as in progress (start).
721 * @param menu Menu pointer.
723 virtual void InitializeVoting(IBaseMenu *menu) =0;
726 * @brief Use this to notify that all clients' displays have been
727 * processed (i.e., there are no more clients to display to).
729 virtual void StartVoting() =0;
732 * @brief Notifies the vote handler that the voting should be
735 * Cancellation is not immediate and will only occur once every menu
736 * has been cancelled from clients. Thus this should only be called
737 * from the beginning of IBaseMenu::Cancel.
739 virtual void CancelVoting() =0;
743 * @brief Manages menu creation and displaying.
745 class IMenuManager : public SMInterface
748 virtual const char *GetInterfaceName()
750 return SMINTERFACE_MENUMANAGER_NAME;
752 virtual unsigned int GetInterfaceVersion()
754 return SMINTERFACE_MENUMANAGER_VERSION;
756 virtual bool IsVersionCompatible(unsigned int version)
758 if (version < 5 || version > GetInterfaceVersion())
766 * @brief Finds a style by name.
768 * @param name Name of the style (case insensitive).
769 * @return IMenuStyle pointer, or NULL if not found.
771 virtual IMenuStyle *FindStyleByName(const char *name) =0;
774 * @brief Returns the default draw style Core is using.
776 * @return Menu style pointer.
778 virtual IMenuStyle *GetDefaultStyle() =0;
781 * @brief Given a set of menu states, converts it to an IDisplay object.
783 * The state parameter is both INPUT and OUTPUT.
784 * INPUT: menu, mh, firstItem, lastItem
785 * OUTPUT: display, firstItem, lastItem, slots
787 * @param client Client index.
788 * @param states Menu states.
789 * @return IDisplay pointer, or NULL if no items could be
790 * found in the IBaseMenu pointer, or NULL if any
791 * other error occurred. Any valid pointer must
792 * be freed using IMenuPanel::DeleteThis.
794 virtual IMenuPanel *RenderMenu(int client, menu_states_t &states, ItemOrder order) =0;
797 * @brief Creates a standard voting wrapper. The wrapper is not
798 * re-entrant; a second menu cannot be displayed on the same handler
801 * @param mh Menu handler to wrap around.
802 * @return An IMenuHandler pointer that is a wrapper
803 * around IMenuHandler callbacks to invoke
804 * voting related callbacks.
806 virtual IVoteMenuHandler *CreateVoteWrapper(IMenuHandler *mh) =0;
809 * @brief Frees a standard voting wrapper.
811 * @param mh Menu handler pointer created by
812 * CreateVoteWrapper(). NULL values will be
815 virtual void ReleaseVoteWrapper(IVoteMenuHandler *mh) =0;
819 #endif //_INCLUDE_SOURCEMOD_MENU_SYSTEM_H_