Homepage Demos Overview Downloads Tutorials Reference
Credits

ValueEditControl.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_ValueEditControl_h
00003 #define INCLUDED_ValueEditControl_h
00004 
00005 #include "StringInputControl.h"
00006 #include "Events/EventListener.h"
00007 #include "Events/EventBase.h"
00008 #include "Motion/MotionManager.h"
00009 #include "Events/EventRouter.h"
00010 #include "Shared/WorldState.h"
00011 #include "Shared/ERS210Info.h"
00012 #include "Shared/ERS220Info.h"
00013 #include "Shared/ERS7Info.h"
00014 #include "Wireless/Wireless.h"
00015 #include <vector>
00016 #include <sstream>
00017 
00018 //! allows real-time modification of a value through a pointer @todo needs some work to really be useful again
00019 template< class T >
00020 class ValueEditControl : public StringInputControl, public EventListener {
00021  public:
00022   //!constructor
00023   ValueEditControl(const std::string& n, T* t) : StringInputControl(n,"Please enter a new value for "+n), target(t), cur(), copies() {}
00024   //!constructor
00025   ValueEditControl(const std::string& n, const std::string& p, T* t) : StringInputControl(n,p), target(t), cur(), copies() {}
00026   //!constructor
00027   ValueEditControl(const std::string& n, const std::string& d, const std::string& p, T* t) : StringInputControl(n,d,p), target(t), cur(), copies() {}
00028   //!copy constructor
00029   ValueEditControl(const ValueEditControl<T>& vec) : StringInputControl(vec), target(vec.target), cur(vec.cur), copies(vec.copies) {}
00030   //!assignment operator
00031   ValueEditControl operator=(const ValueEditControl<T>& vec) { StringInputControl::operator=(vec); target=vec.target; cur=vec.cur; copies=vec.copies; return *this; }
00032   //!destructor
00033   virtual ~ValueEditControl() {}
00034 
00035   //!reads in current value from target
00036   virtual ControlBase * activate(MotionManager::MC_ID display, Socket * gui) {
00037     cur=*target;
00038     erouter->removeListener(this);
00039     return StringInputControl::activate(display,gui);
00040   }
00041   //! will increment/decrement the current and then assign it to the target when head buttons pressed
00042   virtual void processEvent(const EventBase& e) {
00043     if(state->robotDesign&WorldState::ERS210Mask) {
00044       switch(e.getSourceID()) {
00045       case ERS210Info::HeadFrButOffset: doNextItem(); doSelect(); break;
00046       case ERS210Info::HeadBkButOffset: doNextItem(); doSelect(); break;
00047       default:
00048         serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00049         break;
00050       }
00051     } else if(state->robotDesign&WorldState::ERS220Mask) {
00052       switch(e.getSourceID()) {
00053       case ERS220Info::HeadFrButOffset: doNextItem(); doSelect(); break;
00054       case ERS220Info::HeadBkButOffset: doNextItem(); doSelect(); break;
00055       default:
00056         serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00057         break;
00058       }
00059     } else if(state->robotDesign&WorldState::ERS7Mask) {
00060       switch(e.getSourceID()) {
00061       case ERS7Info::FrontBackButOffset: doNextItem(); doSelect(); break;
00062       case ERS7Info::RearBackButOffset: doNextItem(); doSelect(); break;
00063       default:
00064         serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00065         break;
00066       }
00067     }
00068   }
00069   //! displays current value
00070   virtual void refresh() {
00071     //Do console only if GUI is connected
00072     if(gui_comm!=NULL && wireless->isConnected(gui_comm->sock)) {
00073       std::stringstream ss;
00074       ss << getName();
00075       if(cur!=*target)
00076         ss << ": " << cur;
00077       sout->printf("%s\n",ss.str().c_str());
00078     }
00079 
00080     StringInputControl::refresh();
00081   }
00082 
00083   //! request to continue receiving events so we can modify the value while running
00084   virtual void pause() {
00085     unsigned int FrButOffset,BkButOffset;
00086     if(state->robotDesign&WorldState::ERS210Mask) {
00087       FrButOffset=ERS210Info::HeadFrButOffset;
00088       BkButOffset=ERS210Info::HeadBkButOffset;
00089     } else if(state->robotDesign&WorldState::ERS220Mask) {
00090       FrButOffset=ERS220Info::HeadFrButOffset;
00091       BkButOffset=ERS220Info::HeadBkButOffset;
00092     } else if(state->robotDesign&WorldState::ERS7Mask) {
00093       FrButOffset=ERS7Info::FrontBackButOffset;
00094       BkButOffset=ERS7Info::RearBackButOffset;
00095     } else {
00096       serr->printf("ValueEditControl: Unsupported model!\n");
00097       return;
00098     }
00099     erouter->addListener(this,EventBase(EventBase::buttonEGID,FrButOffset,EventBase::deactivateETID,0));
00100     erouter->addListener(this,EventBase(EventBase::buttonEGID,BkButOffset,EventBase::deactivateETID,0));
00101     //    erouter->addListener(this,EventBase(EventBase::buttonEGID,ChinButOffset,EventBase::deactivateETID,0));
00102     StringInputControl::pause();
00103   }
00104 
00105   //! if the value of the #target!=#cur, assigns the current value to the target and all the #copies
00106   virtual ControlBase * doSelect()   {
00107     if(*target!=cur) {
00108       *target=cur;
00109       for(typename std::vector<T*>::iterator it=copies.begin(); it!=copies.end(); it++)
00110         **it=cur;
00111       //      if(display) {
00112       //        display->flash(FaceLEDMask,100);
00113       //        display->clear();
00114       //      }
00115       std::stringstream ss;
00116       ss << getName() << " set to " << *target;
00117       sout->printf("%s\n",ss.str().c_str());
00118     }
00119     return NULL;
00120   }
00121   //! adds one to the current value
00122   virtual ControlBase * doNextItem() {
00123     cur++;
00124     refresh();
00125     return this;
00126   }
00127   //! subtracts one from the current value
00128   virtual ControlBase * doPrevItem() {
00129     cur--;
00130     refresh();
00131     return this;
00132   }
00133 
00134   virtual ControlBase * takeInput(const std::string& str) {
00135     cur = (T)atof(str.c_str());
00136     return doSelect();
00137   }
00138 
00139   /*!@name Target
00140    * accessors for the target pointer */
00141   virtual T* getTarget() const { return target; } //!< returns the target pointer
00142   virtual ValueEditControl& setTarget(T* t) { target=t; return *this; } //!< sets the target pointer - the object pointed to will be overwritten on activate(); returns @c *this
00143   //@}
00144 
00145   /*!@name Copies
00146    * accessors for the copies vector, so you can assign the same value to several places if you need to */
00147   virtual std::vector<T*>& getCopies() { return copies; } //!< returns a reference to the vector #copies
00148   virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; } //!< #copies.push_back(t)
00149   //@}
00150 
00151   //! shows current value
00152   virtual std::string getName() const {
00153     std::stringstream ss;
00154     ss << StringInputControl::getName() << " (" << *target << ")";
00155     return ss.str();
00156   }
00157 
00158  protected:
00159   T* target; //!< the main target
00160   T cur; //!< the value to use when set
00161   std::vector<T*> copies; //!< additional targets
00162 };
00163 
00164 /*! @file
00165  * @brief Defines ValueEditControl class, which will allow modification of a value through a pointer
00166  * @author ejt (Creator)
00167  *
00168  * $Author: ejt $
00169  * $Name: tekkotsu-2_2_1 $
00170  * $Revision: 1.13 $
00171  * $State: Exp $
00172  * $Date: 2004/10/07 19:07:04 $
00173  */
00174 
00175 #endif

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:40 2004 by Doxygen 1.3.9.1