extensions/clientprefs/menus.cpp
author Matt Woodrow <pred@alliedmods.net>
Tue Jul 15 00:24:08 2008 +0000 (2008-07-15)
changeset 2207 3a97a56ded62
parent 2048 2454ff4d6afb
child 3536 fddd9e8bdef5
permissions -rw-r--r--
Fixed amb1810 - Clientprefs no longer blocks load.
     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 *)strtol(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, NULL);
    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 *)strtol(info, NULL, 16);
    98 
    99 	if (data->handler->forward != NULL)
   100 	{
   101 		char buffer[100];
   102 		UTIL_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 	ItemDrawInfo draw;
   122 
   123 	const char *info = menu->GetItemInfo(item, &draw);
   124 
   125 	AutoMenuData *data = (AutoMenuData *)strtol(info, NULL, 16);
   126 
   127 	switch (data->type)
   128 	{
   129 	case CookieMenu_YesNo:
   130 		{
   131 			if (item == 0)
   132 			{
   133 				g_CookieManager.SetCookieValue(data->pCookie, client, "yes");
   134 			}
   135 			else
   136 			{
   137 				g_CookieManager.SetCookieValue(data->pCookie, client, "no");
   138 			}
   139 			break;
   140 		}
   141 	case CookieMenu_YesNo_Int:
   142 	case CookieMenu_OnOff_Int:
   143 		{
   144 			if (item == 0)
   145 			{
   146 				g_CookieManager.SetCookieValue(data->pCookie, client, "1");
   147 			}
   148 			else
   149 			{
   150 				g_CookieManager.SetCookieValue(data->pCookie, client, "0");
   151 			}
   152 			break;
   153 		}
   154 	case CookieMenu_OnOff:
   155 		{
   156 			if (item == 0)
   157 			{
   158 				g_CookieManager.SetCookieValue(data->pCookie, client, "on");
   159 			}
   160 			else
   161 			{
   162 				g_CookieManager.SetCookieValue(data->pCookie, client, "off");
   163 			}
   164 			break;
   165 		}
   166 	}
   167 
   168 	char message[255];
   169 	char *value;
   170 	g_CookieManager.GetCookieValue(data->pCookie, client, &value);
   171 	Translate(message, sizeof(message), "[SM] %T", 4, NULL, "Cookie Changed Value", &client, &(data->pCookie->name), value);
   172 
   173 	gamehelpers->TextMsg(client, 3, message);
   174 }
   175 
   176 void AutoMenuHandler::OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
   177 {
   178 	menu->Destroy(true);
   179 }