From 6f4784e3d119542480b8778bdc34c3e389aa137c Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 6 Jun 2007 06:27:47 +0000 Subject: [PATCH] - vote menus now get cancelled properly (and get a new callback with it) - added api for seeing if a menu is in a VoteDisplay - fixed another item count bug - removed some cruft from the vote handler header --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40890 --- core/MenuManager.cpp | 21 ++++++++++++++++++++- core/MenuManager.h | 3 ++- core/MenuStyle_Base.cpp | 4 ++++ core/MenuStyle_Base.h | 1 + public/IMenuManager.h | 37 +++++++++++++++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/core/MenuManager.cpp b/core/MenuManager.cpp index 6d12fd94..87ca61f6 100644 --- a/core/MenuManager.cpp +++ b/core/MenuManager.cpp @@ -95,6 +95,19 @@ void VoteMenuHandler::DecrementPlayerCount() void VoteMenuHandler::EndVoting() { + if (m_bCancelled) + { + /* If we were cancelled, don't bother tabulating anything. + * Reset just in case someone tries to redraw, which means + * we need to save our states. + */ + IBaseMenu *menu = m_pCurMenu; + InternalReset(); + m_pHandler->OnMenuVoteCancel(menu); + m_pHandler->OnMenuEnd(menu); + return; + } + unsigned int chosen = 0; unsigned int highest = 0; unsigned int dup_count = 0; @@ -104,7 +117,7 @@ void VoteMenuHandler::EndVoting() { /* Pick a random item and then jump far, far away. */ srand((unsigned int)(time(NULL))); - chosen = (unsigned int)rand() % static_cast(m_Votes.size()); + chosen = (unsigned int)rand() % m_Items; goto picked_item; } @@ -207,6 +220,12 @@ void VoteMenuHandler::InternalReset() m_bStarted = false; m_pCurMenu = NULL; m_NumVotes = 0; + m_bCancelled = false; +} + +void VoteMenuHandler::CancelVoting() +{ + m_bCancelled = true; } /******************************* diff --git a/core/MenuManager.h b/core/MenuManager.h index 365718db..8ce6870d 100644 --- a/core/MenuManager.h +++ b/core/MenuManager.h @@ -40,6 +40,7 @@ public: //IVoteMenuHandler bool IsVoteInProgress(); void InitializeVoting(IBaseMenu *menu); void StartVoting(); + void CancelVoting(); public: void Reset(IMenuHandler *mh); private: @@ -51,9 +52,9 @@ private: unsigned int m_Clients; unsigned int m_Items; CVector m_Votes; - CVector m_Dups; IBaseMenu *m_pCurMenu; bool m_bStarted; + bool m_bCancelled; unsigned int m_NumVotes; }; diff --git a/core/MenuStyle_Base.cpp b/core/MenuStyle_Base.cpp index 89a1a1cf..0fef5d7c 100644 --- a/core/MenuStyle_Base.cpp +++ b/core/MenuStyle_Base.cpp @@ -673,3 +673,7 @@ bool CBaseMenu::BroadcastVote(int clients[], return true; } +bool CBaseMenu::IsVoteInProgress() +{ + return (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress()); +} diff --git a/core/MenuStyle_Base.h b/core/MenuStyle_Base.h index f81a87f7..b68d25b2 100644 --- a/core/MenuStyle_Base.h +++ b/core/MenuStyle_Base.h @@ -115,6 +115,7 @@ public: unsigned int numClients, unsigned int maxTime, unsigned int flags=0); + bool IsVoteInProgress(); public: virtual void VoteDisplay(int client, unsigned int maxTime) =0; private: diff --git a/public/IMenuManager.h b/public/IMenuManager.h index 1ac849f6..2e17d641 100644 --- a/public/IMenuManager.h +++ b/public/IMenuManager.h @@ -23,7 +23,7 @@ #include #define SMINTERFACE_MENUMANAGER_NAME "IMenuManager" -#define SMINTERFACE_MENUMANAGER_VERSION 2 +#define SMINTERFACE_MENUMANAGER_VERSION 3 /** * @file IMenuManager.h @@ -486,6 +486,7 @@ namespace SourceMod /** * @brief Cancels the menu on all client's displays. While the menu is * being cancelled, the menu may not be re-displayed to any clients. + * If a vote menu is currently active, it will be cancelled as well. * * @return Number of menus cancelled. */ @@ -515,6 +516,13 @@ namespace SourceMod unsigned int numClients, unsigned int maxTime, unsigned int flags=0) =0; + + /** + * @brief Returns whether a vote menu is active. + * + * @return True if a vote menu is active, false otherwise. + */ + virtual bool IsVoteInProgress() =0; }; /** @@ -631,7 +639,11 @@ namespace SourceMod /** * @brief Called when a vote ends. This is automatically called by the - * wrapper, and never needs to called from a style implementation. + * wrapper, and never needs to called from a style implementation. + * + * This function does not replace OnMenuEnd(), nor does it have the + * same meaning as OnMenuEnd(), meaning you should not destroy a menu + * while it is in this function. * * @param menu Menu pointer. * @param item Item position that was chosen by a majority. @@ -639,6 +651,17 @@ namespace SourceMod virtual void OnMenuVoteEnd(IBaseMenu *menu, unsigned int item) { } + + /** + * @brief Called when a vote is cancelled. If this is called, then + * OnMenuVoteEnd() will not be called. In both cases, OnMenuEnd will + * always be called. + * + * @param menu Menu pointer. + */ + virtual void OnMenuVoteCancel(IBaseMenu *menu) + { + } }; /** @@ -666,6 +689,16 @@ namespace SourceMod * processed (i.e., there are no more clients to display to). */ virtual void StartVoting() =0; + + /** + * @brief Notifies the vote handler that the voting should be + * cancelled. + * + * Cancellation is not immediate and will only occur once every menu + * has been cancelled from clients. Thus this should only be called + * from the beginning of IBaseMenu::Cancel. + */ + virtual void CancelVoting() =0; }; /**