Skip to content

Commit

Permalink
DolphinQt: Break mapping indicators into separate classes. Ensure "st…
Browse files Browse the repository at this point in the history
…ate lock" is held on redraw.
  • Loading branch information
jordan-woyak committed Feb 24, 2020
1 parent 7accd98 commit 38f36be
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ void InputStateDelegate::paint(QPainter* painter, const QStyleOptionViewItem& op
rect.setWidth(rect.width() * std::clamp(state, 0.0, 1.0));

// Create a temporary indicator object to retreive color constants.
MappingIndicator indicator(nullptr);
MappingIndicator indicator;

painter->save();

Expand Down
136 changes: 53 additions & 83 deletions Source/Core/DolphinQt/Config/Mapping/MappingIndicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ void MappingIndicator::AdjustGateColor(QColor* color)
color->setHsvF(color->hueF(), color->saturationF(), 1 - color->valueF());
}

MappingIndicator::MappingIndicator(ControllerEmu::ControlGroup* group) : m_group(group)
MappingIndicator::MappingIndicator()
{
// TODO: Make these magic numbers less ugly.
int required_height = 106;

if (group && ControllerEmu::GroupType::MixedTriggers == group->type)
required_height = 64 + 1;
const int required_height = 106;
setFixedHeight(required_height);
}

MixedTriggersIndicator::MixedTriggersIndicator(ControllerEmu::MixedTriggers& group) : m_group(group)
{
const int required_height = 64 + 1;
setFixedHeight(required_height);
}

Expand Down Expand Up @@ -209,8 +211,16 @@ void GenerateFibonacciSphere(int point_count, F&& callback)

} // namespace

void MappingIndicator::DrawCursor(ControllerEmu::Cursor& cursor)
void MappingIndicator::paintEvent(QPaintEvent*)
{
const auto lock = ControllerEmu::EmulatedController::GetStateLock();
Draw();
}

void CursorIndicator::Draw()
{
auto& cursor = m_cursor_group;

const auto center = cursor.GetCenter();

QColor tv_brush_color = CURSOR_TV_COLOR;
Expand Down Expand Up @@ -291,40 +301,19 @@ void MappingIndicator::DrawCursor(ControllerEmu::Cursor& cursor)
}
}

void MappingIndicator::DrawReshapableInput(ControllerEmu::ReshapableInput& stick)
void ReshapableInputIndicator::DrawReshapableInput(
ControllerEmu::ReshapableInput& stick,
const ControllerEmu::ReshapableInput::ReshapeData& adj_coord, QColor gate_brush_color)
{
// Some hacks for pretty colors:
const bool is_c_stick = m_group->name == "C-Stick";
const bool is_tilt = m_group->name == "Tilt";

const auto center = stick.GetCenter();

QColor gate_brush_color = STICK_GATE_COLOR;

if (is_c_stick)
gate_brush_color = C_STICK_GATE_COLOR;
else if (is_tilt)
gate_brush_color = TILT_GATE_COLOR;

QColor gate_pen_color = gate_brush_color.darker(125);

AdjustGateColor(&gate_brush_color);
AdjustGateColor(&gate_pen_color);

const auto raw_coord = stick.GetReshapableState(false);

Common::DVec2 adj_coord;
if (is_tilt)
{
WiimoteEmu::EmulateTilt(&m_motion_state, static_cast<ControllerEmu::Tilt*>(&stick),
1.f / INDICATOR_UPDATE_FREQ);
adj_coord = Common::DVec2{-m_motion_state.angle.y, m_motion_state.angle.x} / MathUtil::PI;
}
else
{
adj_coord = stick.GetReshapableState(true);
}

UpdateCalibrationWidget(raw_coord);

// Bounding box size:
Expand Down Expand Up @@ -391,12 +380,33 @@ void MappingIndicator::DrawReshapableInput(ControllerEmu::ReshapableInput& stick
}
}

void MappingIndicator::DrawMixedTriggers()
void AnalogStickIndicator::Draw()
{
// Some hacks for pretty colors:
const bool is_c_stick = m_group.name == "C-Stick";

const auto gate_brush_color = is_c_stick ? C_STICK_GATE_COLOR : STICK_GATE_COLOR;

const auto adj_coord = m_group.GetReshapableState(true);

DrawReshapableInput(m_group, adj_coord, gate_brush_color);
}

void TiltIndicator::Draw()
{
WiimoteEmu::EmulateTilt(&m_motion_state, &m_group, 1.f / INDICATOR_UPDATE_FREQ);
const auto adj_coord =
Common::DVec2{-m_motion_state.angle.y, m_motion_state.angle.x} / MathUtil::PI;

DrawReshapableInput(m_group, adj_coord, TILT_GATE_COLOR);
}

void MixedTriggersIndicator::Draw()
{
QPainter p(this);
p.setRenderHint(QPainter::TextAntialiasing, true);

const auto& triggers = *static_cast<ControllerEmu::MixedTriggers*>(m_group);
const auto& triggers = m_group;
const ControlState threshold = triggers.GetThreshold();
const ControlState deadzone = triggers.GetDeadzone();

Expand Down Expand Up @@ -486,8 +496,10 @@ void MappingIndicator::DrawMixedTriggers()
}
}

void MappingIndicator::DrawForce(ControllerEmu::Force& force)
void SwingIndicator::Draw()
{
auto& force = m_swing_group;

const auto center = force.GetCenter();

QColor gate_brush_color = SWING_GATE_COLOR;
Expand Down Expand Up @@ -597,39 +609,7 @@ void MappingIndicator::DrawForce(ControllerEmu::Force& force)
}
}

void MappingIndicator::paintEvent(QPaintEvent*)
{
switch (m_group->type)
{
case ControllerEmu::GroupType::Cursor:
DrawCursor(*static_cast<ControllerEmu::Cursor*>(m_group));
break;
case ControllerEmu::GroupType::Stick:
case ControllerEmu::GroupType::Tilt:
DrawReshapableInput(*static_cast<ControllerEmu::ReshapableInput*>(m_group));
break;
case ControllerEmu::GroupType::MixedTriggers:
DrawMixedTriggers();
break;
case ControllerEmu::GroupType::Force:
DrawForce(*static_cast<ControllerEmu::Force*>(m_group));
break;
default:
break;
}
}

ShakeMappingIndicator::ShakeMappingIndicator(ControllerEmu::Shake* group)
: MappingIndicator(group), m_shake_group(*group)
{
}

void ShakeMappingIndicator::paintEvent(QPaintEvent*)
{
DrawShake();
}

void ShakeMappingIndicator::DrawShake()
void ShakeMappingIndicator::Draw()
{
constexpr std::size_t HISTORY_COUNT = INDICATOR_UPDATE_FREQ;

Expand Down Expand Up @@ -706,12 +686,7 @@ void ShakeMappingIndicator::DrawShake()
}
}

AccelerometerMappingIndicator::AccelerometerMappingIndicator(ControllerEmu::IMUAccelerometer* group)
: MappingIndicator(group), m_accel_group(*group)
{
}

void AccelerometerMappingIndicator::paintEvent(QPaintEvent*)
void AccelerometerMappingIndicator::Draw()
{
const auto accel_state = m_accel_group.GetState();
const auto state = accel_state.value_or(Common::Vec3{});
Expand Down Expand Up @@ -793,12 +768,7 @@ void AccelerometerMappingIndicator::paintEvent(QPaintEvent*)
fmt::format("{:.2f} g", state.Length() / WiimoteEmu::GRAVITY_ACCELERATION)));
}

GyroMappingIndicator::GyroMappingIndicator(ControllerEmu::IMUGyroscope* group)
: MappingIndicator(group), m_gyro_group(*group), m_state(Common::Matrix33::Identity())
{
}

void GyroMappingIndicator::paintEvent(QPaintEvent*)
void GyroMappingIndicator::Draw()
{
const auto gyro_state = m_gyro_group.GetState();
const auto raw_gyro_state = m_gyro_group.GetRawState();
Expand Down Expand Up @@ -915,7 +885,7 @@ void GyroMappingIndicator::paintEvent(QPaintEvent*)
p.drawEllipse(QPointF{}, INPUT_DOT_RADIUS, INPUT_DOT_RADIUS);
}

void MappingIndicator::DrawCalibration(QPainter& p, Common::DVec2 point)
void ReshapableInputIndicator::DrawCalibration(QPainter& p, Common::DVec2 point)
{
// Bounding box size:
const double scale = GetScale();
Expand All @@ -942,24 +912,24 @@ void MappingIndicator::DrawCalibration(QPainter& p, Common::DVec2 point)
p.drawEllipse(QPointF{point.x, point.y} * scale, INPUT_DOT_RADIUS, INPUT_DOT_RADIUS);
}

void MappingIndicator::UpdateCalibrationWidget(Common::DVec2 point)
void ReshapableInputIndicator::UpdateCalibrationWidget(Common::DVec2 point)
{
if (m_calibration_widget)
m_calibration_widget->Update(point);
}

bool MappingIndicator::IsCalibrating() const
bool ReshapableInputIndicator::IsCalibrating() const
{
return m_calibration_widget && m_calibration_widget->IsCalibrating();
}

void MappingIndicator::SetCalibrationWidget(CalibrationWidget* widget)
void ReshapableInputIndicator::SetCalibrationWidget(CalibrationWidget* widget)
{
m_calibration_widget = widget;
}

CalibrationWidget::CalibrationWidget(ControllerEmu::ReshapableInput& input,
MappingIndicator& indicator)
ReshapableInputIndicator& indicator)
: m_input(input), m_indicator(indicator), m_completion_action{}
{
m_indicator.SetCalibrationWidget(this);
Expand Down
Loading

0 comments on commit 38f36be

Please sign in to comment.