extensions/topmenus/TopMenu.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2510 3b2349600557
child 4078 483b617ef4f2
permissions -rw-r--r--
Backed out changeset fa286305bd3f
ds@1990
     1
/**
ds@1990
     2
 * vim: set ts=4 :
ds@1990
     3
 * =============================================================================
ds@1990
     4
 * SourceMod TopMenus Extension
ds@1990
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1990
     6
 * =============================================================================
ds@1990
     7
 *
ds@1990
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1990
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1990
    10
 * Free Software Foundation.
ds@1990
    11
 * 
ds@1990
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1990
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1990
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1990
    15
 * details.
ds@1990
    16
 *
ds@1990
    17
 * You should have received a copy of the GNU General Public License along with
ds@1990
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1990
    19
 *
ds@1990
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1990
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1990
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1990
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1990
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1990
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1990
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1990
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1990
    28
 *
ds@1990
    29
 * Version: $Id$
ds@1990
    30
 */
ds@1990
    31
ds@1990
    32
#ifndef _INCLUDE_SOURCEMOD_TOP_MENU_H_
ds@1990
    33
#define _INCLUDE_SOURCEMOD_TOP_MENU_H_
ds@1990
    34
ds@1990
    35
#include <sh_list.h>
ds@1990
    36
#include <sh_vector.h>
ds@1990
    37
#include <sm_trie_tpl.h>
ds@1990
    38
#include <ITopMenus.h>
ds@1990
    39
#include "smsdk_ext.h"
ds@1990
    40
#include "sm_memtable.h"
ds@1990
    41
ds@1990
    42
using namespace SourceHook;
ds@1990
    43
using namespace SourceMod;
ds@1990
    44
pred@2323
    45
#define ABSOLUTE_PLAYER_LIMIT 255  // not 256, so we can send the limit as a byte 
pred@2323
    46
ds@1990
    47
struct config_category_t
ds@1990
    48
{
ds@1990
    49
	int name;
ds@1990
    50
	CVector<int> commands;
ds@1990
    51
};
ds@1990
    52
ds@1990
    53
struct config_root_t
ds@1990
    54
{
ds@1990
    55
	config_root_t() : strings(1024)
ds@1990
    56
	{
ds@1990
    57
	}
ds@1990
    58
	BaseStringTable strings;
ds@1990
    59
	CVector<config_category_t *> cats;
ds@1990
    60
};
ds@1990
    61
ds@1990
    62
struct topmenu_object_t
ds@1990
    63
{
ds@1990
    64
	char name[64];						/** Name */
ds@1990
    65
	char cmdname[64];					/** Command name */ 
ds@1990
    66
	FlagBits flags;						/** Admin flags */
ds@1990
    67
	ITopMenuObjectCallbacks *callbacks;	/** Callbacks */
ds@1990
    68
	IdentityToken_t *owner;				/** Owner */
ds@1990
    69
	unsigned int object_id;				/** Object ID */
ds@1990
    70
	topmenu_object_t *parent;			/** Parent, if any */
ds@1990
    71
	TopMenuObjectType type;				/** Object Type */
ds@1990
    72
	bool is_free;						/** Free or not? */
ds@1990
    73
	char info[255];						/** Info string */
dvander@2510
    74
	unsigned int cat_id;				/** Set if a category */
ds@1990
    75
};
ds@1990
    76
ds@1990
    77
struct topmenu_category_t
ds@1990
    78
{
ds@1990
    79
	CVector<topmenu_object_t *> obj_list;	/** Full object list */
ds@1990
    80
	CVector<topmenu_object_t *> sorted;		/** Sorted items */
ds@1990
    81
	CVector<topmenu_object_t *> unsorted;	/** Unsorted items */
ds@1990
    82
	topmenu_object_t *obj;					/** Bound object */
ds@1990
    83
	unsigned int serial;					/** Serial number */
ds@1990
    84
	bool reorder;							/** Whether ordering needs updating */
ds@1990
    85
};
ds@1990
    86
ds@1990
    87
struct topmenu_player_category_t
ds@1990
    88
{
ds@1990
    89
	IBaseMenu *menu;					/** menu pointer */
ds@1990
    90
	unsigned int serial;				/** last known serial */
ds@1990
    91
};
ds@1990
    92
ds@1990
    93
struct topmenu_player_t
ds@1990
    94
{
ds@1990
    95
	int user_id;						/** userid on server */
ds@1990
    96
	unsigned int menu_serial;			/** menu serial no */
ds@1990
    97
	IBaseMenu *root;					/** root menu display */
ds@1990
    98
	topmenu_player_category_t *cats;	/** category display */
ds@1990
    99
	unsigned int cat_count;				/** number of categories */
ds@1990
   100
	unsigned int last_category;			/** last category they selected */
ds@1990
   101
	unsigned int last_position;			/** last position in that category */
ds@1990
   102
	unsigned int last_root_pos;			/** last page in the root menu */
ds@1990
   103
};
ds@1990
   104
ds@1990
   105
class TopMenu : 
ds@1990
   106
	public ITopMenu,
ds@1990
   107
	public IMenuHandler,
ds@1990
   108
	public ITextListener_SMC
ds@1990
   109
{
ds@1990
   110
	friend class TopMenuManager;
ds@1990
   111
public:
ds@1990
   112
	TopMenu(ITopMenuObjectCallbacks *callbacks);
ds@1990
   113
	~TopMenu();
ds@1990
   114
public: //ITopMenu
ds@1990
   115
	virtual unsigned int AddToMenu(const char *name,
ds@1990
   116
		TopMenuObjectType type,
ds@1990
   117
		ITopMenuObjectCallbacks *callbacks,
ds@1990
   118
		IdentityToken_t *owner,
ds@1990
   119
		const char *cmdname,
ds@1990
   120
		FlagBits flags,
ds@1990
   121
		unsigned int parent);
ds@1990
   122
	unsigned int AddToMenu2(const char *name,
ds@1990
   123
		TopMenuObjectType type,
ds@1990
   124
		ITopMenuObjectCallbacks *callbacks,
ds@1990
   125
		IdentityToken_t *owner,
ds@1990
   126
		const char *cmdname,
ds@1990
   127
		FlagBits flags,
ds@1990
   128
		unsigned int parent,
ds@1990
   129
		const char *info_string);
ds@1990
   130
	virtual void RemoveFromMenu(unsigned int object_id);
ds@1990
   131
	virtual bool DisplayMenu(int client, 
ds@1990
   132
		unsigned int hold_time, 
ds@1990
   133
		TopMenuPosition position);
ds@1990
   134
	virtual bool LoadConfiguration(const char *file, char *error, size_t maxlength);
ds@1990
   135
	virtual unsigned int FindCategory(const char *name);
ds@1990
   136
	const char *GetObjectInfoString(unsigned int object_id);
ds@1990
   137
	const char *GetObjectName(unsigned int object_id);
ds@1990
   138
public: //IMenuHandler
ds@1990
   139
	virtual void OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page);
ds@1990
   140
	virtual void OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style);
ds@1990
   141
	virtual unsigned int OnMenuDisplayItem(IBaseMenu *menu, 
ds@1990
   142
		int client, 
ds@1990
   143
		IMenuPanel *panel,
ds@1990
   144
		unsigned int item, 
ds@1990
   145
		const ItemDrawInfo &dr);
ds@1990
   146
	virtual void OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason);
ds@1990
   147
public: //ITextListener_SMC
ds@1990
   148
	void ReadSMC_ParseStart();
ds@1990
   149
	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
ds@1990
   150
	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
ds@1990
   151
	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
ds@1990
   152
public:
ds@1990
   153
	unsigned int CalcMemUsage();
ds@1990
   154
private:
ds@1990
   155
	void SortCategoriesIfNeeded();
ds@1990
   156
	void SortCategoryIfNeeded(unsigned int category);
ds@1990
   157
private:
ds@1990
   158
	bool DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position);
ds@1990
   159
	void CreatePlayers(int max_clients);
ds@1990
   160
	void UpdateClientRoot(int client, IGamePlayer *pGamePlayer=NULL);
dvander@2510
   161
	void UpdateClientCategory(int client, unsigned int category, bool bSkipRootCheck=false);
ds@1990
   162
	void TearDownClient(topmenu_player_t *player);
ds@1990
   163
private:
ds@1990
   164
	void OnClientConnected(int client);
ds@1990
   165
	void OnClientDisconnected(int client);
ds@1990
   166
	void OnServerActivated(int max_clients);
ds@1990
   167
	bool OnIdentityRemoval(IdentityToken_t *owner);
pred@2323
   168
	void OnMaxPlayersChanged(int newvalue);
ds@1990
   169
private:
ds@1990
   170
	config_root_t m_Config;					/* Configuration from file */
ds@1990
   171
	topmenu_player_t *m_clients;			/* Client array */
ds@1990
   172
	CVector<unsigned int> m_SortedCats;		/* Sorted categories */
ds@1990
   173
	CVector<unsigned int> m_UnsortedCats;	/* Un-sorted categories */
ds@1990
   174
	CVector<topmenu_category_t *> m_Categories; /* Category array */
ds@1990
   175
	CVector<topmenu_object_t *> m_Objects;	/* Object array */
ds@1990
   176
	KTrie<topmenu_object_t *> m_ObjLookup;	/* Object lookup trie */
ds@1990
   177
	unsigned int m_SerialNo;				/* Serial number for updating */
ds@1990
   178
	ITopMenuObjectCallbacks *m_pTitle;		/* Title callbacks */
ds@1990
   179
	int m_max_clients;						/* Maximum number of clients */
ds@1990
   180
	bool m_bCatsNeedResort;					/* True if categories need a resort */
ds@1990
   181
};
ds@1990
   182
ds@1990
   183
unsigned int strncopy(char *dest, const char *src, size_t count);
ds@1990
   184
ds@1990
   185
#endif //_INCLUDE_SOURCEMOD_TOP_MENU_H_