touch_from_button.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2020 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/frontend/framebuffer_layout.h"
  5. #include "core/settings.h"
  6. #include "input_common/touch_from_button.h"
  7. namespace InputCommon {
  8. class TouchFromButtonDevice final : public Input::TouchDevice {
  9. public:
  10. TouchFromButtonDevice() {
  11. const auto button_index =
  12. static_cast<std::size_t>(Settings::values.touch_from_button_map_index);
  13. const auto& buttons = Settings::values.touch_from_button_maps[button_index].buttons;
  14. for (const auto& config_entry : buttons) {
  15. const Common::ParamPackage package{config_entry};
  16. map.emplace_back(
  17. Input::CreateDevice<Input::ButtonDevice>(config_entry),
  18. std::clamp(package.Get("x", 0), 0, static_cast<int>(Layout::ScreenUndocked::Width)),
  19. std::clamp(package.Get("y", 0), 0,
  20. static_cast<int>(Layout::ScreenUndocked::Height)));
  21. }
  22. }
  23. std::tuple<float, float, bool> GetStatus() const override {
  24. for (const auto& m : map) {
  25. const bool state = std::get<0>(m)->GetStatus();
  26. if (state) {
  27. const float x = static_cast<float>(std::get<1>(m)) /
  28. static_cast<int>(Layout::ScreenUndocked::Width);
  29. const float y = static_cast<float>(std::get<2>(m)) /
  30. static_cast<int>(Layout::ScreenUndocked::Height);
  31. return {x, y, true};
  32. }
  33. }
  34. return {};
  35. }
  36. private:
  37. // A vector of the mapped button, its x and its y-coordinate
  38. std::vector<std::tuple<std::unique_ptr<Input::ButtonDevice>, int, int>> map;
  39. };
  40. std::unique_ptr<Input::TouchDevice> TouchFromButtonFactory::Create(
  41. const Common::ParamPackage& params) {
  42. return std::make_unique<TouchFromButtonDevice>();
  43. }
  44. } // namespace InputCommon