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