3 * =============================================================================
4 * SourceMod TopMenus Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_TOP_MENU_H_
33 #define _INCLUDE_SOURCEMOD_TOP_MENU_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"
42 using namespace SourceHook;
43 using namespace SourceMod;
45 #define ABSOLUTE_PLAYER_LIMIT 255 // not 256, so we can send the limit as a byte
47 struct config_category_t
50 CVector<int> commands;
55 config_root_t() : strings(1024)
58 BaseStringTable strings;
59 CVector<config_category_t *> cats;
62 struct topmenu_object_t
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 */
77 struct topmenu_category_t
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 */
87 struct topmenu_player_category_t
89 IBaseMenu *menu; /** menu pointer */
90 unsigned int serial; /** last known serial */
93 struct topmenu_player_t
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 */
108 public ITextListener_SMC
110 friend class TopMenuManager;
112 TopMenu(ITopMenuObjectCallbacks *callbacks);
115 virtual unsigned int AddToMenu(const char *name,
116 TopMenuObjectType type,
117 ITopMenuObjectCallbacks *callbacks,
118 IdentityToken_t *owner,
121 unsigned int parent);
122 unsigned int AddToMenu2(const char *name,
123 TopMenuObjectType type,
124 ITopMenuObjectCallbacks *callbacks,
125 IdentityToken_t *owner,
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,
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);
153 unsigned int CalcMemUsage();
155 void SortCategoriesIfNeeded();
156 void SortCategoryIfNeeded(unsigned int category);
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);
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);
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 */
183 unsigned int strncopy(char *dest, const char *src, size_t count);
185 #endif //_INCLUDE_SOURCEMOD_TOP_MENU_H_