00001 #ifndef _RR_CONSOLE_H_
00002 #define _RR_CONSOLE_H_
00003
00004 #include <list>
00005 #include <string>
00006
00007 #include <vrs/callback.h>
00008 #include <vrs/container/dictionary.h>
00009 #include <vrs/sg/scenething.h>
00010 #include <vrs/sharedobj.h>
00011
00012 #include <random_utils/TimeableObject.h>
00013
00014 namespace VRS
00015 {
00016 class BehaviorCallback;
00017 class Camera;
00018 class Canvas;
00019 class Font;
00020 class KeyEvent;
00021 class ResizeEvent;
00022 class Text;
00023 class Translation;
00024 }
00025
00026
00027 namespace random_racer
00028 {
00029
00035 class Console : public VRS::SharedObj,
00036 private random_utils::TimeableObject
00037 {
00041 class TextBuffer : public VRS::SceneThing
00042 {
00043 int m_maxLines;
00044 double m_lineHeight;
00045 VRS::Vector m_startOffset;
00046
00047 public:
00048 TextBuffer(int maxLines, const VRS::Vector& startOffset,
00049 double lineHeight);
00050
00051 void addLine(VRS::SO<VRS::Text> newLine);
00052 void addLine(const std::string& newLine);
00053
00054 inline int maxLines() { return m_maxLines; }
00055 inline int numLines() { return objects(); }
00056 };
00057
00058 enum Keys
00059 {
00060 BackSpaceKey = 8,
00061 DeleteKey = 127,
00062 DeleteLineKey = 21,
00063 EnterKey = 3,
00064 ReturnKey = 13,
00065 UpKey = 65297,
00066 DownKey = 65296,
00067 LeftKey = 65298,
00068 RightKey = 65299,
00069 TabKey = '\t',
00070 ShiftTabKey = 25
00071 };
00072
00073
00074
00075 static const float c_marginY, c_marginX, c_pixelsPerChar;
00076
00081 VRS::SO<VRS::SceneThing> m_sceneThing;
00082
00086 VRS::SO<VRS::Font> m_font;
00087
00092 VRS::SO<VRS::Text> m_currentLine;
00093
00097 VRS::SO<TextBuffer> m_textBuffer;
00098
00102 VRS::SO<VRS::Canvas> m_canvas;
00103
00107 VRS::SO<VRS::BehaviorCallback> m_eventCallback;
00108
00112 VRS::SO<VRS::Camera> m_cam;
00113
00117 std::list<std::string> m_history;
00118
00122 std::list<std::string>::iterator m_histIter;
00123
00127 std::string m_currentTabWord;
00128
00133 std::list<std::string> m_tabList;
00134
00139 std::list<std::string>::iterator m_tabIter;
00140
00145 bool m_startNewTabCompletition;
00146
00151 bool m_lastTabWasForward;
00152
00156 VRS::NonPersistentDictionary<std::string, VRS::SO<VRS::CallbackBase> >
00157 m_registry;
00158
00159
00164 static const unsigned int c_animationDuration;
00165
00170 double m_animationDistance;
00171
00176 double m_distancePerTick;
00177
00182 unsigned int m_lastTick;
00183
00187 bool m_animationInProgress;
00188
00192 void startAnimation();
00193
00197 VRS::SO<VRS::Translation> m_translation;
00198
00199
00203 static VRS::SO<Console> s_instance;
00204
00208 Console();
00209
00213 bool dispatchCommand(const std::string&, bool batchMode = false);
00214
00219 void handleEvents();
00220
00225 void resizeEvent(VRS::SO<VRS::ResizeEvent> event);
00226
00230 void keyEvent(VRS::SO<VRS::KeyEvent> event);
00231
00236 virtual void timerTick();
00237
00238 public:
00243 static VRS::SO<Console> get();
00244
00250 inline VRS::SO<VRS::SceneThing> sceneThing() { return m_sceneThing; }
00251
00255 inline VRS::SO<VRS::Canvas> canvas() { return m_canvas; }
00256
00265 void setCanvas(VRS::SO<VRS::Canvas> canvas);
00266
00273 void attach(VRS::SO<VRS::Canvas> canvas);
00274
00279 void detach();
00280
00285 inline VRS::SO<VRS::Font> font() { return m_font; }
00286
00290 bool isVisible();
00291
00295 inline bool isHidden() { return !isVisible(); }
00296
00300 inline void addLine(const std::string& p_newLine)
00301 {
00302 m_textBuffer->addLine(p_newLine);
00303 }
00304
00309 inline bool registerCommand(
00310 const std::string& p_name,
00311 VRS::SO<VRS::Callback> p_callBack)
00312 {
00313 return m_registry.insert(p_name, (VRS::CallbackBase*)p_callBack);
00314 }
00315
00321 inline bool registerCommand(
00322 const std::string& p_name,
00323 VRS::SO<VRS::Callback1<const std::string&> > p_callBack)
00324 {
00325 return m_registry.insert(p_name, (VRS::CallbackBase*)p_callBack);
00326 }
00327
00334 inline bool registerCommand(
00335 const std::string& p_name,
00336 VRS::SO<VRS::Callback2<const std::string&,const std::string&> > p_callBack)
00337 {
00338 return m_registry.insert(p_name, (VRS::CallbackBase*)p_callBack);
00339 }
00340
00344 inline bool unregisterCommand(const std::string& p_name)
00345 {
00346 return m_registry.erase(p_name);
00347 }
00348
00353 bool toggleVisibility();
00354
00358 void hide();
00359
00363 void show();
00364
00368 void handleKey(unsigned int key);
00369
00375 void historyUp(bool reallyUp = true);
00376
00380 inline void historyDown()
00381 {
00382 historyUp(false);
00383 }
00384
00390 void tabCompleteForward(bool forward = true);
00391
00395 inline void tabCompleteBackward()
00396 {
00397 tabCompleteForward(false);
00398 }
00399
00407 void autoexec();
00408 };
00409
00410
00411 }
00412
00413
00414 #endif // _RR_CONSOLE_H_