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 <vector>
00009 #include "Motion/MotionManager.h"
00010 #include "Events/EventRouter.h"
00011 #include "Shared/WorldState.h"
00012 #include <sstream>
00013
00014
00015 template< class T >
00016 class ValueEditControl : public StringInputControl, public EventListener {
00017 public:
00018
00019 ValueEditControl(const std::string& n, T* t) : StringInputControl(n,"Please enter a new value for "+n), target(t), cur(), copies() {}
00020
00021 ValueEditControl(const std::string& n, const std::string& p, T* t) : StringInputControl(n,p), target(t), cur(), copies() {}
00022
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
00025 ValueEditControl(const ValueEditControl<T>& vec) : StringInputControl(vec), target(vec.target), cur(vec.cur), copies(vec.copies) {}
00026
00027 ValueEditControl operator=(const ValueEditControl<T>& vec) { StringInputControl::operator=(vec); target=vec.target; cur=vec.cur; copies=vec.copies; return *this; }
00028
00029 virtual ~ValueEditControl() {}
00030
00031
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
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
00043 default:
00044 serr->printf("*** WARNING ValueEditControl got an unasked for event\n");
00045 break;
00046 }
00047 }
00048
00049 virtual void refresh() {
00050
00051 if(gui_comm!=NULL && wireless->isConnected(gui_comm->sock)) {
00052 std::stringstream ss;
00053 ss << getName();
00054 if(cur!=*target)
00055 ss << ": " << cur;
00056 sout->printf("%s\n",ss.str().c_str());
00057 }
00058
00059 StringInputControl::refresh();
00060 }
00061
00062
00063 virtual void pause() {
00064 erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadFrButSID,EventBase::deactivateETID,0));
00065 erouter->addListener(this,EventBase(EventBase::buttonEGID,ButtonSourceID::HeadBkButSID,EventBase::deactivateETID,0));
00066
00067 StringInputControl::pause();
00068 }
00069
00070
00071 virtual ControlBase * doSelect() {
00072 if(*target!=cur) {
00073 *target=cur;
00074 for(typename std::vector<T*>::iterator it=copies.begin(); it!=copies.end(); it++)
00075 **it=cur;
00076
00077
00078
00079
00080 std::stringstream ss;
00081 ss << getName() << " set to " << *target;
00082 sout->printf("%s\n",ss.str().c_str());
00083 }
00084 return NULL;
00085 }
00086
00087 virtual ControlBase * doNextItem() {
00088 cur++;
00089 refresh();
00090 return this;
00091 }
00092
00093 virtual ControlBase * doPrevItem() {
00094 cur--;
00095 refresh();
00096 return this;
00097 }
00098
00099 virtual ControlBase * takeInput(const std::string& str) {
00100 cur = (T)atof(str.c_str());
00101 return doSelect();
00102 }
00103
00104
00105
00106 virtual T* getTarget() const { return target; }
00107 virtual ValueEditControl& setTarget(T* t) { target=t; return *this; }
00108
00109
00110
00111
00112 virtual std::vector<T*>& getCopies() { return copies; }
00113 virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; }
00114
00115
00116
00117 virtual std::string getName() const {
00118 std::stringstream ss;
00119 ss << StringInputControl::getName() << " (" << *target << ")";
00120 return ss.str();
00121 }
00122
00123 protected:
00124 T* target;
00125 T cur;
00126 std::vector<T*> copies;
00127 };
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 #endif