Refresh controller only when necessary

This commit is contained in:
german 2021-02-02 22:32:45 -06:00
parent c9597af39d
commit d6a0975e5d
2 changed files with 37 additions and 15 deletions

View File

@ -11,10 +11,10 @@
PlayerControlPreview::PlayerControlPreview(QWidget* parent) : QFrame(parent) { PlayerControlPreview::PlayerControlPreview(QWidget* parent) : QFrame(parent) {
UpdateColors(); UpdateColors();
QTimer* timer = new QTimer(this); QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&PlayerControlPreview::update)); connect(timer, &QTimer::timeout, this, QOverload<>::of(&PlayerControlPreview::UpdateInput));
// refresh at 40hz // refresh at 60hz
timer->start(25); timer->start(16);
} }
PlayerControlPreview::~PlayerControlPreview() = default; PlayerControlPreview::~PlayerControlPreview() = default;
@ -155,12 +155,8 @@ void PlayerControlPreview::UpdateColors() {
// colors.right = QColor(Settings::values.players.GetValue()[player_index].body_color_right); // colors.right = QColor(Settings::values.players.GetValue()[player_index].body_color_right);
} }
void PlayerControlPreview::paintEvent(QPaintEvent* event) { void PlayerControlPreview::UpdateInput() {
QFrame::paintEvent(event); bool input_changed = false;
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
const QPointF center = rect().center();
const auto& button_state = buttons; const auto& button_state = buttons;
for (std::size_t index = 0; index < button_values.size(); ++index) { for (std::size_t index = 0; index < button_values.size(); ++index) {
bool value = false; bool value = false;
@ -169,7 +165,10 @@ void PlayerControlPreview::paintEvent(QPaintEvent* event) {
} }
bool blink = mapping_active && index == button_mapping_index; bool blink = mapping_active && index == button_mapping_index;
if (analog_mapping_index == Settings::NativeAnalog::NUM_STICKS_HID) { if (analog_mapping_index == Settings::NativeAnalog::NUM_STICKS_HID) {
blink &= blink_counter > 12; blink &= blink_counter > 25;
}
if (button_values[index] != value || blink) {
input_changed = true;
} }
button_values[index] = value || blink; button_values[index] = value || blink;
} }
@ -178,17 +177,42 @@ void PlayerControlPreview::paintEvent(QPaintEvent* event) {
for (std::size_t index = 0; index < axis_values.size(); ++index) { for (std::size_t index = 0; index < axis_values.size(); ++index) {
const auto [stick_x_f, stick_y_f] = analog_state[index]->GetStatus(); const auto [stick_x_f, stick_y_f] = analog_state[index]->GetStatus();
const auto [stick_x_rf, stick_y_rf] = analog_state[index]->GetRawStatus(); const auto [stick_x_rf, stick_y_rf] = analog_state[index]->GetRawStatus();
if (static_cast<int>(stick_x_rf * 45) !=
static_cast<int>(axis_values[index].raw_value.x() * 45) ||
static_cast<int>(-stick_y_rf * 45) !=
static_cast<int>(axis_values[index].raw_value.y() * 45)) {
input_changed = true;
}
axis_values[index].properties = analog_state[index]->GetAnalogProperties(); axis_values[index].properties = analog_state[index]->GetAnalogProperties();
axis_values[index].value = QPointF(stick_x_f, -stick_y_f); axis_values[index].value = QPointF(stick_x_f, -stick_y_f);
axis_values[index].raw_value = QPointF(stick_x_rf, -stick_y_rf); axis_values[index].raw_value = QPointF(stick_x_rf, -stick_y_rf);
const bool blink_analog = mapping_active && index == analog_mapping_index; const bool blink_analog = mapping_active && index == analog_mapping_index;
if (blink_analog) { if (blink_analog) {
input_changed = true;
axis_values[index].value = axis_values[index].value =
QPointF(blink_counter < 12 ? -blink_counter / 12.0f : 0, QPointF(blink_counter < 25 ? -blink_counter / 25.0f : 0,
blink_counter > 12 ? -(blink_counter - 12) / 12.0f : 0); blink_counter > 25 ? -(blink_counter - 25) / 25.0f : 0);
} }
} }
if (input_changed) {
update();
}
if (mapping_active) {
blink_counter = (blink_counter + 1) % 50;
}
}
void PlayerControlPreview::paintEvent(QPaintEvent* event) {
QFrame::paintEvent(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
const QPointF center = rect().center();
switch (controller_type) { switch (controller_type) {
case Settings::ControllerType::Handheld: case Settings::ControllerType::Handheld:
DrawHandheldController(p, center); DrawHandheldController(p, center);
@ -207,9 +231,6 @@ void PlayerControlPreview::paintEvent(QPaintEvent* event) {
DrawProController(p, center); DrawProController(p, center);
break; break;
} }
if (mapping_active) {
blink_counter = (blink_counter + 1) % 24;
}
} }
void PlayerControlPreview::DrawLeftController(QPainter& p, const QPointF center) { void PlayerControlPreview::DrawLeftController(QPainter& p, const QPointF center) {

View File

@ -32,6 +32,7 @@ public:
void BeginMappingButton(std::size_t button_id); void BeginMappingButton(std::size_t button_id);
void BeginMappingAnalog(std::size_t button_id); void BeginMappingAnalog(std::size_t button_id);
void EndMapping(); void EndMapping();
void UpdateInput();
protected: protected:
void paintEvent(QPaintEvent* event) override; void paintEvent(QPaintEvent* event) override;