Skip to content

Commit

Permalink
PFBA: add option to configure menu(s) button(s)
Browse files Browse the repository at this point in the history
PFBA: minor cleanup/changes
  • Loading branch information
Cpasjuste committed Apr 26, 2017
1 parent f1f0002 commit 2f57e3f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 69 deletions.
4 changes: 3 additions & 1 deletion pfba/deps/libcross2d/src/sdl2/sdl2_input.cpp
Expand Up @@ -16,7 +16,9 @@ static int key_id[KEY_COUNT]{
Input::Key::KEY_FIRE3,
Input::Key::KEY_FIRE4,
Input::Key::KEY_FIRE5,
Input::Key::KEY_FIRE6
Input::Key::KEY_FIRE6,
Input::Key::KEY_MENU1,
Input::Key::KEY_MENU2
};

SDL2Input::SDL2Input() {
Expand Down
59 changes: 34 additions & 25 deletions pfba/deps/libcross2d/src/sfml/sfml_input.cpp
Expand Up @@ -18,7 +18,9 @@ static int key_id[KEY_COUNT]{
Input::Key::KEY_FIRE3,
Input::Key::KEY_FIRE4,
Input::Key::KEY_FIRE5,
Input::Key::KEY_FIRE6
Input::Key::KEY_FIRE6,
Input::Key::KEY_MENU1,
Input::Key::KEY_MENU2
};

SFMLInput::SFMLInput(SFMLRenderer *rdr) {
Expand Down Expand Up @@ -95,14 +97,13 @@ Input::Player *SFMLInput::Update(int rotate) {
return players;
}

/*
if (event.type == sf::Event::KeyPressed) {
printf("%i\n", (int) event.key.code);
}
if (event.type == sf::Event::JoystickButtonPressed) {
printf("%i\n", (int) event.joystickButton.button);
}
*/

}

for (int i = 0; i < PLAYER_COUNT; i++) {
Expand Down Expand Up @@ -134,30 +135,34 @@ void SFMLInput::process_axis(Input::Player &player, int rotate) {
}

// X AXIS
if (sf::Joystick::hasAxis(player.id, sf::Joystick::X)) {
int x = (int) sf::Joystick::getAxisPosition(player.id, sf::Joystick::X) * 320;
if (sf::Joystick::hasAxis((unsigned int) player.id, sf::Joystick::X)) {
int x = (int) sf::Joystick::getAxisPosition((unsigned int) player.id, sf::Joystick::X) * 320;
if (x > player.dead_zone) {
player.lx.value = x;
player.state |= (rotate == 1) ? Input::Key::KEY_DOWN : (rotate == 3) ? Input::Key::KEY_UP : Input::Key::KEY_RIGHT;
player.lx.value = (short) x;
player.state |= (rotate == 1) ? Input::Key::KEY_DOWN : (rotate == 3) ? Input::Key::KEY_UP
: Input::Key::KEY_RIGHT;
} else if (x < -player.dead_zone) {
player.lx.value = x;
player.state |= (rotate == 1) ? Input::Key::KEY_UP : (rotate == 3) ? Input::Key::KEY_DOWN : Input::Key::KEY_LEFT;
player.lx.value = (short) x;
player.state |= (rotate == 1) ? Input::Key::KEY_UP : (rotate == 3) ? Input::Key::KEY_DOWN
: Input::Key::KEY_LEFT;
} else {
player.lx.value = 0;
}
}

// Y AXIS
if (sf::Joystick::hasAxis(player.id, sf::Joystick::Y)) {
int y = (int) sf::Joystick::getAxisPosition(player.id, sf::Joystick::Y) * 320;
if (sf::Joystick::hasAxis((unsigned int) player.id, sf::Joystick::Y)) {
int y = (int) sf::Joystick::getAxisPosition((unsigned int) player.id, sf::Joystick::Y) * 320;
if (y > player.dead_zone) {
player.ly.value = y;
player.state |= (rotate == 1) ? Input::Key::KEY_LEFT : (rotate == 3) ? Input::Key::KEY_RIGHT : Input::Key::KEY_DOWN;
player.ly.value = (short) y;
player.state |= (rotate == 1) ? Input::Key::KEY_LEFT : (rotate == 3) ? Input::Key::KEY_RIGHT
: Input::Key::KEY_DOWN;
} else if (y < -player.dead_zone) {
player.state |= (rotate == 1) ? Input::Key::KEY_RIGHT : (rotate == 3) ? Input::Key::KEY_LEFT : Input::Key::KEY_UP;
player.ly.value = y;
player.state |= (rotate == 1) ? Input::Key::KEY_RIGHT : (rotate == 3) ? Input::Key::KEY_LEFT
: Input::Key::KEY_UP;
player.ly.value = (short) y;
} else {
player.ly.value = 0;
player.ly.value = 0;/**/
}
}
}
Expand All @@ -169,22 +174,26 @@ void SFMLInput::process_hat(Input::Player &player, int rotate) {
}

// X AXIS
if (sf::Joystick::hasAxis(player.id, sf::Joystick::PovX)) {
int x = (int) sf::Joystick::getAxisPosition(player.id, sf::Joystick::PovX) * 320;
if (sf::Joystick::hasAxis((unsigned int) player.id, sf::Joystick::PovX)) {
int x = (int) sf::Joystick::getAxisPosition((unsigned int) player.id, sf::Joystick::PovX) * 320;
if (x > player.dead_zone) {
player.state |= (rotate == 1) ? Input::Key::KEY_DOWN : (rotate == 3) ? Input::Key::KEY_UP : Input::Key::KEY_RIGHT;
player.state |= (rotate == 1) ? Input::Key::KEY_DOWN : (rotate == 3) ? Input::Key::KEY_UP
: Input::Key::KEY_RIGHT;
} else if (x < -player.dead_zone) {
player.state |= (rotate == 1) ? Input::Key::KEY_UP : (rotate == 3) ? Input::Key::KEY_DOWN : Input::Key::KEY_LEFT;
player.state |= (rotate == 1) ? Input::Key::KEY_UP : (rotate == 3) ? Input::Key::KEY_DOWN
: Input::Key::KEY_LEFT;
}
}

// Y AXIS
if (sf::Joystick::hasAxis(player.id, sf::Joystick::PovY)) {
int y = (int) sf::Joystick::getAxisPosition(player.id, sf::Joystick::PovY) * 320;
if (sf::Joystick::hasAxis((unsigned int) player.id, sf::Joystick::PovY)) {
int y = (int) sf::Joystick::getAxisPosition((unsigned int) player.id, sf::Joystick::PovY) * 320;
if (y > player.dead_zone) {
player.state |= (rotate == 1) ? Input::Key::KEY_LEFT : (rotate == 3) ? Input::Key::KEY_RIGHT : Input::Key::KEY_DOWN;
player.state |= (rotate == 1) ? Input::Key::KEY_LEFT : (rotate == 3) ? Input::Key::KEY_RIGHT
: Input::Key::KEY_DOWN;
} else if (y < -player.dead_zone) {
player.state |= (rotate == 1) ? Input::Key::KEY_RIGHT : (rotate == 3) ? Input::Key::KEY_LEFT : Input::Key::KEY_UP;
player.state |= (rotate == 1) ? Input::Key::KEY_RIGHT : (rotate == 3) ? Input::Key::KEY_LEFT
: Input::Key::KEY_UP;
}
}

Expand All @@ -202,7 +211,7 @@ void SFMLInput::process_buttons(Input::Player &player, int rotate) {
if (mapping < 0)
mapping = 0;

if (sf::Joystick::isButtonPressed(player.id, mapping)) {
if (sf::Joystick::isButtonPressed((unsigned int) player.id, (unsigned int) mapping)) {
if (rotate && key_id[i] == Input::Key::KEY_UP) {
if (rotate == 1) {
player.state |= Input::Key::KEY_RIGHT;
Expand Down
12 changes: 7 additions & 5 deletions pfba/deps/libcross2d/src/skeleton/input.h
Expand Up @@ -9,11 +9,11 @@
#include <vector>

#define PLAYER_COUNT 4
#define KEY_COUNT 12
#define KEY_COUNT 14

#define EV_RESIZE 0x1000
#define EV_QUIT 0x2000
#define EV_REFRESH 0x4000
#define EV_RESIZE 0x4000
#define EV_QUIT 0x8000
#define EV_REFRESH 0x10000

class Input {

Expand All @@ -31,7 +31,9 @@ class Input {
KEY_FIRE3 = 0x0100,
KEY_FIRE4 = 0x0200,
KEY_FIRE5 = 0x0400,
KEY_FIRE6 = 0x0800
KEY_FIRE6 = 0x0800,
KEY_MENU1 = 0x1000,
KEY_MENU2 = 0x2000,
};

struct Axis {
Expand Down
20 changes: 17 additions & 3 deletions pfba/gui/config.cpp
Expand Up @@ -81,11 +81,11 @@ Config::Config(const std::string &cfgPath, Renderer *renderer) {

// default rom config
options_gui.push_back(Option("EMULATION", {"EMULATION"}, 0, Option::Index::MENU_ROM_OPTIONS, Option::Type::MENU));
options_gui.push_back(Option("SCALING", {"NONE", "2X", "FIT", "FIT 4:3", "FULL"}, 3, Option::Index::ROM_SCALING));
options_gui.push_back(Option("SCALING", {"NONE", "2X", "FIT", "FIT 4:3", "FULL"}, 2, Option::Index::ROM_SCALING));
options_gui.push_back(
Option("FILTER", {"POINT", "LINEAR"}, 1, Option::Index::ROM_FILTER));
Option("FILTER", {"POINT", "LINEAR"}, 0, Option::Index::ROM_FILTER));
options_gui.push_back(
Option("SHADER", renderer->shaders->GetNames(), 2, Option::Index::ROM_SHADER));
Option("SHADER", renderer->shaders->GetNames(), 0, Option::Index::ROM_SHADER));
options_gui.push_back(
Option("ROTATION", {"OFF", "ON", "OFF+FLIP", "OFF+CAB MODE"}, 0, Option::Index::ROM_ROTATION));
options_gui.push_back(Option("SHOW_FPS", {"NO", "YES"}, 0, Option::Index::ROM_SHOW_FPS));
Expand Down Expand Up @@ -119,6 +119,10 @@ Config::Config(const std::string &cfgPath, Renderer *renderer) {
Option("JOY_COIN1", {"6"}, KEY_JOY_COIN1_DEFAULT, Option::Index::JOY_COIN1, Option::Type::INPUT));
options_gui.push_back(
Option("JOY_START1", {"7"}, KEY_JOY_START1_DEFAULT, Option::Index::JOY_START1, Option::Type::INPUT));
options_gui.push_back(
Option("JOY_MENU1", {"6"}, KEY_JOY_MENU1_DEFAULT, Option::Index::JOY_MENU1, Option::Type::INPUT));
options_gui.push_back(
Option("JOY_MENU2", {"7"}, KEY_JOY_MENU2_DEFAULT, Option::Index::JOY_MENU2, Option::Type::INPUT));
// TODO: add gui option for axis in option menu
options_gui.push_back(
Option("JOY_AXIS_LX", {"0"}, KEY_JOY_AXIS_LX, Option::Index::JOY_AXIS_LX, Option::Type::HIDDEN));
Expand Down Expand Up @@ -148,6 +152,8 @@ Config::Config(const std::string &cfgPath, Renderer *renderer) {
options_gui.push_back(Option("KEY_FIRE6", {"81"}, 81, Option::Index::KEY_FIRE6, Option::Type::INPUT)); // KP_6
options_gui.push_back(Option("KEY_COIN1", {"36"}, 36, Option::Index::KEY_COIN1, Option::Type::INPUT)); // ESCAPE
options_gui.push_back(Option("KEY_START1", {"58"}, 58, Option::Index::KEY_START1, Option::Type::INPUT));// ENTER
options_gui.push_back(Option("KEY_MENU1", {"58"}, 58, Option::Index::KEY_MENU1, Option::Type::INPUT));
options_gui.push_back(Option("KEY_MENU2", {"36"}, 36, Option::Index::KEY_MENU2, Option::Type::INPUT));
#endif

//
Expand Down Expand Up @@ -367,6 +373,8 @@ int *Config::GetGuiPlayerInputKeys(int player) {
keyboard_keys[9] = GetGuiValue(Option::Index::KEY_FIRE4);
keyboard_keys[10] = GetGuiValue(Option::Index::KEY_FIRE5);
keyboard_keys[11] = GetGuiValue(Option::Index::KEY_FIRE6);
keyboard_keys[12] = GetGuiValue(Option::Index::KEY_MENU1);
keyboard_keys[13] = GetGuiValue(Option::Index::KEY_MENU2);
#endif

return keyboard_keys;
Expand All @@ -387,6 +395,8 @@ int *Config::GetGuiPlayerInputButtons(int player) {
joystick_keys[9] = GetGuiValue(Option::Index::JOY_FIRE4);
joystick_keys[10] = GetGuiValue(Option::Index::JOY_FIRE5);
joystick_keys[11] = GetGuiValue(Option::Index::JOY_FIRE6);
joystick_keys[12] = GetGuiValue(Option::Index::JOY_MENU1);
joystick_keys[13] = GetGuiValue(Option::Index::JOY_MENU2);

return joystick_keys;
}
Expand All @@ -407,6 +417,8 @@ int *Config::GetRomPlayerInputKeys(int player) {
keyboard_keys[9] = GetRomValue(Option::Index::KEY_FIRE4);
keyboard_keys[10] = GetRomValue(Option::Index::KEY_FIRE5);
keyboard_keys[11] = GetRomValue(Option::Index::KEY_FIRE6);
keyboard_keys[12] = GetRomValue(Option::Index::KEY_MENU1);
keyboard_keys[13] = GetRomValue(Option::Index::KEY_MENU2);
#endif

return keyboard_keys;
Expand All @@ -427,6 +439,8 @@ int *Config::GetRomPlayerInputButtons(int player) {
joystick_keys[9] = GetRomValue(Option::Index::JOY_FIRE4);
joystick_keys[10] = GetRomValue(Option::Index::JOY_FIRE5);
joystick_keys[11] = GetRomValue(Option::Index::JOY_FIRE6);
joystick_keys[12] = GetRomValue(Option::Index::JOY_MENU1);
joystick_keys[13] = GetRomValue(Option::Index::JOY_MENU2);

return joystick_keys;
}
40 changes: 28 additions & 12 deletions pfba/gui/config.h
Expand Up @@ -8,6 +8,7 @@
#include "libconfig.h"

#include "gui.h"

class Gui;

#include "option.h"
Expand All @@ -25,10 +26,12 @@ class Gui;
#define KEY_JOY_FIRE6_DEFAULT 5
#define KEY_JOY_COIN1_DEFAULT 10
#define KEY_JOY_START1_DEFAULT 11
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 1
#define KEY_JOY_AXIS_RX 2
#define KEY_JOY_AXIS_RY 3
#define KEY_JOY_MENU1_DEFAULT 11
#define KEY_JOY_MENU2_DEFAULT 10
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 1
#define KEY_JOY_AXIS_RX 2
#define KEY_JOY_AXIS_RY 3
#elif __3DS__
#define KEY_JOY_UP_DEFAULT 6
#define KEY_JOY_DOWN_DEFAULT 7
Expand All @@ -42,10 +45,12 @@ class Gui;
#define KEY_JOY_FIRE6_DEFAULT 8
#define KEY_JOY_COIN1_DEFAULT 2
#define KEY_JOY_START1_DEFAULT 3
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 0
#define KEY_JOY_AXIS_RX 0
#define KEY_JOY_AXIS_RY 0
#define KEY_JOY_MENU1_DEFAULT 3
#define KEY_JOY_MENU2_DEFAULT 2
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 0
#define KEY_JOY_AXIS_RX 0
#define KEY_JOY_AXIS_RY 0
#else
#define KEY_JOY_UP_DEFAULT -1 // use hat
#define KEY_JOY_DOWN_DEFAULT -1 // use hat
Expand All @@ -59,37 +64,48 @@ class Gui;
#define KEY_JOY_FIRE6_DEFAULT 5
#define KEY_JOY_COIN1_DEFAULT 6
#define KEY_JOY_START1_DEFAULT 7
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 1
#define KEY_JOY_AXIS_RX 4
#define KEY_JOY_AXIS_RY 5
#define KEY_JOY_MENU1_DEFAULT 7
#define KEY_JOY_MENU2_DEFAULT 6
#define KEY_JOY_AXIS_LX 0
#define KEY_JOY_AXIS_LY 1
#define KEY_JOY_AXIS_RX 4
#define KEY_JOY_AXIS_RY 5
#endif

class Config {

public:

Config(const std::string &cfgPath, Renderer *renderer);

~Config() {};

void Load(RomList::Rom *rom = NULL);

void Save(RomList::Rom *rom = NULL);

int GetGuiValue(int id);

int GetRomValue(int id);

const char *GetRomPath(int n);

std::vector<std::string> GetRomPaths();

std::vector<Option> *GetGuiOptions();

std::vector<Option> *GetRomOptions();

int GetOptionPos(std::vector<Option> *options, int index);

Option *GetOption(std::vector<Option> *options, int index);

int *GetGuiPlayerInputKeys(int player);

int *GetGuiPlayerInputButtons(int player);

int *GetRomPlayerInputKeys(int player);

int *GetRomPlayerInputButtons(int player);

std::vector<RomList::Hardware> hardwareList;
Expand Down
17 changes: 9 additions & 8 deletions pfba/gui/gui.cpp
Expand Up @@ -565,8 +565,8 @@ void Gui::RunOptionMenu(bool isRomConfig) {
}
}
} else if (key & Input::Key::KEY_FIRE2
|| (key & Input::Key::KEY_START && !isRomConfig)
|| (key & Input::Key::KEY_COIN && isRomConfig)) {
|| (key & Input::Key::KEY_MENU1 && !isRomConfig)
|| (key & Input::Key::KEY_MENU2 && isRomConfig)) {
if (menu_current->parent == NULL) {
break;
} else {
Expand Down Expand Up @@ -616,6 +616,7 @@ void Gui::RunOptionMenu(bool isRomConfig) {
} else {
config->Save();
}
UpdateInputMapping(isRomConfig);
}

if (stop) {
Expand Down Expand Up @@ -674,7 +675,7 @@ void Gui::Run() {
&& romSelected->state != RomList::RomState::MISSING) {
RunRom(romSelected);
}
} else if (key & Input::Key::KEY_START) {
} else if (key & Input::Key::KEY_MENU1) {
RunOptionMenu();
if (title != NULL) {
// refresh preview/title image
Expand All @@ -683,7 +684,7 @@ void Gui::Run() {
renderer->DrawTexture(title, GetRectRomPreview(), true);
Flip();
}
} else if (key & Input::Key::KEY_COIN) {
} else if (key & Input::Key::KEY_MENU2) {
if (romSelected != NULL) {
config->Load(romSelected);
RunOptionMenu(true);
Expand Down Expand Up @@ -1041,19 +1042,19 @@ void Gui::UpdateInputMapping(bool isRomConfig) {

if (isRomConfig) {
input->SetKeyboardMapping(config->GetRomPlayerInputKeys(0));
int deadzone = 2000 + config->GetRomValue(Option::Index::JOY_DEADZONE) * 2000;
int dz = 2000 + config->GetRomValue(Option::Index::JOY_DEADZONE) * 2000;
for (int i = 0; i < PLAYER_COUNT; i++) {
input->SetJoystickMapping(i, config->GetRomPlayerInputButtons(i), deadzone);
input->SetJoystickMapping(i, config->GetRomPlayerInputButtons(i), dz);
input->players[i].lx.id = config->GetRomValue(Option::Index::JOY_AXIS_LX);
input->players[i].ly.id = config->GetRomValue(Option::Index::JOY_AXIS_LY);
input->players[i].rx.id = config->GetRomValue(Option::Index::JOY_AXIS_RX);
input->players[i].ry.id = config->GetRomValue(Option::Index::JOY_AXIS_RY);
}
} else {
input->SetKeyboardMapping(config->GetGuiPlayerInputKeys(0));
int deadzone = 2000 + config->GetGuiValue(Option::Index::JOY_DEADZONE) * 2000;
int dz = 2000 + config->GetGuiValue(Option::Index::JOY_DEADZONE) * 2000;
for (int i = 0; i < PLAYER_COUNT; i++) {
input->SetJoystickMapping(i, config->GetGuiPlayerInputButtons(i), deadzone);
input->SetJoystickMapping(i, config->GetGuiPlayerInputButtons(i), dz);
input->players[i].lx.id = config->GetGuiValue(Option::Index::JOY_AXIS_LX);
input->players[i].ly.id = config->GetGuiValue(Option::Index::JOY_AXIS_LY);
input->players[i].rx.id = config->GetGuiValue(Option::Index::JOY_AXIS_RX);
Expand Down
6 changes: 1 addition & 5 deletions pfba/gui/option.cpp
Expand Up @@ -18,13 +18,9 @@ const char *Option::GetName() {
return text.c_str();
}

void Option::SetName(const std::string &name) {
text = name;
}

const char *Option::GetValue() {
if(value >= options.size()) {
return "<NA>";
return "NONE";
}
return options[value].c_str();
}
Expand Down

0 comments on commit 2f57e3f

Please sign in to comment.