00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CScrollXZCamCtrl.h"
00014 #include "CCamera.h"
00015 #include "CEngineFactory.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024 CScrollXZCamCtrl::CScrollXZCamCtrl(float fScrollFrame, CAABox3f scrollArea, float fSpeed,
00025 const TSmartPointer<CTimer> &pTimer, word wInputPriority)
00026 {
00027 assert((bool) pTimer && "Error: Timer is NULL in CScrollXZCamCtrl::CScrollXZCamCtrl");
00028
00029 m_fScrollFrame = fScrollFrame;
00030 m_ScrollArea = scrollArea;
00031 m_fSpeed = fSpeed;
00032
00033 m_pTimer = pTimer;
00034 m_pCamera = NULL;
00035 m_bForward = false;
00036 m_bBackward = false;
00037 m_bLeft = false;
00038 m_bRight = false;
00039
00040 SetPriority(wInputPriority);
00041 }
00042
00043 void CScrollXZCamCtrl::Enable(const TSmartPointer<CCamera> &pCamera)
00044 {
00045 if(!IsEnabled())
00046 {
00047 CEngineFactory::InstancePtr()->GetInputMgr()->RegisterHandler(this);
00048 m_bForward = false;
00049 m_bBackward = false;
00050 m_bLeft = false;
00051 m_bRight = false;
00052 }
00053 ICamCtrl::Enable(pCamera);
00054
00055
00056 pCamera->SetPos(CVector3f(m_ScrollArea.SizeX() / 2.0f, 50, m_ScrollArea.SizeZ() / 2.0f));
00057 pCamera->SetRot(CVector3f(3.0f*MY_PI/8.0f, 0.0f, 0.0f));
00058 }
00059
00060 void CScrollXZCamCtrl::Update()
00061 {
00062 if(IsEnabled())
00063 {
00064 word wWidth, wHeight;
00065 CEngineFactory::InstancePtr()->GetGuiMgr()->GetVScreenSize(wWidth, wHeight);
00066 CVector2f vPos = CEngineFactory::InstancePtr()->GetGuiMgr()->GetCursorPos();
00067 CVector3f vDir;
00068 float dt = (float) m_pTimer->GetFrameDuration();
00069 if(m_bLeft || (vPos.m_X <= m_fScrollFrame && vPos.m_X >= 0))
00070 {
00071
00072 vDir = -m_pCamera->GetRight();
00073 vDir.m_Y = 0.0f;
00074 if(vDir.m_X != 0.0f || vDir.m_Z != 0.0f)
00075 {
00076 vDir.Normalize();
00077 m_pCamera->Move(vDir * m_fSpeed * dt);
00078 }
00079 }
00080 if(m_bRight || (((float) wWidth) - vPos.m_X <= m_fScrollFrame + 1 && vPos.m_X <= wWidth))
00081 {
00082
00083 vDir = m_pCamera->GetRight();
00084 vDir.m_Y = 0;
00085 if(vDir.m_X != 0.0f || vDir.m_Z != 0.0f)
00086 {
00087 vDir.Normalize();
00088 m_pCamera->Move(vDir * m_fSpeed * dt);
00089 }
00090 }
00091
00092 if(m_bForward || (vPos.m_Y <= m_fScrollFrame && vPos.m_Y >= 0))
00093 {
00094
00095 vDir = m_pCamera->GetUp();
00096 vDir.m_Y = 0;
00097 if(vDir.m_X != 0.0f || vDir.m_Z != 0.0f)
00098 {
00099 vDir.Normalize();
00100 m_pCamera->Move(vDir * m_fSpeed * dt);
00101 }
00102 }
00103 if(m_bBackward || (((float) wHeight) - vPos.m_Y <= m_fScrollFrame + 1 && vPos.m_Y <= wHeight))
00104 {
00105
00106 vDir = -m_pCamera->GetUp();
00107 vDir.m_Y = 0;
00108 if(vDir.m_X != 0.0f || vDir.m_Z != 0.0f)
00109 {
00110 vDir.Normalize();
00111 m_pCamera->Move(vDir * m_fSpeed * dt);
00112 }
00113 }
00114
00115 vDir = m_pCamera->GetPos();
00116 if(vDir.m_X < m_ScrollArea.m_MinX)
00117 {
00118 vDir.m_X = m_ScrollArea.m_MinX;
00119 }
00120 else if(vDir.m_X > m_ScrollArea.m_MaxX)
00121 {
00122 vDir.m_X = m_ScrollArea.m_MaxX;
00123 }
00124
00125 if(vDir.m_Y < m_ScrollArea.m_MinY)
00126 {
00127 vDir.m_Y = m_ScrollArea.m_MinY;
00128 }
00129 else if(vDir.m_Y > m_ScrollArea.m_MaxY)
00130 {
00131 vDir.m_Y = m_ScrollArea.m_MaxY;
00132 }
00133
00134 if(vDir.m_Z < m_ScrollArea.m_MinZ)
00135 {
00136 vDir.m_Z = m_ScrollArea.m_MinZ;
00137 }
00138 else if(vDir.m_Z > m_ScrollArea.m_MaxZ)
00139 {
00140 vDir.m_Z = m_ScrollArea.m_MaxZ;
00141 }
00142
00143 m_pCamera->SetPos(vDir);
00144
00145 }
00146 }
00147
00148 void CScrollXZCamCtrl::Disable()
00149 {
00150 if(IsEnabled())
00151 {
00152 CEngineFactory::InstancePtr()->GetInputMgr()->UnregisterHandler(this);
00153 }
00154 ICamCtrl::Disable();
00155 m_bForward = false;
00156 m_bBackward = false;
00157 m_bLeft = false;
00158 m_bRight = false;
00159 }
00160
00161 CScrollXZCamCtrl::~CScrollXZCamCtrl()
00162 {
00163 m_pTimer = NULL;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173 bool CScrollXZCamCtrl::OnKeyDown(dword dwKey)
00174 {
00175 if(!IsEnabled())
00176 return false;
00177
00178 switch(dwKey)
00179 {
00180 case cg_dwCursorUp:
00181 m_bForward = true;
00182 return true;
00183 case cg_dwCursorDown:
00184 m_bBackward = true;
00185 return true;
00186 case cg_dwCursorLeft:
00187 m_bLeft = true;
00188 return true;
00189 case cg_dwCursorRight:
00190 m_bRight = true;
00191 return true;
00192 }
00193
00194 return false;
00195
00196 }
00197
00198 bool CScrollXZCamCtrl::OnKeyUp(dword dwKey)
00199 {
00200 if(!IsEnabled())
00201 return false;
00202
00203 switch(dwKey)
00204 {
00205 case cg_dwCursorUp:
00206 m_bForward = false;
00207 return true;
00208 case cg_dwCursorDown:
00209 m_bBackward = false;
00210 return true;
00211 case cg_dwCursorLeft:
00212 m_bLeft = false;
00213 return true;
00214 case cg_dwCursorRight:
00215 m_bRight = false;
00216 return true;
00217 }
00218
00219 return false;
00220 }