summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt2/Config/Mapping/MappingButton.cpp
blob: dcb92e607297da63dc76bfad0af60c84926e5fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <QMouseEvent>
#include <QRegExp>
#include <QString>
#include <thread>

#include "DolphinQt2/Config/Mapping/MappingButton.h"

#include "Common/Thread.h"
#include "DolphinQt2/Config/Mapping/MappingWidget.h"
#include "DolphinQt2/Config/Mapping/MappingWindow.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref)
    : ElidedButton(QString::fromStdString(ref->expression)), m_parent(widget), m_reference(ref)
{
  Connect();
}

void MappingButton::Connect()
{
  connect(this, &MappingButton::clicked, this, &MappingButton::OnButtonPressed);
}

static QString
GetExpressionForControl(const QString& control_name,
                        const ciface::Core::DeviceQualifier* control_device = nullptr,
                        const ciface::Core::DeviceQualifier* default_device = nullptr)
{
  QString expr;

  // non-default device
  if (control_device && default_device && !(*control_device == *default_device))
  {
    expr += QString::fromStdString(control_device->ToString());
    expr += QStringLiteral(":");
  }

  // append the control name
  expr += control_name;

  QRegExp reg(QStringLiteral("[a-zA-Z0-9_]*"));
  if (!reg.exactMatch(expr))
    expr = QStringLiteral("`%1`").arg(expr);

  return expr;
}

void MappingButton::OnButtonPressed()
{
  if (m_block || m_parent->GetDevice() == nullptr)
    return;

  // Make sure that we don't block event handling
  std::thread([this] {
    if (m_reference->IsInput())
    {
      const auto dev = m_parent->GetDevice();

      setText(QStringLiteral("..."));

      Common::SleepCurrentThread(100);

      SetBlockInputs(true);

      if (m_parent->GetFirstButtonPress())
        m_reference->Detect(10, dev.get());

      // Avoid that the button press itself is registered as an event
      Common::SleepCurrentThread(100);

      ciface::Core::Device::Control* const ctrl = m_reference->Detect(5000, dev.get());

      SetBlockInputs(false);
      if (ctrl)
      {
        m_reference->expression =
            GetExpressionForControl(QString::fromStdString(ctrl->GetName())).toStdString();
        Update();
      }
      else
      {
        OnButtonTimeout();
      }
    }
    else
    {
      // TODO: Implement Output
    }
  }).detach();
}

void MappingButton::OnButtonTimeout()
{
  setText(QStringLiteral(""));
}

void MappingButton::Clear()
{
  m_reference->expression.clear();
  Update();
}

void MappingButton::Update()
{
  const auto lock = ControllerEmu::EmulatedController::GetStateLock();
  m_reference->UpdateReference(g_controller_interface, m_parent->GetParent()->GetDeviceQualifier());
  setText(QString::fromStdString(m_reference->expression));
  m_parent->SaveSettings();
}

void MappingButton::SetBlockInputs(const bool block)
{
  m_parent->SetBlockInputs(block);
  m_block = block;
}

void MappingWindow::OnDefaultFieldsPressed()
{
  if (m_controller == nullptr)
    return;

  m_controller->LoadDefaults(g_controller_interface);
  m_controller->UpdateReferences(g_controller_interface);
  emit Update();
}

bool MappingButton::event(QEvent* event)
{
  return !m_block ? QPushButton::event(event) : true;
}

void MappingButton::mouseReleaseEvent(QMouseEvent* event)
{
  if (m_reference->IsInput())
  {
    switch (event->button())
    {
    case Qt::MouseButton::LeftButton:
      QPushButton::mouseReleaseEvent(event);
      break;
    case Qt::MouseButton::MiddleButton:
      Clear();
      break;
    case Qt::MouseButton::RightButton:
      // TODO Open advanced dialog
      break;
    default:
      break;
    }
  }
  else
  {
    // TODO Open output dialog
  }
}