示例#1
0
/**
 * Example strings:
 *   Key55
 *   Joy{uuid}/Button2
 *   Joy{uuid}/Hat0Dir3
 */
bool KeyBinding::FromString(const char *str, KeyBinding &kb)
{
	const char *digits = "1234567890";
	const char *p = str;

	if (strcmp(p, "disabled") == 0) {
		kb.Clear();
	} else if (strncmp(p, "Key", 3) == 0) {
		kb.type = KEYBOARD_KEY;
		p += 3;

		kb.u.keyboard.key = SDL_Keycode(atoi(p));
		p += strspn(p, digits);

		if (strncmp(p, "Mod", 3) == 0) {
			p += 3;
			kb.u.keyboard.mod = SDL_Keymod(atoi(p));
		} else {
			kb.u.keyboard.mod = KMOD_NONE;
		}
	} else if (strncmp(p, "Joy", 3) == 0) {
		p += 3;

		const int JoyUUIDLength = 33;
		char joyUUIDBuf[JoyUUIDLength];

		// read the UUID
		if (!ReadToTok('/', &p, joyUUIDBuf, JoyUUIDLength)) {
			return false;
		}
		// force terminate
		joyUUIDBuf[JoyUUIDLength-1] = '\0';
		// now, locate the internal ID.		
		int joy = Pi::JoystickFromGUIDString(joyUUIDBuf);
		if (joy == -1) {
			return false;
		}
		if (strncmp(p, "Button", 6) == 0) {
			p += 6;
			kb.type = JOYSTICK_BUTTON;
			kb.u.joystickButton.joystick = joy;
			kb.u.joystickButton.button = atoi(p);
		} else if (strncmp(p, "Hat", 3) == 0) {
			p += 3;
			kb.type = JOYSTICK_HAT;
			kb.u.joystickHat.joystick = joy;
			kb.u.joystickHat.hat = atoi(p);
			p += strspn(p, digits);

			if (strncmp(p, "Dir", 3) != 0)
				return false;

			p += 3;
			kb.u.joystickHat.direction = atoi(p);
		} else
			return false;
	}

	return true;
}
示例#2
0
bool AxisBinding::FromString(const char *str, AxisBinding &ab) {
	if (strcmp(str, "disabled") == 0) {
		ab.Clear();
		return true;
	}

	const char *p = str;

	if (p[0] == '-') {
		ab.direction = NEGATIVE;
		p++;
	}
	else
		ab.direction = POSITIVE;

	if (strncmp(p, "Joy", 3) != 0)
		return false;
	p += 3;

	const int JoyUUIDLength = 33;
	char joyUUIDBuf[JoyUUIDLength];

	// read the UUID
	if (!ReadToTok('/', &p, joyUUIDBuf, JoyUUIDLength)) {
		return false;
	}
	// force terminate
	joyUUIDBuf[JoyUUIDLength-1] = '\0';
	// now, map the GUID to a joystick number
	const int joystick = Pi::JoystickFromGUIDString(joyUUIDBuf);
	if (joystick == -1) {
		return false;
	}
	// found a joystick
	assert(joystick < 256);
	ab.joystick = Uint8(joystick);

	if (strncmp(p, "Axis", 4) != 0)
		return false;

	p += 4;
	ab.axis = atoi(p);

	return true;
}