added new menu feature for old plugins

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401711
This commit is contained in:
David Anderson 2007-11-20 17:16:50 +00:00
parent 81f6856ad9
commit 09b6c5ead1
4 changed files with 126 additions and 6 deletions

View File

@ -206,7 +206,9 @@ CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
if (m_FreeDisplays.empty()) if (m_FreeDisplays.empty())
{ {
display = new CRadioDisplay(); display = new CRadioDisplay();
} else { }
else
{
display = m_FreeDisplays.front(); display = m_FreeDisplays.front();
m_FreeDisplays.pop(); m_FreeDisplays.pop();
display->Reset(); display->Reset();
@ -214,6 +216,15 @@ CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
return display; return display;
} }
IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
{
CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
pPanel->DirectSet(str, keys);
return pPanel;
}
void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display) void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
{ {
m_FreeDisplays.push(display); m_FreeDisplays.push(display);
@ -275,6 +286,13 @@ void CRadioDisplay::Reset()
keys = 0; keys = 0;
} }
void CRadioDisplay::DirectSet(const char *str, int keymap)
{
m_Title.clear();
m_BufferText.assign(str);
keys = keymap;
}
unsigned int CRadioDisplay::GetCurrentKey() unsigned int CRadioDisplay::GetCurrentKey()
{ {
return m_NextPos; return m_NextPos;
@ -390,12 +408,22 @@ bool CRadioMenuPlayer::Radio_NeedsRefresh()
} }
void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text) void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
{
if (title[0] != '\0')
{ {
display_len = UTIL_Format(display_pkt, display_len = UTIL_Format(display_pkt,
sizeof(display_pkt), sizeof(display_pkt),
"%s\n%s", "%s\n%s",
title, title,
text); text);
}
else
{
display_len = UTIL_Format(display_pkt,
sizeof(display_pkt),
"%s",
text);
}
display_keys = keys; display_keys = keys;
} }

View File

@ -92,6 +92,7 @@ public:
CRadioDisplay *MakeRadioDisplay(CRadioMenu *menu=NULL); CRadioDisplay *MakeRadioDisplay(CRadioMenu *menu=NULL);
void FreeRadioDisplay(CRadioDisplay *display); void FreeRadioDisplay(CRadioDisplay *display);
CRadioMenuPlayer *GetRadioMenuPlayer(int client); CRadioMenuPlayer *GetRadioMenuPlayer(int client);
IMenuPanel *MakeRadioDisplay(const char *str, int keys);
private: private:
CRadioMenuPlayer *m_players; CRadioMenuPlayer *m_players;
CStack<CRadioDisplay *> m_FreeDisplays; CStack<CRadioDisplay *> m_FreeDisplays;
@ -118,6 +119,8 @@ public: //IMenuPanel
unsigned int GetCurrentKey(); unsigned int GetCurrentKey();
bool SetCurrentKey(unsigned int key); bool SetCurrentKey(unsigned int key);
int GetAmountRemaining(); int GetAmountRemaining();
public:
void DirectSet(const char *str, int keymap);
private: private:
String m_BufferText; String m_BufferText;
String m_Title; String m_Title;

View File

@ -37,6 +37,7 @@
#include "MenuStyle_Radio.h" #include "MenuStyle_Radio.h"
#include "HandleSys.h" #include "HandleSys.h"
#include "PluginSys.h" #include "PluginSys.h"
#include "PlayerManager.h"
#include "sm_stringutil.h" #include "sm_stringutil.h"
#include "sourcemm_api.h" #include "sourcemm_api.h"
#if defined MENU_DEBUG #if defined MENU_DEBUG
@ -1398,6 +1399,73 @@ static cell_t GetMenuSelectionPosition(IPluginContext *pContext, const cell_t *p
return *s_CurSelectPosition; return *s_CurSelectPosition;
} }
class EmptyMenuHandler : public IMenuHandler
{
public:
} s_EmptyMenuHandler;
static cell_t InternalShowMenu(IPluginContext *pContext, const cell_t *params)
{
int client = params[1];
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
if (pPlayer == NULL)
{
return pContext->ThrowNativeError("Invalid client index %d", client);
}
else if (!pPlayer->IsInGame())
{
return pContext->ThrowNativeError("Client %d is not in game", client);
}
if (!g_RadioMenuStyle.IsSupported())
{
return pContext->ThrowNativeError("Radio menus are not supported on this mod");
}
char *str;
pContext->LocalToString(params[2], &str);
IMenuPanel *pPanel = g_RadioMenuStyle.MakeRadioDisplay(str, params[4]);
if (pPanel == NULL)
{
return 0;
}
IMenuHandler *pHandler;
CPanelHandler *pActualHandler = NULL;
if (params[5] != -1)
{
IPluginFunction *pFunction = pContext->GetFunctionById(params[5]);
if (pFunction == NULL)
{
return pContext->ThrowNativeError("Invalid function index %x", params[5]);
}
pActualHandler = g_MenuHelpers.GetPanelHandler(pFunction);
}
if (pActualHandler == NULL)
{
pHandler = &s_EmptyMenuHandler;
}
else
{
pHandler = pActualHandler;
}
bool bSuccess = pPanel->SendDisplay(client, pHandler, params[3]);
pPanel->DeleteThis();
if (!bSuccess && pActualHandler != NULL)
{
g_MenuHelpers.FreePanelHandler(pActualHandler);
}
return bSuccess ? 1 : 0;
}
REGISTER_NATIVES(menuNatives) REGISTER_NATIVES(menuNatives)
{ {
{"AddMenuItem", AddMenuItem}, {"AddMenuItem", AddMenuItem},
@ -1430,6 +1498,7 @@ REGISTER_NATIVES(menuNatives)
{"GetPanelCurrentKey", GetPanelCurrentKey}, {"GetPanelCurrentKey", GetPanelCurrentKey},
{"GetPanelStyle", GetPanelStyle}, {"GetPanelStyle", GetPanelStyle},
{"InsertMenuItem", InsertMenuItem}, {"InsertMenuItem", InsertMenuItem},
{"InternalShowMenu", InternalShowMenu},
{"IsVoteInProgress", IsVoteInProgress}, {"IsVoteInProgress", IsVoteInProgress},
{"RedrawMenuItem", RedrawMenuItem}, {"RedrawMenuItem", RedrawMenuItem},
{"RemoveAllMenuItems", RemoveAllMenuItems}, {"RemoveAllMenuItems", RemoveAllMenuItems},

View File

@ -732,6 +732,26 @@ native bool:SetPanelCurrentKey(Handle:panel, key);
*/ */
native RedrawMenuItem(const String:text[]); native RedrawMenuItem(const String:text[]);
/**
* This function is provided for legacy code only. Some older plugins may use
* network messages instead of the panel API. This function wraps the panel
* API for eased portability into the SourceMod menu system.
*
* This function is only usable with the Radio Menu style. You do not need to
* split up your menu into multiple packets; SourceMod will break the string
* up internally.
*
* @param client Client index.
* @param str Full menu string as would be passed over the network.
* @param time Time to hold the menu for.
* @param keys Selectable key bitstring.
* @param handler Optional handler function, with the same rules as
* SendPanelToClient().
* @return True on success, false on failure.
* @error Invalid client index, or radio menus not supported.
*/
native bool:InternalShowMenu(client, const String:str[], time, keys=-1, MenuHandler:handler=MenuHandler:-1);
/** /**
* Retrieves voting information from MenuAction_VoteEnd. * Retrieves voting information from MenuAction_VoteEnd.
* *