Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

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       cout << "*** WARNING ValueEditControl got an unasked for event" << endl;
00045       break;
00046     }
00047   }
00048   //! displays current value
00049   /*  virtual void refresh() {
00050       cout << getName();
00051       if(cur!=*target)
00052       cout << ": " << cur;
00053       cout << endl;
00054       StringInputControl::refresh();
00055       }*/
00056   //! request to continue receiving events so we can modify the value while running
00057   virtual void pause() {
00058     erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadFrButSID,EventBase::deactivateETID,0));
00059     erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadBkButSID,EventBase::deactivateETID,0));
00060     //    erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::ChinButSID,EventBase::deactivateETID,0));
00061     StringInputControl::pause();
00062   }
00063 
00064   //! if the value of the #target!=#cur, assigns the current value to the target and all the #copies
00065   virtual ControlBase * doSelect()   {
00066     if(*target!=cur) {
00067       *target=cur;
00068       for(typename std::vector<T*>::iterator it=copies.begin(); it!=copies.end(); it++)
00069         **it=cur;
00070       //      if(display) {
00071       //        display->flash(FaceLEDMask,100);
00072       //        display->clear();
00073       //      }
00074       cout << getName() << " set to " << *target << endl;
00075     }
00076     return NULL;
00077   }
00078   //! adds one to the current value
00079   virtual ControlBase * doNextItem() {
00080     cur++;
00081     refresh();
00082     return this;
00083   }
00084   //! subtracts one from the current value
00085   virtual ControlBase * doPrevItem() {
00086     cur--;
00087     refresh();
00088     return this;
00089   }
00090 
00091   virtual ControlBase * takeInput(const std::string& str) {
00092     cur = (T)atof(str.c_str());
00093     return doSelect();
00094   }
00095 
00096   /*!@name Target
00097    * accessors for the target pointer */
00098   virtual T* getTarget() const { return target; } //!< returns the target pointer
00099   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
00100   //@}
00101 
00102   /*!@name Copies
00103    * accessors for the copies vector, so you can assign the same value to several places if you need to */
00104   virtual std::vector<T*>& getCopies() { return copies; } //!< returns a reference to the vector #copies
00105   virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; } //!< #copies.push_back(t)
00106   //@}
00107 
00108   //! shows current value
00109   virtual std::string getName() const {
00110     std::stringstream ss;
00111     ss << StringInputControl::getName() << " (" << *target << ")";
00112     return ss.str();
00113   }
00114 
00115  protected:
00116   T* target; //!< the main target
00117   T cur; //!< the value to use when set
00118   std::vector<T*> copies; //!< additional targets
00119 };
00120 
00121 /*! @file
00122  * @brief Defines ValueEditControl class, which will allow modification of a value through a pointer
00123  * @author ejt (Creator)
00124  *
00125  * $Author: ejt $
00126  * $Name: tekkotsu-1_4_1 $
00127  * $Revision: 1.8 $
00128  * $State: Exp $
00129  * $Date: 2003/06/09 20:10:17 $
00130  */
00131 
00132 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:32 2003 by Doxygen 1.3.2