Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Added joystick support #39
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed May 10, 2023
1 parent 5f5a0a2 commit b251d48
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
14 changes: 9 additions & 5 deletions include/modals/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "util.h"
#include <string>
#include "style.h"
#include <format>

namespace vector_audio::modals {
class Settings {
Expand Down Expand Up @@ -130,14 +131,17 @@ class Settings {

ImGui::TextUnformatted("Push to talk key: ");
std::string ptt_key_name;
shared::ptt == sf::Keyboard::Unknown ? ptt_key_name = "Not set" : ptt_key_name = vector_audio::util::getKeyName(shared::ptt);
ImGui::SameLine();
if (shared::ptt == sf::Keyboard::Unknown && shared::joyStickId == -1) {
ptt_key_name = "Not set";
} else if (shared::ptt != -1) {
ptt_key_name = "Key: "+ vector_audio::util::getKeyName(shared::ptt);
} else if (shared::joyStickId != -1) {
ptt_key_name = fmt::format("Joystick {} Button {}", shared::joyStickId, shared::joyStickPtt);
}

vector_audio::style::button_yellow();
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::Button(ptt_key_name.c_str());
ImGui::Button(ptt_key_name.c_str(), ImVec2(-1.0F, 0.0F));
ImGui::PopItemFlag();
vector_audio::style::button_reset_colour();

vector_audio::style::button_blue();
if (shared::capture_ptt_flag) {
Expand Down
2 changes: 2 additions & 0 deletions include/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ inline int headsetOutputChannel = 0;
inline bool capture_ptt_flag = false;

inline sf::Keyboard::Key ptt = sf::Keyboard::Unknown;
inline int joyStickId = -1;
inline int joyStickPtt = -1;
inline bool isPttOpen = false;

inline std::vector<StationElement> FetchedStations;
Expand Down
36 changes: 28 additions & 8 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "shared.h"
#include "style.h"
#include "util.h"
#include <SFML/Window/Joystick.hpp>
#include <spdlog/spdlog.h>

namespace vector_audio::application {
Expand Down Expand Up @@ -35,6 +36,11 @@ App::App()
vector_audio::shared::ptt = static_cast<sf::Keyboard::Key>(
toml::find_or<int>(cfg::config, "user", "ptt", -1));

vector_audio::shared::joyStickId = static_cast<int>(
toml::find_or<int>(cfg::config, "user", "joyStickId", -1));
vector_audio::shared::joyStickPtt = static_cast<int>(
toml::find_or<int>(cfg::config, "user", "joyStickPtt", -1));

auto audio_providers = mClient_->GetAudioApis();
vector_audio::shared::configAudioApi = toml::find_or<std::string>(
cfg::config, "audio", "api", std::string("Default API"));
Expand Down Expand Up @@ -259,18 +265,32 @@ void App::render_frame()
vector_audio::shared::mPeak = mClient_->GetInputPeak();
vector_audio::shared::mVu = mClient_->GetInputVu();

// Set the Ptt if required
if (mClient_->IsVoiceConnected() && shared::ptt != sf::Keyboard::Unknown) {
// Set the Ptt if required, input based on event
if (mClient_->IsVoiceConnected() && (shared::ptt != sf::Keyboard::Unknown || shared::joyStickId != -1)) {
if (shared::isPttOpen) {
if (!sf::Keyboard::isKeyPressed(shared::ptt)) {
mClient_->SetPtt(false);
shared::isPttOpen = false;
if (shared::joyStickId != -1) {
if (!sf::Joystick::isButtonPressed(shared::joyStickId, shared::joyStickPtt)) {
shared::isPttOpen = false;
}
} else {
if (!sf::Keyboard::isKeyPressed(shared::ptt)) {
shared::isPttOpen = false;
}
}

mClient_->SetPtt(shared::isPttOpen);
} else {
if (sf::Keyboard::isKeyPressed(shared::ptt)) {
mClient_->SetPtt(true);
shared::isPttOpen = true;
if (shared::joyStickId != -1) {
if (sf::Joystick::isButtonPressed(shared::joyStickId, shared::joyStickPtt)) {
shared::isPttOpen = true;
}
} else {
if (sf::Keyboard::isKeyPressed(shared::ptt)) {
shared::isPttOpen = true;
}
}

mClient_->SetPtt(shared::isPttOpen);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,28 @@ int main(int, char**)
if (vector_audio::shared::capture_ptt_flag) {
vector_audio::shared::ptt = event.key.code;

vector_audio::shared::joyStickId = -1; vector_audio::shared::joyStickPtt = -1;
vector_audio::configuration::config["user"]["joyStickId"] = vector_audio::shared::joyStickId;
vector_audio::configuration::config["user"]["joyStickPtt"] = vector_audio::shared::joyStickPtt;
vector_audio::configuration::config["user"]["ptt"] = static_cast<int>(vector_audio::shared::ptt);
vector_audio::configuration::write_config_async();
vector_audio::shared::capture_ptt_flag = false;
}
} else if (event.type == sf::Event::JoystickButtonPressed) {

if (vector_audio::shared::capture_ptt_flag) {
vector_audio::shared::ptt = sf::Keyboard::Unknown;

vector_audio::shared::joyStickId = event.joystickButton.joystickId;
vector_audio::shared::joyStickPtt = event.joystickButton.button;

vector_audio::configuration::config["user"]["joyStickId"] = vector_audio::shared::joyStickId;
vector_audio::configuration::config["user"]["joyStickPtt"] = vector_audio::shared::joyStickPtt;
vector_audio::configuration::config["user"]["ptt"] = static_cast<int>(vector_audio::shared::ptt);
vector_audio::configuration::write_config_async();
vector_audio::shared::capture_ptt_flag = false;
}

}
}

Expand Down

0 comments on commit b251d48

Please sign in to comment.