Add ability to disable TopMenu Title Caching (bug 6034, r=dvander).

This commit is contained in:
Peace-Maker 2014-05-09 12:30:52 -07:00
parent 5149205109
commit 530078b3ae
4 changed files with 73 additions and 10 deletions

View File

@ -49,6 +49,7 @@ TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks)
m_SerialNo = 1; m_SerialNo = 1;
m_pTitle = callbacks; m_pTitle = callbacks;
m_max_clients = 0; m_max_clients = 0;
m_bCacheTitles = true;
if (playerhelpers->IsServerActivated()) if (playerhelpers->IsServerActivated())
{ {
@ -421,6 +422,13 @@ bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition po
return false; return false;
} }
if (!m_bCacheTitles)
{
char renderbuf[128];
m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
pClient->root->SetDefaultTitle(renderbuf);
}
bool return_value = false; bool return_value = false;
if (position == TopMenuPosition_LastCategory && if (position == TopMenuPosition_LastCategory &&
@ -460,6 +468,18 @@ bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int ho
topmenu_player_category_t *player_cat = &(pClient->cats[category]); topmenu_player_category_t *player_cat = &(pClient->cats[category]);
// Refresh the title if the topmenu wants that.
if (!m_bCacheTitles)
{
char renderbuf[128];
m_Categories[category]->obj->callbacks->OnTopMenuDisplayTitle(this,
client,
m_Categories[category]->obj->object_id,
renderbuf,
sizeof(renderbuf));
player_cat->menu->SetDefaultTitle(renderbuf);
}
pClient->last_category = category; pClient->last_category = category;
if (last_position) if (last_position)
{ {
@ -473,6 +493,11 @@ bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int ho
return return_value; return return_value;
} }
void TopMenu::SetTitleCaching(bool cache_titles)
{
m_bCacheTitles = cache_titles;
}
void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page) void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
{ {
const char *item_name = menu->GetItemInfo(item, NULL); const char *item_name = menu->GetItemInfo(item, NULL);
@ -691,9 +716,12 @@ void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer)
} }
/* Set the menu's title */ /* Set the menu's title */
char renderbuf[128]; if (m_bCacheTitles)
m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf)); {
root_menu->SetDefaultTitle(renderbuf); char renderbuf[128];
m_pTitle->OnTopMenuDisplayTitle(this, client, 0, renderbuf, sizeof(renderbuf));
root_menu->SetDefaultTitle(renderbuf);
}
/* The client is now fully updated */ /* The client is now fully updated */
pClient->root = root_menu; pClient->root = root_menu;
@ -810,13 +838,16 @@ void TopMenu::UpdateClientCategory(int client, unsigned int category, bool bSkip
} }
/* Set the menu's title */ /* Set the menu's title */
char renderbuf[128]; if (m_bCacheTitles)
cat->obj->callbacks->OnTopMenuDisplayTitle(this, {
client, char renderbuf[128];
cat->obj->object_id, cat->obj->callbacks->OnTopMenuDisplayTitle(this,
renderbuf, client,
sizeof(renderbuf)); cat->obj->object_id,
cat_menu->SetDefaultTitle(renderbuf); renderbuf,
sizeof(renderbuf));
cat_menu->SetDefaultTitle(renderbuf);
}
/* We're done! */ /* We're done! */
player_cat->menu = cat_menu; player_cat->menu = cat_menu;

View File

@ -154,6 +154,7 @@ public: //ITextListener_SMC
SMCResult ReadSMC_LeavingSection(const SMCStates *states); SMCResult ReadSMC_LeavingSection(const SMCStates *states);
public: public:
unsigned int CalcMemUsage(); unsigned int CalcMemUsage();
void SetTitleCaching(bool cache_titles);
private: private:
void SortCategoriesIfNeeded(); void SortCategoriesIfNeeded();
void SortCategoryIfNeeded(unsigned int category); void SortCategoryIfNeeded(unsigned int category);
@ -181,6 +182,7 @@ private:
ITopMenuObjectCallbacks *m_pTitle; /* Title callbacks */ ITopMenuObjectCallbacks *m_pTitle; /* Title callbacks */
int m_max_clients; /* Maximum number of clients */ int m_max_clients; /* Maximum number of clients */
bool m_bCatsNeedResort; /* True if categories need a resort */ bool m_bCatsNeedResort; /* True if categories need a resort */
bool m_bCacheTitles; /* True if the categorie titles should be cached */
}; };
unsigned int strncopy(char *dest, const char *src, size_t count); unsigned int strncopy(char *dest, const char *src, size_t count);

View File

@ -374,6 +374,23 @@ static cell_t GetTopMenuName(IPluginContext *pContext, const cell_t *params)
return strncopy(buffer, str, params[4]); return strncopy(buffer, str, params[4]);
} }
static cell_t SetTopMenuTitleCaching(IPluginContext *pContext, const cell_t *params)
{
HandleError err;
TopMenu *pMenu;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
if ((err = handlesys->ReadHandle(params[1], hTopMenuType, &sec, (void **)&pMenu))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
bool cache_titles = params[2]==1?true:false;
pMenu->SetTitleCaching(cache_titles);
return 0;
}
sp_nativeinfo_t g_TopMenuNatives[] = sp_nativeinfo_t g_TopMenuNatives[] =
{ {
{"AddToTopMenu", AddToTopMenu}, {"AddToTopMenu", AddToTopMenu},
@ -384,5 +401,6 @@ sp_nativeinfo_t g_TopMenuNatives[] =
{"FindTopMenuCategory", FindTopMenuCategory}, {"FindTopMenuCategory", FindTopMenuCategory},
{"GetTopMenuInfoString", GetTopMenuInfoString}, {"GetTopMenuInfoString", GetTopMenuInfoString},
{"GetTopMenuObjName", GetTopMenuName}, {"GetTopMenuObjName", GetTopMenuName},
{"SetTopMenuTitleCaching", SetTopMenuTitleCaching},
{NULL, NULL}, {NULL, NULL},
}; };

View File

@ -258,6 +258,17 @@ native bool:DisplayTopMenu(Handle:topmenu, client, TopMenuPosition:position);
*/ */
native TopMenuObject:FindTopMenuCategory(Handle:topmenu, const String:name[]); native TopMenuObject:FindTopMenuCategory(Handle:topmenu, const String:name[]);
/**
* Change the menu title caching behaviour of the TopMenu. By default the titles are cached to reduce overhead.
* If you need dynamic menu titles, which can change everytime the menu is displayed to a user, set this to false.
*
* @param topmenu TopMenu Handle.
* @param cache_titles Cache the menu titles and don't call the handler with TopMenuAction_DisplayTitle everytime the menu is drawn?
* @noreturn
* @error Invalid TopMenu Handle
*/
native SetTopMenuTitleCaching(Handle:topmenu, bool:cache_titles);
/** /**
* Do not edit below this line! * Do not edit below this line!
*/ */
@ -286,5 +297,6 @@ public __ext_topmenus_SetNTVOptional()
MarkNativeAsOptional("RemoveFromTopMenu"); MarkNativeAsOptional("RemoveFromTopMenu");
MarkNativeAsOptional("DisplayTopMenu"); MarkNativeAsOptional("DisplayTopMenu");
MarkNativeAsOptional("FindTopMenuCategory"); MarkNativeAsOptional("FindTopMenuCategory");
MarkNativeAsOptional("SetTopMenuTitleCaching");
} }
#endif #endif