00001 #ifndef GLUE_2DTOBTNFILTER_H
00002 #define GLUE_2DTOBTNFILTER_H
00003
00004 #define OUTSIDE_GLUE_CORE
00005
00006 #include "event/event.H"
00007 #include "dev/dev.H"
00008 #include "dev/dev2d.H"
00009 #include "dev/buttons.H"
00010 #include "mlib/points.H"
00011 #include <math.h>
00012 #include "config/config.H"
00013
00014
00015 class twodtobtnFilter:public EVENTfilter
00016 {
00017 protected:
00018 str_ptr _name;
00019 str_ptr _btnleft;
00020 str_ptr _btnright;
00021 str_ptr _btnup;
00022 str_ptr _btndown;
00023 str_ptr _btncenter;
00024 str_ptr _dev2d;
00025 double _distance;
00026 int _prevState;
00027 public:
00028 twodtobtnFilter(str_ptr name):_name(name){
00029 _btnleft=CONFIGval(_name+str_ptr("_left"),"left",true);
00030 _btnright=CONFIGval(_name+str_ptr("_right"),"right",true);
00031 _btnup=CONFIGval(_name+str_ptr("_up"),"up",true);
00032 _btndown=CONFIGval(_name+str_ptr("_down"),"down",true);
00033 _btncenter=CONFIGval(_name+str_ptr("_center"),"center",true);
00034
00035 _dev2d=CONFIGval(_name+str_ptr("_2d"),"",true);
00036
00037 _distance=CONFIGval(_name+str_ptr("_distance"),0.5,false);
00038 _prevState=-1;
00039 }
00040
00041 EVENTptr filter(cEVENTptr &evt){
00042
00043
00044 if (EVENT2d::isa(evt)){
00045
00046
00047 EVENT2dptr e2d=EVENT2d::cast(evt);
00048 if (e2d){
00049 if (e2d->source()->evt_name()==_dev2d){
00050
00051
00052
00053 XYpt cur=e2d->cur();
00054 int new_state;
00055 if (hypot(cur[0],cur[1])>_distance){
00056
00057 double angle=atan2(cur[1],cur[0]);
00058 if (angle>=-3*M_PI/4.0 && angle<=-M_PI/4.0){
00059 new_state=1;
00060 }else if (angle>-M_PI/4.0 && angle<=M_PI/4.0){
00061 new_state=2;
00062 }else if (angle >M_PI/4.0 && angle<=3*M_PI/4.0){
00063 new_state=3;
00064 }else{
00065 new_state=4;
00066 }
00067 }else{
00068 new_state=0;
00069 }
00070
00071
00072 if (new_state!=_prevState){
00073
00074 switch(_prevState){
00075 case(0):
00076 EVENTmgr::distrib(new EVENTbtn(_btncenter,false));
00077 break;
00078 case(1):
00079 EVENTmgr::distrib(new EVENTbtn(_btndown,false));
00080 break;
00081 case(2):
00082 EVENTmgr::distrib(new EVENTbtn(_btnright,false));
00083 break;
00084 case(3):
00085 EVENTmgr::distrib(new EVENTbtn(_btnup,false));
00086 break;
00087 case(4):
00088 EVENTmgr::distrib(new EVENTbtn(_btnleft,false));
00089 break;
00090 }
00091
00092
00093 _prevState=new_state;
00094 switch(_prevState){
00095 case(0):
00096 return new EVENTbtn(_btncenter,true);
00097 break;
00098 case(1):
00099 return new EVENTbtn(_btndown,true);
00100 break;
00101 case(2):
00102 return new EVENTbtn(_btnright,true);
00103 break;
00104 case(3):
00105 return new EVENTbtn(_btnup,true);
00106 break;
00107 case(4):
00108 return new EVENTbtn(_btnleft,true);
00109 break;
00110 }
00111 }
00112
00113 }else{
00114
00115 }
00116
00117 }else {
00118
00119 return evt;
00120 }
00121 }
00122
00123 return evt;
00124 }
00125 };
00126
00127
00128 #endif