extensions/topmenus/TopMenu.h
author Fyren <fyrenmoo@gmail.com>
Wed Jan 14 17:00:24 2009 -0500 (2009-01-14)
changeset 2530 204824176286
parent 2510 3b2349600557
child 4078 483b617ef4f2
permissions -rw-r--r--
Fixed admin-sql-threaded not working on L4D (bug 3559, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod TopMenus 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 #ifndef _INCLUDE_SOURCEMOD_TOP_MENU_H_
    33 #define _INCLUDE_SOURCEMOD_TOP_MENU_H_
    34 
    35 #include <sh_list.h>
    36 #include <sh_vector.h>
    37 #include <sm_trie_tpl.h>
    38 #include <ITopMenus.h>
    39 #include "smsdk_ext.h"
    40 #include "sm_memtable.h"
    41 
    42 using namespace SourceHook;
    43 using namespace SourceMod;
    44 
    45 #define ABSOLUTE_PLAYER_LIMIT 255  // not 256, so we can send the limit as a byte 
    46 
    47 struct config_category_t
    48 {
    49 	int name;
    50 	CVector<int> commands;
    51 };
    52 
    53 struct config_root_t
    54 {
    55 	config_root_t() : strings(1024)
    56 	{
    57 	}
    58 	BaseStringTable strings;
    59 	CVector<config_category_t *> cats;
    60 };
    61 
    62 struct topmenu_object_t
    63 {
    64 	char name[64];						/** Name */
    65 	char cmdname[64];					/** Command name */ 
    66 	FlagBits flags;						/** Admin flags */
    67 	ITopMenuObjectCallbacks *callbacks;	/** Callbacks */
    68 	IdentityToken_t *owner;				/** Owner */
    69 	unsigned int object_id;				/** Object ID */
    70 	topmenu_object_t *parent;			/** Parent, if any */
    71 	TopMenuObjectType type;				/** Object Type */
    72 	bool is_free;						/** Free or not? */
    73 	char info[255];						/** Info string */
    74 	unsigned int cat_id;				/** Set if a category */
    75 };
    76 
    77 struct topmenu_category_t
    78 {
    79 	CVector<topmenu_object_t *> obj_list;	/** Full object list */
    80 	CVector<topmenu_object_t *> sorted;		/** Sorted items */
    81 	CVector<topmenu_object_t *> unsorted;	/** Unsorted items */
    82 	topmenu_object_t *obj;					/** Bound object */
    83 	unsigned int serial;					/** Serial number */
    84 	bool reorder;							/** Whether ordering needs updating */
    85 };
    86 
    87 struct topmenu_player_category_t
    88 {
    89 	IBaseMenu *menu;					/** menu pointer */
    90 	unsigned int serial;				/** last known serial */
    91 };
    92 
    93 struct topmenu_player_t
    94 {
    95 	int user_id;						/** userid on server */
    96 	unsigned int menu_serial;			/** menu serial no */
    97 	IBaseMenu *root;					/** root menu display */
    98 	topmenu_player_category_t *cats;	/** category display */
    99 	unsigned int cat_count;				/** number of categories */
   100 	unsigned int last_category;			/** last category they selected */
   101 	unsigned int last_position;			/** last position in that category */
   102 	unsigned int last_root_pos;			/** last page in the root menu */
   103 };
   104 
   105 class TopMenu : 
   106 	public ITopMenu,
   107 	public IMenuHandler,
   108 	public ITextListener_SMC
   109 {
   110 	friend class TopMenuManager;
   111 public:
   112 	TopMenu(ITopMenuObjectCallbacks *callbacks);
   113 	~TopMenu();
   114 public: //ITopMenu
   115 	virtual unsigned int AddToMenu(const char *name,
   116 		TopMenuObjectType type,
   117 		ITopMenuObjectCallbacks *callbacks,
   118 		IdentityToken_t *owner,
   119 		const char *cmdname,
   120 		FlagBits flags,
   121 		unsigned int parent);
   122 	unsigned int AddToMenu2(const char *name,
   123 		TopMenuObjectType type,
   124 		ITopMenuObjectCallbacks *callbacks,
   125 		IdentityToken_t *owner,
   126 		const char *cmdname,
   127 		FlagBits flags,
   128 		unsigned int parent,
   129 		const char *info_string);
   130 	virtual void RemoveFromMenu(unsigned int object_id);
   131 	virtual bool DisplayMenu(int client, 
   132 		unsigned int hold_time, 
   133 		TopMenuPosition position);
   134 	virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength);
   135 	virtual unsigned int FindCategory(const char *name);
   136 	const char *GetObjectInfoString(unsigned int object_id);
   137 	const char *GetObjectName(unsigned int object_id);
   138 public: //IMenuHandler
   139 	virtual void OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page);
   140 	virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style);
   141 	virtual unsigned int OnMenuDisplayItem(IBaseMenu *menu, 
   142 		int client, 
   143 		IMenuPanel *panel,
   144 		unsigned int item, 
   145 		const ItemDrawInfo &dr);
   146 	virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason);
   147 public: //ITextListener_SMC
   148 	void ReadSMC_ParseStart();
   149 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   150 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   151 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   152 public:
   153 	unsigned int CalcMemUsage();
   154 private:
   155 	void SortCategoriesIfNeeded();
   156 	void SortCategoryIfNeeded(unsigned int category);
   157 private:
   158 	bool DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position);
   159 	void CreatePlayers(int max_clients);
   160 	void UpdateClientRoot(int client, IGamePlayer *pGamePlayer=NULL);
   161 	void UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck=false);
   162 	void TearDownClient(topmenu_player_t *player);
   163 private:
   164 	void OnClientConnected(int client);
   165 	void OnClientDisconnected(int client);
   166 	void OnServerActivated(int max_clients);
   167 	bool OnIdentityRemoval(IdentityToken_t *owner);
   168 	void OnMaxPlayersChanged(int newvalue);
   169 private:
   170 	config_root_t m_Config;					/* Configuration from file */
   171 	topmenu_player_t *m_clients;			/* Client array */
   172 	CVector<unsigned int> m_SortedCats;		/* Sorted categories */
   173 	CVector<unsigned int> m_UnsortedCats;	/* Un-sorted categories */
   174 	CVector<topmenu_category_t *> m_Categories; /* Category array */
   175 	CVector<topmenu_object_t *> m_Objects;	/* Object array */
   176 	KTrie<topmenu_object_t *> m_ObjLookup;	/* Object lookup trie */
   177 	unsigned int m_SerialNo;				/* Serial number for updating */
   178 	ITopMenuObjectCallbacks *m_pTitle;		/* Title callbacks */
   179 	int m_max_clients;						/* Maximum number of clients */
   180 	bool m_bCatsNeedResort;					/* True if categories need a resort */
   181 };
   182 
   183 unsigned int strncopy(char *dest, const char *src, size_t count);
   184 
   185 #endif //_INCLUDE_SOURCEMOD_TOP_MENU_H_