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 "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
00020 template< class T >
00021 class ValueEditControl : public StringInputControl, public EventListener {
00022 public:
00023
00024 ValueEditControl(const std::string& n, T* t) : StringInputControl(n,"Please enter a new value for "+n), target(t), cur(), copies() {}
00025
00026 ValueEditControl(const std::string& n, const std::string& p, T* t) : StringInputControl(n,p), target(t), cur(), copies() {}
00027
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
00030 ValueEditControl(const ValueEditControl<T>& vec) : StringInputControl(vec), target(vec.target), cur(vec.cur), copies(vec.copies) {}
00031
00032 ValueEditControl operator=(const ValueEditControl<T>& vec) { StringInputControl::operator=(vec); target=vec.target; cur=vec.cur; copies=vec.copies; return *this; }
00033
00034 virtual ~ValueEditControl() {}
00035
00036
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
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
00056 virtual void refresh() {
00057
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
00070 virtual void pause() {
00071 erouter->addListener(this,Controller::nextItem);
00072 erouter->addListener(this,Controller::prevItem);
00073
00074 StringInputControl::pause();
00075 }
00076
00077
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
00084
00085
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
00094 virtual ControlBase * doNextItem() {
00095 cur=(T)(cur+1);
00096 refresh();
00097 return this;
00098 }
00099
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
00113
00114 virtual T* getTarget() const { return target; }
00115 virtual ValueEditControl& setTarget(T* t) { target=t; return *this; }
00116
00117
00118
00119
00120 virtual std::vector<T*>& getCopies() { return copies; }
00121 virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; }
00122
00123
00124
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;
00133 T cur;
00134 std::vector<T*> copies;
00135 };
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 #endif