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