📄 chvrmltransition.cpp
字号:
/*----------------------------------------------------------------------------
_ _ _
/\ | | | (_)
/ \ _ __ __| |_ __ ___ _ __ ___ ___ __| |_ __ _
/ /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
/ ____ \| | | | (_| | | | (_) | | | | | | __/ (_| | | (_| |
/_/ \_\_| |_|\__,_|_| \___/|_| |_| |_|\___|\__,_|_|\__,_|
The contents of this file are subject to the Andromedia Public
License Version 1.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.andromedia.com/APL/
Software distributed under the License is distributed on an
"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is Pueblo client code, released November 4, 1998.
The Initial Developer of the Original Code is Andromedia Incorporated.
Portions created by Andromedia are Copyright (C) 1998 Andromedia
Incorporated. All Rights Reserved.
Andromedia Incorporated 415.365.6700
818 Mission Street - 2nd Floor 415.365.6701 fax
San Francisco, CA 94103
Contributor(s):
--------------------------------------------------------------------------
Chaco team: Dan Greening, Glenn Crocker, Jim Doubek,
Coyote Lussier, Pritham Shetty.
Wrote and designed original codebase.
------------------------------------------------------------------------------
Implementation for the ChVrmlStateTransition classes for navigation.
Note: The "crabbish" functions means take a step to the side, either left/right,
up or down. (Remember the song about "Crabs walk sideways, and lobsters walk straight"?)
----------------------------------------------------------------------------*/
// $Header: /home/cvs/chaco/modules/client/msw/ChGraphx/ChVrmlTransition.cpp,v 2.16 1996/07/08 21:30:37 jimd Exp $
#include "grheader.h"
#include "ChVrmlTransition.h"
#include "ChMaze.h"
#include "ChMazCam.h"
#include "ChRenderData.h"
#if 0
// An example of how to do a statically initialized STM
ChStateTransition::transition mySTM[1][1] =
{
{1, ChStateTransition::ChTransitionActionHandler(ChVrmlStateTransition::MyDefaultHandler)} //elt 0, 0
};
ChStateTransition::transition *ChVrmlStateTransition::pSTM = &(mySTM[0][0]);
//then add these to the class defn
static transition *pSTM;
virtual transition * GetMatrix() {return pSTM;};
#endif
#include <ChKeyMap.h>
#define ACTION_MOD_KEY_UP (1L << 4)
#define AddCell(i, j, k, cls, hdlr) \
SetCell(i, j, k, ChStateTransition::ChTransitionActionHandler(cls::hdlr))
const chint32 maxClickTicks = 500; // milliseconds for a 'Click' as opposed to a press
const chint32 maxClickFrames = 1; // number of rendered frames for a 'Click' as opposed to a press
const chint32 animMoveTowardsTicks = 2000; // milliseconds for animation in a MoveTowards (reg speed)
const float secondsPerRotation = 3.;
const float turnSlewRate = 360. / secondsPerRotation;
const float pitchSlewRate = 360. / secondsPerRotation;
const float spinThresholdSpeed = 2;
//-------------------------------------------------------------------------------------------
ChVrmlStateTransition::ChVrmlStateTransition(ChMazeWnd *pWnd) :
ChStateTransition(s_numStates, e_numEvents),
m_pWnd(pWnd),
m_lTime(0),
m_keyFlags(0),
m_look(0,0,-1)
{
m_pKeyMap = new ChKeyMap;
}
ChVrmlStateTransition::~ChVrmlStateTransition()
{
delete m_pKeyMap;
};
bool ChVrmlStateTransition::TranslateMouseEvent(int mouseMsg, int x, int y, chuint32 uFlags, int &event, chuint32 *&pData)
{
bool boolProcessed = true;
switch(mouseMsg)
{
case WM_LBUTTONDOWN:
{
event = ep_lbutton;
//TRACE("LButtonUp\n");
break;
}
case WM_RBUTTONDOWN:
{
event = ep_rbutton;
break;
}
case WM_LBUTTONUP:
{
event = er_lbutton;
//TRACE("LButtonUp\n");
break;
}
case WM_RBUTTONUP:
{
event = er_rbutton;
break;
}
case WM_MOUSEMOVE:
{
event = e_move;
break;
}
default:
{
boolProcessed = false;
}
}
if(boolProcessed)
{
chflag32 flMods = 0;
if (GetKeyState( VK_MENU ) & 0x8000)
{
flMods |= ACTION_MOD_ALT;
}
if (uFlags & MK_SHIFT)
{
flMods |= ACTION_MOD_SHIFT;
}
if (uFlags & MK_CONTROL)
{
flMods |= ACTION_MOD_CONTROL;
}
m_flags = flMods;
m_mouseFlags = uFlags;
m_ix = x;
m_iy = y;
}
return boolProcessed;
};
bool ChVrmlStateTransition::TranslateEvent(int iMsg, void *pMsgData, int &event, chuint32 *&pData)
{
if(iMsg == WM_TIMER)
{
event = e_tick;
return true;
}
return false;
};
bool ChVrmlStateTransition::Examine()
{
if ((m_iAnchorX != m_ix) || (m_iAnchorY != m_ix))
{
RECT r;
m_pWnd->GetClientRect(&r);
ChPoint ptLastMouse(m_iAnchorX, m_iAnchorY);
ChPoint ptMouse(m_ix, m_iy);
m_pWnd->GetCameraControl()->RotateBall(ptLastMouse, ptMouse, r.bottom - r.top);
m_iAnchorX = m_ix;
m_iAnchorY = m_iy;
m_mouseVelocity.set( ptMouse.x - ptLastMouse.x, ptMouse.y - ptLastMouse.y, 0.);
}
else
{
m_mouseVelocity.set(0.,0.,0.);
}
AccumMouseVelocity();
RecordTime();
return true;
}
bool ChVrmlStateTransition::ExamineStart()
{
RecordTime();
RecordPressTime();
m_iAnchorX = m_ix;
m_iAnchorY = m_iy;
m_mouseAvgVelocity.set(0.,0.,0.);
m_mouseVelocity.set(0.,0.,0.);
return true;
};
void ChVrmlStateTransition::AccumMouseVelocity()
{
// just a simple one tap iir filter to damp jiggle as mouse button
// is released.
const float mouseVelocityWeight = .2; // weight of 1st (only) filter tap
GxVec3f velocity = m_mouseVelocity;
velocity *= mouseVelocityWeight;
m_mouseAvgVelocity *= (1. - mouseVelocityWeight - .001); // ff guarantees stable decay
m_mouseAvgVelocity += velocity;
}
bool ChVrmlStateTransition::Spin()
{
RECT r;
m_pWnd->GetClientRect(&r);
ChPoint ptLastMouse(0, 0);
ChPoint ptMouse(int(m_mouseAvgVelocity.x()), m_mouseAvgVelocity.y());
m_pWnd->GetCameraControl()->RotateBall(ptLastMouse, ptMouse, r.bottom - r.top);
RecordTime();
return true;
}
bool ChVrmlStateTransition::SpinStart()
{
RecordTime();
return true;
};
void ChVrmlStateTransition::RecordTime()
{
// first record current velocity
const chint32 minTickTime = 33;
chint32 ticks = GetElapsedTicks();
//ticks = min(ticks, minTickTime);
GxVec3f loc = m_pWnd->GetCameraControl()->GetLoc();
GxVec3f dir = m_pWnd->GetCameraControl()->GetLookDir();
float secs = ticks / 1000.;
if(secs > 0.0)
{
m_velocity = loc;
m_velocity -= m_loc;
m_velocity *= 1. / secs;
m_loc = loc;
dir.normalize();
m_slewAxis = m_look.cross(dir);
if(m_slewAxis.dot(m_slewAxis) <= 1e-8) m_slewAxis.set(0, 1, 0);
m_slewAxis.normalize();
float cosa = m_look.dot(dir);
if(cosa > 1.0) cosa = 1.0;
if(cosa < -1.0) cosa = -1.0;
m_slewRate = acos(cosa) / secs;
//TRACE3("Amount turned %f, rate %f, ticks %ld\n", double(acos(cosa)), double(m_slewRate), ticks);
m_look = dir;
}
else
{
//TRACE("Record with no time elapsed\n");
}
// largest value is 2147483647, then it wraps to 0
m_lTime = ::GetMessageTime();
}
void ChVrmlStateTransition::RecordPressTime()
{
// largest value is 2147483647, then it wraps to 0
m_lButtonTime = ::GetMessageTime();
m_frameCount = m_pWnd->GetRenderContext()->GetFrameCount();
}
chint32 ChVrmlStateTransition::GetElapsedTicks()
{
double dTime = ::GetMessageTime();
while(dTime < m_lTime) dTime += 2147483648.; // while instead of if is in case they hold
// down key for over 23 days :-)
return chint32(dTime - m_lTime);
}
chint32 ChVrmlStateTransition::GetElapsedPressTicks()
{
double dTime = ::GetMessageTime();
while(dTime < m_lButtonTime) dTime += 2147483648.; // while instead of if is in case they hold
// down key for over 23 days :-)
return chint32(dTime - m_lButtonTime);
}
bool ChVrmlStateTransition::IsWithinFastClickTime()
{
if(GetElapsedPressTicks() < maxClickTicks)
{
return true;
}
if(m_pWnd->GetRenderContext()->GetFrameCount() - m_frameCount <= maxClickFrames)
{
return true;
}
return false;
}
float ChVrmlStateTransition::Smooth(float val, float begin, float end, float idle )
{
// Simple Harmonic motion smoother - just testing
// doesn't use range #s yet
return .5 * (cos((val - 1.) * 3.14159265359) + 1);
}
const float HACK_AGAIN = 30.;
float ChVrmlStateTransition::GetMoveAmountDistance()
{
//return m_pWnd->GetMoveAmountDistance() / 3.;
float unitsPerSecond = m_pWnd->GetMoveAmountDistance() * HACK_AGAIN;
if(GetKeyState( VK_SHIFT ) & 0x8000)
{
unitsPerSecond *= 2;
}
chint32 ticks = GetElapsedTicks();
ticks = max(ticks, 30);
return unitsPerSecond * ticks / 1000.;
};
float ChVrmlStateTransition::GetMoveAmountAngle()
{
//float speedFactor = m_pWnd->GetUserSpeedFactor();
float speedFactor = 1;
if(GetKeyState( VK_SHIFT ) & 0x8000)
{
speedFactor *= 2;
}
chint32 ticks = GetElapsedTicks();
//ticks = max(ticks, 30);
float amount = speedFactor * turnSlewRate * ticks / 1000.;
//TRACE2("Amount to turn %f, ticks %ld\n", double(3.141593 * amount / 180.), ticks);
return amount;
};
float ChVrmlStateTransition::GetMoveAmountAngle(int iPixels)
{
RECT r;
m_pWnd->GetClientRect(&r);
float speedFactor = 2. * float(iPixels) / max((r.bottom - r.top), (r.right - r.left));
chint32 ticks = GetElapsedTicks();
ticks = max(ticks, 30);
return speedFactor * turnSlewRate * ticks / 1000.;
};
bool ChVrmlStateTransition::OnLeftClick(int x, int y, chuint32 uFlags)
{
ReleaseCapture();
return m_pWnd->OnLeftClick( x, y, uFlags);
}
bool ChVrmlStateTransition::OnRightClick(int x, int y, chuint32 uFlags)
{
ReleaseCapture();
return m_pWnd->OnRightClick( x, y, uFlags);
}
void ChVrmlStateTransition::GetMouseAnchorLoc(int &iX, int &iY)
{
iX = m_iAnchorX;
iY = m_iAnchorY;
}
void ChVrmlStateTransition::GetMouseLoc(int &iX, int &iY)
{
iX = m_ix;
iY = m_iy;
}
bool ChVrmlStateTransition::IsInAnchoredMove()
{ // return whether you need cross hairs
bool boolInAnchoredMove = false;
if(m_iState == s_lbuttondrag ||
m_iState == s_lbuttondown ||
m_iState == s_lbuttondownMove ||
m_iState == s_lbuttondragMove)
{
boolInAnchoredMove = true;
}
return boolInAnchoredMove;
}
bool ChVrmlStateTransition::GetVelocity(GxVec3f &velocity)
{
// in world units/sec (usually m/s)
if(m_iState == s_start || m_iState == s_alt)
{
velocity.set(0, 0, 0);
return false;
}
velocity = m_velocity;
return true;
}
bool ChVrmlStateTransition::GetAngularVelocity(GxVec3f &axis, float &slewRate)
{
// in rads/sec
if(m_iState == s_start || m_iState == s_alt)
{
axis.set(0, 1, 0);
slewRate = 0.;
return false;
}
axis = m_slewAxis;
slewRate = m_slewRate;
return true;
}
// --------------------------------------------------------------------------
// Walker
// --------------------------------------------------------------------------
ChVrmlWalkSTM::ChVrmlWalkSTM(ChMazeWnd *pWnd) : ChVrmlStateTransition(pWnd)
{
Init();
}
void ChVrmlWalkSTM::Init()
{
// Now populate the STM
// SetCell( int iState, int iEvent, int iNewState, ChTransitionActionHandler action)
AddCell(s_start, ep_alt, s_alt, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_alt, er_alt, s_start, ChVrmlWalkSTM, DefaultHandler);
// --
AddCell(s_start, ep_up, s_up, ChVrmlWalkSTM, MoveForwardStart);
AddCell(s_up, er_up, s_start, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_up, e_tick, s_up, ChVrmlWalkSTM, MoveForward);
AddCell(s_up, ep_alt, s_moveUp, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_alt, ep_up, s_moveUp, ChVrmlWalkSTM, MoveUpStart);
AddCell(s_moveUp, er_up, s_alt, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_moveUp, e_tick, s_moveUp, ChVrmlWalkSTM, MoveUp);
AddCell(s_moveUp, er_alt, s_up, ChVrmlWalkSTM, DefaultHandler);
// --
AddCell(s_start, ep_down, s_down, ChVrmlWalkSTM, MoveReverseStart);
AddCell(s_down, er_down, s_start, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_down, e_tick, s_down, ChVrmlWalkSTM, MoveReverse);
AddCell(s_down, ep_alt, s_moveDown, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_alt, ep_down, s_moveDown, ChVrmlWalkSTM, MoveDownStart);
AddCell(s_moveDown, er_down, s_alt, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_moveDown, e_tick, s_moveDown, ChVrmlWalkSTM, MoveDown);
AddCell(s_moveDown, er_alt, s_down, ChVrmlWalkSTM, DefaultHandler);
// --
AddCell(s_start, ep_left, s_left, ChVrmlWalkSTM, TurnLeftStart);
AddCell(s_left, er_left, s_start, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_left, e_tick, s_left, ChVrmlWalkSTM, TurnLeft);
AddCell(s_left, ep_alt, s_moveLeft, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_alt, ep_left, s_moveLeft, ChVrmlWalkSTM, MoveLeftStart);
AddCell(s_moveLeft, er_left , s_alt, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_moveLeft, e_tick, s_moveLeft, ChVrmlWalkSTM, MoveLeft);
AddCell(s_moveLeft, er_alt, s_left, ChVrmlWalkSTM, DefaultHandler);
// --
AddCell(s_start, ep_right, s_right, ChVrmlWalkSTM, TurnRightStart);
AddCell(s_right, er_right, s_start, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_right, e_tick, s_right, ChVrmlWalkSTM, TurnRight);
AddCell(s_right, ep_alt, s_moveRight, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_alt, ep_right, s_moveRight, ChVrmlWalkSTM, MoveRightStart);
AddCell(s_moveRight, er_right, s_alt, ChVrmlWalkSTM, DefaultHandler);
AddCell(s_moveRight, e_tick, s_moveRight, ChVrmlWalkSTM, MoveRight);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -