00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "mouseWatcherGroup.h"
00016 #include "lineSegs.h"
00017 #include "indent.h"
00018 #include "lightMutexHolder.h"
00019
00020 TypeHandle MouseWatcherGroup::_type_handle;
00021
00022
00023
00024
00025
00026
00027 MouseWatcherGroup::
00028 MouseWatcherGroup() :
00029 _lock("MouseWatcherGroup")
00030 {
00031 _sorted = true;
00032 #ifndef NDEBUG
00033 _show_regions = false;
00034 _color.set(0.4, 0.6f, 1.0f, 1.0f);
00035 #endif // NDEBUG
00036 }
00037
00038
00039
00040
00041
00042
00043 MouseWatcherGroup::
00044 ~MouseWatcherGroup() {
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054 void MouseWatcherGroup::
00055 add_region(MouseWatcherRegion *region) {
00056 PT(MouseWatcherRegion) pt = region;
00057
00058 LightMutexHolder holder(_lock);
00059
00060
00061
00062
00063 #ifdef _DEBUG
00064
00065 Regions::const_iterator ri =
00066 find(_regions.begin(), _regions.end(), pt);
00067 nassertv(ri == _regions.end());
00068 #endif // _DEBUG
00069
00070 #ifndef NDEBUG
00071
00072 if (_show_regions) {
00073 nassertv(_vizzes.size() == _regions.size());
00074 _vizzes.push_back(make_viz_region(pt));
00075 }
00076 #endif // NDEBUG
00077
00078 _regions.push_back(pt);
00079 _sorted = false;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 bool MouseWatcherGroup::
00089 has_region(MouseWatcherRegion *region) const {
00090 LightMutexHolder holder(_lock);
00091
00092 PT(MouseWatcherRegion) ptr = region;
00093
00094 if (_sorted) {
00095
00096 Regions::const_iterator ri = lower_bound(_regions.begin(), _regions.end(), ptr);
00097 return (ri != _regions.end() && (*ri) == ptr);
00098 }
00099
00100
00101 Regions::const_iterator ri = find(_regions.begin(), _regions.end(), ptr);
00102 return (ri != _regions.end());
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112 bool MouseWatcherGroup::
00113 remove_region(MouseWatcherRegion *region) {
00114 LightMutexHolder holder(_lock);
00115 return do_remove_region(region);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125 MouseWatcherRegion *MouseWatcherGroup::
00126 find_region(const string &name) const {
00127 LightMutexHolder holder(_lock);
00128
00129 Regions::const_iterator ri;
00130 for (ri = _regions.begin(); ri != _regions.end(); ++ri) {
00131 MouseWatcherRegion *region = (*ri);
00132 if (region->get_name() == name) {
00133 return region;
00134 }
00135 }
00136
00137 return (MouseWatcherRegion *)NULL;
00138 }
00139
00140
00141
00142
00143
00144
00145 void MouseWatcherGroup::
00146 clear_regions() {
00147 LightMutexHolder holder(_lock);
00148
00149 _regions.clear();
00150 _sorted = true;
00151
00152 #ifndef NDEBUG
00153 if (_show_regions) {
00154 _show_regions_root.node()->remove_all_children();
00155 _vizzes.clear();
00156 }
00157 #endif // NDEBUG
00158 }
00159
00160
00161
00162
00163
00164
00165
00166 void MouseWatcherGroup::
00167 sort_regions() {
00168 LightMutexHolder holder(_lock);
00169 do_sort_regions();
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 bool MouseWatcherGroup::
00179 is_sorted() const {
00180 LightMutexHolder holder(_lock);
00181
00182 return _sorted;
00183 }
00184
00185
00186
00187
00188
00189
00190 int MouseWatcherGroup::
00191 get_num_regions() const {
00192 LightMutexHolder holder(_lock);
00193
00194 return _regions.size();
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 MouseWatcherRegion *MouseWatcherGroup::
00206 get_region(int n) const {
00207 LightMutexHolder holder(_lock);
00208 if (n >= 0 && n < (int)_regions.size()) {
00209 return _regions[n];
00210 }
00211 return NULL;
00212 }
00213
00214
00215
00216
00217
00218
00219 void MouseWatcherGroup::
00220 output(ostream &out) const {
00221 out << "MouseWatcherGroup (" << _regions.size() << " regions)";
00222 }
00223
00224
00225
00226
00227
00228
00229 void MouseWatcherGroup::
00230 write(ostream &out, int indent_level) const {
00231 LightMutexHolder holder(_lock);
00232
00233 Regions::const_iterator ri;
00234 for (ri = _regions.begin(); ri != _regions.end(); ++ri) {
00235 MouseWatcherRegion *region = (*ri);
00236 region->write(out, indent_level);
00237 }
00238 }
00239
00240 #ifndef NDEBUG
00241
00242
00243
00244
00245
00246
00247
00248
00249 void MouseWatcherGroup::
00250 show_regions(const NodePath &render2d, const string &bin_name, int draw_order) {
00251 LightMutexHolder holder(_lock);
00252 do_show_regions(render2d, bin_name, draw_order);
00253 }
00254 #endif // NDEBUG
00255
00256 #ifndef NDEBUG
00257
00258
00259
00260
00261
00262
00263
00264 void MouseWatcherGroup::
00265 set_color(const LColor &color) {
00266 LightMutexHolder holder(_lock);
00267
00268 _color = color;
00269 do_update_regions();
00270 }
00271 #endif // NDEBUG
00272
00273 #ifndef NDEBUG
00274
00275
00276
00277
00278
00279
00280 void MouseWatcherGroup::
00281 hide_regions() {
00282 LightMutexHolder holder(_lock);
00283 do_hide_regions();
00284 }
00285 #endif // NDEBUG
00286
00287 #ifndef NDEBUG
00288
00289
00290
00291
00292
00293
00294 void MouseWatcherGroup::
00295 update_regions() {
00296 LightMutexHolder holder(_lock);
00297 do_update_regions();
00298 }
00299 #endif // NDEBUG
00300
00301
00302
00303
00304
00305
00306
00307
00308 void MouseWatcherGroup::
00309 do_sort_regions() {
00310 if (!_sorted) {
00311 sort(_regions.begin(), _regions.end());
00312 _sorted = true;
00313 }
00314 }
00315
00316
00317
00318
00319
00320
00321
00322 bool MouseWatcherGroup::
00323 do_remove_region(MouseWatcherRegion *region) {
00324
00325 PT(MouseWatcherRegion) ptr = region;
00326 Regions::iterator ri;
00327
00328 if (_sorted) {
00329
00330 ri = lower_bound(_regions.begin(), _regions.end(), ptr);
00331 } else {
00332
00333 ri = find(_regions.begin(), _regions.end(), ptr);
00334 }
00335
00336 if (ri != _regions.end() && (*ri) == ptr) {
00337
00338 #ifndef NDEBUG
00339
00340 if (_show_regions) {
00341 nassertr(_vizzes.size() == _regions.size(), false);
00342 size_t index = ri - _regions.begin();
00343 Vizzes::iterator vi = _vizzes.begin() + index;
00344 _show_regions_root.node()->remove_child(*vi);
00345 _vizzes.erase(vi);
00346 }
00347 #endif // NDEBUG
00348
00349 _regions.erase(ri);
00350 return true;
00351 }
00352
00353
00354 return false;
00355 }
00356
00357 #ifndef NDEBUG
00358
00359
00360
00361
00362
00363
00364 void MouseWatcherGroup::
00365 do_show_regions(const NodePath &render2d, const string &bin_name,
00366 int draw_order) {
00367 do_hide_regions();
00368 _show_regions = true;
00369 _show_regions_root = render2d.attach_new_node("show_regions");
00370 _show_regions_root.set_bin(bin_name, draw_order);
00371 do_update_regions();
00372 }
00373 #endif // NDEBUG
00374
00375 #ifndef NDEBUG
00376
00377
00378
00379
00380
00381
00382 void MouseWatcherGroup::
00383 do_hide_regions() {
00384 _show_regions_root.remove_node();
00385 _show_regions = false;
00386 _vizzes.clear();
00387 }
00388 #endif // NDEBUG
00389
00390
00391 #ifndef NDEBUG
00392
00393
00394
00395
00396
00397
00398 void MouseWatcherGroup::
00399 do_update_regions() {
00400 nassertv(_lock.debug_is_locked());
00401
00402 if (_show_regions) {
00403 _show_regions_root.node()->remove_all_children();
00404 _vizzes.clear();
00405 _vizzes.reserve(_regions.size());
00406
00407 Regions::const_iterator ri;
00408 for (ri = _regions.begin(); ri != _regions.end(); ++ri) {
00409 _vizzes.push_back(make_viz_region(*ri));
00410 }
00411 }
00412 }
00413 #endif // NDEBUG
00414
00415
00416 #ifndef NDEBUG
00417
00418
00419
00420
00421
00422
00423
00424 PandaNode *MouseWatcherGroup::
00425 make_viz_region(MouseWatcherRegion *region) {
00426 nassertr(_lock.debug_is_locked(), NULL);
00427
00428 LineSegs ls("show_regions");
00429 ls.set_color(_color);
00430
00431 const LVecBase4 &f = region->get_frame();
00432
00433 ls.move_to(LVector3::rfu(f[0], 0.0f, f[2]));
00434 ls.draw_to(LVector3::rfu(f[1], 0.0f, f[2]));
00435 ls.draw_to(LVector3::rfu(f[1], 0.0f, f[3]));
00436 ls.draw_to(LVector3::rfu(f[0], 0.0f, f[3]));
00437 ls.draw_to(LVector3::rfu(f[0], 0.0f, f[2]));
00438
00439 PT(PandaNode) node = ls.create();
00440 _show_regions_root.attach_new_node(node);
00441
00442 return node;
00443 }
00444 #endif // NDEBUG