code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ConfigureInput</class>
 <widget class="QWidget" name="ConfigureInput">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>680</width>
    <height>540</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>ConfigureInput</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_5">
   <property name="spacing">
    <number>2</number>
   </property>
   <property name="leftMargin">
    <number>3</number>
   </property>
   <property name="topMargin">
    <number>3</number>
   </property>
   <property name="rightMargin">
    <number>3</number>
   </property>
   <property name="bottomMargin">
    <number>3</number>
   </property>
   <item>
    <widget class="QTabWidget" name="tabWidget">
     <property name="currentIndex">
      <number>0</number>
     </property>
     <widget class="QWidget" name="tabPlayer1">
      <property name="accessibleName">
       <string>Player 1</string>
      </property>
      <attribute name="title">
       <string>Player 1</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer2">
      <property name="accessibleName">
       <string>Player 2</string>
      </property>
      <attribute name="title">
       <string>Player 2</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer3">
      <property name="accessibleName">
       <string>Player 3</string>
      </property>
      <attribute name="title">
       <string>Player 3</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer4">
      <property name="accessibleName">
       <string>Player 4</string>
      </property>
      <attribute name="title">
       <string>Player 4</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer5">
      <property name="accessibleName">
       <string>Player 5</string>
      </property>
      <attribute name="title">
       <string>Player 5</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer6">
      <property name="accessibleName">
       <string>Player 6</string>
      </property>
      <attribute name="title">
       <string>Player 6</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer7">
      <property name="accessibleName">
       <string>Player 7</string>
      </property>
      <attribute name="title">
       <string>Player 7</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabPlayer8">
      <property name="accessibleName">
       <string>Player 8</string>
      </property>
      <attribute name="title">
       <string>Player 8</string>
      </attribute>
     </widget>
     <widget class="QWidget" name="tabAdvanced">
      <property name="accessibleName">
       <string>Advanced</string>
      </property>
      <attribute name="title">
       <string>Advanced</string>
      </attribute>
     </widget>
    </widget>
   </item>
   <item alignment="Qt::AlignVCenter">
    <widget class="QWidget" name="consoleInputSettings" native="true">
     <layout class="QHBoxLayout" name="buttonsBottomRightHorizontalLayout">
      <property name="spacing">
       <number>3</number>
      </property>
      <property name="leftMargin">
       <number>0</number>
      </property>
      <property name="topMargin">
       <number>3</number>
      </property>
      <property name="rightMargin">
       <number>0</number>
      </property>
      <property name="bottomMargin">
       <number>0</number>
      </property>
      <item alignment="Qt::AlignVCenter">
       <widget class="QGroupBox" name="handheldGroup">
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="title">
         <string>Console Mode</string>
        </property>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <property name="spacing">
          <number>6</number>
         </property>
         <property name="leftMargin">
          <number>8</number>
         </property>
         <property name="topMargin">
          <number>6</number>
         </property>
         <property name="rightMargin">
          <number>3</number>
         </property>
         <property name="bottomMargin">
          <number>6</number>
         </property>
         <item>
          <widget class="QRadioButton" name="radioDocked">
           <property name="text">
            <string>Docked</string>
           </property>
           <property name="checked">
            <bool>true</bool>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QRadioButton" name="radioUndocked">
           <property name="text">
            <string>Handheld</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <widget class="QGroupBox" name="vibrationGroup">
        <property name="title">
         <string>Vibration</string>
        </property>
        <property name="checkable">
         <bool>true</bool>
        </property>
        <layout class="QHBoxLayout" name="horizontalLayout_2">
         <property name="leftMargin">
          <number>3</number>
         </property>
         <property name="topMargin">
          <number>3</number>
         </property>
         <property name="rightMargin">
          <number>3</number>
         </property>
         <property name="bottomMargin">
          <number>3</number>
         </property>
         <item>
          <widget class="QPushButton" name="vibrationButton">
           <property name="minimumSize">
            <size>
             <width>68</width>
             <height>0</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>68</width>
             <height>16777215</height>
            </size>
           </property>
           <property name="styleSheet">
            <string notr="true">min-width: 68px;</string>
           </property>
           <property name="text">
            <string>Configure</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <widget class="QGroupBox" name="motionGroup">
        <property name="title">
         <string>Motion</string>
        </property>
        <property name="checkable">
         <bool>true</bool>
        </property>
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <property name="leftMargin">
          <number>3</number>
         </property>
         <property name="topMargin">
          <number>3</number>
         </property>
         <property name="rightMargin">
          <number>3</number>
         </property>
         <property name="bottomMargin">
          <number>3</number>
         </property>
         <item>
          <widget class="QPushButton" name="motionButton">
           <property name="minimumSize">
            <size>
             <width>68</width>
             <height>0</height>
            </size>
           </property>
           <property name="maximumSize">
            <size>
             <width>68</width>
             <height>16777215</height>
            </size>
           </property>
           <property name="styleSheet">
            <string notr="true">min-width: 68px;</string>
           </property>
           <property name="text">
            <string>Configure</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item alignment="Qt::AlignVCenter">
       <widget class="QWidget" name="connectedControllers" native="true">
        <layout class="QGridLayout" name="gridLayout_2">
         <property name="leftMargin">
          <number>5</number>
         </property>
         <property name="topMargin">
          <number>0</number>
         </property>
         <property name="rightMargin">
          <number>0</number>
         </property>
         <property name="bottomMargin">
          <number>0</number>
         </property>
         <property name="spacing">
          <number>3</number>
         </property>
         <item row="1" column="2">
          <widget class="QCheckBox" name="checkboxPlayer2Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="1" column="0">
          <widget class="QLabel" name="label_2">
           <property name="text">
            <string>Controllers</string>
           </property>
          </widget>
         </item>
         <item row="1" column="4">
          <widget class="QCheckBox" name="checkboxPlayer4Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="1" column="3">
          <widget class="QCheckBox" name="checkboxPlayer3Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="1" column="5">
          <widget class="QCheckBox" name="checkboxPlayer5Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="0" column="1">
          <widget class="QLabel" name="label">
           <property name="text">
            <string>1</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="1" column="7">
          <widget class="QCheckBox" name="checkboxPlayer7Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="1" column="6">
          <widget class="QCheckBox" name="checkboxPlayer6Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="1" column="1">
          <widget class="QCheckBox" name="checkboxPlayer1Connected">
           <property name="layoutDirection">
            <enum>Qt::LeftToRight</enum>
           </property>
           <property name="checked">
            <bool>true</bool>
           </property>
          </widget>
         </item>
         <item row="1" column="8">
          <widget class="QCheckBox" name="checkboxPlayer8Connected">
           <property name="text">
            <string/>
           </property>
          </widget>
         </item>
         <item row="0" column="2">
          <widget class="QLabel" name="label_3">
           <property name="text">
            <string>2</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="3">
          <widget class="QLabel" name="label_4">
           <property name="text">
            <string>3</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="4">
          <widget class="QLabel" name="label_5">
           <property name="text">
            <string>4</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="5">
          <widget class="QLabel" name="label_6">
           <property name="text">
            <string>5</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="6">
          <widget class="QLabel" name="label_7">
           <property name="text">
            <string>6</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="7">
          <widget class="QLabel" name="label_8">
           <property name="text">
            <string>7</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="8">
          <widget class="QLabel" name="label_9">
           <property name="text">
            <string>8</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
         <item row="0" column="0">
          <widget class="QLabel" name="label_10">
           <property name="text">
            <string>Connected</string>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item alignment="Qt::AlignBottom">
       <widget class="QPushButton" name="buttonRestoreDefaults">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>68</width>
          <height>0</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>68</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="sizeIncrement">
         <size>
          <width>0</width>
          <height>0</height>
         </size>
        </property>
        <property name="baseSize">
         <size>
          <width>0</width>
          <height>0</height>
         </size>
        </property>
        <property name="layoutDirection">
         <enum>Qt::LeftToRight</enum>
        </property>
        <property name="styleSheet">
         <string notr="true">min-width: 68px;</string>
        </property>
        <property name="text">
         <string>Defaults</string>
        </property>
       </widget>
      </item>
      <item alignment="Qt::AlignBottom">
       <widget class="QPushButton" name="buttonClearAll">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="minimumSize">
         <size>
          <width>68</width>
          <height>0</height>
         </size>
        </property>
        <property name="maximumSize">
         <size>
          <width>68</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="sizeIncrement">
         <size>
          <width>0</width>
          <height>0</height>
         </size>
        </property>
        <property name="baseSize">
         <size>
          <width>0</width>
          <height>0</height>
         </size>
        </property>
        <property name="layoutDirection">
         <enum>Qt::LeftToRight</enum>
        </property>
        <property name="styleSheet">
         <string notr="true">min-width: 68px;</string>
        </property>
        <property name="text">
         <string>Clear</string>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>


code:
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <QColorDialog>
#include "common/settings.h"
#include "core/core.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "ui_configure_input_advanced.h"
#include "yuzu/configuration/configure_input_advanced.h"

ConfigureInputAdvanced::ConfigureInputAdvanced(Core::HID::HIDCore& hid_core_, QWidget* parent)
    : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputAdvanced>()), hid_core{hid_core_} {
    ui->setupUi(this);

    controllers_color_buttons = {{
        {
            ui->player1_left_body_button,
            ui->player1_left_buttons_button,
            ui->player1_right_body_button,
            ui->player1_right_buttons_button,
        },
        {
            ui->player2_left_body_button,
            ui->player2_left_buttons_button,
            ui->player2_right_body_button,
            ui->player2_right_buttons_button,
        },
        {
            ui->player3_left_body_button,
            ui->player3_left_buttons_button,
            ui->player3_right_body_button,
            ui->player3_right_buttons_button,
        },
        {
            ui->player4_left_body_button,
            ui->player4_left_buttons_button,
            ui->player4_right_body_button,
            ui->player4_right_buttons_button,
        },
        {
            ui->player5_left_body_button,
            ui->player5_left_buttons_button,
            ui->player5_right_body_button,
            ui->player5_right_buttons_button,
        },
        {
            ui->player6_left_body_button,
            ui->player6_left_buttons_button,
            ui->player6_right_body_button,
            ui->player6_right_buttons_button,
        },
        {
            ui->player7_left_body_button,
            ui->player7_left_buttons_button,
            ui->player7_right_body_button,
            ui->player7_right_buttons_button,
        },
        {
            ui->player8_left_body_button,
            ui->player8_left_buttons_button,
            ui->player8_right_body_button,
            ui->player8_right_buttons_button,
        },
    }};

    for (std::size_t player_idx = 0; player_idx < controllers_color_buttons.size(); ++player_idx) {
        auto& color_buttons = controllers_color_buttons[player_idx];
        for (std::size_t button_idx = 0; button_idx < color_buttons.size(); ++button_idx) {
            connect(color_buttons[button_idx], &QPushButton::clicked, this,
                    [this, player_idx, button_idx] {
                        OnControllerButtonClick(player_idx, button_idx);
                    });
        }
    }

    connect(ui->mouse_enabled, &QCheckBox::stateChanged, this,
            &ConfigureInputAdvanced::UpdateUIEnabled);
    connect(ui->debug_enabled, &QCheckBox::stateChanged, this,
            &ConfigureInputAdvanced::UpdateUIEnabled);
    connect(ui->touchscreen_enabled, &QCheckBox::stateChanged, this,
            &ConfigureInputAdvanced::UpdateUIEnabled);
    connect(ui->enable_ring_controller, &QCheckBox::stateChanged, this,
            &ConfigureInputAdvanced::UpdateUIEnabled);

    connect(ui->debug_configure, &QPushButton::clicked, this,
            [this] { CallDebugControllerDialog(); });
    connect(ui->touchscreen_advanced, &QPushButton::clicked, this,
            [this] { CallTouchscreenConfigDialog(); });
    connect(ui->buttonMotionTouch, &QPushButton::clicked, this,
            [this] { CallMotionTouchConfigDialog(); });
    connect(ui->ring_controller_configure, &QPushButton::clicked, this,
            [this] { CallRingControllerDialog(); });
    connect(ui->camera_configure, &QPushButton::clicked, this, [this] { CallCameraDialog(); });

#ifndef _WIN32
    ui->enable_raw_input->setVisible(false);
#endif

    LoadConfiguration();
}

ConfigureInputAdvanced::~ConfigureInputAdvanced() = default;

void ConfigureInputAdvanced::OnControllerButtonClick(std::size_t player_idx,
                                                     std::size_t button_idx) {
    const QColor new_bg_color = QColorDialog::getColor(controllers_colors[player_idx][button_idx]);
    if (!new_bg_color.isValid()) {
        return;
    }
    controllers_colors[player_idx][button_idx] = new_bg_color;
    controllers_color_buttons[player_idx][button_idx]->setStyleSheet(
        QStringLiteral("background-color: %1; min-width: 60px;")
            .arg(controllers_colors[player_idx][button_idx].name()));
}

void ConfigureInputAdvanced::ApplyConfiguration() {
    for (std::size_t player_idx = 0; player_idx < controllers_color_buttons.size(); ++player_idx) {
        auto& player = Settings::values.players.GetValue()[player_idx];
        std::array<u32, 4> colors{};
        std::transform(controllers_colors[player_idx].begin(), controllers_colors[player_idx].end(),
                       colors.begin(), [](QColor color) { return color.rgb(); });

        player.body_color_left = colors[0];
        player.button_color_left = colors[1];
        player.body_color_right = colors[2];
        player.button_color_right = colors[3];

        hid_core.GetEmulatedControllerByIndex(player_idx)->ReloadColorsFromSettings();
    }

    Settings::values.debug_pad_enabled = ui->debug_enabled->isChecked();
    Settings::values.mouse_enabled = ui->mouse_enabled->isChecked();
    Settings::values.keyboard_enabled = ui->keyboard_enabled->isChecked();
    Settings::values.emulate_analog_keyboard = ui->emulate_analog_keyboard->isChecked();
    Settings::values.touchscreen.enabled = ui->touchscreen_enabled->isChecked();
    Settings::values.enable_raw_input = ui->enable_raw_input->isChecked();
    Settings::values.enable_udp_controller = ui->enable_udp_controller->isChecked();
    Settings::values.controller_navigation = ui->controller_navigation->isChecked();
    Settings::values.enable_ring_controller = ui->enable_ring_controller->isChecked();
    Settings::values.enable_ir_sensor = ui->enable_ir_sensor->isChecked();
    Settings::values.enable_joycon_driver = ui->enable_joycon_driver->isChecked();
    Settings::values.enable_procon_driver = ui->enable_procon_driver->isChecked();
    Settings::values.random_amiibo_id = ui->random_amiibo_id->isChecked();
}

void ConfigureInputAdvanced::LoadConfiguration() {
    for (std::size_t player_idx = 0; player_idx < controllers_color_buttons.size(); ++player_idx) {
        auto& player = Settings::values.players.GetValue()[player_idx];
        std::array<u32, 4> colors = {
            player.body_color_left,
            player.button_color_left,
            player.body_color_right,
            player.button_color_right,
        };

        std::transform(colors.begin(), colors.end(), controllers_colors[player_idx].begin(),
                       [](u32 rgb) { return QColor::fromRgb(rgb); });

        for (std::size_t button_idx = 0; button_idx < colors.size(); ++button_idx) {
            controllers_color_buttons[player_idx][button_idx]->setStyleSheet(
                QStringLiteral("background-color: %1; min-width: 60px;")
                    .arg(controllers_colors[player_idx][button_idx].name()));
        }
    }

    ui->debug_enabled->setChecked(Settings::values.debug_pad_enabled.GetValue());
    ui->mouse_enabled->setChecked(Settings::values.mouse_enabled.GetValue());
    ui->keyboard_enabled->setChecked(Settings::values.keyboard_enabled.GetValue());
    ui->emulate_analog_keyboard->setChecked(Settings::values.emulate_analog_keyboard.GetValue());
    ui->touchscreen_enabled->setChecked(Settings::values.touchscreen.enabled);
    ui->enable_raw_input->setChecked(Settings::values.enable_raw_input.GetValue());
    ui->enable_udp_controller->setChecked(Settings::values.enable_udp_controller.GetValue());
    ui->controller_navigation->setChecked(Settings::values.controller_navigation.GetValue());
    ui->enable_ring_controller->setChecked(Settings::values.enable_ring_controller.GetValue());
    ui->enable_ir_sensor->setChecked(Settings::values.enable_ir_sensor.GetValue());
    ui->enable_joycon_driver->setChecked(Settings::values.enable_joycon_driver.GetValue());
    ui->enable_procon_driver->setChecked(Settings::values.enable_procon_driver.GetValue());
    ui->random_amiibo_id->setChecked(Settings::values.random_amiibo_id.GetValue());

    UpdateUIEnabled();
}

void ConfigureInputAdvanced::changeEvent(QEvent* event) {
    if (event->type() == QEvent::LanguageChange) {
        RetranslateUI();
    }

    QWidget::changeEvent(event);
}

void ConfigureInputAdvanced::RetranslateUI() {
    ui->retranslateUi(this);
}

void ConfigureInputAdvanced::UpdateUIEnabled() {
    ui->debug_configure->setEnabled(ui->debug_enabled->isChecked());
    ui->touchscreen_advanced->setEnabled(ui->touchscreen_enabled->isChecked());
    ui->ring_controller_configure->setEnabled(ui->enable_ring_controller->isChecked());
#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0) || !defined(YUZU_USE_QT_MULTIMEDIA)
    ui->enable_ir_sensor->setEnabled(false);
    ui->camera_configure->setEnabled(false);
#endif
}

repo_name: litucks/torzu

license: mit


code:
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <memory>
#include <QWidget>

class QColor;
class QPushButton;

namespace Ui {
class ConfigureInputAdvanced;
}

namespace Core::HID {
class HIDCore;
} // namespace Core::HID

class ConfigureInputAdvanced : public QWidget {
    Q_OBJECT

public:
    explicit ConfigureInputAdvanced(Core::HID::HIDCore& hid_core_, QWidget* parent = nullptr);
    ~ConfigureInputAdvanced() override;

    void ApplyConfiguration();

signals:
    void CallDebugControllerDialog();
    void CallMouseConfigDialog();
    void CallTouchscreenConfigDialog();
    void CallMotionTouchConfigDialog();
    void CallRingControllerDialog();
    void CallCameraDialog();

private:
    void changeEvent(QEvent* event) override;
    void RetranslateUI();
    void UpdateUIEnabled();

    void OnControllerButtonClick(std::size_t player_idx, std::size_t button_idx);

    void LoadConfiguration();

    std::unique_ptr<Ui::ConfigureInputAdvanced> ui;

    std::array<std::array<QColor, 4>, 8> controllers_colors;
    std::array<std::array<QPushButton*, 4>, 8> controllers_color_buttons;

    Core::HID::HIDCore& hid_core;
};