extensions/clientprefs/menus.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 3804 90159516acd3
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Client Preferences Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "menus.h"
    33 
    34 ClientMenuHandler g_Handler;
    35 AutoMenuHandler g_AutoHandler;
    36 
    37 void ClientMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
    38 {
    39 	ItemDrawInfo draw;
    40 
    41 	const char *info = menu->GetItemInfo(item, &draw);
    42 
    43 	AutoMenuData *data = (AutoMenuData *)strtoul(info, NULL, 16);
    44 
    45 	if (data->handler->forward != NULL)
    46 	{
    47 		data->handler->forward->PushCell(client);
    48 		data->handler->forward->PushCell(CookieMenuAction_SelectOption);
    49 		data->handler->forward->PushCell(data->datavalue);
    50 		data->handler->forward->PushString("");
    51 		data->handler->forward->PushCell(0);
    52 		data->handler->forward->Execute(NULL);
    53 	}
    54 
    55 	if (!data->handler->isAutoMenu)
    56 	{
    57 		return;
    58 	}
    59 
    60 	IBaseMenu *submenu = menus->GetDefaultStyle()->CreateMenu(&g_AutoHandler, g_ClientPrefs.GetIdentity());
    61 
    62 	char message[256];
    63 
    64 	Translate(message, sizeof(message), "%T:", 2, NULL, "Choose Option", &client);
    65 	submenu->SetDefaultTitle(message);
    66 
    67 	if (data->type == CookieMenu_YesNo || data->type == CookieMenu_YesNo_Int)
    68 	{
    69 		Translate(message, sizeof(message), "%T", 2, NULL, "Yes", &client);
    70 		submenu->AppendItem(info, message);
    71 
    72 		Translate(message, sizeof(message), "%T", 2, NULL, "No", &client);
    73 		submenu->AppendItem(info, message);
    74 	}
    75 	else if (data->type == CookieMenu_OnOff || data->type == CookieMenu_OnOff_Int)
    76 	{
    77 		Translate(message, sizeof(message), "%T", 2, NULL, "On", &client);
    78 		submenu->AppendItem(info, message);
    79 
    80 		Translate(message, sizeof(message), "%T", 2, NULL, "Off", &client);
    81 		submenu->AppendItem(info, message);
    82 	}
    83 
    84 	submenu->Display(client, 0, NULL);
    85 }
    86 
    87 unsigned int ClientMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, 
    88 										   int client, 
    89 										   IMenuPanel *panel,
    90 										   unsigned int item, 
    91 										   const ItemDrawInfo &dr)
    92 {
    93 	ItemDrawInfo draw;
    94 
    95 	const char *info = menu->GetItemInfo(item, &draw);
    96 
    97 	AutoMenuData *data = (AutoMenuData *)strtoul(info, NULL, 16);
    98 
    99 	if (data->handler->forward != NULL)
   100 	{
   101 		char buffer[100];
   102 		g_pSM->Format(buffer, sizeof(buffer), "%s", dr.display);
   103 
   104 		data->handler->forward->PushCell(client);
   105 		data->handler->forward->PushCell(CookieMenuAction_DisplayOption);
   106 		data->handler->forward->PushCell(data->datavalue);
   107 		data->handler->forward->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   108 		data->handler->forward->PushCell(sizeof(buffer));
   109 		data->handler->forward->Execute(NULL);
   110 
   111 		ItemDrawInfo newdraw(buffer, draw.style);
   112 
   113 		return panel->DrawItem(newdraw);
   114 	}
   115 
   116 	return 0;
   117 }
   118 
   119 void AutoMenuHandler::OnMenuSelect(SourceMod::IBaseMenu *menu, int client, unsigned int item)
   120 {
   121 	static const char settings[CookieMenu_Elements][2][4] = { {"yes", "no"}, {"1", "0"}, {"on", "off"}, {"1", "0"} };
   122 	ItemDrawInfo draw;
   123 
   124 	const char *info = menu->GetItemInfo(item, &draw);
   125 
   126 	AutoMenuData *data = (AutoMenuData *)strtoul(info, NULL, 16);
   127 
   128 	g_CookieManager.SetCookieValue(data->pCookie, client, settings[data->type][item]);
   129 	
   130 	char message[255];
   131 	char *value;
   132 	g_CookieManager.GetCookieValue(data->pCookie, client, &value);
   133 	Translate(message, sizeof(message), "[SM] %T", 4, NULL, "Cookie Changed Value", &client, &(data->pCookie->name), value);
   134 
   135 	gamehelpers->TextMsg(client, 3, message);
   136 }
   137 
   138 void AutoMenuHandler::OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
   139 {
   140 	HandleSecurity sec = HandleSecurity(g_ClientPrefs.GetIdentity(), g_ClientPrefs.GetIdentity());
   141 	HandleError err = handlesys->FreeHandle(menu->GetHandle(), &sec);
   142 	if (HandleError_None != err)
   143 	{
   144 		g_pSM->LogError(myself, "Error %d when attempting to free automenu handle", err);
   145 	}
   146 }