00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "buttonRegistry.h"
00016 #include "config_util.h"
00017
00018 #include <stdio.h>
00019
00020
00021
00022
00023
00024
00025 ButtonRegistry *ButtonRegistry::_global_pointer = NULL;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 bool ButtonRegistry::
00050 register_button(ButtonHandle &button_handle, const string &name,
00051 ButtonHandle alias, char ascii_equivalent) {
00052 NameRegistry::iterator ri;
00053 ri = _name_registry.find(name);
00054
00055 if (ri == _name_registry.end()) {
00056
00057
00058
00059 int index = -1;
00060 if (ascii_equivalent != '\0') {
00061 if (_handle_registry[ascii_equivalent] == (RegistryNode *)NULL) {
00062 index = ascii_equivalent;
00063 } else {
00064 util_cat->error()
00065 << "Attempt to register multiple buttons under ASCII equivalent "
00066 << ascii_equivalent << "\n";
00067 }
00068 }
00069
00070 #ifdef NOTIFY_DEBUG
00071
00072
00073 if (util_cat->is_spam()) {
00074 util_cat->spam()
00075 << "Registering button " << name << "\n";
00076 }
00077 #endif
00078
00079 if (index == -1) {
00080
00081 index = _handle_registry.size();
00082 _handle_registry.push_back(NULL);
00083 }
00084
00085 ButtonHandle new_handle;
00086 new_handle._index = index;
00087
00088 RegistryNode *rnode = new RegistryNode(new_handle, alias, name);
00089 _handle_registry[index] = rnode;
00090 _name_registry[name] = rnode;
00091
00092 button_handle = new_handle;
00093 return true;
00094 }
00095
00096 RegistryNode *rnode = (*ri).second;
00097 nassertr(rnode->_name == (*ri).first, false);
00098 nassertr(rnode->_handle._index >= 0 &&
00099 rnode->_handle._index < (int)_handle_registry.size(), false);
00100 nassertr(_handle_registry[rnode->_handle._index] == rnode, false);
00101 nassertr(rnode->_handle._index != 0, false);
00102
00103 if (button_handle != rnode->_handle) {
00104
00105 util_cat->warning()
00106 << "Attempt to register button " << name << " more than once!\n";
00107
00108 button_handle = rnode->_handle;
00109 }
00110 return false;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 ButtonHandle ButtonRegistry::
00121 get_button(const string &name) {
00122 NameRegistry::const_iterator ri;
00123 ri = _name_registry.find(name);
00124
00125 if (ri != _name_registry.end()) {
00126 return (*ri).second->_handle;
00127 }
00128
00129 ButtonHandle button;
00130 register_button(button, name);
00131 return button;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141 ButtonHandle ButtonRegistry::
00142 find_ascii_button(char ascii_equivalent) const {
00143 if (_handle_registry[ascii_equivalent] == (RegistryNode *)NULL) {
00144 return ButtonHandle::none();
00145 }
00146 return _handle_registry[ascii_equivalent]->_handle;
00147 }
00148
00149
00150
00151
00152
00153
00154 void ButtonRegistry::
00155 write(ostream &out) const {
00156 out << "ASCII equivalents:\n";
00157 for (int i = 1; i < 128; i++) {
00158 if (_handle_registry[i] != (RegistryNode *)NULL) {
00159 char hex[12];
00160 sprintf(hex, "%02x", (unsigned int)i);
00161 nassertv(strlen(hex) < 12);
00162
00163 out << " " << hex << " " << _handle_registry[i]->_name << "\n";
00164 }
00165 }
00166
00167 out << "\nOther buttons:\n";
00168 NameRegistry::const_iterator ri;
00169 for (ri = _name_registry.begin(); ri != _name_registry.end(); ++ri) {
00170 if (!(*ri).second->_handle.has_ascii_equivalent()) {
00171 out << " " << (*ri).second->_name;
00172 if ((*ri).second->_alias != ButtonHandle::none()) {
00173 out << " (alias " << (*ri).second->_alias << ")";
00174 }
00175 out << "\n";
00176 }
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 ButtonRegistry::
00187 ButtonRegistry() {
00188
00189
00190
00191
00192 _handle_registry.reserve(128);
00193 int i;
00194 for (i = 0; i < 128; i++) {
00195 _handle_registry.push_back(NULL);
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 void ButtonRegistry::
00206 init_global_pointer() {
00207 _global_pointer = new ButtonRegistry;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 ButtonRegistry::RegistryNode *ButtonRegistry::
00217 look_up(ButtonHandle handle) const {
00218 nassertr(handle._index != 0, NULL);
00219
00220 if (handle._index < 0 ||
00221 handle._index >= (int)_handle_registry.size()) {
00222 util_cat->fatal()
00223 << "Invalid ButtonHandle index " << handle._index
00224 << "! Is memory corrupt?\n";
00225 return (RegistryNode *)NULL;
00226 }
00227
00228 return _handle_registry[handle._index];
00229 }