Add DisplayTopMenuCategory native (bug 6033, r=dvander).
authorPeace-Maker <januk@freenet.de>
Fri May 09 12:32:49 2014 -0700 (2014-05-09)
changeset 43515ccbfd632820
parent 4350 9f00539117ce
child 4352 da4a56294886
Add DisplayTopMenuCategory native (bug 6033, r=dvander).
extensions/topmenus/TopMenu.cpp
extensions/topmenus/TopMenu.h
extensions/topmenus/smn_topmenus.cpp
plugins/include/topmenus.inc
     1.1 --- a/extensions/topmenus/TopMenu.cpp	Fri May 09 12:30:52 2014 -0700
     1.2 +++ b/extensions/topmenus/TopMenu.cpp	Fri May 09 12:32:49 2014 -0700
     1.3 @@ -186,28 +186,14 @@
     1.4  	topmenu_category_t *parent_cat = NULL;
     1.5  	if (type == TopMenuObject_Item)
     1.6  	{
     1.7 -		/* Check parent index.  Note it will be >= 1 here. */
     1.8 -		if (parent > m_Objects.size() || m_Objects[parent - 1]->is_free)
     1.9 +		size_t category_id;
    1.10 +		if (!FindCategoryByObject(parent, &category_id))
    1.11  		{
    1.12  			return 0;
    1.13  		}
    1.14 +
    1.15  		parent_obj = m_Objects[parent - 1];
    1.16 -
    1.17 -		/* Find an equivalent pointer in the category array. */
    1.18 -		for (size_t i = 0; i < m_Categories.size(); i++)
    1.19 -		{
    1.20 -			if (m_Categories[i]->obj == parent_obj)
    1.21 -			{
    1.22 -				parent_cat = m_Categories[i];
    1.23 -				break;
    1.24 -			}
    1.25 -		}
    1.26 -
    1.27 -		/* If none was found, leave. */
    1.28 -		if (parent_cat == NULL)
    1.29 -		{
    1.30 -			return 0;
    1.31 -		}
    1.32 +		parent_cat = m_Categories[category_id];
    1.33  	}
    1.34  
    1.35  	/* Re-use an old object pointer if we can. */
    1.36 @@ -454,6 +440,82 @@
    1.37  	return return_value;
    1.38  }
    1.39  
    1.40 +bool TopMenu::DisplayMenuAtCategory(int client, unsigned int object_id)
    1.41 +{
    1.42 +	if (m_clients == NULL)
    1.43 +	{
    1.44 +		return false;
    1.45 +	}
    1.46 +
    1.47 +	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
    1.48 +	if (!pPlayer->IsInGame())
    1.49 +	{
    1.50 +		return false;
    1.51 +	}
    1.52 +
    1.53 +	// Get the category this object is in.
    1.54 +	size_t category_id;
    1.55 +	if (!FindCategoryByObject(object_id, &category_id))
    1.56 +	{
    1.57 +		return false;
    1.58 +	}
    1.59 +
    1.60 +	topmenu_category_t *category = m_Categories[category_id];
    1.61 +
    1.62 +	UpdateClientRoot(client, pPlayer);
    1.63 +
    1.64 +	topmenu_player_t *pClient = &m_clients[client];
    1.65 +	if (pClient->root == NULL)
    1.66 +	{
    1.67 +		return false;
    1.68 +	}
    1.69 +
    1.70 +	if (!m_bCacheTitles)
    1.71 +	{
    1.72 +		char renderbuf[128];
    1.73 +		m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
    1.74 +		pClient->root->SetDefaultTitle(renderbuf);
    1.75 +	}
    1.76 +
    1.77 +	bool return_value = false;
    1.78 +
    1.79 +	return_value = DisplayCategory(client, category_id, 0, true);
    1.80 +	if (!return_value)
    1.81 +	{
    1.82 +		return_value = pClient->root->DisplayAtItem(client, 0, pClient->last_root_pos);
    1.83 +	}
    1.84 +
    1.85 +	return return_value;
    1.86 +}
    1.87 +
    1.88 +bool TopMenu::FindCategoryByObject(unsigned int obj_id, size_t *index)
    1.89 +{
    1.90 +	if (obj_id == 0 
    1.91 +		|| obj_id > m_Objects.size() 
    1.92 +		|| m_Objects[obj_id - 1]->is_free)
    1.93 +	{
    1.94 +		return false;
    1.95 +	}
    1.96 +
    1.97 +	topmenu_object_t *obj = m_Objects[obj_id - 1];
    1.98 +	if (obj->type != TopMenuObject_Category)
    1.99 +	{
   1.100 +		return false;
   1.101 +	}
   1.102 +
   1.103 +	/* Find an equivalent pointer in the category array. */
   1.104 +	for (size_t i = 0; i < m_Categories.size(); i++)
   1.105 +	{
   1.106 +		if (m_Categories[i]->obj == obj)
   1.107 +		{
   1.108 +			*index = i;
   1.109 +			return true;
   1.110 +		}
   1.111 +	}
   1.112 +
   1.113 +	return false;
   1.114 +}
   1.115 +
   1.116  bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position)
   1.117  {
   1.118  	UpdateClientCategory(client, category);
     2.1 --- a/extensions/topmenus/TopMenu.h	Fri May 09 12:30:52 2014 -0700
     2.2 +++ b/extensions/topmenus/TopMenu.h	Fri May 09 12:32:49 2014 -0700
     2.3 @@ -155,6 +155,7 @@
     2.4  public:
     2.5  	unsigned int CalcMemUsage();
     2.6  	void SetTitleCaching(bool cache_titles);
     2.7 +	bool DisplayMenuAtCategory(int client, unsigned int object_id);
     2.8  private:
     2.9  	void SortCategoriesIfNeeded();
    2.10  	void SortCategoryIfNeeded(unsigned int category);
    2.11 @@ -164,6 +165,7 @@
    2.12  	void UpdateClientRoot(int client, IGamePlayer *pGamePlayer=NULL);
    2.13  	void UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck=false);
    2.14  	void TearDownClient(topmenu_player_t *player);
    2.15 +	bool FindCategoryByObject(unsigned int obj_id, size_t *index);
    2.16  private:
    2.17  	void OnClientConnected(int client);
    2.18  	void OnClientDisconnected(int client);
     3.1 --- a/extensions/topmenus/smn_topmenus.cpp	Fri May 09 12:30:52 2014 -0700
     3.2 +++ b/extensions/topmenus/smn_topmenus.cpp	Fri May 09 12:32:49 2014 -0700
     3.3 @@ -326,6 +326,32 @@
     3.4  	return pMenu->DisplayMenu(client, 0, (TopMenuPosition)params[3]);
     3.5  }
     3.6  
     3.7 +static cell_t DisplayTopMenuCategory(IPluginContext *pContext, const cell_t *params)
     3.8 +{
     3.9 +	HandleError err;
    3.10 +	TopMenu *pMenu;
    3.11 +	HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
    3.12 +
    3.13 +	if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu))
    3.14 +		!= HandleError_None)
    3.15 +	{
    3.16 +		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
    3.17 +	}
    3.18 +
    3.19 +	int client = params[3];
    3.20 +	IGamePlayer *player = playerhelpers->GetGamePlayer(client);
    3.21 +	if (!player)
    3.22 +	{
    3.23 +		return pContext->ThrowNativeError("Invalid client index %d", client);
    3.24 +	}
    3.25 +	else if (!player->IsInGame())
    3.26 +	{
    3.27 +		return pContext->ThrowNativeError("Client %d is not in game", client);
    3.28 +	}
    3.29 +
    3.30 +	return pMenu->DisplayMenuAtCategory(client, params[2]);
    3.31 +}
    3.32 +
    3.33  static cell_t GetTopMenuInfoString(IPluginContext *pContext, const cell_t *params)
    3.34  {
    3.35  	HandleError err;
    3.36 @@ -396,6 +422,7 @@
    3.37  	{"AddToTopMenu",			AddToTopMenu},
    3.38  	{"CreateTopMenu",			CreateTopMenu},
    3.39  	{"DisplayTopMenu",			DisplayTopMenu},
    3.40 +	{"DisplayTopMenuCategory",	DisplayTopMenuCategory},
    3.41  	{"LoadTopMenuConfig",		LoadTopMenuConfig},
    3.42  	{"RemoveFromTopMenu",		RemoveFromTopMenu},
    3.43  	{"FindTopMenuCategory",		FindTopMenuCategory},
     4.1 --- a/plugins/include/topmenus.inc	Fri May 09 12:30:52 2014 -0700
     4.2 +++ b/plugins/include/topmenus.inc	Fri May 09 12:32:49 2014 -0700
     4.3 @@ -248,6 +248,17 @@
     4.4  native bool:DisplayTopMenu(Handle:topmenu, client, TopMenuPosition:position);
     4.5  
     4.6  /**
     4.7 + * Displays a TopMenu category to a client.
     4.8 + *
     4.9 + * @param topmenu			TopMenu Handle.
    4.10 + * @param category		Category object id.
    4.11 + * @param client			Client index.
    4.12 + * @return					True on success, false on failure.
    4.13 + * @error					Invalid TopMenu Handle or client not in game.
    4.14 + */
    4.15 +native bool:DisplayTopMenuCategory(Handle:topmenu, TopMenuObject:category, client);
    4.16 +
    4.17 +/**
    4.18   * Finds a category's object ID in a TopMenu.
    4.19   *
    4.20   * @param topmenu			TopMenu Handle.
    4.21 @@ -296,6 +307,7 @@
    4.22  	MarkNativeAsOptional("AddToTopMenu");
    4.23  	MarkNativeAsOptional("RemoveFromTopMenu");
    4.24  	MarkNativeAsOptional("DisplayTopMenu");
    4.25 +	MarkNativeAsOptional("DisplayTopMenuCategory");
    4.26  	MarkNativeAsOptional("FindTopMenuCategory");
    4.27  	MarkNativeAsOptional("SetTopMenuTitleCaching");
    4.28  }