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 cout << "*** WARNING ValueEditControl got an unasked for event" << endl;
00045 break;
00046 }
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
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
00061 StringInputControl::pause();
00062 }
00063
00064
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
00071
00072
00073
00074 cout << getName() << " set to " << *target << endl;
00075 }
00076 return NULL;
00077 }
00078
00079 virtual ControlBase * doNextItem() {
00080 cur++;
00081 refresh();
00082 return this;
00083 }
00084
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
00097
00098 virtual T* getTarget() const { return target; }
00099 virtual ValueEditControl& setTarget(T* t) { target=t; return *this; }
00100
00101
00102
00103
00104 virtual std::vector<T*>& getCopies() { return copies; }
00105 virtual ValueEditControl& addCopy(T* t) { copies.push_back(t); return *this; }
00106
00107
00108
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;
00117 T cur;
00118 std::vector<T*> copies;
00119 };
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 #endif