Example #1
0
void PollForHcEvents()
{
    // This is a simulation. The controls are read from the keyboard
    // and then sent to the SerialHcLoop which reads them from a buffer and actions them as it would normally
    SDL_Event event;

    while(SDL_PollEvent( &event ))
    {
        /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
        switch( event.type )
        {
        case SDL_KEYDOWN:
            SDLKey key;
            key = event.key.keysym.sym;

            EnterCriticalSection(&s_SyncRoot);
            s_KeybordInput.push(SDLKey(key));
            LeaveCriticalSection(&s_SyncRoot);

            break;

        case SDL_KEYUP:
            break;

        default:
            break;
        }
    }
}
Example #2
0
void save_hotkeys(config& cfg)
{
	cfg.clear_children(hotkey_tag_name);

	for(std::vector<hotkey_item>::iterator i = hotkeys_.begin(); i != hotkeys_.end(); ++i) {
		if (i->hidden() || i->get_type() == hotkey_item::UNBOUND || !i->is_in_active_scope())
			continue;

		config& item = cfg.add_child(hotkey_tag_name);
		item["command"] = i->get_command();
		if (i->get_type() == hotkey_item::CLEARED)
		{
			item["key"] = CLEARED_TEXT;
			continue;
		}

		if (i->get_type() == hotkey_item::BY_KEYCODE) {
			item["key"] = SDL_GetKeyName(SDLKey(i->get_keycode()));
			item["shift"] = i->get_shift() ? "yes" : "no";
		} else if (i->get_type() == hotkey_item::BY_CHARACTER) {
			item["key"] = std::string(1, static_cast<char>(i->get_character()));
		}
		item["alt"] = i->get_alt() ? "yes" : "no";
		item["ctrl"] = i->get_ctrl() ? "yes" : "no";
		item["cmd"] = i->get_cmd() ? "yes" : "no";
	}
}
Example #3
0
void save_hotkeys(config& cfg)
{
	cfg.clear_children(hotkey_tag_name);

	for(std::map<int, hotkey_item>::iterator it = hotkeys.begin(); it != hotkeys.end(); ++ it) {
		hotkey_item& key = it->second;
		if (key.hidden() || key.get_type() == hotkey_item::UNBOUND)
			continue;

		config& item = cfg.add_child(hotkey_tag_name);
		item["command"] = key.get_command();
		if (key.get_type() == hotkey_item::CLEARED)
		{
			item["key"] = CLEARED_TEXT;
			continue;
		}

		if (key.get_type() == hotkey_item::BY_KEYCODE) {
			item["key"] = SDL_GetKeyName(SDLKey(key.get_keycode()));
			item["shift"] = key.get_shift();
		} else if (key.get_type() == hotkey_item::BY_CHARACTER) {
			item["key"] = utils::wchar_to_string(key.get_character());
		}
		item["alt"] = key.get_alt();
		item["ctrl"] = key.get_ctrl();
		item["cmd"] = key.get_cmd();
	}
}
Example #4
0
static void bind_key_events(std::vector<IOEventTrigger>& bindings,
		const char* keys, IOEvent::event_t ev, SDLMod required, SDLMod rejected,
		bool allow_heldkey) {
	for (int i = 0; keys[i] != '\0'; i++) {
		IOEvent event(ev, i);
		IOEventTrigger trigger(event, IOEventTrigger::NONE, SDLKey(keys[i]),
				required, rejected, allow_heldkey);
		bindings.push_back(trigger);
	}
}
Example #5
0
void TCOD_noteye_check() {
  if(!tnoteye.mode) return;
  TCOD_noteye_init();

  if(delayed) TCOD_noteye_sendscreen(tbuffer);
  
  int i;
  for(i=0; i < MAXCLIENT; i++) if(client[i] == NULL) break;
  if(i < MAXCLIENT && i < tnoteye.maxClients) {
    TCPsocket skt = SDLNet_TCP_Accept(server->socket);
    if(skt) {
      client[i] = new NTCPStream(skt);
      client[i]->writeStr("NotEye stream");
      client[i]->writeInt(NOTEYEVER);
      client[i]->writeStr(tnoteye.name);
      if(scr) {
        TCOD_noteye_render();
        client[i]->writeInt(nepScreen);
        client[i]->writeScr(scr);
        client[i]->flush();
        }
      }
    }
  
  for(int i=0; i < tnoteye.maxClients; i++) while(client[i] && client[i]->ready()) {
    if(client[i]->eof()) {
      delete client[i];
      client[i] = NULL;
      }
    else {
      int nep = client[i]->readInt();
      if(nep == nepKey) {
        SDL_Event ev;
        SDL_KeyboardEvent& kev(ev.key);
        kev.keysym.sym = SDLKey(client[i]->readInt());
        kev.keysym.mod = SDLMod(client[i]->readInt());
        ev.type = client[i]->readInt() == evKeyDown ? SDL_KEYDOWN : SDL_KEYUP;
        kev.keysym.unicode = client[i]->readInt();
        SDL_PushEvent(&ev);
        }
      else if(nep == nepMouse) {
        tnoteye.x = client[i]->readInt();
        tnoteye.y = client[i]->readInt();
        tnoteye.state = client[i]->readInt();
        tnoteye.active = true;
        }
      else if(nep == nepMessage) {
        string s = client[i]->readStr();
        TCOD_noteye_writestr(s.c_str());
        }
      else fprintf(stderr, "Unknown NEP: %d\n", nep);
      }
    }
  }
Example #6
0
/**
 * Exampe strings:
 *   Key55
 *   Joy0Button2
 *   Joy0Hat0Dir3
 */
bool KeyBindingFromString(const std::string &str, KeyBinding *kb)
{
	const char *digits = "1234567890";
	const char *p = str.c_str();

	if (strncmp(p, "Key", 3) == 0) {
		kb->type = KEYBOARD_KEY;
		p += 3;

		kb->u.keyboard.key = SDLKey(atoi(p));
		p += strspn(p, digits);

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

		return true;

	} else if (strncmp(p, "Joy", 3) == 0) {
		p += 3;

		int joy = atoi(p);
		p += strspn(p, digits);

		if (strncmp(p, "Button", 6) == 0) {
			p += 6;
			kb->type = JOYSTICK_BUTTON;
			kb->u.joystickButton.joystick = joy;
			kb->u.joystickButton.button = atoi(p);
			return true;
		} 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);
			return true;
		}

		return false;
	}

	return false;
}
Example #7
0
void IOController::trigger_events(const BBox& playing_area) {

	// Loop over all possible keys to find active key states
	for (int i = SDLK_FIRST + 1; i < SDLK_LAST; i++) {
		IOEventTrigger::trigger_t notrigger = IOEventTrigger::NONE;
		if (iostate.key_press_states[i]) {
			// Trigger key press events
			__trigger_events(notrigger, SDLKey(i), iostate.keymod, false);
		} else if (iostate.key_down_states[i]) {
			// Trigger key hold events
			__trigger_events(notrigger, SDLKey(i), iostate.keymod, true);
		}
	}

	if (playing_area.contains(mouse_x(), mouse_y())) {
		if (mouse_left_click() || mouse_left_down()) {
			__trigger_events(IOEventTrigger::MOUSE_LEFT_CLICK, SDLKey(0),
					iostate.keymod, !mouse_left_click());
		}
		if (mouse_right_click() || mouse_right_down()) {
			__trigger_events(IOEventTrigger::MOUSE_RIGHT_CLICK, SDLKey(0),
					iostate.keymod, !mouse_left_click());
		}
	}

	if (iostate.mouse_middleclick || iostate.mouse_middledown) {
		__trigger_events(IOEventTrigger::MOUSE_MIDDLE_CLICK, SDLKey(0),
				iostate.keymod, !iostate.mouse_middleclick);
	}
	if (mouse_upwheel()) {
		__trigger_events(IOEventTrigger::MOUSE_WHEEL_UP, SDLKey(0),
				iostate.keymod, false);
	}
	if (mouse_downwheel()) {
		__trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0),
				iostate.keymod, false);
	}
	if (mouse_downwheel()) {
		__trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0),
				iostate.keymod, false);
	}
}
Example #8
0
//#################### LOADING METHODS ####################
InputBinding_CPtr BindingFile::load(const std::string& filename)
{
	std::map<InputAction,Inputter_CPtr> inputters;

	// Construct the string -> key map.
	std::map<std::string,SDLKey> stringToKey;
	for(int i=0; i<SDLK_LAST; ++i)
	{
		SDLKey key = SDLKey(i);
		std::string name = SDL_GetKeyName(key);
		stringToKey.insert(std::make_pair(name,key));
	}

	XMLLexer_Ptr lexer(new XMLLexer(filename));
	XMLParser parser(lexer);
	XMLElement_CPtr root = parser.parse();
	XMLElement_CPtr bindingElt = root->find_unique_child("binding");

	std::vector<XMLElement_CPtr> actionElts = bindingElt->find_children("action");
	for(size_t i=0, size=actionElts.size(); i<size; ++i)
	{
		const XMLElement_CPtr& actionElt = actionElts[i];
		const std::string& actionName = actionElt->attribute("name");
		const std::string& actionInput = actionElt->attribute("input");

		InputAction action = lexical_cast<InputAction>(actionName);
		Inputter_CPtr inputter;

		std::map<std::string,SDLKey>::const_iterator jt = stringToKey.find(actionInput);
		if(jt != stringToKey.end())
		{
			inputter.reset(new KeyInputter(jt->second));
		}
		else if(actionInput == "left mouse") inputter.reset(new MouseButtonInputter(MOUSE_BUTTON_LEFT));
		else if(actionInput == "middle mouse") inputter.reset(new MouseButtonInputter(MOUSE_BUTTON_MIDDLE));
		else if(actionInput == "right mouse") inputter.reset(new MouseButtonInputter(MOUSE_BUTTON_RIGHT));
		else throw Exception("Unknown input action: " + actionInput);

		inputters.insert(std::make_pair(action, inputter));
	}

	return InputBinding_CPtr(new InputBinding(inputters));
}
Example #9
0
void hotkey_item::set_key(int character, int keycode, bool shift, bool ctrl, bool alt, bool cmd)
{
	const std::string keyname = SDL_GetKeyName(SDLKey(keycode));

	LOG_G << "setting hotkey: char=" << lexical_cast<std::string>(character)
		   << " keycode="  << lexical_cast<std::string>(keycode) << " "
		   << (shift ? "shift," : "")
		   << (ctrl ? "ctrl," : "")
		   << (alt ? "alt," : "")
		   << (cmd ? "cmd," : "")
		   << "\n";

	// Sometimes control modifies by -64, ie ^A == 1.
	if (character < 64 && ctrl) {
		if (shift)
			character += 64;
		else
			character += 96;
		LOG_G << "Mapped to character " << lexical_cast<std::string>(character) << "\n";
	}

	// For some reason on Mac OS, if cmd and shift are down, the character doesn't get upper-cased
	if (cmd && character > 96 && character < 123 && shift)
		character -= 32;

	// We handle simple cases by character, others by the actual key.
	if (isprint(character) && !isspace(character)) {
		type_ = BY_CHARACTER;
		character_ = character;
		ctrl_ = ctrl;
		alt_ = alt;
		cmd_ = cmd;
		LOG_G << "type = BY_CHARACTER\n";
	} else {
		type_ = BY_KEYCODE;
		keycode_ = keycode;
		shift_ = shift;
		ctrl_ = ctrl;
		alt_ = alt;
		cmd_ = cmd;
		LOG_G << "type = BY_KEYCODE\n";
	}
}
Example #10
0
void
loop_keyboard_func(SDLKey key, SDLMod mod, bool release)
{
	if(GameMode::currentMode!=NULL){		
		if (key < SDLK_UP) {
            if ( isalpha( key ) ) {
                key = SDLKey(tolower( key ));
            }
        }
		
		if(GameMode::currentMode->keyboardEvent(key,release)) return;
		else if(release){
			if(GameMode::currentMode->keyReleaseEvent(key)) return;
		}else{
			if(GameMode::currentMode->keyPressEvent(key)) return;
		}
		ppogl::UIManager::getInstance().keyboardEvent(key,mod,release);
	}
}
Example #11
0
/* Listen for client commands
 */
int ClientConnection::recieve_thread_func()
{
	while (status == 0)
	{
		Uint16 msg;
		int result = SDLNet_TCP_Recv(socket, &msg, 2);
		if (result == 1)
			result = SDLNet_TCP_Recv(socket, (char*) (&msg) + 1, 1); // Recieve the other half
		if (result <= 0)
		{
			if (result == 0)
				printf("%s:%d SDLNet_TCP_Recv: Connection closed by peer\n", __FILE__, __LINE__);
			else
				printf("%s:%d SDLNet_TCP_Recv: %s\n", __FILE__, __LINE__, SDLNet_GetError());
			status = -1;
			// An error may have occured, but sometimes you can just ignore it
			// It may be good to disconnect sock because it is likely invalid now.
			break;
		}

		Uint32 arrival_time = SDL_GetTicks();
		SDLKey key = (SDLKey) SDLNet_Read16(&msg);
		bool released = false;
		if (Sint16(key) < 0)
		{
			released = true;
			key = SDLKey(-Sint16(key));
		}

		ReceivedPacket packet =
		{ key, released, arrival_time };

		//printf("Recieved %s (%d) from %s\n", SDL_GetKeyName(key), key, name.c_str());

		SDL_LockMutex(messages_mutex);
		messages.push_back(packet);
		SDL_UnlockMutex(messages_mutex);

	}
	//SDLNet_TCP_Close(client_data->socket);
	return 0;
}
Example #12
0
void save_hotkeys(config& cfg)
{
	cfg.clear_children(hotkey_tag_name);

	for(std::vector<hotkey_item>::iterator i = hotkeys_.begin(); i != hotkeys_.end(); ++i) {
		if (i->hidden() || i->get_type() == hotkey_item::UNBOUND || !i->is_in_active_scope())
			continue;

		config& item = cfg.add_child(hotkey_tag_name);
		item["command"] = i->get_command();
		if (i->get_type() == hotkey_item::CLEARED)
		{
			item["key"] = CLEARED_TEXT;
			continue;
		}

		if (i->get_type() == hotkey_item::BUTTON)
		{
			item["joystick"] = i->get_joystick();
			item["button"] = i->get_button();
		}

		if (i->get_type() == hotkey_item::HAT)
		{
			item["joystick"] = i->get_joystick();
			item["hat"] = i->get_hat();
			item["value"] = i->get_value();
		}

		if (i->get_type() == hotkey_item::BY_KEYCODE) {
			item["key"] = SDL_GetKeyName(SDLKey(i->get_keycode()));
			item["shift"] = i->get_shift();
		} else if (i->get_type() == hotkey_item::BY_CHARACTER) {
			item["key"] = utils::wchar_to_string(i->get_character());
		}
		item["alt"] = i->get_alt();
		item["ctrl"] = i->get_ctrl();
		item["cmd"] = i->get_cmd();
	}
}
Example #13
0
void set_keys(
	short *keycodes)
{
	struct key_definition *definitions;
	
	/* all of them have the same ordering, so which one we pick is moot. */
	definitions = all_key_definitions[_standard_keyboard_setup]; 
	
	for (unsigned index= 0; index<NUMBER_OF_STANDARD_KEY_DEFINITIONS; index++)
	{
#ifdef SDL
		current_key_definitions[index].offset= SDLKey(keycodes[index]);
#else
		current_key_definitions[index].offset= keycodes[index];
#endif
		current_key_definitions[index].action_flag= definitions[index].action_flag;
#ifdef mac
		assert(current_key_definitions[index].offset <= 0x7f);
#endif
	}
	precalculate_key_information();
}
Example #14
0
int F1SpiritApp::gameoptions_cycle(KEYBOARDSTATE *k)
{
	if (state_cycle == 0) {

		menu_fading = -1;
		menu_fading_ctnt = MENU_CONSTANT;
		menu_current_menu = 5;

		/* ... */

	} 

	if (menu_fading == 0) {} else {
		if (menu_fading > 0)
			menu_fading_ctnt++;
		else
			menu_fading_ctnt--;

		if (menu_fading_ctnt <= 0 && menu_fading == -1) {
			menu_fading = 0;
		} 

		if (menu_fading_ctnt >= MENU_CONSTANT && menu_fading == 1) {
			menu_fading = 0;
			race_game->race_state = 3;
			race_game->race_state_timmer = 0;
			race_desired_action = 0;
			Sound_unpause_music();
			race_game->resumeSFX();
			race_game->rain_channel = 0;
			return APP_STATE_RACE;
		} 

		if (menu_fading_ctnt >= MENU_CONSTANT && menu_fading == 2) {
			menu_fading = 0;
			Sound_unpause_music();
			race_game->resumeSFX();
			race_game->rain_channel = 0;
			return APP_STATE_RACE;
		} 

		if (menu_fading_ctnt >= MENU_CONSTANT && menu_fading == 3) {
			menu_fading = 0;
			race_game->race_state = 3;
			race_game->race_state_timmer = 0;
			race_desired_action = 1;
			Sound_unpause_music();
			race_game->resumeSFX();
			race_game->rain_channel = 0;
			return APP_STATE_RACE;
		} 
	} 

	if (state_cycle == 0 || menu_state == MENU_CONSTANT*2 || menu_redefining_key || menu_force_rebuild_menu) {
		int browsing = 0;

		if (menu_options[1] != 0)
			browsing = 1;

		if (state_cycle != 0) {

#ifdef F1SPIRIT_DEBUG_MESSAGES
			output_debug_message("Executing gameoptions action: %i(%i)\n", menu_option_type[browsing][menu_selected[browsing]], menu_option_parameter[browsing][menu_selected[browsing]]);
#endif

			switch (menu_option_type[browsing][menu_selected[browsing]]) {

				case 0:  /* QUIT RACE: */
					menu_fading = 1;
					menu_fading_ctnt = 0;
					break;

				case 1:  /* MENU CHANGE: */
					menu_current_menu = menu_option_parameter[browsing][menu_selected[browsing]];

					if (menu_current_menu == 23) {
						menu_readme_start_y = 0;
						menu_readme_move_y = 0;
					} 

					break;

				case 7:  /* SET A KEY: */
					break;

				case 8:  /* SET A JOYSTICK: */
					menu_current_menu = menu_option_parameter[browsing][menu_selected[browsing]];

					{
						int i;
						FILE *fp;
						PlayerCCar *v;
						current_player->set_joystick(menu_selected[0] - 7, menu_selected[1] - 1);
						i = menu_selected[0] - 7;
						v = race_game->player_cars[i];

						if (menu_selected[1] - 1 == -1) {
							v->up = current_player->get_key(i, 0);
							v->down = current_player->get_key(i, 1);
							v->left = current_player->get_key(i, 2);
							v->right = current_player->get_key(i, 3);
							v->accelerate = current_player->get_key(i, 4);
							v->brake = current_player->get_key(i, 5);
						} else {
							int j = menu_selected[1] - 1;
							v->up = k->joystick_0_pos + j * k->joystick_size + 2;
							v->down = k->joystick_0_pos + j * k->joystick_size + 3;
							v->left = k->joystick_0_pos + j * k->joystick_size;
							v->right = k->joystick_0_pos + j * k->joystick_size + 1;
							v->accelerate = k->joystick_0_pos + j * k->joystick_size + 4;
							v->brake = k->joystick_0_pos + j * k->joystick_size + 5;
						} 

						if (v->up < 0)
							v->up = 0;

						if (v->up >= k->k_size)
							v->up = 0;

						if (v->down < 0)
							v->down = 0;

						if (v->down >= k->k_size)
							v->down = 0;

						if (v->left < 0)
							v->left = 0;

						if (v->left >= k->k_size)
							v->left = 0;

						if (v->up < 0)
							v->up = 0;

						if (v->right >= k->k_size)
							v->right = 0;

						if (v->up < 0)
							v->up = 0;

						if (v->accelerate >= k->k_size)
							v->accelerate = 0;

						if (v->up < 0)
							v->up = 0;

						if (v->brake >= k->k_size)
							v->brake = 0;

						v = 0;

						fp = f1open(player_filename, "wb", USERDATA);

						if (fp != 0) {
							current_player->save(fp);
							fclose(fp);
						} 
					}

					break;

				case 11:  /* MENU CHANGE TO SUBMENU: */
					menu_current_menu = menu_option_parameter[browsing][menu_selected[browsing]];
					break;

				case 13: { /* CONTINUE RACE: */
						menu_selecting_player++;
						menu_fading = 2;
						menu_fading_ctnt = 0;
					}

					break;

				case 15: { /* CHANGE MUSIC VOLUME: */
					}

					break;

				case 16: { /* CHANGE SFX VOLUME: */
					}

					break;

				case 24: { /* RESTART RACE: */
						menu_selecting_player++;
						menu_fading = 3;
						menu_fading_ctnt = 0;

						// also stop music/sfx on restart
						Mix_HaltChannel(SFX_RAIN);
						Sound_pause_music();
					}

					break;

			} 
		} 

		delete []menu_title[0];

		menu_title[0] = 0;

		delete []menu_options[0];

		menu_options[0] = 0;

		delete []menu_title[1];

		menu_title[1] = 0;

		delete []menu_options[1];

		menu_options[1] = 0;

#ifdef F1SPIRIT_DEBUG_MESSAGES

		output_debug_message("Generating a new gameoptions menu: %i\n", menu_current_menu);

#endif

		switch (menu_current_menu) {

			case 5:

			case 6:

			case 7:

			case 8:

			case 19:

			case 20:

			case 22:

			case 24: {
					int i, l, pos;
					char *volumes[5] = {"NONE", "LOW ", "MED.", "HIGH", "MAX."};

					l = strlen("PLAYER 000\n") * current_player->get_nplayers() + strlen("CONTINUE\nQUIT\nRESTART\n\nMUSIC VOL: MAX.\nSFX VOL: MAX.\nPLAYER 1\nPLAYER 2\nPLAYER 3\nPLAYER 4\n") + 1;
					if (arcade)
						l = strlen("CONTINUE\nQUIT\n\nMUSIC VOL: MAX.\nSFX VOL: MAX.\nPLAYER 1\n")+1;
					menu_options[0] = new char[l];
					pos = 0;
					{
						int v1 = 0, v2 = 0;
						v1 = current_player->get_music_volume() / 32;
						v2 = current_player->get_sfx_volume() / 32;

						if (arcade)
							sprintf(menu_options[0], "CONTINUE\nQUIT\n\nMUSIC VOL: %s\nSFX VOL: %s\n", volumes[v1], volumes[v2]);
						else
							sprintf(menu_options[0], "CONTINUE\nQUIT\nRESTART\n\nMUSIC VOL: %s\nSFX VOL: %s\n", volumes[v1], volumes[v2]);
					}

					pos = strlen(menu_options[0]);

					if(arcade) {
						for (i = 0;i < 1;i++) {
							sprintf(menu_options[0] + pos, "PLAYER %i\n", i + 1);
							pos = strlen(menu_options[0]);
						} 
					} else 
					{
						for (i = 0;i < current_player->get_nplayers();i++) {
							sprintf(menu_options[0] + pos, "PLAYER %i\n", i + 1);
							pos = strlen(menu_options[0]);
						} 
					}

					menu_title[0] = new char[strlen("PAUSE") + 1];

					strcpy(menu_title[0], "PAUSE");

					if (arcade)
						menu_noptions[0] = 5+1;
					else
						menu_noptions[0] = current_player->get_nplayers() + 7;

					menu_option_type[0][0] = 13;
					menu_option_type[0][1] = 0;

					if (arcade) {
						menu_option_type[0][2] = -1;
						menu_option_type[0][3] = 15;
						menu_option_type[0][4] = 16;
						menu_option_type[0][5] = 11;
						menu_option_parameter[0][5] = 22;
					} else 
					{
						menu_option_type[0][2] = 24;
						menu_option_type[0][3] = -1;
						menu_option_type[0][4] = 15;
						menu_option_type[0][5] = 16;
						menu_option_type[0][6] = 11;
						menu_option_parameter[0][6] = 22;

						for (i = 0;i < current_player->get_nplayers();i++) {
							menu_option_type[0][i + 7] = 11;
							menu_option_parameter[0][i + 7] = 22;
						} 
					}

					menu_first_option[0] = 0;
				}

				if (menu_current_menu == 22) {
					menu_title[1] = 0;
					menu_options[1] = new char[strlen("KEYBOARD CFG.\nJOYSTICK/KEYS\nBACK\n") + 1];
					strcpy(menu_options[1], "KEYBOARD CFG.\nJOYSTICK/KEYS\nBACK\n");
					menu_noptions[1] = 3;
					menu_option_type[1][0] = 1;
					menu_option_type[1][1] = 1;
					menu_option_type[1][2] = 1;
					menu_option_parameter[1][0] = 7;
					menu_option_parameter[1][1] = 8;
					menu_option_parameter[1][2] = 5;
					menu_first_option[1] = 0;
				} 

				if (menu_current_menu == 7) {
					char tmp[256];
					menu_title[1] = 0;
					sprintf(tmp, "GEAR UP: %s\nGEAR DOWN: %s\nLEFT: %s\nRIGHT: %s\nACCEL.: %s\nBRAKE: %s\nBACK\n",
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 0))),
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 1))),
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 2))),
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 3))),
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 4))),
					        SDL_GetKeyName(SDLKey(current_player->get_key(menu_selected[0] - 6, 5))));
					//    strupr(tmp);
					menu_options[1] = new char[strlen(tmp) + 1];
					strcpy(menu_options[1], tmp);
					menu_noptions[1] = 7;
					menu_option_type[1][0] = 7;
					menu_option_type[1][1] = 7;
					menu_option_type[1][2] = 7;
					menu_option_type[1][3] = 7;
					menu_option_type[1][4] = 7;
					menu_option_type[1][5] = 7;
					menu_option_type[1][6] = 1;
					menu_option_parameter[1][0] = 7;
					menu_option_parameter[1][1] = 7;
					menu_option_parameter[1][2] = 7;
					menu_option_parameter[1][3] = 7;
					menu_option_parameter[1][4] = 7;
					menu_option_parameter[1][5] = 7;
					menu_option_parameter[1][6] = 6;
					menu_first_option[1] = 0;

					if (current_player->get_joystick(menu_selected[0]) != -1) {
						current_player->set_joystick(menu_selected[0], -1);
						{
							FILE *fp;

							fp = f1open(player_filename, "wb", USERDATA);

							if (fp != 0) {
								current_player->save(fp);
								fclose(fp);
							} 
						}
					} 
				} 

				if (menu_current_menu == 8) {
					int nj = SDL_NumJoysticks();
					int i, pos;

					menu_options[1] = new char[nj * strlen("JOYSTICK 000\n") + strlen("KEYBOARD\nBACK\n") + 1];
					pos = 0;
					sprintf(menu_options[1] + pos, "KEYBOARD\n");
					pos = strlen(menu_options[1]);

					for (i = 0;i < nj;i++) {
						sprintf(menu_options[1] + pos, "JOYSTICK %i\n", i);
						pos = strlen(menu_options[1]);
					} 

					sprintf(menu_options[1] + pos, "BACK\n");

					menu_noptions[1] = nj + 2;

					for (i = 0;i < nj + 1;i++) {
						menu_option_type[1][i] = 8;
						menu_option_parameter[1][i] = 6;
					} 

					menu_option_type[1][nj + 1] = 1;

					menu_option_parameter[1][nj + 1] = 6;

					menu_first_option[1] = 0;
				} 
		} 


		if ((menu_options[1] == 0 && menu_prev_nmenus != 2 && !menu_force_rebuild_menu)) {
			menu_selected[0] = 0;
			menu_selected_timmer[0] = 0;
		} 

		if (!menu_redefining_key && !menu_force_rebuild_menu)
			menu_selected[1] = 0;

		if (!menu_redefining_key && !menu_force_rebuild_menu)
			menu_selected_timmer[1] = 0;

		if (!menu_redefining_key && !menu_force_rebuild_menu)
			menu_state = 0;

		menu_force_rebuild_menu = false;
	} else {
		if (menu_fading == 0) {
			if (menu_state < MENU_CONSTANT)
				menu_state++;

			if (menu_state == MENU_CONSTANT) {
				menu_prev_nmenus = 0;

				if (menu_options[0] != 0)
					menu_prev_nmenus++;

				if (menu_options[1] != 0)
					menu_prev_nmenus++;
			} 

			if (menu_state > MENU_CONSTANT)
				menu_state++;
		} 
	} 


	if (menu_readme_move_y != 0) {
		menu_readme_start_y += menu_readme_move_y;

		if (menu_readme_move_y > 0)
			menu_readme_move_y--;

		if (menu_readme_move_y < 0)
			menu_readme_move_y++;

		if (menu_readme_start_y < 0)
			menu_readme_start_y = 0;
	} 

	if (menu_state == MENU_CONSTANT) {
		int browsing = 0;

		if (menu_options[1] != 0)
			browsing = 1;

		if (!menu_redefining_key) {
			if ((k->keyboard[SDLK_DOWN] && !k->old_keyboard[SDLK_DOWN]) ||
			        (k->keyboard[SDLK_RIGHT] && !k->old_keyboard[SDLK_RIGHT])) {
				if ((k->keyboard[SDLK_RIGHT] && !k->old_keyboard[SDLK_RIGHT]) &&
				        (menu_option_type[browsing][menu_selected[browsing]] == 15 ||
				         menu_option_type[browsing][menu_selected[browsing]] == 16)) {
					if (menu_option_type[browsing][menu_selected[browsing]] == 15) {
						int v = current_player->get_music_volume();

						if (v == 128)
							current_player->set_music_volume(0);

						if (v == 96)
							current_player->set_music_volume(128);

						if (v == 64)
							current_player->set_music_volume(96);

						if (v == 32)
							current_player->set_music_volume(64);

						if (v == 0)
							current_player->set_music_volume(32);
					} else {
						int v = current_player->get_sfx_volume();

						if (v == 128)
							current_player->set_sfx_volume(0);

						if (v == 96)
							current_player->set_sfx_volume(128);

						if (v == 64)
							current_player->set_sfx_volume(96);

						if (v == 32)
							current_player->set_sfx_volume(64);

						if (v == 0)
							current_player->set_sfx_volume(32);
					} 

					menu_force_rebuild_menu = true;

					Sound_play(S_menu_move, 128);

					{
						FILE *fp;

						fp = f1open(player_filename, "wb", USERDATA);

						if (fp != 0) {
							current_player->save(fp);
							fclose(fp);
						} 
					}
				} else {
					menu_selected[browsing]++;

					if (menu_option_type[browsing][menu_selected[browsing]] == -1)
						menu_selected[browsing]++;

					Sound_play(S_menu_move, 128);

					if (menu_selected[browsing] >= menu_noptions[browsing])
						menu_selected[browsing] -= menu_noptions[browsing];

					menu_selected_timmer[browsing] = 0;
				} 
			} 

			if ((k->keyboard[SDLK_UP] && !k->old_keyboard[SDLK_UP]) ||
			        (k->keyboard[SDLK_LEFT] && !k->old_keyboard[SDLK_LEFT])) {

				if ((k->keyboard[SDLK_LEFT] && !k->old_keyboard[SDLK_LEFT]) &&
				        (menu_option_type[browsing][menu_selected[browsing]] == 15 ||
				         menu_option_type[browsing][menu_selected[browsing]] == 16)) {
					if (menu_option_type[browsing][menu_selected[browsing]] == 15) {
						int v = current_player->get_music_volume();

						if (v == 128)
							current_player->set_music_volume(96);

						if (v == 96)
							current_player->set_music_volume(64);

						if (v == 64)
							current_player->set_music_volume(32);

						if (v == 32)
							current_player->set_music_volume(0);

						if (v == 0)
							current_player->set_music_volume(128);
					} else {
						int v = current_player->get_sfx_volume();

						if (v == 128)
							current_player->set_sfx_volume(96);

						if (v == 96)
							current_player->set_sfx_volume(64);

						if (v == 64)
							current_player->set_sfx_volume(32);

						if (v == 32)
							current_player->set_sfx_volume(0);

						if (v == 0)
							current_player->set_sfx_volume(128);
					} 

					menu_force_rebuild_menu = true;

					Sound_play(S_menu_move, 128);

					{
						FILE *fp;

						fp = f1open(player_filename, "wb", USERDATA);

						if (fp != 0) {
							current_player->save(fp);
							fclose(fp);
						} 
					}
				} else {
					menu_selected[browsing]--;

					if (menu_option_type[browsing][menu_selected[browsing]] == -1)
						menu_selected[browsing]--;

					Sound_play(S_menu_move, 128);

					if (menu_selected[browsing] < 0)
						menu_selected[browsing] += menu_noptions[browsing];

					menu_selected_timmer[browsing] = 0;
				} 
			} 

			if (k->keyboard[SDLK_ESCAPE] && !k->old_keyboard[SDLK_ESCAPE]) {
				if (menu_current_menu == 5)
					menu_selected[browsing] = 0;
				else
					menu_selected[browsing] = menu_noptions[browsing] - 1;

				Sound_play(S_menu_select, 128);

				menu_state++;
			} 

			if (menu_option_type[browsing][menu_selected[browsing]] != 2 &&
			        ((k->keyboard[SDLK_SPACE] && !k->old_keyboard[SDLK_SPACE])
#ifdef PANDORA
					||  (k->keyboard[SDLK_PAGEDOWN] && !k->old_keyboard[SDLK_PAGEDOWN])
#endif
				))
				{
				if (menu_option_type[browsing][menu_selected[browsing]] != 19 &&
				        menu_option_type[browsing][menu_selected[browsing]] != 20)
					Sound_play(S_menu_select, 128);

				if (menu_option_type[browsing][menu_selected[browsing]] == 7) {
					/* redefining a key: */
					menu_redefining_key = true;
				} else {
					if (menu_option_type[browsing][menu_selected[browsing]] == 15 ||
					        menu_option_type[browsing][menu_selected[browsing]] == 16 ||
					        menu_option_type[browsing][menu_selected[browsing]] == 19 ||
					        menu_option_type[browsing][menu_selected[browsing]] == 20) {
						if (menu_option_type[browsing][menu_selected[browsing]] == 15) {
							int v = current_player->get_music_volume();

							if (v == 128)
								current_player->set_music_volume(0);

							if (v == 96)
								current_player->set_music_volume(128);

							if (v == 64)
								current_player->set_music_volume(96);

							if (v == 32)
								current_player->set_music_volume(64);

							if (v == 0)
								current_player->set_music_volume(32);
						} 

						if (menu_option_type[browsing][menu_selected[browsing]] == 16) {
							int v = current_player->get_sfx_volume();

							if (v == 128)
								current_player->set_sfx_volume(0);

							if (v == 96)
								current_player->set_sfx_volume(128);

							if (v == 64)
								current_player->set_sfx_volume(96);

							if (v == 32)
								current_player->set_sfx_volume(64);

							if (v == 0)
								current_player->set_sfx_volume(32);
						} 

						menu_force_rebuild_menu = true;
					} else {
						menu_state++;
					} 
				} 
			} 


		} else {
			int i, np;
			np = menu_selected[0] - 6;
			PlayerCCar *v = race_game->player_cars[np];

			for (i = 0;i < SDLK_LAST;i++) {
				if (k->keyboard[i] && !k->old_keyboard[i]) {
					current_player->set_key(np, menu_selected[1], i);

					if (current_player->get_joystick(np) == -1) {
						v->up = current_player->get_key(np, 0);
						v->down = current_player->get_key(np, 1);
						v->left = current_player->get_key(np, 2);
						v->right = current_player->get_key(np, 3);
						v->accelerate = current_player->get_key(np, 4);
						v->brake = current_player->get_key(np, 5);
					} 

					if (v->up < 0)
						v->up = 0;

					if (v->up >= k->k_size)
						v->up = 0;

					if (v->down < 0)
						v->down = 0;

					if (v->down >= k->k_size)
						v->down = 0;

					if (v->left < 0)
						v->left = 0;

					if (v->left >= k->k_size)
						v->left = 0;

					if (v->up < 0)
						v->up = 0;

					if (v->right >= k->k_size)
						v->right = 0;

					if (v->up < 0)
						v->up = 0;

					if (v->accelerate >= k->k_size)
						v->accelerate = 0;

					if (v->up < 0)
						v->up = 0;

					if (v->brake >= k->k_size)
						v->brake = 0;

					v = 0;

					menu_redefining_key = false;

					menu_force_rebuild_menu = true;

					Sound_play(S_menu_select, 128);

					{
						FILE *fp;

						fp = f1open(player_filename, "wb", USERDATA);

						if (fp != 0) {
							current_player->save(fp);
							fclose(fp);
						} 
					}
				} 
			} 
		} 
	} 

	menu_selected_timmer[0]++;

	menu_selected_timmer[1]++;

	return APP_STATE_GAMEOPTIONS;
} 
Example #15
0
/**
 * @brief Returns the human-readable name of a keyboard key.
 * @param key a keyboard key
 * @return the corresponding name
 */
const std::string InputEvent::get_keyboard_key_name(KeyboardKey key) {
  return SDL_GetKeyName(SDLKey(key));
}
Example #16
0
bool CARCONTROLMAP_LOCAL::Load(const std::string & controlfile, std::ostream & info_output, std::ostream & error_output)
{
	CONFIG controls_config;
	if (!controls_config.Load(controlfile))
	{
		info_output << "Failed to load car controls file " << controlfile << std::endl;
		return false;
	}

	controls.clear();
	for (CONFIG::const_iterator i = controls_config.begin(); i != controls_config.end(); ++i)
	{
		std::string type, name;
		if (!controls_config.GetParam(i, "type", type)) continue;
		if (!controls_config.GetParam(i, "name", name)) continue;

		CARINPUT::CARINPUT carinput = GetInputFromString(name);
		if (carinput == CARINPUT::INVALID)
		{
			error_output << "Unknown input type in section " << i->first << std::endl;
			continue;
		}

		CONTROL newctrl;
		if (type == "joy")
		{
			newctrl.joynum = 0;
			std::string joy_type;
			if (!controls_config.GetParam(i, "joy_index", newctrl.joynum, error_output)) continue;
			if (!controls_config.GetParam(i, "joy_type", joy_type, error_output)) continue;

			newctrl.type = CONTROL::JOY;
			if (joy_type == "button")
			{
				newctrl.joytype = CONTROL::JOYBUTTON;
				newctrl.joybutton = 0;
				newctrl.joypushdown = false;
				newctrl.onetime = false;
				if (!controls_config.GetParam(i, "joy_button", newctrl.joybutton, error_output)) continue;
				if (!controls_config.GetParam(i, "down", newctrl.joypushdown, error_output)) continue;
				if (!controls_config.GetParam(i, "once", newctrl.onetime, error_output)) continue;
			}
			else if (joy_type == "axis")
			{
				newctrl.joytype = CONTROL::JOYAXIS;
				int joy_axis = 0;
				std::string axis_type;
				float deadzone = 0.0;
				float exponent = 0.0;
				float gain = 0.0;
				if (!controls_config.GetParam(i, "joy_axis", joy_axis, error_output)) continue;
				if (!controls_config.GetParam(i, "joy_axis_type", axis_type, error_output)) continue;
				if (!controls_config.GetParam(i, "deadzone", deadzone, error_output)) continue;
				if (!controls_config.GetParam(i, "exponent", exponent, error_output)) continue;
				if (!controls_config.GetParam(i, "gain", gain, error_output)) continue;

				newctrl.joyaxis = joy_axis;
				if (axis_type == "positive")
				{
					newctrl.joyaxistype = CONTROL::POSITIVE;
				}
				else if (axis_type == "negative")
				{
					newctrl.joyaxistype = CONTROL::NEGATIVE;
				}
				else if (axis_type == "both")
				{
					newctrl.joyaxistype = CONTROL::BOTH;
				}
				else
				{
					error_output << "Error parsing controls, invalid joystick axis type in section " << i->first << std::endl;
					continue;
				}
				newctrl.deadzone = deadzone;
				newctrl.exponent = exponent;
				newctrl.gain = gain;
			}
			else
			{
				error_output << "Error parsing controls, invalid joystick type in section " << i->first << std::endl;
				continue;
			}
		}
		else if (type == "key")
		{
			newctrl.type = CONTROL::KEY;
			int keycode = 0;
			bool key_down = false;
			bool key_once = false;
			if (!controls_config.GetParam(i, "keycode", keycode))
			{
				std::string keyname;
				if (!controls_config.GetParam(i, "key", keyname, error_output)) continue;

				if (legacy_keycodes.find(keyname) == legacy_keycodes.end())
				{
					error_output << "Unknown keyname \"" << keyname << "\" parsing controls in section " << i->first << std::endl;
					continue;
				}
				else
				{
					keycode = legacy_keycodes[keyname];
				}
			}

			if (!controls_config.GetParam(i, "down", key_down, error_output)) continue;
			if (!controls_config.GetParam(i, "once", key_once, error_output)) continue;
			if (keycode < SDLK_LAST && keycode > SDLK_FIRST)
			{
				newctrl.keycode = SDLKey(keycode);
			}
			newctrl.keypushdown = key_down;
			newctrl.onetime = key_once;
		}
		else if (type == "mouse")
		{
			newctrl.type = CONTROL::MOUSE;

			std::string mouse_type = "";
			std::string mouse_direction = "";
			controls_config.GetParam(i, "mouse_type", mouse_type );
			if (mouse_type == "button")
			{
				int mouse_btn = 0;
				bool mouse_btn_down = false;
				bool mouse_btn_once = false;
				newctrl.mousetype = CONTROL::MOUSEBUTTON;
				controls_config.GetParam(i, "mouse_button", mouse_btn);
				controls_config.GetParam(i, "down", mouse_btn_down);
				controls_config.GetParam(i, "once", mouse_btn_once);
				newctrl.mbutton = mouse_btn;
				newctrl.mouse_push_down = mouse_btn_down;
				newctrl.onetime = mouse_btn_once;
			}
			else if (mouse_type == "motion")
			{
				newctrl.mousetype = CONTROL::MOUSEMOTION;
				controls_config.GetParam(i, "mouse_motion", mouse_direction);
				if (mouse_direction == "left")
				{
					newctrl.mdir = CONTROL::LEFT;
				}
				else if (mouse_direction == "right")
				{
					newctrl.mdir = CONTROL::RIGHT;
				}
				else if (mouse_direction == "up")
				{
					newctrl.mdir = CONTROL::UP;
				}
				else if (mouse_direction == "down")
				{
					newctrl.mdir = CONTROL::DOWN;
				}
				else
				{
					error_output << "Error parsing controls, invalid mouse direction type " << mouse_direction << " in section " << i->first << std::endl;
				}

				newctrl.deadzone=0;
				newctrl.exponent=1;
				newctrl.gain=1;
				controls_config.GetParam(i, "deadzone", newctrl.deadzone);
				controls_config.GetParam(i, "exponent", newctrl.exponent);
				controls_config.GetParam(i, "gain", newctrl.gain);
			}
			else
			{
				error_output << "Error parsing controls, invalid mouse type " << mouse_type << " in section " << i->first << std::endl;
			}
		}
		else
		{
			error_output << "Error parsing controls, invalid control type in section " << i->first << std::endl;
			continue;
		}

		controls[carinput].push_back(newctrl);
	}

	inputs.resize(CARINPUT::INVALID, 0.0); //this looks weird, but it initialize all inputs and sets them to zero
	lastinputs.resize(CARINPUT::INVALID, 0.0); //this looks weird, but it initialize all inputs and sets them to zero
	return true;
}
Example #17
0
void InputHandler::setMouseBtn(Uint8 btn, const std::string& action) {
  setKey(SDLKey(SDLK_LAST + btn), action);
}
Example #18
0
void InputHandler::mouseUp(Uint8 btn, Uint16 /*x*/, Uint16 /*y*/) {
  keyUp(SDLKey(SDLK_LAST + btn));
}
void
JoystickKeyboardController::read(const Reader& lisp)
{
  const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
  if(keymap_lisp) {
    keymap.clear();
    keymap_lisp->get("jump-with-up", jump_with_up_kbd);
    lisp::ListIterator iter(keymap_lisp);
    while(iter.next()) {
      if(iter.item() == "map") {
        int key = -1;
        std::string control;
        const lisp::Lisp* map = iter.lisp();
        map->get("key", key);
        map->get("control", control);
        if(key < SDLK_FIRST || key >= SDLK_LAST) {
          log_info << "Invalid key '" << key << "' in keymap" << std::endl;
          continue;
        }

        int i = 0;
        for(i = 0; Controller::controlNames[i] != 0; ++i) {
          if(control == Controller::controlNames[i])
            break;
        }
        if(Controller::controlNames[i] == 0) {
          log_info << "Invalid control '" << control << "' in keymap" << std::endl;
          continue;
        }
        keymap[SDLKey(key)] = Control(i);
      }
    }
  }

  const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick");
  if(joystick_lisp) {
    joystick_lisp->get("dead-zone", dead_zone);
    joystick_lisp->get("jump-with-up", jump_with_up_joy);
    lisp::ListIterator iter(joystick_lisp);
    while(iter.next()) {
      if(iter.item() == "map") {
        int button = -1;
        int axis   = 0;
        int hat    = -1;
        std::string control;
        const lisp::Lisp* map = iter.lisp();

        map->get("control", control);
        int i = 0;
        for(i = 0; Controller::controlNames[i] != 0; ++i) {
          if(control == Controller::controlNames[i])
            break;
        }
        if(Controller::controlNames[i] == 0) {
          log_info << "Invalid control '" << control << "' in buttonmap" << std::endl;
          continue;
        }

        if (map->get("button", button)) {
          if(button < 0 || button >= max_joybuttons) {
            log_info << "Invalid button '" << button << "' in buttonmap" << std::endl;
            continue;
          }
          bind_joybutton(0, button, Control(i));
        }

        if (map->get("axis",   axis)) {
          if (axis == 0 || abs(axis) > max_joyaxis) {
            log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
            continue;
          }
          bind_joyaxis(0, axis, Control(i));
        }

        if (map->get("hat",   hat)) {
          if (hat != SDL_HAT_UP   &&
              hat != SDL_HAT_DOWN &&
              hat != SDL_HAT_LEFT &&
              hat != SDL_HAT_RIGHT) {
            log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
            continue;
          } else {
            bind_joyhat(0, hat, Control(i));
          }
        }
      }
    }
  }
}
Example #20
0
IOController::IOController() {
	//XXX: For now we hardcode the specific actions
	SDLMod item_mod = SDLMod(KMOD_LSHIFT | KMOD_RSHIFT);
	char action_key = 'j';

	// Spell choice
	bind_key_events(event_bindings, "yuiop", IOEvent::ACTIVATE_SPELL_N,
			KMOD_NONE, item_mod/*rejected*/, true);
//	bind_key_events(event_bindings, "12345", IOEvent::ACTIVATE_SPELL_N,
//			KMOD_NONE, item_mod/*rejected*/, true);

//	// Item choice
//	bind_key_events(event_bindings, "yuiop67890", IOEvent::USE_ITEM_N, item_mod,
//			KMOD_NONE, false);
	bind_key_events(event_bindings, "1234567890", IOEvent::USE_ITEM_N,
			KMOD_NONE, KMOD_NONE, false);

	/*Scroll spell up*/
	{
		IOEvent event(IOEvent::TOGGLE_ACTION_UP);
		IOEventTrigger trigger1(event, IOEventTrigger::MOUSE_WHEEL_UP,
				SDLKey(0), KMOD_NONE, KMOD_NONE, true);
		IOEventTrigger trigger2(event, IOEventTrigger::NONE, SDLKey('e'),
				KMOD_NONE, KMOD_NONE);
		IOEventTrigger trigger3(event, IOEventTrigger::NONE, SDLKey('.'),
				KMOD_NONE, KMOD_NONE);
		event_bindings.push_back(trigger1);
		event_bindings.push_back(trigger2);
		event_bindings.push_back(trigger3);
	}
	/*Scroll spell down*/
	{
		IOEvent event(IOEvent::TOGGLE_ACTION_DOWN);
		IOEventTrigger trigger1(event, IOEventTrigger::MOUSE_WHEEL_DOWN,
				SDLKey(0), KMOD_NONE, KMOD_NONE, true);
		IOEventTrigger trigger2(event, IOEventTrigger::NONE, SDLKey('q'),
				KMOD_NONE, KMOD_NONE);
		IOEventTrigger trigger3(event, IOEventTrigger::NONE, SDLKey(','),
				KMOD_NONE, KMOD_NONE);
		event_bindings.push_back(trigger1);
		event_bindings.push_back(trigger2);
		event_bindings.push_back(trigger3);
	}
	/*Use weapon*/
	{
		IOEvent event(IOEvent::USE_WEAPON);
		IOEventTrigger trigger1(event, IOEventTrigger::MOUSE_MIDDLE_CLICK,
				SDLKey(0), KMOD_NONE, KMOD_NONE, true);
		IOEventTrigger trigger2(event, IOEventTrigger::NONE, SDLKey('h'),
				KMOD_NONE, KMOD_NONE, true);
		event_bindings.push_back(trigger1);
		event_bindings.push_back(trigger2);
	}

	/*Do autotarget action */
	{
		IOEvent event(IOEvent::AUTOTARGET_CURRENT_ACTION);
		IOEventTrigger trigger1(event, IOEventTrigger::NONE, SDLKey(action_key),
				KMOD_NONE, KMOD_NONE, true);
		IOEventTrigger trigger2(event, IOEventTrigger::NONE, SDLK_SPACE,
				KMOD_NONE, KMOD_NONE, true);
		event_bindings.push_back(trigger1);
		event_bindings.push_back(trigger2);
	}

	/*Do targetted action */{
		IOEvent event(IOEvent::MOUSETARGET_CURRENT_ACTION);
		IOEventTrigger trigger(event, IOEventTrigger::MOUSE_LEFT_CLICK,
				SDLKey(0), KMOD_NONE, KMOD_NONE, true);
		event_bindings.push_back(trigger);
	}

}
Example #21
0
const std::vector <float> & CARCONTROLMAP_LOCAL::ProcessInput(const std::string & joytype, EVENTSYSTEM_SDL & eventsystem, float steerpos, float dt, bool joy_200, float carms, float speedsens, int screenw, int screenh, float button_ramp, bool hgateshifter)
{
	assert(inputs.size() == CARINPUT::INVALID); //this looks weird, but it ensures that our inputs vector contains exactly one item per input
	assert(lastinputs.size() == CARINPUT::INVALID); //this looks weird, but it ensures that our inputs vector contains exactly one item per input

	for (std::map <CARINPUT::CARINPUT, std::vector <CONTROL> >::iterator n = controls.begin(); n != controls.end(); ++n)
	{
		float newval = 0.0;

		for (std::vector <CONTROL>::iterator i = n->second.begin(); i != n->second.end(); ++i)
		{
			bool handled = false;
			float tempval = newval;

			if (i->type == CONTROL::JOY)
			{
				//cout << "type joy" << std::endl;

				if (i->joytype == CONTROL::JOYAXIS)
				{
					float val = eventsystem.GetJoyAxis(i->joynum, i->joyaxis);
					if (i->joyaxistype == CONTROL::NEGATIVE)
						val = -val;
					val = ApplyDeadzone(i->deadzone,val);
					val = ApplyGain(i->gain,val);

					double absval = val;
					bool neg = false;
					if (val < 0)
					{
						absval = -val;
						neg = true;
					}
					val = ApplyExponent(i->exponent,absval);
					if (neg)
						val = -val;

					tempval = val;
					handled = true;
				}
				else if (i->joytype == CONTROL::JOYBUTTON)
				{
					TOGGLE button = eventsystem.GetJoyButton(i->joynum, i->joybutton);

					if (i->onetime)
					{
						if (i->joypushdown && button.GetImpulseRising())
							tempval = 1.0;
						else if (!i->joypushdown && button.GetImpulseFalling())
							tempval = 1.0;
						else
							tempval = 0.0;
						handled = true;
					}
					else
					{
						float downval = 1.0;
						float upval = 0.0;
						if (!i->joypushdown)
						{
							downval = 0.0;
							upval = 1.0;
						}

						tempval = Ramp(lastinputs[n->first], button.GetState() ? downval : upval, button_ramp, dt);
						handled = true;
					}
				}
			}
			else if (i->type == CONTROL::KEY)
			{
				//cout << "type key" << std::endl;

				EVENTSYSTEM_SDL::BUTTON_STATE keystate = eventsystem.GetKeyState(SDLKey(i->keycode));

				if (i->onetime)
				{
					if (i->keypushdown && keystate.just_down)
						tempval = 1.0;
					else if (!i->keypushdown && keystate.just_up)
						tempval = 1.0;
					else
						tempval = 0.0;
					handled = true;
				}
				else
				{
					float downval = 1.0;
					float upval = 0.0;
					if (!i->keypushdown)
					{
						downval = 0.0;
						upval = 1.0;
					}

					//if (inputs[n->first] != keystate.down ? downval : upval) std::cout << "Key ramp: " << i->keycode << ", " << n->first << std::endl;
					tempval = Ramp(lastinputs[n->first], keystate.down ? downval : upval, button_ramp, dt);

					handled = true;
				}
			}
			else if (i->type == CONTROL::MOUSE)
			{
				//cout << "type mouse" << std::endl;

				if (i->mousetype == CONTROL::MOUSEBUTTON)
				{
					//cout << "mousebutton" << std::endl;

					EVENTSYSTEM_SDL::BUTTON_STATE buttonstate = eventsystem.GetMouseButtonState(i->mbutton);

					if (i->onetime)
					{
						if (i->mouse_push_down && buttonstate.just_down)
							tempval = 1.0;
						else if (!i->mouse_push_down && buttonstate.just_up)
							tempval = 1.0;
						else
							tempval = 0.0;
						handled = true;
					}
					else
					{
						float downval = 1.0;
						float upval = 0.0;
						if (!i->mouse_push_down)
						{
							downval = 0.0;
							upval = 1.0;
						}

						tempval = Ramp(lastinputs[n->first], buttonstate.down ? downval : upval, button_ramp, dt);
						handled = true;
					}
				}
				else if (i->mousetype == CONTROL::MOUSEMOTION)
				{
					//cout << "mousemotion" << std::endl;

					std::vector <int> pos = eventsystem.GetMousePosition();
					//std::cout << pos[0] << "," << pos[1] << std::endl;

					float xval = (pos[0]-screenw/2.0)/(screenw/4.0);
					if (xval < -1) xval = -1;
					if (xval > 1) xval = 1;

					float yval = (pos[1]-screenh/2.0)/(screenh/4.0);
					if (yval < -1) yval = -1;
					if (yval > 1) yval = 1;

					float val = 0;

					if (i->mdir == CONTROL::UP)
						val = -yval;
					else if (i->mdir == CONTROL::DOWN)
						val = yval;
					else if (i->mdir == CONTROL::LEFT)
						val = -xval;
					else if (i->mdir == CONTROL::RIGHT)
						val = xval;

					if (val < 0)
						val = 0;
					else if (val > 1)
						val = 1;

					val = ApplyDeadzone(i->deadzone,val);
					val = ApplyGain(i->gain,val);

					if (val < 0)
						val = 0;
					else if (val > 1)
						val = 1;

					double absval = val;
					bool neg = false;
					if (val < 0)
					{
						absval = -val;
						neg = true;
					}
					val = ApplyExponent(i->exponent,absval);
					if (neg)
						val = -val;

					if (val < 0)
						val = 0;
					else if (val > 1)
						val = 1;

					tempval = val;

					//cout << val << std::endl;

					handled = true;
				}
				//else cout << "mouse???" << std::endl;
			}
			//else cout << "type invalid" << std::endl;

			if (tempval > newval)
				newval = tempval;

			assert(handled);
		}

		if (newval < 0)
			newval = 0;
		if (newval > 1.0)
			newval = 1.0;

		inputs[n->first] = newval;

		//std::cout << "New input value: " << inputs[n->first] << std::endl;
	}

	if (hgateshifter)
	{
		bool havegear = inputs[CARINPUT::FIRST_GEAR] ||
				inputs[CARINPUT::SECOND_GEAR] ||
				inputs[CARINPUT::THIRD_GEAR] ||
				inputs[CARINPUT::FOURTH_GEAR] ||
				inputs[CARINPUT::FIFTH_GEAR] ||
				inputs[CARINPUT::SIXTH_GEAR] ||
				inputs[CARINPUT::REVERSE];
		if (!havegear)
			inputs[CARINPUT::NEUTRAL] = 1.0;
	}

	lastinputs = inputs;

	//do steering processing
	ProcessSteering(joytype, steerpos, dt, joy_200, carms*2.23693629, speedsens);

	return inputs;
}