JK2eA
 All Classes Functions Variables
ControllerGraph.h
00001 #pragma once
00002 
00003 #ifdef _JK2_VERBOSE
00004 #include <JK2lib.h>
00005 #endif
00006 
00007 #include <RandomNum.h>
00008 #include "Controller.h"
00009 #include "CurveGraph.h"
00010 #include "Observable.h"
00011 
00017 template<class T>
00018 class CControllerGraph : public CController {
00019 private:
00020         T*                          m_pParameter;       
00021         CCurveGraph<T>* m_pGraph;               
00022 
00023 public:         
00024 
00031         CControllerGraph(T* parameter, CCurveGraph<T>* graph, const std::string& name);
00036         CControllerGraph(const std::string& name);
00040         virtual ~CControllerGraph();
00045         void Assign(CCurveGraph<T>* graph);
00050         void Connect(T* parameter);
00055         inline T* GetParameter() { return m_pParameter; }
00062         virtual void Update(float dt);
00063         
00064 
00065         CNTRL_CLASS_NAME(CControllerGraph);
00066 };
00067 
00068 template<class T>
00069 CControllerGraph<T>::CControllerGraph(T* parameter, CCurveGraph<T>* graph, const std::string& name) :
00070 CController(name)
00071 {
00072 #ifdef _JK2_VERBOSE
00073         LOGMsg("Creating Controller - %s (%s)", m_sName.c_str(), GetCntrlType());
00074 #endif
00075         Assign(graph);
00076         Connect(parameter);
00077 }
00078 template<class T>
00079 CControllerGraph<T>::CControllerGraph(const std::string& name) : CController(name)
00080 {
00081 #ifdef _JK2_VERBOSE
00082         LOGMsg("Creating Controller - %s (%s)", m_sName.c_str(), GetCntrlType());
00083 #endif
00084         m_pGraph = NULL;
00085         m_pParameter = NULL;
00086 }
00087 
00088 template<class T>
00089 CControllerGraph<T>::~CControllerGraph()
00090 {
00091         SAFE_DELETE(m_pGraph);
00092 #ifdef _JK2_VERBOSE
00093         LOGMsg("Destroying Controller - %s (%s)", m_sName.c_str(), GetCntrlType());
00094 #endif
00095 }
00096 
00097 template<class T>
00098 void CControllerGraph<T>::Assign(CCurveGraph<T>* graph)
00099 {
00100         m_pGraph = graph;
00101 }
00102 
00103 template<class T>
00104 void CControllerGraph<T>::Connect(T* parameter)
00105 {
00106         m_pParameter = parameter;
00107 }
00108 
00109 template<class T>
00110 void CControllerGraph<T>::Update(float dt)
00111 {
00112         if(m_pParameter==NULL) return;
00113         if(m_pGraph==NULL) return;
00114 
00115         CNTRL_SKIP_UPDATE()
00116 
00117 
00118         m_fLocalTime += dt * m_fTimeScale;
00119 
00120         if(m_pGraph->IsEnd(m_fLocalTime)) {
00121                 switch(m_eORT) {
00122                 case ORT_LOOP:
00123                         Reset(); break;
00124                 case ORT_RANDOM:
00125                         m_fLocalTime = CRandomNum::UniformF(0.0, m_pGraph->GetTotalTime()); break;
00126                 case ORT_FINISH:
00127                         m_eState = CLS_FINISHED; return;
00128                 }
00129         }
00130 
00131         // najdi node (keyframe) ktery odpovida tomuto casu
00132 
00133         *m_pParameter = m_pGraph->Interpolate(m_fLocalTime);
00134         
00135         //LOGMsg("Controller %s :: Update(%f) totalTime %f -> param", m_sName.c_str(), dt, m_fLocalTime); 
00136 }