ValueEditControl.h
Go to the documentation of this file.00001 
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 
00019 template< class T >
00020 class ValueEditControl : public StringInputControl, public EventListener {
00021  public:
00022 
00023   ValueEditControl(const std::string& n, T* t) : StringInputControl(n,"Please enter a new value for "+n), target(t), cur(), copies() {}
00024 
00025   ValueEditControl(const std::string& n, const std::string& p, T* t) : StringInputControl(n,p), target(t), cur(), copies() {}
00026 
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 
00029   ValueEditControl(const ValueEditControl<T>& vec) : StringInputControl(vec), target(vec.target), cur(vec.cur), copies(vec.copies) {}
00030 
00031   ValueEditControl operator=(const ValueEditControl<T>& vec) { StringInputControl::operator=(vec); target=vec.target; cur=vec.cur; copies=vec.copies; return *this; }
00032 
00033   virtual ~ValueEditControl() {}
00034 
00035 
00036   virtual ControlBase * activate(MC_ID display, Socket * gui) {
00037     cur=*target;
00038     erouter->removeListener(this);
00039     return StringInputControl::activate(display,gui);
00040   }
00041 
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 
00055   virtual void refresh() {
00056     
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 
00069   virtual void pause() {
00070     erouter->addListener(this,Controller::nextItem);
00071     erouter->addListener(this,Controller::prevItem);
00072     
00073     StringInputControl::pause();
00074   }
00075 
00076 
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       
00083       
00084       
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 
00093   virtual ControlBase * doNextItem() {
00094     cur=(T)(cur+1);
00095     refresh();
00096     return this;
00097   }
00098 
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 
00112 
00113   virtual T* getTarget() const { return target; } 
00114   virtual ValueEditControl& setTarget(T* t) { target=t; return *this; } 
00115 
00116 
00117 
00118 
00119   virtual std::vector<T*>& getCopies() { return copies; } 
00120   virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; } 
00121 
00122 
00123 
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; 
00132   T cur; 
00133   std::vector<T*> copies; 
00134 };
00135 
00136 
00137 
00138 
00139 
00140 
00141 #endif