extensions/topmenus/TopMenu.h
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4351 5ccbfd632820
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     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 <ITopMenus.h>
    38 #include "smsdk_ext.h"
    39 #include "sm_memtable.h"
    40 #include <sm_namehashset.h>
    41 
    42 using namespace SourceHook;
    43 using namespace SourceMod;
    44 
    45 struct config_category_t
    46 {
    47 	int name;
    48 	CVector<int> commands;
    49 };
    50 
    51 struct config_root_t
    52 {
    53 	config_root_t() : strings(1024)
    54 	{
    55 	}
    56 	BaseStringTable strings;
    57 	CVector<config_category_t *> cats;
    58 };
    59 
    60 struct topmenu_object_t
    61 {
    62 	char name[64];						/** Name */
    63 	char cmdname[64];					/** Command name */ 
    64 	FlagBits flags;						/** Admin flags */
    65 	ITopMenuObjectCallbacks *callbacks;	/** Callbacks */
    66 	IdentityToken_t *owner;				/** Owner */
    67 	unsigned int object_id;				/** Object ID */
    68 	topmenu_object_t *parent;			/** Parent, if any */
    69 	TopMenuObjectType type;				/** Object Type */
    70 	bool is_free;						/** Free or not? */
    71 	char info[255];						/** Info string */
    72 	unsigned int cat_id;				/** Set if a category */
    73 
    74 	static inline bool matches(const char *name, const topmenu_object_t *topmenu)
    75 	{
    76 		return strcmp(name, topmenu->name) == 0;
    77 	}
    78 };
    79 
    80 struct topmenu_category_t
    81 {
    82 	CVector<topmenu_object_t *> obj_list;	/** Full object list */
    83 	CVector<topmenu_object_t *> sorted;		/** Sorted items */
    84 	CVector<topmenu_object_t *> unsorted;	/** Unsorted items */
    85 	topmenu_object_t *obj;					/** Bound object */
    86 	unsigned int serial;					/** Serial number */
    87 	bool reorder;							/** Whether ordering needs updating */
    88 };
    89 
    90 struct topmenu_player_category_t
    91 {
    92 	IBaseMenu *menu;					/** menu pointer */
    93 	unsigned int serial;				/** last known serial */
    94 };
    95 
    96 struct topmenu_player_t
    97 {
    98 	int user_id;						/** userid on server */
    99 	unsigned int menu_serial;			/** menu serial no */
   100 	IBaseMenu *root;					/** root menu display */
   101 	topmenu_player_category_t *cats;	/** category display */
   102 	unsigned int cat_count;				/** number of categories */
   103 	unsigned int last_category;			/** last category they selected */
   104 	unsigned int last_position;			/** last position in that category */
   105 	unsigned int last_root_pos;			/** last page in the root menu */
   106 };
   107 
   108 class TopMenu : 
   109 	public ITopMenu,
   110 	public IMenuHandler,
   111 	public ITextListener_SMC
   112 {
   113 	friend class TopMenuManager;
   114 public:
   115 	TopMenu(ITopMenuObjectCallbacks *callbacks);
   116 	~TopMenu();
   117 public: //ITopMenu
   118 	virtual unsigned int AddToMenu(const char *name,
   119 		TopMenuObjectType type,
   120 		ITopMenuObjectCallbacks *callbacks,
   121 		IdentityToken_t *owner,
   122 		const char *cmdname,
   123 		FlagBits flags,
   124 		unsigned int parent);
   125 	unsigned int AddToMenu2(const char *name,
   126 		TopMenuObjectType type,
   127 		ITopMenuObjectCallbacks *callbacks,
   128 		IdentityToken_t *owner,
   129 		const char *cmdname,
   130 		FlagBits flags,
   131 		unsigned int parent,
   132 		const char *info_string);
   133 	virtual void RemoveFromMenu(unsigned int object_id);
   134 	virtual bool DisplayMenu(int client, 
   135 		unsigned int hold_time, 
   136 		TopMenuPosition position);
   137 	virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength);
   138 	virtual unsigned int FindCategory(const char *name);
   139 	const char *GetObjectInfoString(unsigned int object_id);
   140 	const char *GetObjectName(unsigned int object_id);
   141 public: //IMenuHandler
   142 	virtual void OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page);
   143 	virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style);
   144 	virtual unsigned int OnMenuDisplayItem(IBaseMenu *menu, 
   145 		int client, 
   146 		IMenuPanel *panel,
   147 		unsigned int item, 
   148 		const ItemDrawInfo &dr);
   149 	virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason);
   150 public: //ITextListener_SMC
   151 	void ReadSMC_ParseStart();
   152 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
   153 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
   154 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   155 public:
   156 	unsigned int CalcMemUsage();
   157 	void SetTitleCaching(bool cache_titles);
   158 	bool DisplayMenuAtCategory(int client, unsigned int object_id);
   159 private:
   160 	void SortCategoriesIfNeeded();
   161 	void SortCategoryIfNeeded(unsigned int category);
   162 private:
   163 	bool DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position);
   164 	void CreatePlayers(int max_clients);
   165 	void UpdateClientRoot(int client, IGamePlayer *pGamePlayer=NULL);
   166 	void UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck=false);
   167 	void TearDownClient(topmenu_player_t *player);
   168 	bool FindCategoryByObject(unsigned int obj_id, size_t *index);
   169 private:
   170 	void OnClientConnected(int client);
   171 	void OnClientDisconnected(int client);
   172 	void OnServerActivated(int max_clients);
   173 	bool OnIdentityRemoval(IdentityToken_t *owner);
   174 	void OnMaxPlayersChanged(int newvalue);
   175 private:
   176 	config_root_t m_Config;					/* Configuration from file */
   177 	topmenu_player_t *m_clients;			/* Client array */
   178 	CVector<unsigned int> m_SortedCats;		/* Sorted categories */
   179 	CVector<unsigned int> m_UnsortedCats;	/* Un-sorted categories */
   180 	CVector<topmenu_category_t *> m_Categories; /* Category array */
   181 	CVector<topmenu_object_t *> m_Objects;	/* Object array */
   182 	NameHashSet<topmenu_object_t *> m_ObjLookup; /* Object lookup trie */
   183 	unsigned int m_SerialNo;				/* Serial number for updating */
   184 	ITopMenuObjectCallbacks *m_pTitle;		/* Title callbacks */
   185 	int m_max_clients;						/* Maximum number of clients */
   186 	bool m_bCatsNeedResort;					/* True if categories need a resort */
   187 	bool m_bCacheTitles;						/* True if the categorie titles should be cached */
   188 };
   189 
   190 unsigned int strncopy(char *dest, const char *src, size_t count);
   191 
   192 #endif //_INCLUDE_SOURCEMOD_TOP_MENU_H_