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

Tekkotsu v4.0
Generated Thu Nov 22 00:54:56 2007 by Doxygen 1.5.4