touch_from_button.cpp 2.1 KB

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