00001 #ifndef Impala_Visualization_AppController_h 00002 #define Impala_Visualization_AppController_h 00003 00004 #include <vector> 00005 00006 #include "Basis/ILog.h" 00007 #include "Basis/Timer.h" 00008 #include "OglGui/Window.h" 00009 #include "Visualization/AppControl.h" 00010 00011 namespace Impala { 00012 namespace Visualization { 00013 00014 class AppController : public OglGui::KeyListener 00015 { 00016 public: 00017 00018 ~AppController() 00019 { 00020 } 00021 00022 static AppController& 00023 Instance() 00024 { 00025 static AppController theController; 00026 return theController; 00027 } 00028 00029 void 00030 AddControl(AppControl* ctr, bool forceCurrent = false) 00031 { 00032 int ctrId = ctr->GetControlId(); 00033 ILOG_DEBUG("AddControl: id = " << ctr->GetControlId() << 00034 ", always active = " << ctr->GetAlwaysActive()); 00035 mControls.push_back(ctr); 00036 if (!mCurControl || forceCurrent) 00037 { 00038 mCurControl = ctr; 00039 mCurControl->SetActive(true); 00040 } 00041 } 00042 00043 void 00044 SetCurrentControl(int id) 00045 { 00046 AppControl* ctr = GetControl(id); 00047 if (ctr == mCurControl) 00048 { 00049 if (mCurControl && mStarted) 00050 mCurControl->HandleActivate(); 00051 return; 00052 } 00053 if (mCurControl) 00054 { 00055 mCurControl->HandleDeactivate(); 00056 mCurControl->SetActive(false); 00057 } 00058 mCurControl = ctr; 00059 if (mCurControl) 00060 { 00061 mCurControl->SetActive(true); 00062 mCurControl->HandleActivate(); 00063 } 00064 } 00065 00066 virtual int 00067 MainLoop() 00068 { 00069 00070 if (mControls.size() == 0) 00071 return 0; 00072 for (int s=0 ; s<mControls.size() ; s++) 00073 { 00074 mControls[s]->HandleStart(); 00075 if (mControls[s]->GetActive()) 00076 mControls[s]->HandleActivate(); 00077 } 00078 mTimer = new Timer(1); 00079 #ifndef NO_EVENT_LOOP 00080 OglEventLoop(); 00081 #endif 00082 return 1; 00083 /* 00084 #ifdef OGL_USING_GLUT 00085 ILOG_DEBUG("MainLoop: GLUT mode. Setting up glutIdleFunc() ..."); 00086 glutIdleFunc(AppController::DoProcessEvents); 00087 ILOG_DEBUG("MainLoop: Starting OglMainLoop()"); 00088 OglEventLoop(); 00089 return 1; 00090 #else 00091 ILOG_DEBUG("MainLoop: GLAUX mode."); 00092 bool done = GetAllDone(); 00093 while (!done) 00094 { 00095 for (int i=0 ; i<mControls.size() ; i++) 00096 { 00097 if (mControls[i]->GetActive()) 00098 mControls[i]->ComputeCycle(mTimer->SplitTime()); 00099 } 00100 done = (OglGui::Sys::Instance().WindowManage() == 1); 00101 if (!done) 00102 done = GetAllDone(); 00103 } 00104 return 1; 00105 #endif 00106 */ 00107 } 00108 00109 // required for GLUT 00110 void 00111 ProcessEvents() 00112 { 00113 bool done = GetAllDone(); 00114 if (!done) 00115 { 00116 for (int i=0 ; i<mControls.size() ; i++) 00117 { 00118 if (mControls[i]->GetActive()) 00119 mControls[i]->ComputeCycle(mTimer->SplitTime()); 00120 } 00121 done = GetAllDone(); 00122 } 00123 if (done) 00124 exit(0); 00125 } 00126 00127 static void 00128 DoProcessEvents() 00129 { 00130 AppController::Instance().ProcessEvents(); 00131 } 00132 00133 // specialization of base class 00134 00135 virtual void 00136 KeyEvent(OglGui::Window* src, int c, int state) 00137 { 00138 ILOG_DEBUG("KeyEvent: " << c); 00139 for (int i=0 ; i<mControls.size() ; i++) 00140 if (mControls[i]->GetActive()) 00141 mControls[i]->KeyEvent(src, c, state); 00142 } 00143 00144 private: 00145 00146 AppController() 00147 { 00148 OglGui::Window::SetGlobalKeyListener(this); 00149 mCurControl = 0; 00150 mStarted = false; 00151 } 00152 00153 AppController(const AppController&) 00154 { 00155 } 00156 00157 AppController& 00158 operator=(const AppController&); 00159 00160 AppControl* 00161 GetControl(int id) 00162 { 00163 int sz = mControls.size(); 00164 for (int i=0 ; i<mControls.size() ; i++) 00165 if (mControls[i]->GetControlId() == id) 00166 return mControls[i]; 00167 ILOG_DEBUG("GetControl: no control " << id); 00168 return 0; 00169 } 00170 00171 bool 00172 GetAllDone() 00173 { 00174 for (int i=0 ; i<mControls.size() ; i++) 00175 if (mControls[i]->GetActive() && mControls[i]->Done()) 00176 return true; 00177 return false; 00178 } 00179 00180 AppControl* mCurControl; 00181 std::vector<AppControl*> mControls; 00182 bool mStarted; 00183 Timer* mTimer; 00184 00185 ILOG_VAR_DEC; 00186 }; 00187 00188 ILOG_VAR_INIT(AppController, Visualization); 00189 00190 } // namespace Visualization 00191 } // namespace Impala 00192 00193 #endif