⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menujoystickaxis.cpp

📁 Blood 2全套源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
void CMenuJoystickAxisBase::SaveNewActions(CClientDE *pClientDE)
{
	pClientDE->RunConsoleString("AddAction AxisYaw -10001");
	pClientDE->RunConsoleString("AddAction AxisPitch -10002");
	pClientDE->RunConsoleString("AddAction AxisLeftRight -10003");
	pClientDE->RunConsoleString("AddAction AxisForwardBackward -10004");
}

// get a binding for a joystick trigger
void CMenuJoystickAxisBase::GetJoystickBindingInfo(CClientDE *pClientDE)
{
//	pClientDE->CPrint("Enter GetJoystickBindingInfo. m_sActionAnalog = %s m_sActionDigitalHigh = %s m_sActionDigitalLow = %s", m_sActionAnalog, m_sActionDigitalHigh, m_sActionDigitalLow ); // BLB TEMP

	// Get the bindings
	DeviceBinding *pBinding=pClientDE->GetDeviceBindings(DEVICETYPE_JOYSTICK);

	// Iterate through each binding look for the one with our action name
	DeviceBinding* pCurrentBinding = pBinding;
	while (pCurrentBinding != NULL)
	{
		if ((stricmp(pCurrentBinding->strDeviceName, m_sDeviceName) == 0) &&
			(GetAxisIndex(pClientDE, pCurrentBinding->strTriggerName) > 0))
		{
//			pClientDE->CPrint("Checking device = %s trigger = %s", pCurrentBinding->strDeviceName, pCurrentBinding->strTriggerName ); // BLB TEMP

			DBOOL		bFound = DFALSE;
			GameAction* pDigitalLowAction = NULL;
			GameAction* pDigitalHighAction = NULL;
			GameAction* pAnalogAction = NULL;
			GameAction* pAction = pCurrentBinding->pActionHead;

			// go through all actions looking for our actions
			while (pAction != NULL)
			{
				char *sActionName=pAction->strActionName;
				if (sActionName != NULL)
				{
//					pClientDE->CPrint("Checking action = %s", sActionName ); // BLB TEMP
					if (stricmp(sActionName, m_sActionAnalog) == 0) pAnalogAction = pAction;
					if (stricmp(sActionName, m_sActionDigitalHigh) == 0) pDigitalHighAction = pAction;
					if (stricmp(sActionName, m_sActionDigitalLow) == 0) pDigitalLowAction = pAction;
				}
				pAction = pAction->pNext;
				if (pAction == pCurrentBinding->pActionHead) break;
			}

			// if we found an analog action set up the variables for it
			if (pAnalogAction != NULL)
			{
				strncpy(m_sActionName, pAnalogAction->strActionName, MAX_ACTIONNAME_LEN);
				m_sActionName[MAX_ACTIONNAME_LEN-1] = '\0';
				m_fRangeLow = pAnalogAction->nRangeLow;
				m_fRangeHigh = pAnalogAction->nRangeHigh;

				m_bAnalog = TRUE;
				bFound = DTRUE;

//				pClientDE->CPrint("Digital binding read Device = %s Trigger = %s Action1 = %s", m_sDeviceName, pCurrentBinding->strTriggerName, m_sActionName ); // BLB TEMP
			}

			// otherwise if we found the two digital actions with no analog we are in digital mode
			else if ((pDigitalLowAction != NULL) && (pDigitalHighAction != NULL))
			{
				strncpy(m_sActionName, pDigitalLowAction->strActionName, MAX_ACTIONNAME_LEN);
				m_sActionName[MAX_ACTIONNAME_LEN-1] = '\0';
				m_fRangeLow = pDigitalLowAction->nRangeLow;
				m_fRangeHigh = pDigitalLowAction->nRangeHigh;

				strncpy(m_sActionName2, pDigitalHighAction->strActionName, MAX_ACTIONNAME_LEN);
				m_sActionName2[MAX_ACTIONNAME_LEN-1] = '\0';
				m_fRangeLow2 = pDigitalHighAction->nRangeLow;
				m_fRangeHigh2 = pDigitalHighAction->nRangeHigh;

				m_bAnalog = FALSE;
				bFound = DTRUE;

//				pClientDE->CPrint("Digital binding read Device = %s Trigger = %s Action1 = %s Action2 = %s", m_sDeviceName, pCurrentBinding->strTriggerName, m_sActionName, m_sActionName2 ); // BLB TEMP
			}

			else
			{
//				pClientDE->CPrint("No binding found!"); // BLB TEMP
			}

			// if we did find some bindings transfer the information
			if (bFound)
			{
				strncpy(m_sTriggerName, pCurrentBinding->strTriggerName, INPUTNAME_LEN);
				m_sTriggerName[INPUTNAME_LEN-1] = '\0';
				m_fScale = pCurrentBinding->nScale;
				m_fRangeScaleMin = pCurrentBinding->nRangeScaleMin;
				m_fRangeScaleMax = pCurrentBinding->nRangeScaleMax;
				m_fRangeScalePreCenterOffset = pCurrentBinding->nRangeScalePreCenterOffset;
				m_bBindingsRead = DTRUE;
			}
		}
		pCurrentBinding = pCurrentBinding->pNext;
		if (pCurrentBinding == pBinding) break;
	}

	// Free the device bindings
	pClientDE->FreeDeviceBindings(pBinding);

	if (!m_bBindingsRead)
	{
		strncpy(m_sActionName, m_sActionAnalog, MAX_ACTIONNAME_LEN);
		m_sActionName[MAX_ACTIONNAME_LEN-1] = '\0';
	}

//	pClientDE->CPrint("Exit GetJoystickBindingInfo." ); // BLB TEMP
}

// Send out a rangebind to the console
void CMenuJoystickAxisBase::DoRangeBind(CClientDE *pClientDE, char *lpszDevice, char *lpszControlName, char *lpszActionName, float nRangeLow, float nRangeHigh)
{
	assert(lpszControlName);
	assert(lpszActionName);
	assert(lpszDevice);

	if (lpszControlName == NULL) return;
	if (lpszActionName == NULL) return;
	if (lpszDevice == NULL) return;

	char tempStr[512];

	// Set the binding
	sprintf(tempStr, "rangebind \"%s\" \"%s\" %f %f \"%s\"", lpszDevice, lpszControlName, nRangeLow, nRangeHigh, lpszActionName);
	pClientDE->RunConsoleString(tempStr);
}

// Clear a rangebind to the console
void CMenuJoystickAxisBase::ClearRangeBind(CClientDE *pClientDE, char *lpszDevice, char *lpszControlName)
{
	assert(lpszControlName);
	assert(lpszDevice);

	if (lpszControlName == NULL) return;
	if (lpszDevice == NULL) return;

	char tempStr[512];

	// Set the binding
	sprintf(tempStr, "rangebind \"%s\" \"%s\" 0 0 \"\"", lpszDevice, lpszControlName);
	pClientDE->RunConsoleString(tempStr);
}

// Send out a rangescale to the console
void CMenuJoystickAxisBase::DoRangeScale(CClientDE *pClientDE, char *lpszDevice, char *lpszControlName, float nScale, float nRangeScaleMin, float nRangeScaleMax, float nRangeScalePreCenterOffset)
{
	assert(lpszControlName);
	assert(lpszDevice);

	if (lpszControlName == NULL) return;
	if (lpszDevice == NULL) return;

	char tempStr[512];

	// Set the rangescale
	sprintf(tempStr, "rangescale \"%s\" \"%s\" %f %f %f %f", lpszDevice, lpszControlName, nScale, nRangeScaleMin, nRangeScaleMax, nRangeScalePreCenterOffset);
	pClientDE->RunConsoleString(tempStr);
}

// Set a consolve var to a float
void CMenuJoystickAxisBase::SetConsoleVar(CClientDE *pClientDE, char *lpszName, float nVal)
{
	assert(lpszName);

	if (lpszName == NULL) return;

	char tempStr[512];

	// Set the rangescale
	sprintf(tempStr, "+%s %f", lpszName, nVal);
	pClientDE->RunConsoleString(tempStr);
}

// find the index into the axis array for the given axis name (return 0 if not found)
int CMenuJoystickAxisBase::GetAxisIndex(CClientDE *pClientDE, char* sAxisName)
{
	// loop through all axis trying to find the matching axis (skip 0 becaus it is the none axis)
	for (int i = 1; i < m_nNumJoystickAxis; i++)
	{
		if (stricmp(sAxisName, m_aryAxisInfo[i].GetName()) == 0) return i;
	}
	return 0;
}

// scales a value from one range to another in a linear fashion
float CMenuJoystickAxisBase::ScaleToRange(float fFromVal, float fFromMin, float fFromMax, float fToMin, float fToMax)
{
	float fMultiplier = 1.0f;
	float fOffset;

	float fFromMinMinusMax = fFromMin - fFromMax;
	if (fFromMinMinusMax != 0.0f) fMultiplier = (fToMin - fToMax) / fFromMinMinusMax;
	fOffset = fToMin - (fMultiplier * fFromMin);

	float fRetVal = (fFromVal * fMultiplier) + fOffset;
	if (fRetVal < fToMin) fRetVal = fToMin;
	if (fRetVal > fToMax) fRetVal = fToMax;

	return fRetVal;
}


// Load variables from the console
void CMenuJoystickAxisBase::LoadFromConsole(CClientDE *pClientDE)
{

	if (!m_bBindingsRead)
	{
		// try to read digital bindings
		GetJoystickBindingInfo(pClientDE);
	}

	// read in the dead zone console variable
	HCONSOLEVAR hVar = pClientDE->GetConsoleVar(m_sDeadZoneConsoleVar);
	if (m_bAnalog) 
	{
		if (hVar != NULL) m_fDeadZone = pClientDE->GetVarValueFloat(hVar) / m_fScale;
		//pClientDE->CPrint("Read Dead Zone Analog : m_fDeadZone = %f m_fScale = %f ", m_fDeadZone, m_fScale ); // BLB TEMP
	}
	else
	{
		if (hVar != NULL) m_fDeadZone = pClientDE->GetVarValueFloat(hVar);
		//pClientDE->CPrint("Read Dead Zone Digital : m_fDeadZone = %f", m_fDeadZone ); // BLB TEMP
	}

	// convert all of the variables read in from the bindings to our internal controls variables
	if (m_bBindingsRead)
	{
		// figure out the index of the axis that we found
		m_nAxis = GetAxisIndex(pClientDE, m_sTriggerName);

		// scale the sensitivity slider bar from the rangescale values that we read in
		if (m_bAnalog) m_nSensitivity = (int)ScaleToRange(m_fScale, m_fScaleCenter-m_fScaleRangeDiv2, m_fScaleCenter+m_fScaleRangeDiv2, (float)SENSITIVITYSLIDERLOW, (float)SENSITIVITYSLIDERHIGH);

		// scale the dead zone that we read in to the dead zone slider bar
		m_nDeadZone = (int)ScaleToRange(m_fDeadZone, 0.0f, 0.90f, (float)DEADZONESLIDERLOW, (float)DEADZONESLIDERHIGH);

		// figure out if the bindings that were read in had inverted axis
		if (m_bAnalog)
		{
			if (m_fRangeScaleMin > m_fRangeScaleMax) m_bInvertAxis = DTRUE;
			else m_bInvertAxis = DFALSE;
		}
		else
		{
			if (stricmp(m_sActionName, m_sActionDigitalLow) == 0)
			{
				if (m_fRangeLow < m_fRangeLow2) m_bInvertAxis = DFALSE;
				else m_bInvertAxis = DTRUE;
			}
			else	
			{
				if (m_fRangeLow <= m_fRangeLow2) m_bInvertAxis = DTRUE;
				else m_bInvertAxis = DFALSE;
			}
		}

		// figure out the correct value of the center offset flag from the data read in
		if ((long)m_fRangeScalePreCenterOffset != 100) m_bCenterOffset = DFALSE;
		else m_bCenterOffset = DTRUE;
	}

	// save off the original values
	m_nOrigAxis=m_nAxis;
	m_nOrigSensitivity=m_nSensitivity;
	m_nOrigDeadZone=m_nDeadZone;
	m_bOrigInvertAxis=m_bInvertAxis;
	m_bOrigCenterOffset=m_bCenterOffset;
	m_bOrigAnalog = m_bAnalog;

	// clear the old binding
	if (m_bBindingsRead)
	{
		if (m_nAxis > MENUJOYSTICK_AXIS_NONE)
		{
			strncpy(m_sTriggerName, m_aryAxisInfo[m_nAxis].GetName(), INPUTNAME_LEN);
			m_sTriggerName[INPUTNAME_LEN-1] = '\0';

			ClearRangeBind(pClientDE, m_sDeviceName, m_sTriggerName);
		}
	}
}


// Save an analog binding to the consle
void CMenuJoystickAxisBase::SaveToConsoleAnalog(CClientDE *pClientDE)
{
	// set the new scale if it has been adjusted or was never read in
	m_fScale = ScaleToRange((float)m_nSensitivity, (float)SENSITIVITYSLIDERLOW, (float)SENSITIVITYSLIDERHIGH, m_fScaleCenter-m_fScaleRangeDiv2, m_fScaleCenter+m_fScaleRangeDiv2);
	if (m_fScale < 0.001) m_fScale = 0.001f; // don't let the scale get too small

	// adjust values for the invert axis flag if it has been adjusted or was never read in
	if (m_bInvertAxis)
	{
		if (m_fRangeScaleMin < m_fRangeScaleMax)
		{
			float fTemp;
			fTemp = m_fRangeScaleMin;
			m_fRangeScaleMin = m_fRangeScaleMax;
			m_fRangeScaleMax = fTemp;
		}
	}
	else
	{
		if (m_fRangeScaleMin > m_fRangeScaleMax)
		{
			float fTemp;
			fTemp = m_fRangeScaleMin;
			m_fRangeScaleMin = m_fRangeScaleMax;
			m_fRangeScaleMax = fTemp;
		}
	}

	// set the center offsets if it has changed or was never read in
	if (m_bCenterOffset) m_fRangeScalePreCenterOffset = 100.0f;
	else m_fRangeScalePreCenterOffset = 0.0f;

	// write out the rangebind and rangescale
	if ((m_sDeviceName[0] != '\0') && (m_sTriggerName[0] != '\0') && (m_sActionName[0] != '\0'))
	{
		char tempStr[1024];
		if (m_bAddDigitalBindingsToAnalog)
		{
			if (m_bInvertAxis)
			{
				sprintf(tempStr, "rangebind \"%s\" \"%s\" %f %f \"%s\" %f %f \"%s\" %f %f \"%s\"", m_sDeviceName, m_sTriggerName, 0.0f, 0.0f, m_sActionAnalog,
					(m_fDeadZone*m_fScale)-0.001f, (1.0f*m_fScale)+0.001f, m_sActionDigitalLow, -((m_fDeadZone*m_fScale)-0.001f), -((1.0f*m_fScale)+0.001f), m_sActionDigitalHigh);
			}
			else
			{
				sprintf(tempStr, "rangebind \"%s\" \"%s\" %f %f \"%s\" %f %f \"%s\" %f %f \"%s\"", m_sDeviceName, m_sTriggerName, 0.0f, 0.0f, m_sActionAnalog,
					(m_fDeadZone*m_fScale)-0.001f, (1.0f*m_fScale)+0.001f, m_sActionDigitalHigh, -((m_fDeadZone*m_fScale)-0.001f), -((1.0f*m_fScale)+0.001f), m_sActionDigitalLow);
			}
		}
		else
		{
			sprintf(tempStr, "rangebind \"%s\" \"%s\" %f %f \"%s\"", m_sDeviceName, m_sTriggerName, 0.0f, 0.0f, m_sActionAnalog);
		}
		pClientDE->RunConsoleString(tempStr);
		DoRangeScale(pClientDE, m_sDeviceName, m_sTriggerName, m_fScale, m_fRangeScaleMin, m_fRangeScaleMax, m_fRangeScalePreCenterOffset);
		//pClientDE->CPrint("Joystick Analog Binding : %s m_fScale=%f m_fRangeScaleMin=%f m_fRangeScaleMax=%f m_fRangeScalePreCenterOffset=%f ", tempStr, m_fScale, m_fRangeScaleMin, m_fRangeScaleMax, m_fRangeScalePreCenterOffset ); // BLB TEMP
	}
}


// Save a digital binding to the consle
void CMenuJoystickAxisBase::SaveToConsoleDigital(CClientDE *pClientDE)
{
	// save off the range of the device
	float fDeviceRangeHigh = m_aryAxisInfo[m_nAxis].GetRangeHigh();
	float fDeviceRangeLow = m_aryAxisInfo[m_nAxis].GetRangeLow();

	// figure out the intermediate range information
	float fRange = fDeviceRangeHigh - fDeviceRangeLow;
	float fHalfRange = fRange / 2.0f;
	float fActiveRange = fHalfRange - (m_fDeadZone * fRange);

	// figure out the numbers to write out
	float fLeftLow = fDeviceRangeLow;
	float fLeftHigh = fDeviceRangeLow + fActiveRange;
	float fRightLow = fDeviceRangeHigh - fActiveRange;
	float fRightHigh = fDeviceRangeHigh;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -