core/smn_menus.cpp
changeset 1163 0bb03cb6277b
parent 1011 b413072540fd
child 1231 17badaf66ce4
     1.1 --- a/core/smn_menus.cpp	Tue Jul 24 22:20:58 2007 +0000
     1.2 +++ b/core/smn_menus.cpp	Wed Jul 25 01:18:11 2007 +0000
     1.3 @@ -45,6 +45,7 @@
     1.4  	MenuAction_VoteStart = (1<<6), 	/**< (VOTE ONLY): A vote sequence has started */
     1.5  	MenuAction_VoteCancel = (1<<7),	/**< (VOTE ONLY): A vote sequence has been cancelled (nothing passed) */
     1.6  	MenuAction_DrawItem = (1<<8),	/**< A style is being drawn; return the new style (param1=client, param2=item) */
     1.7 +	MenuAction_DisplayItem = (1<<9),	/**< the odd duck */
     1.8  };
     1.9  
    1.10  class CPanelHandler : public IMenuHandler
    1.11 @@ -80,6 +81,7 @@
    1.12  		unsigned int totalVotes);
    1.13  	void OnMenuVoteCancel(IBaseMenu *menu);
    1.14  	void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style);
    1.15 +	unsigned int OnMenuDisplayItem(IBaseMenu *menu, int client, IMenuPanel *panel, unsigned int item, const ItemDrawInfo &dr);
    1.16  #if 0
    1.17  	void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style);
    1.18  	void OnMenuDisplayItem(IBaseMenu *menu, int client, unsigned int item, const char **display);
    1.19 @@ -104,11 +106,13 @@
    1.20  	virtual void OnSourceModAllInitialized()
    1.21  	{
    1.22  		m_PanelType = g_HandleSys.CreateType("IMenuPanel", this, 0, NULL, NULL, g_pCoreIdent, NULL);
    1.23 +		m_TempPanelType = g_HandleSys.CreateType("TempIMenuPanel", this, m_PanelType, NULL, NULL, g_pCoreIdent, NULL);
    1.24  		g_PluginSys.AddPluginsListener(this);
    1.25  	}
    1.26  
    1.27  	virtual void OnSourceModShutdown()
    1.28  	{
    1.29 +		g_HandleSys.RemoveType(m_TempPanelType, g_pCoreIdent);
    1.30  		g_HandleSys.RemoveType(m_PanelType, g_pCoreIdent);
    1.31  
    1.32  		while (!m_FreePanelHandlers.empty())
    1.33 @@ -126,6 +130,11 @@
    1.34  
    1.35  	virtual void OnHandleDestroy(HandleType_t type, void *object)
    1.36  	{
    1.37 +		if (type == m_TempPanelType)
    1.38 +		{
    1.39 +			return;
    1.40 +		}
    1.41 +
    1.42  		IMenuPanel *panel = (IMenuPanel *)object;
    1.43  		panel->DeleteThis();
    1.44  	}
    1.45 @@ -155,6 +164,11 @@
    1.46  		return m_PanelType;
    1.47  	}
    1.48  
    1.49 +	inline HandleType_t GetTempPanelType()
    1.50 +	{
    1.51 +		return m_TempPanelType;
    1.52 +	}
    1.53 +
    1.54  	CPanelHandler *GetPanelHandler(IPluginFunction *pFunction)
    1.55  	{
    1.56  		CPanelHandler *handler;
    1.57 @@ -200,6 +214,7 @@
    1.58  
    1.59  private:
    1.60  	HandleType_t m_PanelType;
    1.61 +	HandleType_t m_TempPanelType;
    1.62  	CStack<CPanelHandler *> m_FreePanelHandlers;
    1.63  	CStack<CMenuHandler *> m_FreeMenuHandlers;
    1.64  	CVector<CPanelHandler *> m_PanelHandlers;
    1.65 @@ -235,6 +250,10 @@
    1.66  	g_MenuHelpers.FreePanelHandler(this);
    1.67  }
    1.68  
    1.69 +static IMenuPanel *s_pCurPanel = NULL;
    1.70 +static unsigned int s_CurPanelReturn = 0;
    1.71 +static const ItemDrawInfo *s_CurDrawInfo = NULL;
    1.72 +
    1.73  /**
    1.74   * MENU HANDLER WRAPPER
    1.75   */
    1.76 @@ -257,14 +276,14 @@
    1.77  	if ((m_Flags & (int)MenuAction_Display) == (int)MenuAction_Display)
    1.78  	{
    1.79  		HandleSecurity sec;
    1.80 -		sec.pIdentity = m_pBasic->GetParentContext()->GetIdentity();
    1.81 -		sec.pOwner = g_pCoreIdent;
    1.82 +		sec.pIdentity = g_pCoreIdent;
    1.83 +		sec.pOwner = m_pBasic->GetParentContext()->GetIdentity();
    1.84  	
    1.85  		HandleAccess access;
    1.86  		g_HandleSys.InitAccessDefaults(NULL, &access);
    1.87  		access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
    1.88  
    1.89 -		Handle_t hndl =  g_HandleSys.CreateHandleEx(g_MenuHelpers.GetPanelType(), panel, &sec, &access, NULL);
    1.90 +		Handle_t hndl = g_HandleSys.CreateHandleEx(g_MenuHelpers.GetTempPanelType(), panel, &sec, &access, NULL);
    1.91  
    1.92  		DoAction(menu, MenuAction_Display, client, hndl);
    1.93  
    1.94 @@ -319,6 +338,38 @@
    1.95  	}
    1.96  }
    1.97  
    1.98 +unsigned int CMenuHandler::OnMenuDisplayItem(IBaseMenu *menu,
    1.99 +									 int client,
   1.100 +									 IMenuPanel *panel,
   1.101 +									 unsigned int item,
   1.102 +									 const ItemDrawInfo &dr)
   1.103 +{
   1.104 +	if ((m_Flags & (int)MenuAction_DisplayItem) == (int)MenuAction_DisplayItem)
   1.105 +	{
   1.106 +		IMenuPanel *oldpanel = s_pCurPanel;
   1.107 +		unsigned int oldret = s_CurPanelReturn;
   1.108 +		const ItemDrawInfo *oldinfo = s_CurDrawInfo;
   1.109 +		s_pCurPanel = panel;
   1.110 +		s_CurPanelReturn = 0;
   1.111 +		s_CurDrawInfo = &dr;
   1.112 +
   1.113 +		cell_t res = DoAction(menu, MenuAction_DisplayItem, client, item, 0);
   1.114 +
   1.115 +		if (!res)
   1.116 +		{
   1.117 +			res = s_CurPanelReturn;
   1.118 +		}
   1.119 +
   1.120 +		s_pCurPanel = oldpanel;
   1.121 +		s_CurPanelReturn = oldret;
   1.122 +		s_CurDrawInfo = oldinfo;
   1.123 +
   1.124 +		return res;
   1.125 +	}
   1.126 +
   1.127 +	return 0;
   1.128 +}
   1.129 +
   1.130  cell_t CMenuHandler::DoAction(IBaseMenu *menu, MenuAction action, cell_t param1, cell_t param2, cell_t def_res)
   1.131  {
   1.132  #if defined MENU_DEBUG
   1.133 @@ -1002,6 +1053,27 @@
   1.134  	return panel->SetCurrentKey(params[2]) ? 1 : 0;
   1.135  }
   1.136  
   1.137 +static cell_t RedrawMenuItem(IPluginContext *pContext, const cell_t *params)
   1.138 +{
   1.139 +	if (!s_pCurPanel)
   1.140 +	{
   1.141 +		return pContext->ThrowNativeError("You can only call this once from a MenuAction_DisplayItem callback");
   1.142 +	}
   1.143 +
   1.144 +	char *str;
   1.145 +	pContext->LocalToString(params[1], &str);
   1.146 +
   1.147 +	ItemDrawInfo dr = *s_CurDrawInfo;
   1.148 +	dr.display = str;
   1.149 +
   1.150 +	if ((s_CurPanelReturn = s_pCurPanel->DrawItem(dr)) != 0)
   1.151 +	{
   1.152 +		s_pCurPanel = NULL;
   1.153 +	}
   1.154 +
   1.155 +	return s_CurPanelReturn;
   1.156 +}
   1.157 +
   1.158  REGISTER_NATIVES(menuNatives)
   1.159  {
   1.160  	{"AddMenuItem",				AddMenuItem},
   1.161 @@ -1028,6 +1100,7 @@
   1.162  	{"GetPanelStyle",			GetPanelStyle},
   1.163  	{"InsertMenuItem",			InsertMenuItem},
   1.164  	{"IsVoteInProgress",		IsVoteInProgress},
   1.165 +	{"RedrawMenuItem",			RedrawMenuItem},
   1.166  	{"RemoveAllMenuItems",		RemoveAllMenuItems},
   1.167  	{"RemoveMenuItem",			RemoveMenuItem},
   1.168  	{"SendPanelToClient",		SendPanelToClient},