Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mmenu.C

Go to the documentation of this file.
00001 
00002 #include "std/time.H"
00003 #include "geom/text2d.H"
00004 #include "geom/world.H"
00005 #include "mmenu.H"
00006 
00007 using mlib::Wpt;
00008 using mlib::Wtransf;
00009 using mlib::XYpt;
00010 using mlib::CXYpt;
00011 using mlib::XYvec;
00012 using mlib::CXYvec;
00013 using mlib::VEXEL;
00014 
00015 int
00016 MMENU::invoke(
00017    CEvent &e, 
00018    State *&s
00019    )
00020 {
00021    if (_entry.consumes(e)) {
00022       s = _entry.event(e);
00023       return true;
00024    }
00025    else
00026       return false;
00027 }
00028 
00029 static const double _MENU_OFFSET = 0.02;
00030 
00031 int
00032 MMENU::down(
00033    CEvent  &e,
00034    State  *&
00035    )
00036 {
00037    DEVice_buttons *btns = (DEVice_buttons *)e._d;
00038    DEVice_2d      *ptr  = btns->ptr2d();
00039    _view = e.view();
00040 
00041   _view->schedule(this);
00042 
00043   _t0   = the_time();
00044   _d    = ptr->cur();
00045   _sel = -1;
00046   for(int i=0;i<2;i++) _d[i] += _MENU_OFFSET;
00047   /*
00048   _disp = false;
00049   _sel  = NUM_DIRS;
00050   */
00051 
00052   return 0;
00053 }
00054   
00055 int
00056 MMENU::move(
00057    CEvent  &e,
00058    State  *&
00059    )
00060 {
00061    DEVice_2d  *ptr = (DEVice_2d *)e._d;
00062    CXYpt       p   = ptr->cur();
00063 //  Find if cursor is touching any geoms
00064    _sel = -1;
00065    for(int i=0;i<_items.num();i++){
00066       GEOMptr gp(_items[i]._g);
00067       if(gp){
00068          CBBOX b = gp->bbox();
00069          if (TEXT2D::isa(gp)) {
00070             TEXT2Dptr  t2d  = (TEXT2D *)&*gp;
00071 
00072             t2d->set_is2d(1); // Make sure it is screen space text
00073        
00074             BBOX2D bbox = t2d->bbox2d(0, 0, 1);
00075             if ( bbox.contains(p) ){
00076                _sel = i;
00077                gp->set_color(_highlight_col);
00078             }
00079 
00080          }
00081       }
00082    }
00083 
00084    /*
00085    if (_disp && _sel != NUM_DIRS && _geoms[_sel])
00086       _geoms[_sel]->set_color(_nhighlight_col);
00087     
00088    _sel = NUM_DIRS;
00089    XYvec  v = (p - _d);
00090    if (v.length() > 0.05) {
00091       double a = atan2(v[0], v[1]) * (180.0 / M_PI);
00092       if (a < -45.0/2.0) 
00093          a += 360.0;
00094      
00095       for (int i=0; i<NUM_DIRS; i++)
00096          if (_geoms[i] && (a > i*45.0 - 45.0/2 && a < i*45.0 + 45.0/2))
00097             _sel = DIR(i);
00098     }
00099  
00100    if (_disp && _sel != NUM_DIRS && _geoms[_sel])
00101       _geoms[_sel]->set_color(_highlight_col);
00102    */
00103    return 0;
00104    
00105 }
00106 
00107 void MMENU::call(int sel){
00108    assert( (sel>=0) && (sel<_items.num()) );
00109    if( _items[sel]._f )
00110        _items[sel]._f->exec();
00111 }
00112 
00113 int
00114 MMENU::up(
00115    CEvent  &e,
00116    State  *&
00117    )
00118 {
00119 //   DEVice_buttons *btns = (DEVice_buttons *)e._d;
00120    for(int i=0;i<_items.num();i++){
00121       if(GEOMptr gp = _items[i]._g)
00122          WORLD::undisplay(gp, false);
00123    }
00124 
00125    _view->unschedule(this);
00126 
00127    if(_sel>=0){
00128       call(_sel);
00129    }
00130 
00131    return 0;
00132 }
00133 
00134 int
00135 MMENU::tick() {
00136    // bounding box of entire dialog box
00137    BBOX2D dbbox;
00138 
00139    mlib::XYpt curloc(_d);
00140 
00141    for(int i=0;i<_items.num();i++){
00142       GEOMptr gp(_items[i]._g);
00143       if(gp){
00144          WORLD::display(gp, false);
00145 
00146          if(i != _sel)
00147             gp->set_color(_nhighlight_col);
00148 
00149          // this is fairly absurd... why isn't bbox2d a virtual method?
00150          if (TEXT2D::isa(gp)) {
00151             TEXT2Dptr  t2d  = (TEXT2D *)&*gp;
00152 
00153             t2d->set_is2d(1); // Make sure it is screen space text
00154        
00155             BBOX2D bbox = t2d->bbox2d(0, 0, 1);
00156 //            t2d->set_loc( bbox.min() );
00157             curloc[1] +=  bbox.min()[1] - bbox.max()[1];
00158 
00159             t2d->set_loc(curloc);
00160             dbbox += t2d->bbox2d(0,0,1);
00161          } else {
00162             Wpt curpt(curloc);
00163             Wtransf newloc(curpt);
00164 
00165             curloc[1] += -_MENU_OFFSET;
00166             gp->set_xform(newloc);
00167          }
00168 
00169          curloc[1] -= _MENU_OFFSET;
00170 
00171       }
00172    }
00173 
00174    double cur;
00175    for(int k=0;k<2;k++){
00176       if( (cur=dbbox.max()[k]) > 1.0 ){
00177          _d[k] -= (cur-1.0);
00178       }
00179       if( (cur=dbbox.min()[k]) < -1.0 ){
00180          _d[k] -= (cur+1.0);
00181       }
00182    }
00183    return 0;
00184 }
00185 
00186 #if 0
00187    const double threshold = 0.5;
00188    double dist = 75;
00189   
00190    if (!_disp && the_time() - _t0 > threshold) {
00191       for (int i=0; i<NUM_DIRS; i++) {
00192          if (_geoms[i]) {
00193             WORLD::display(_geoms[i], false);
00194 
00195             VEXEL vec(dist * sin(i * 2.0 * M_PI/ 8), 
00196                       dist * cos(i * 2.0 * M_PI/ 8));
00197             Wtransf newloc(Wpt(_d + vec));
00198 
00199             // Need to recenter it if it is text...
00200             if (TEXT2D::isa(_geoms[i])) {
00201                _geoms[i]->set_color(_nhighlight_col);
00202                TEXT2Dptr  t2d  = (TEXT2D *)&*_geoms[i];
00203                t2d->set_is2d(1); // Make sure it is screen space text
00204                t2d->set_loc(XYpt(newloc.origin()));
00205 
00206                BBOX2D bbox = t2d->bbox2d(0, 0, 1);
00207                XYvec shift;
00208                // Shift left for all but eastern (right) choices
00209                   shift = -XYvec(bbox.dim()[0]/2, 0);
00210 //               }
00211                t2d->set_loc(bbox.min() + shift);
00212             } else {
00213                {
00214                   _geoms[i]->set_xform(newloc);
00215                }
00216             }
00217          }
00218       }
00219     
00220       _disp = true;
00221    }
00222   
00223    return 0;
00224 }
00225 #endif

Generated on Mon Sep 18 11:39:32 2006 for jot by  doxygen 1.4.4