Skip to content

Commit

Permalink
Gamepad hotkeys, toggle fullscreen hotkey (#92)
Browse files Browse the repository at this point in the history
Co-authored-by: mpc <mpc@notamail.com>
  • Loading branch information
yuupo123 and mpc authored Nov 25, 2020
1 parent 3928fc2 commit 5d15d41
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ KeyBindings keyboard() {
{"toggle_pause", "keyboard|Space"},
{"toggle_framelimit", "keyboard|Tab"},
{"rewind_state", "keyboard|Backspace"},
{"toggle_fullscreen", "keyboard|F4"}
};
}
} // namespace DefaultHotkeys
Expand Down
21 changes: 11 additions & 10 deletions src/platform/windows/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,41 +109,41 @@ void GUI::mainMenu(std::unique_ptr<System>& sys) {
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Emulation")) {
if (ImGui::MenuItem("Pause/Resume", Key(config.hotkeys["toggle_pause"]).getName())) {
if (ImGui::MenuItem("Pause/Resume", Key(config.hotkeys["toggle_pause"]).getButton().c_str())) {
if (sys->state == System::State::pause) {
sys->state = System::State::run;
} else if (sys->state == System::State::run) {
sys->state = System::State::pause;
}
}
if (ImGui::MenuItem("Soft reset", Key(config.hotkeys["reset"]).getName())) {
if (ImGui::MenuItem("Soft reset", Key(config.hotkeys["reset"]).getButton().c_str())) {
bus.notify(Event::System::SoftReset{});
}

std::string key = "Shift+";
key += Key(config.hotkeys["reset"]).getName();
key += Key(config.hotkeys["reset"]).getButton();
if (ImGui::MenuItem("Hard reset", key.c_str())) {
bus.notify(Event::System::HardReset{});
}

const char* shellStatus = sys->cdrom->getShell() ? "Close disk tray" : "Open disk tray";
if (ImGui::MenuItem(shellStatus, Key(config.hotkeys["close_tray"]).getName())) {
if (ImGui::MenuItem(shellStatus, Key(config.hotkeys["close_tray"]).getButton().c_str())) {
sys->cdrom->toggleShell();
}

if (ImGui::MenuItem("Single frame", Key(config.hotkeys["single_frame"]).getName())) {
if (ImGui::MenuItem("Single frame", Key(config.hotkeys["single_frame"]).getButton().c_str())) {
singleFrame = true;
sys->state = System::State::run;
}

ImGui::Separator();

if (ImGui::MenuItem("Quick save", Key(config.hotkeys["quick_save"]).getName())) {
if (ImGui::MenuItem("Quick save", Key(config.hotkeys["quick_save"]).getButton().c_str())) {
bus.notify(Event::System::SaveState{});
}

bool quickLoadStateExists = fs::exists(state::getStatePath(sys.get()));
if (ImGui::MenuItem("Quick load", Key(config.hotkeys["quick_load"]).getName(), nullptr, quickLoadStateExists))
if (ImGui::MenuItem("Quick load", Key(config.hotkeys["quick_load"]).getButton().c_str(), nullptr, quickLoadStateExists))
bus.notify(Event::System::LoadState{});

if (ImGui::BeginMenu("Save")) {
Expand Down Expand Up @@ -231,7 +231,7 @@ void GUI::mainMenu(std::unique_ptr<System>& sys) {
}

bool timeTravel = config.options.emulator.timeTravel;
if (ImGui::MenuItem("Time travel", "backspace", &timeTravel)) {
if (ImGui::MenuItem("Time travel", Key(config.hotkeys["rewind_state"]).getButton().c_str(), &timeTravel)) {
config.options.emulator.timeTravel = timeTravel;
}

Expand Down Expand Up @@ -480,13 +480,14 @@ void GUI::drawControls(std::unique_ptr<System>& sys) {
if (ImGui::BeginPopupContextItem(nullptr, 0)) {
if (ImGui::Selectable("Controller")) showControllerSetupWindow = !showControllerSetupWindow;
ImGui::Separator();
ImGui::MenuItem("Show menu", Key(config.hotkeys["toggle_menu"]).getName(), &showMenu);
ImGui::MenuItem("Show menu", Key(config.hotkeys["toggle_menu"]).getButton().c_str(), &showMenu);
ImGui::EndPopup();
}

ImGui::SameLine();

if (symbolButton("Toggle fullscreen", ICON_FA_COMPRESS)) {
std::string hint = "Toggle fullscreen (" + (std::string)Key(config.hotkeys["toggle_fullscreen"]).getButton().c_str() + ")";
if (symbolButton(hint.c_str(), ICON_FA_COMPRESS)) {
bus.notify(Event::Gui::ToggleFullscreen{});
}

Expand Down
17 changes: 10 additions & 7 deletions src/platform/windows/gui/options/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ void button(const std::string& button, const char* tooltip, int controller = 0)
int ctrl = controller - 1;
auto& relatedConfig = hotkey ? config.hotkeys[button] : config.controller[ctrl].keys[button];

if (button == currentButton && inputManager->lastPressedKey.type != Key::Type::None) {
Key::Type buttonType = inputManager->lastPressedKey.type;
bool restrictedDevices = buttonType == Key::Type::None || (hotkey && buttonType == Key::Type::MouseMove);
if (button == currentButton && !restrictedDevices) {
relatedConfig = inputManager->lastPressedKey.to_string();
inputManager->lastPressedKey = Key();
}

std::string key = relatedConfig;

key = Key(key).getName();
key = Key(key).getButton();

if (hotkey) {
if (ImGui::Button(fmt::format("{}##{}", key, button).c_str(), ImVec2(150.f, 0.f))) {
Expand Down Expand Up @@ -393,18 +395,19 @@ void controllerSetupWindow() {
}

void hotkeysSetupWindow() {
ImGui::SetNextWindowSize(ImVec2(300.f, 300.f), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(300.f, 325.f), ImGuiCond_Once);
ImGui::Begin("Hotkeys", &showHotkeysSetupWindow, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoResize);

button("toggle_menu", "Toggle menu");
button("reset", "Reset");
button("close_tray", "Close disk shell");
button("toggle_fullscreen", "Toggle fullscreen");
button("quick_save", "Quick save");
button("single_frame", "Single frame");
button("quick_load", "Quick load");
button("single_step", "Single step");
button("toggle_pause", "Pause");
button("reset", "Reset");
button("toggle_framelimit", "Toggle framelimit");
button("close_tray", "Close disk tray");
button("single_frame", "Single frame");
button("single_step", "Single step");
button("rewind_state", "Time travel");

pressKeyPopup();
Expand Down
14 changes: 12 additions & 2 deletions src/platform/windows/input/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ bool Key::operator==(const Key& rhs) const {
return false;
}

const char* Key::getName() {
return SDL_GetKeyName(key);
std::string Key::getButton() {
auto button = to_string();
size_t dpos = button.find('|');
button = button.substr(dpos + 1);
return button;
}

std::string Key::getDevice() {
auto device = to_string();
size_t dpos = device.find('|');
device = device.substr(0, dpos);
return device;
}
3 changes: 2 additions & 1 deletion src/platform/windows/input/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ struct Key {
Key();
Key(std::string& s);
std::string to_string() const;
std::string getButton();
std::string getDevice();
static Key keyboard(SDL_Keycode keyCode);
static Key mouseMove(int8_t xrel, int8_t yrel);
static Key mouseButton(SDL_MouseButtonEvent button);
static Key controllerMove(SDL_ControllerAxisEvent axis);
static Key controllerButton(SDL_ControllerButtonEvent button);
const char* getName();

bool operator==(const Key& rhs) const;

Expand Down
35 changes: 24 additions & 11 deletions src/platform/windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,21 @@ int main(int argc, char** argv) {
newEvent = false;
if (inputManager->handleEvent(event)) continue;
if (event.type == SDL_QUIT || (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE)) running = false;
if (!inputManager->keyboardCaptured && event.type == SDL_KEYDOWN && event.key.repeat == 0) {

Key button = Key();
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
button = Key::keyboard(event.key.keysym.sym);
} else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
button = Key::controllerButton(event.cbutton);
} else if (event.type == SDL_CONTROLLERAXISMOTION) {
if (std::abs(event.caxis.value) == 32767) button = Key::controllerMove(event.caxis);
}

if (!inputManager->keyboardCaptured && button.type != Key::Type::None) {
if (event.key.keysym.sym == SDLK_ESCAPE) running = false;
if (event.key.keysym.sym == SDLK_AC_BACK) running = false;
if (event.key.keysym.sym == Key(config.hotkeys["toggle_menu"]).key) gui->showMenu = !gui->showMenu;
if (event.key.keysym.sym == Key(config.hotkeys["reset"]).key) {
if (button == Key(config.hotkeys["toggle_menu"])) gui->showMenu = !gui->showMenu;
if (button == Key(config.hotkeys["reset"])) {
if (event.key.keysym.mod & KMOD_SHIFT) {
sys = system_tools::hardReset();
toast("Hard reset");
Expand All @@ -340,40 +350,43 @@ int main(int argc, char** argv) {
toast("Soft reset");
}
}
if (event.key.keysym.sym == Key(config.hotkeys["close_tray"]).key) {
if (button == Key(config.hotkeys["close_tray"])) {
sys->cdrom->toggleShell();
toast(fmt::format("Shell {}", sys->cdrom->getShell() ? "open" : "closed"));
}
if (event.key.keysym.sym == Key(config.hotkeys["quick_save"]).key) {
if (button == Key(config.hotkeys["quick_save"])) {
bus.notify(Event::System::SaveState{});
}
if (event.key.keysym.sym == Key(config.hotkeys["single_frame"]).key) {
if (button == Key(config.hotkeys["single_frame"])) {
gui->singleFrame = true;
sys->state = System::State::run;
}
if (event.key.keysym.sym == Key(config.hotkeys["quick_load"]).key) {
if (button == Key(config.hotkeys["quick_load"])) {
bus.notify(Event::System::LoadState{});
}
if (event.key.keysym.sym == Key(config.hotkeys["single_step"]).key) {
if (button == Key(config.hotkeys["single_step"])) {
sys->singleStep();
}
if (event.key.keysym.sym == Key(config.hotkeys["toggle_pause"]).key) {
if (button == Key(config.hotkeys["toggle_pause"])) {
if (sys->state == System::State::pause) {
sys->state = System::State::run;
} else if (sys->state == System::State::run) {
sys->state = System::State::pause;
}
toast(fmt::format("Emulation {}", sys->state == System::State::run ? "resumed" : "paused"));
}
if (event.key.keysym.sym == Key(config.hotkeys["toggle_framelimit"]).key) {
if (button == Key(config.hotkeys["toggle_framelimit"])) {
frameLimitEnabled = !frameLimitEnabled;
toast(fmt::format("Frame limiter {}", frameLimitEnabled ? "enabled" : "disabled"));
}
if (event.key.keysym.sym == Key(config.hotkeys["rewind_state"]).key) {
if (button == Key(config.hotkeys["rewind_state"])) {
if (state::rewindState(sys.get())) {
toast("Going back 1 second");
}
}
if (button == Key(config.hotkeys["toggle_fullscreen"])) {
bus.notify(Event::Gui::ToggleFullscreen{});
}
}
if (event.type == SDL_DROPFILE) {
std::string path = event.drop.file;
Expand Down

0 comments on commit 5d15d41

Please sign in to comment.