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 <vector>
00009 #include "Motion/MotionManager.h"
00010 #include "Events/EventRouter.h"
00011 #include "Shared/WorldState.h"
00012 #include <sstream>
00013 
00014 //! allows real-time modification of a value through a pointer @todo needs some work to really be useful again
00015 template< class T >
00016 class ValueEditControl : public StringInputControl, public EventListener {
00017  public:
00018   //!constructor
00019   ValueEditControl(const std::string& n, T* t) : StringInputControl(n,"Please enter a new value for "+n), target(t), cur(), copies() {}
00020   //!constructor
00021   ValueEditControl(const std::string& n, const std::string& p, T* t) : StringInputControl(n,p), target(t), cur(), copies() {}
00022   //!constructor
00023   ValueEditControl(const std::string& n, const std::string& d, const std::string& p, T* t) : StringInputControl(n,d,p), target(t), cur(), copies() {}
00024   //!copy constructor
00025   ValueEditControl(const ValueEditControl<T>& vec) : StringInputControl(vec), target(vec.target), cur(vec.cur), copies(vec.copies) {}
00026   //!assignment operator
00027   ValueEditControl operator=(const ValueEditControl<T>& vec) { StringInputControl::operator=(vec); target=vec.target; cur=vec.cur; copies=vec.copies; return *this; }
00028   //!destructor
00029   virtual ~ValueEditControl() {}
00030 
00031   //!reads in current value from target
00032   virtual ControlBase * activate(MotionManager::MC_ID display, Socket * gui) {
00033     cur=*target;
00034     erouter->forgetListener(this);
00035     return StringInputControl::activate(display,gui);
00036   }
00037   //! will increment/decrement the current and then assign it to the target when head buttons pressed
00038   virtual void processEvent(const EventBase& e) {
00039     switch(e.getSourceID()) {
00040     case ButtonSourceID::HeadFrButSID: doNextItem(); doSelect(); break;
00041     case ButtonSourceID::HeadBkButSID: doNextItem(); doSelect(); break;
00042       //    case ButtonSourceID::ChinButSID: doSelect(); break;
00043     default:
00044       serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00045       break;
00046     }
00047   }
00048   //! displays current value
00049   virtual void refresh() {
00050     //Do console only if GUI is connected
00051     if(gui_comm!=NULL && wireless->isConnected(gui_comm->sock)) {
00052       std::stringstream ss;
00053       ss << getName();
00054       if(cur!=*target)
00055         ss << ": " << cur;
00056       sout->printf("%s\n",ss.str().c_str());
00057     }
00058 
00059     StringInputControl::refresh();
00060   }
00061 
00062   //! request to continue receiving events so we can modify the value while running
00063   virtual void pause() {
00064     erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadFrButSID,EventBase::deactivateETID,0));
00065     erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadBkButSID,EventBase::deactivateETID,0));
00066     //    erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::ChinButSID,EventBase::deactivateETID,0));
00067     StringInputControl::pause();
00068   }
00069 
00070   //! if the value of the #target!=#cur, assigns the current value to the target and all the #copies
00071   virtual ControlBase * doSelect()   {
00072     if(*target!=cur) {
00073       *target=cur;
00074       for(typename std::vector<T*>::iterator it=copies.begin(); it!=copies.end(); it++)
00075         **it=cur;
00076       //      if(display) {
00077       //        display->flash(FaceLEDMask,100);
00078       //        display->clear();
00079       //      }
00080       std::stringstream ss;
00081       ss << getName() << " set to " << *target;
00082       sout->printf("%s\n",ss.str().c_str());
00083     }
00084     return NULL;
00085   }
00086   //! adds one to the current value
00087   virtual ControlBase * doNextItem() {
00088     cur++;
00089     refresh();
00090     return this;
00091   }
00092   //! subtracts one from the current value
00093   virtual ControlBase * doPrevItem() {
00094     cur--;
00095     refresh();
00096     return this;
00097   }
00098 
00099   virtual ControlBase * takeInput(const std::string& str) {
00100     cur = (T)atof(str.c_str());
00101     return doSelect();
00102   }
00103 
00104   /*!@name Target
00105    * accessors for the target pointer */
00106   virtual T* getTarget() const { return target; } //!< returns the target pointer
00107   virtual ValueEditControl& setTarget(T* t) { target=t; return *this; } //!< sets the target pointer - the object pointed to will be overwritten on activate() @return @c *this
00108   //@}
00109 
00110   /*!@name Copies
00111    * accessors for the copies vector, so you can assign the same value to several places if you need to */
00112   virtual std::vector<T*>& getCopies() { return copies; } //!< returns a reference to the vector #copies
00113   virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; } //!< #copies.push_back(t)
00114   //@}
00115 
00116   //! shows current value
00117   virtual std::string getName() const {
00118     std::stringstream ss;
00119     ss << StringInputControl::getName() << " (" << *target << ")";
00120     return ss.str();
00121   }
00122 
00123  protected:
00124   T* target; //!< the main target
00125   T cur; //!< the value to use when set
00126   std::vector<T*> copies; //!< additional targets
00127 };
00128 
00129 /*! @file
00130  * @brief Defines ValueEditControl class, which will allow modification of a value through a pointer
00131  * @author ejt (Creator)
00132  *
00133  * $Author: ejt $
00134  * $Name: tekkotsu-1_5 $
00135  * $Revision: 1.9 $
00136  * $State: Rel $
00137  * $Date: 2003/07/14 05:50:25 $
00138  */
00139 
00140 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4