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 "Events/EventRouter.h"
00009 #include "Shared/WorldState.h"
00010 #include "Shared/ERS210Info.h"
00011 #include "Shared/ERS220Info.h"
00012 #include "Shared/ERS7Info.h"
00013 #include "Wireless/Wireless.h"
00014 #include "Behaviors/Controller.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(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& event) {
00043     if(Controller::nextItem == event) {
00044       doNextItem();
00045       doSelect();
00046     } else if(Controller::prevItem == event) {
00047       doPrevItem();
00048       doSelect();
00049     } else {
00050       serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00051     }
00052   }
00053   
00054   //! displays current value
00055   virtual void refresh() {
00056     //Do console only if GUI is connected
00057     if(gui_comm!=NULL && wireless->isConnected(gui_comm->sock)) {
00058       std::stringstream ss;
00059       ss << getName();
00060       if(cur!=*target)
00061         ss << ": " << cur;
00062       sout->printf("%s\n",ss.str().c_str());
00063     }
00064 
00065     StringInputControl::refresh();
00066   }
00067 
00068   //! request to continue receiving events so we can modify the value while running
00069   virtual void pause() {
00070     erouter->addListener(this,Controller::nextItem);
00071     erouter->addListener(this,Controller::prevItem);
00072     //    erouter->addListener(this,EventBase(EventBase::buttonEGID,ChinButOffset,EventBase::deactivateETID,0));
00073     StringInputControl::pause();
00074   }
00075 
00076   //! if the value of the #target!=#cur, assigns the current value to the target and all the #copies
00077   virtual ControlBase * doSelect()   {
00078     if(*target!=cur) {
00079       *target=cur;
00080       for(typename std::vector<T*>::iterator it=copies.begin(); it!=copies.end(); it++)
00081         **it=cur;
00082       //      if(display) {
00083       //        display->flash(FaceLEDMask,100);
00084       //        display->clear();
00085       //      }
00086       std::stringstream ss;
00087       ss << getName() << " set to " << *target;
00088       sout->printf("%s\n",ss.str().c_str());
00089     }
00090     return NULL;
00091   }
00092   //! adds one to the current value
00093   virtual ControlBase * doNextItem() {
00094     cur=(T)(cur+1);
00095     refresh();
00096     return this;
00097   }
00098   //! subtracts one from the current value
00099   virtual ControlBase * doPrevItem() {
00100     cur=(T)(cur-1);
00101     refresh();
00102     return this;
00103   }
00104 
00105   virtual ControlBase * takeInput(const std::string& str) {
00106     cur = (T)atof(str.c_str());
00107     StringInputControl::takeInput(str);
00108     return doSelect();
00109   }
00110 
00111   /*!@name Target
00112    * accessors for the target pointer */
00113   virtual T* getTarget() const { return target; } //!< returns the target pointer
00114   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
00115   //@}
00116 
00117   /*!@name Copies
00118    * accessors for the copies vector, so you can assign the same value to several places if you need to */
00119   virtual std::vector<T*>& getCopies() { return copies; } //!< returns a reference to the vector #copies
00120   virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; } //!< copies.push_back(t)
00121   //@}
00122 
00123   //! shows current value
00124   virtual std::string getName() const {
00125     std::stringstream ss;
00126     ss << StringInputControl::getName() << " (" << *target << ")";
00127     return ss.str();
00128   }
00129 
00130  protected:
00131   T* target; //!< the main target
00132   T cur; //!< the value to use when set
00133   std::vector<T*> copies; //!< additional targets
00134 };
00135 
00136 /*! @file
00137  * @brief Defines ValueEditControl class, which will allow modification of a value through a pointer
00138  * @author ejt (Creator)
00139  */
00140 
00141 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:52 2016 by Doxygen 1.6.3