Klamp't  0.9.0
GenericGUI.h
1 #ifndef GENERIC_GUI_H
2 #define GENERIC_GUI_H
3 
4 #include <KrisLibrary/utils/AnyCollection.h>
5 #include <KrisLibrary/math3d/primitives.h>
6 #include <string>
7 #include <sstream>
8 
9 namespace Klampt {
10  using namespace std;
11 
12 class GenericGUIBase;
13 class GenericBackendBase;
14 
28 {
29  public:
31  virtual ~GenericGUIBase() {}
33  virtual void Run();
37  virtual bool ProcessMessage(const AnyCollection& msg);
39  virtual bool SendMessage(const AnyCollection& msg);
40 
44  void AddRule(const AnyCollection& inschema,const AnyCollection& outschema,bool mapWildcardStrings=true);
46  void AddCommandRule(const AnyCollection& inschema,const string& cmd,const string& args,bool mapWildcardStrings=true);
47  bool LoadRules(const char* fn);
48  bool LoadRules(istream& in);
49 
50  virtual bool OnQuit() { return false; }
51  virtual bool OnCommand(const string& cmd,const string& args) { return false; }
52  virtual bool OnNotify(const string& text,const string& msglevel) { return false; }
53  virtual bool OnPauseIdle(double secs) { return false; }
54  virtual bool OnRefresh() { return false; }
55  virtual bool OnResize(int w,int h) { return false; }
56  virtual bool OnDrawText(double x, double y, double z, const std::string &text, int height) { return false; }
57  virtual bool OnDrawText(int x,int y, const std::string &text, int height) { return false; }
58 
59  bool SendIdle();
60  bool SendGLRender();
61  bool SendGLViewport(int x,int y,int w,int h);
62  bool SendCommand(const string& cmd,const string& args);
63  bool SendCommand(const string& cmd);
64  template <class T>
65  bool SendCommand(const string& cmd,const T& arg1);
66  template <class T1,class T2>
67  bool SendCommand(const string& cmd,const T1& arg1,const T2& arg2);
68  template <class T1,class T2,class T3>
69  bool SendCommand(const string& cmd,const T1& arg1,const T2& arg2,const T3& arg3);
70  bool SendButtonPress(const string& widget);
71  bool SendButtonToggle(const string& widget,int checked);
72  bool SendWidgetValue(const string& widget,const string& value);
73  bool SendMouseClick(int button,int state,int mx,int my);
74  bool SendMouseMove(int mx,int my);
75  bool SendMouseWheel(int dwheel);
76  bool SendScroll(int dy);
77  bool SendKeyDown(const string& key);
78  bool SendKeyUp(const string& key);
79  bool SendSpaceball(const Math3D::RigidTransform& T);
80  bool SendDevice(const string& name,const string& data);
81 
82  GenericBackendBase* backend;
83  vector<pair<AnyCollection,AnyCollection> > rules;
84 };
85 
87 {
88  public:
90  virtual ~GenericBackendBase() {}
92  virtual void Start() {}
93  virtual void Stop() {}
96  virtual bool ProcessMessage(const AnyCollection& msg);
98  virtual bool SendMessage(const AnyCollection& msg) { return gui->ProcessMessage(msg); }
99 
100  //"live" button mappings
101  void MapButtonPress(const string& button,int* var);
102  void MapButtonToggle(const string& button,int* var);
103  void MapWidgetValue(const string& button,string* var);
104  void MapKeyToggle(const string& key,int* var);
105 
106  virtual bool OnIdle() { SendPauseIdle(); return true; }
107  virtual bool OnGLRender() { return false; }
108  virtual bool OnGLViewport(int x,int y,int w, int h) { return false; }
109  virtual bool OnCommand(const string& cmd,const string& args) { return false; }
110  virtual bool OnButtonPress(const string& button);
111  virtual bool OnButtonToggle(const string& button,int checked);
112  virtual bool OnWidgetValue(const string& widget,const string& value);
113  virtual bool OnMouseClick(int button,int state,int mx,int my) { return false; }
114  virtual bool OnMouseMove(int mx,int my) { return false; }
115  virtual bool OnMouseWheel(int dwheel) { return false; }
116  virtual bool OnScroll(int dy) { return false; }
117  virtual bool OnKeyDown(const string& key);
118  virtual bool OnKeyUp(const string& key);
119  virtual bool OnSpaceball(const Math3D::RigidTransform& T) { return false; }
120  virtual bool OnDevice(const string& name,const string& data) { return false; }
121 
122  bool SendQuit();
123  bool SendCommand(const string& cmd,const string& args);
124  bool SendNotify(const string& text,const string& msglevel="");
125  bool SendError(const string& text) { return SendNotify(text,"error"); }
126  bool SendWarning(const string& text) { return SendNotify(text,"warning"); }
127  bool SendPauseIdle(double secs=1e300);
128  bool SendRefresh();
129  bool SendResize(int w,int h);
130  bool SendDrawText(double x, double y, double z, const std::string &text, int height = 10);
131  bool SendDrawText(int x, int y, const std::string &text, int height = 10);
132 
133  GenericGUIBase* gui;
134  map<string,int*> liveButtonPresses;
135  map<string,int*> liveButtonToggles;
136  map<string,string*> liveWidgetValues;
137  map<string,int*> liveKeys;
138 };
139 
155 {
156  public:
157  void Add(std::shared_ptr<GenericBackendBase>& backend);
158  void SetDefaultBroadcasts();
159  void SetBroadcast(const string& msgtype) { targets[msgtype]="all"; }
160  virtual bool ProcessMessage(const AnyCollection& msg);
161 
162  vector<std::shared_ptr<GenericBackendBase> > children;
163  map<string,string> targets;
164 };
165 
166 
171 #define DISPATCHBEGIN() \
172  bool res; \
173  string type; \
174  res = msg["type"].as<string>(type); \
175  if(!res) { \
176  cerr<<"Message doesn't contain type"<<endl; \
177  return false; \
178  } \
179 
180 #define DISPATCHEND() \
181  else { \
182  cerr<<"Unhandled message type "<<type<<endl; \
183  return false; \
184  }
185 
186 
187 #define DISPATCH0(msgtype,func) \
188  else if(type == msgtype) \
189  return func();
190 
191 #define DISPATCH1(msgtype,arg1,func) \
192  else if(type == msgtype) \
193  return func(msg[#arg1]);
194 
195 
196 #define DISPATCH2(msgtype,arg1,arg2,func) \
197  else if(type == msgtype) \
198  return func(msg[#arg1],msg[#arg2]);
199 
200 #define DISPATCH3(msgtype,arg1,arg2,arg3,func) \
201  else if(type == msgtype) \
202  return func(msg[#arg1],msg[#arg2],msg[#arg3]);
203 
204 #define DISPATCH4(msgtype,arg1,arg2,arg3,arg4,func) \
205  else if(type == msgtype) \
206  return func(msg[#arg1],msg[#arg2],msg[#arg3],msg[#arg4]);
207 
208 #define DISPATCH5(msgtype,arg1,arg2,arg3,arg4,arg5,func) \
209  else if(type == msgtype) \
210  return func(msg[#arg1],msg[#arg2],msg[#arg3],msg[#arg4],msg[#arg5]);
211 
212 
217 #define SEND0(msgtype)\
218  AnyCollection msg; \
219  msg["type"] = string(msgtype); \
220  return SendMessage(msg);
221 
222 #define SEND1(msgtype,arg1) \
223  AnyCollection msg; \
224  msg["type"] = string(msgtype); \
225  msg[#arg1] = arg1; \
226  return SendMessage(msg);
227 
228 #define SEND2(msgtype,arg1,arg2) \
229  AnyCollection msg; \
230  msg["type"] = string(msgtype); \
231  msg[#arg1] = arg1; \
232  msg[#arg2] = arg2; \
233  return SendMessage(msg);
234 
235 #define SEND3(msgtype,arg1,arg2,arg3) \
236  AnyCollection msg; \
237  msg["type"] = string(msgtype); \
238  msg[#arg1] = arg1; \
239  msg[#arg2] = arg2; \
240  msg[#arg3] = arg3; \
241  return SendMessage(msg);
242 
243 #define SEND4(msgtype,arg1,arg2,arg3,arg4) \
244  AnyCollection msg; \
245  msg["type"] = string(msgtype); \
246  msg[#arg1] = arg1; \
247  msg[#arg2] = arg2; \
248  msg[#arg3] = arg3; \
249  msg[#arg4] = arg4; \
250  return SendMessage(msg);
251 
252 #define SEND5(msgtype,arg1,arg2,arg3,arg4,arg5) \
253  AnyCollection msg; \
254  msg["type"] = string(msgtype); \
255  msg[#arg1] = arg1; \
256  msg[#arg2] = arg2; \
257  msg[#arg3] = arg3; \
258  msg[#arg4] = arg4; \
259  msg[#arg5] = arg5; \
260  return SendMessage(msg);
261 
262 
263 template <class T>
264 bool GenericGUIBase::SendCommand(const string& cmd,const T& arg1)
265 {
266  stringstream ss;
267  ss<<arg1;
268  return this->SendCommand(cmd,ss.str());
269 }
270 
271 template <class T1,class T2>
272 bool GenericGUIBase::SendCommand(const string& cmd,const T1& arg1,const T2& arg2)
273 {
274  stringstream ss;
275  ss<<arg1<<" "<<arg2;
276  return this->SendCommand(cmd,ss.str());
277 }
278 
279 template <class T1,class T2,class T3>
280 bool GenericGUIBase::SendCommand(const string& cmd,const T1& arg1,const T2& arg2,const T3& arg3)
281 {
282  stringstream ss;
283  ss<<arg1<<" "<<arg2<<" "<<arg3<<endl;
284  return this->SendCommand(cmd,ss.str());
285 }
286 
287 } //namespace Klampt
288 
289 #endif
virtual void Start()
Default implementation of following do nothing.
Definition: GenericGUI.h:92
virtual bool SendMessage(const AnyCollection &msg)
Send a message to the gui.
Definition: GenericGUI.h:98
Definition: GenericGUI.h:86
A base class for a GUI frontend. Performs message passing to the backend in the easily serializable A...
Definition: GenericGUI.h:27
Definition: GenericGUI.h:154
Definition: ContactDistance.h:6