📄 menujoystickaxis.cpp
字号:
#include "MainMenus.h"
#include "MenuJoystickAxis.h"
#include "clientres.h"
// The slider offset
#define MENUJOYSTICK_SLIDEROFFSET 100
#define MENUJOYSTICK_ONOFFOFFSET 100
// Slider ranges
#define DEADZONESLIDERLOW 0
#define DEADZONESLIDERHIGH 9
#define SENSITIVITYSLIDERLOW 0
#define SENSITIVITYSLIDERHIGH 20
// This is just a test function because I wanted to see that available mouse devices BLB TEMP!!!
/*
void FindMouseControls(CClientDE *pClientDE)
{
DeviceObject* pObjects = pClientDE->GetDeviceObjects(DEVICETYPE_MOUSE);
DeviceObject* pObj = pObjects;
DBOOL bFoundIt = DFALSE;
while (pObj != NULL)
{
if ((pObj->m_ObjectName != NULL) && (pObj->m_DeviceName != NULL))
{
pClientDE->CPrint("Mouse DeviceName=%s ObjectName=%s", pObj->m_DeviceName, pObj->m_ObjectName ); // BLB TEMP
}
pObj = pObj->m_pNext;
}
if (pObjects != NULL) pClientDE->FreeDeviceObjects (pObjects);
}
*/
/**************************************************************************/
// Joystick Axis info class
/**************************************************************************/
void CMenuJoystickAxisInfo::Init(char* sName, DDWORD nType, float fRangeLow, float fRangeHigh)
{
if (sName == NULL) m_sName[0] = '\0';
else
{
strncpy(m_sName, sName, INPUTNAME_LEN);
m_sName[INPUTNAME_LEN-1] = '\0';
}
m_nType = nType;
m_fRangeLow = fRangeLow;
m_fRangeHigh = fRangeHigh;
};
/**************************************************************************/
// Joystick Axis base class
/**************************************************************************/
// Constructor
CMenuJoystickAxisBase::CMenuJoystickAxisBase()
{
// names of possible actions
m_sActionDigitalLow[0] = '\0';
m_sActionDigitalHigh[0] = '\0';
m_sActionAnalog[0] = '\0';
m_sDeadZoneConsoleVar[0] = '\0';
// numbers used to make the scale from the slider
m_fScaleCenter = 1.0f;
m_fScaleRangeDiv2 = 0.5f;
// set default values for rangebind and rangescale data
m_sDeviceName[0] = '\0';
m_sTriggerName[0] = '\0';
m_fScale = 1.0f;
m_fRangeScaleMin = -1.0f;
m_fRangeScaleMax = 1.0f;
m_fRangeScalePreCenterOffset;
m_sActionName[0] = '\0';
m_fRangeLow = 0.0f;
m_fRangeHigh = 0.0f;
m_sActionName2[0] = '\0';
m_fRangeLow2 = 0.0f;
m_fRangeHigh2 = 0.0f;
m_fDeadZone = 0.10f;
// set to TRUE if bindings were read in successfully
m_bBindingsRead = DFALSE;
m_bAddDigitalBindingsToAnalog = DFALSE;
// Data members
m_nAxis=MENUJOYSTICK_AXIS_NONE;
m_nSensitivity=1;
m_nDeadZone=1;
m_bAnalog=DFALSE;
m_bInvertAxis=DFALSE;
m_bCenterOffset=DFALSE;
// Stores original value of data members
m_nOrigAxis=m_nAxis;
m_nOrigSensitivity=m_nSensitivity;
m_nOrigDeadZone=m_nDeadZone;
m_bOrigAnalog=m_bAnalog;
m_bOrigInvertAxis=m_bInvertAxis;
m_bOrigCenterOffset=m_bCenterOffset;
// clear the array of axis name strings
m_nNumJoystickAxis = 0;
// Message ID for each control
m_nAxisID=IDS_MENU_JOYSTICK_AXIS;
m_nSensitivityID=IDS_MENU_JOYSTICK_SENSITIVITY;
m_nDeadZoneID=IDS_MENU_JOYSTICK_DEADZONE;
m_nAnalogID=IDS_MENU_JOYSTICK_ANALOG;
m_nInvertAxisID=IDS_MENU_JOYSTICK_INVERTAXIS;
m_nCenterOffsetID=IDS_MENU_JOYSTICK_CENTEROFFSETCORRECTION;
// The controls
m_pAxisCtrl=DNULL;
m_pSensitivityCtrl=DNULL;
m_pAnalogCtrl=DNULL;
m_pDeadZoneCtrl=DNULL;
m_pInvertAxisCtrl=DNULL;
m_pCenterOffsetCtrl=DNULL;
}
// Build the menu items for this class
void CMenuJoystickAxisBase::Build(CClientDE *pClientDE, CMenuBase *pDestMenu)
{
if (!pDestMenu)
{
return;
}
// Get the main menus pointer
CMainMenus *pMainMenus=pDestMenu->GetMainMenus();
if (!pMainMenus)
{
return;
}
// figure out the name of the joystick device
SetDeviceName(pClientDE);
// create the array of possible axis
CreateAxisArray(pClientDE);
// just a test BLB TEMP!!!
// FindMouseControls(pClientDE);
// The axis option
m_pAxisCtrl=BuildAxisOption(pClientDE, pDestMenu, m_nAxisID, &m_nAxis);
// The invert axis control
m_pInvertAxisCtrl=pDestMenu->AddOnOffOption(m_nInvertAxisID, pMainMenus->GetSmallFont(), MENUJOYSTICK_ONOFFOFFSET,
&m_bInvertAxis);
// The dead zone control
m_pDeadZoneCtrl=pDestMenu->AddSliderOption(m_nDeadZoneID, pMainMenus->GetSmallFont(), MENUJOYSTICK_SLIDEROFFSET,
pMainMenus->GetSurfaceSliderBar(), pMainMenus->GetSurfaceSliderTab(),
&m_nDeadZone);
if (m_pDeadZoneCtrl)
{
m_pDeadZoneCtrl->SetSliderRange(DEADZONESLIDERLOW, DEADZONESLIDERHIGH);
m_pDeadZoneCtrl->SetSliderIncrement(1);
}
// Analog on/off
m_pAnalogCtrl=pDestMenu->AddOnOffOption(m_nAnalogID, pMainMenus->GetSmallFont(), MENUJOYSTICK_ONOFFOFFSET, &m_bAnalog);
// The sensitivity control
m_pSensitivityCtrl=pDestMenu->AddSliderOption(m_nSensitivityID, pMainMenus->GetSmallFont(), MENUJOYSTICK_SLIDEROFFSET,
pMainMenus->GetSurfaceSliderBar(), pMainMenus->GetSurfaceSliderTab(),
&m_nSensitivity);
if (m_pSensitivityCtrl)
{
m_pSensitivityCtrl->SetSliderRange(SENSITIVITYSLIDERLOW, SENSITIVITYSLIDERHIGH);
m_pSensitivityCtrl->SetSliderIncrement(1);
}
// The center offset axis control
m_pCenterOffsetCtrl=pDestMenu->AddOnOffOption(m_nCenterOffsetID, pMainMenus->GetSmallFont(), MENUJOYSTICK_ONOFFOFFSET,
&m_bCenterOffset);
}
// Updates the enable/disable status of each control.
// bJoystickOn indicates if the joystick is enabled in the menus.
void CMenuJoystickAxisBase::UpdateEnable(DBOOL bJoystickOn)
{
// If the joystick is on the enable all of the controls.
// The analog status is checked down below which may disable some of the controls.
DBOOL bEnable=bJoystickOn;
if (m_pAxisCtrl) m_pAxisCtrl->Enable(bEnable);
if (m_pInvertAxisCtrl) m_pInvertAxisCtrl->Enable(bEnable);
if (m_pDeadZoneCtrl) m_pDeadZoneCtrl->Enable(bEnable);
if (m_pAnalogCtrl) m_pAnalogCtrl->Enable(bEnable);
if (m_pSensitivityCtrl) m_pSensitivityCtrl->Enable(bEnable);
if (m_pCenterOffsetCtrl) m_pCenterOffsetCtrl->Enable(bEnable);
// Return if the joystick is disabled
if (!bJoystickOn)
{
return;
}
// If the axis is set to none then disable the rest of the controls
if (m_nAxis == MENUJOYSTICK_AXIS_NONE)
{
bEnable=DFALSE;
if (m_pInvertAxisCtrl) m_pInvertAxisCtrl->Enable(bEnable);
if (m_pDeadZoneCtrl) m_pDeadZoneCtrl->Enable(bEnable);
if (m_pAnalogCtrl) m_pAnalogCtrl->Enable(bEnable);
if (m_pSensitivityCtrl) m_pSensitivityCtrl->Enable(bEnable);
if (m_pCenterOffsetCtrl) m_pCenterOffsetCtrl->Enable(bEnable);
return;
}
// Disable the sensitivity and center off controls if in analog mode
if (!m_bAnalog)
{
if (m_pSensitivityCtrl) m_pSensitivityCtrl->Enable(DFALSE);
if (m_pCenterOffsetCtrl) m_pCenterOffsetCtrl->Enable(DFALSE);
}
}
// Build the menu option that allows the user to select the axis
CLTGUITextItemCtrl *CMenuJoystickAxisBase::BuildAxisOption(CClientDE *pClientDE, CMenuBase *pDestMenu, int nMessageID, int *pnDestValue)
{
// Get the main menus pointer
CMainMenus *pMainMenus=pDestMenu->GetMainMenus();
if (!pMainMenus)
{
return DNULL;
}
// Load the display string from the string resource
HSTRING hDisplayString=pClientDE->FormatString(nMessageID);
if (!hDisplayString)
{
return DNULL;
}
// Create the control
CLTGUITextItemCtrl *pCtrl=pDestMenu->AddTextItemOption(DNULL, 0, pMainMenus->GetSmallFont(), 1, DTRUE, pnDestValue);
// Add the strings to the control
int i;
for (i=0; i < m_nNumJoystickAxis; i++)
{
// Construct the string for the option (ie "Select axis [axis name]");
char szBuffer[2048];
sprintf(szBuffer, "%s [%s]", pClientDE->GetStringData(hDisplayString), m_aryAxisInfo[i].GetName());
// Add the string to the control
HSTRING hTemp=pClientDE->CreateString(szBuffer);
pCtrl->AddString(hTemp);
// Free the strings
pClientDE->FreeString(hTemp);
}
return pCtrl;
}
// Set the joystick device name
BOOL CMenuJoystickAxisBase::SetDeviceName(CClientDE *pClientDE)
{
BOOL bDeviceNameFound = FALSE;
// clear the devicename for the case where we don't find one
m_sDeviceName[0] = '\0';
// read in the joystick device to use from the JOYSTICKDEVICENAME console variable or set the default
char* sValue = NULL;
HCONSOLEVAR hVar = pClientDE->GetConsoleVar( "JOYSTICKDEVICENAME");
if (hVar != NULL) sValue = pClientDE->GetVarValueString(hVar);
if (sValue != NULL)
{
strncpy(m_sDeviceName, sValue, INPUTNAME_LEN);
m_sDeviceName[INPUTNAME_LEN-1] = '\0';
}
else
{
strncpy(m_sDeviceName, "Joystick 1", INPUTNAME_LEN);
m_sDeviceName[INPUTNAME_LEN-1] = '\0';
}
// verify that this device name exists
DeviceObject* pObjects = pClientDE->GetDeviceObjects(DEVICETYPE_JOYSTICK);
DeviceObject* pObj = pObjects;
DBOOL bFoundIt = DFALSE;
while ((pObj != NULL) && (bDeviceNameFound == FALSE))
{
if ((pObj->m_ObjectName != NULL) && (pObj->m_DeviceName != NULL) && (pObj->m_DeviceType == DEVICETYPE_JOYSTICK))
{
if (stricmp(pObj->m_DeviceName, m_sDeviceName) == 0) bDeviceNameFound = TRUE;
}
}
// if the name did not exist use the first joystick name that we can find
if ((bDeviceNameFound == FALSE) && (pObjects != NULL))
{
if (pObjects->m_DeviceName != NULL)
{
strncpy(m_sDeviceName, pObjects->m_DeviceName, INPUTNAME_LEN);
m_sDeviceName[INPUTNAME_LEN-1] = '\0';
bDeviceNameFound = TRUE;
}
}
// free all the device objects
if (pObjects != NULL) pClientDE->FreeDeviceObjects (pObjects);
// put the enabledevice command out to the console
char tempStr[512];
sprintf(tempStr, "enabledevice \"%s\"", m_sDeviceName);
pClientDE->RunConsoleString(tempStr);
return bDeviceNameFound;
}
// Set the joystick device name
void CMenuJoystickAxisBase::CreateAxisArray(CClientDE *pClientDE)
{
DeviceObject* pObjects = pClientDE->GetDeviceObjects(DEVICETYPE_JOYSTICK);
DeviceObject* pObj = pObjects;
DBOOL bFoundIt = DFALSE;
// the first axis is always the none axis
HSTRING hAxisName = pClientDE->FormatString(IDS_MENU_JOYSTICK_AXISTYPE_NONE);
m_nNumJoystickAxis = 0;
if (hAxisName != NULL) m_aryAxisInfo[m_nNumJoystickAxis].Init(pClientDE->GetStringData(hAxisName), 0, 0.0f, 0.0f);
else m_aryAxisInfo[m_nNumJoystickAxis].Init("none", 0, 0.0f, 0.0f);
m_nNumJoystickAxis++;
// loop through all joystick objects and store the axis ones with our devicename the aryAxisInfo array
while ((pObj != NULL) && (m_nNumJoystickAxis < MENUJOYSTICK_MAX_AXIS))
{
if ((pObj->m_ObjectName != NULL) && (pObj->m_DeviceName != NULL))
{
if ((pObj->m_DeviceType == DEVICETYPE_JOYSTICK) &&
(stricmp(pObj->m_DeviceName, m_sDeviceName) == 0) &&
((pObj->m_ObjectType == CONTROLTYPE_XAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_YAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_ZAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_RXAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_RYAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_RZAXIS) ||
(pObj->m_ObjectType == CONTROLTYPE_SLIDER)))
{
m_aryAxisInfo[m_nNumJoystickAxis].Init(pObj->m_ObjectName, pObj->m_ObjectType, pObj->m_RangeLow, pObj->m_RangeHigh);
m_nNumJoystickAxis++;
}
}
pObj = pObj->m_pNext;
}
// free the device objects
if (pObjects != NULL) pClientDE->FreeDeviceObjects (pObjects);
}
// Set the joystick device name
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -