touch_from_button.cpp 2.0 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. Input::TouchStatus GetStatus() const override {
  24. Input::TouchStatus touch_status = {};
  25. for (size_t id = 0; id < map.size() && id < touch_status.size(); id++) {
  26. const bool state = std::get<0>(map[id])->GetStatus();
  27. if (state) {
  28. const float x = static_cast<float>(std::get<1>(map[id])) /
  29. static_cast<int>(Layout::ScreenUndocked::Width);
  30. const float y = static_cast<float>(std::get<2>(map[id])) /
  31. static_cast<int>(Layout::ScreenUndocked::Height);
  32. touch_status[id] = {x, y, true};
  33. }
  34. }
  35. return touch_status;
  36. }
  37. private:
  38. // A vector of the mapped button, its x and its y-coordinate
  39. std::vector<std::tuple<std::unique_ptr<Input::ButtonDevice>, int, int>> map;
  40. };
  41. std::unique_ptr<Input::TouchDevice> TouchFromButtonFactory::Create(const Common::ParamPackage&) {
  42. return std::make_unique<TouchFromButtonDevice>();
  43. }
  44. } // namespace InputCommon