Skip to content

Commit

Permalink
Enable mouse scroll wheel control of sliders (#3582)
Browse files Browse the repository at this point in the history
Enable mouse scroll wheel for sliders
  • Loading branch information
stoneman authored Sep 22, 2023
1 parent cc32aaf commit 71e1240
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/css/tabs/pid_tuning.less
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@
}
}
.slidersWarning {
text-align: center;
background: #ffa3a3;
border: 1px solid red;
font-weight: 600;
Expand Down
29 changes: 29 additions & 0 deletions src/js/tabs/motors.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,8 @@ motors.initialize = async function (callback) {
'Home',
'ArrowUp',
'ArrowDown',
'AltLeft',
'AltRight',
];

motorsEnableTestModeElement.on('change', function () {
Expand Down Expand Up @@ -978,6 +980,10 @@ motors.initialize = async function (callback) {
}
});

$('div.sliders input:not(.master)').on('input wheel', function (e) {
self.scrollSlider($(this), e);
});

$('div.sliders input.master').on('input', function () {
const val = $(this).val();

Expand All @@ -986,6 +992,10 @@ motors.initialize = async function (callback) {
$('div.sliders input:not(:last):first').trigger('input');
});

$('div.sliders input.master').on('input wheel', function (e) {
self.scrollSlider($(this), e);
});

// check if motors are already spinning
let motorsRunning = false;

Expand Down Expand Up @@ -1316,6 +1326,25 @@ motors.cleanup = function (callback) {
if (callback) callback();
};

motors.scrollSlider = function(slider, e) {
if (slider.prop('disabled')) {
return;
}

if (!(e.originalEvent?.deltaY && e.originalEvent?.altKey)) {
return;
}

e.preventDefault();

const step = 25;
const delta = e.originalEvent.deltaY > 0 ? -step : step;
const val = parseInt(slider.val()) + delta;
const roundedVal = Math.round(val / step) * step;
slider.val(roundedVal);
slider.trigger('input');
};

TABS.motors = motors;
export {
motors,
Expand Down
31 changes: 31 additions & 0 deletions src/js/tabs/pid_tuning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,10 @@ pid_tuning.initialize = function (callback) {
self.analyticsChanges['PidTuningSliders'] = "On";
});

allPidTuningSliders.each(function (i) {
self.sliderOnScroll($(this));
});

// reset to middle with double click
allPidTuningSliders.dblclick(function() {
const slider = $(this);
Expand Down Expand Up @@ -2118,6 +2122,10 @@ pid_tuning.initialize = function (callback) {
}
});

allFilterTuningSliders.each(function() {
self.sliderOnScroll($(this));
});

// reset to middle with double click
allFilterTuningSliders.dblclick(function() {
const slider = $(this);
Expand Down Expand Up @@ -2970,6 +2978,29 @@ pid_tuning.expertModeChanged = function(expertModeEnabled) {
TuningSliders.setExpertMode(expertModeEnabled);
};

pid_tuning.sliderOnScroll = function(slider, e) {
slider.parent().on('input wheel', function(e) {
if (slider.prop('disabled')) {
return;
}

if (!(e.originalEvent?.deltaY && e.originalEvent?.altKey)) {
return;
}

e.preventDefault();

const step = parseFloat(slider.attr('step'));
const delta = e.originalEvent.deltaY > 0 ? -step : step;
const preScrollSliderValue = isInt(slider.val()) ? parseInt(slider.val()) : parseFloat(slider.val());
const sliderValue = (Math.floor(preScrollSliderValue * 100) + Math.floor(delta * 100)) / 100;

slider.val(sliderValue);
slider.trigger('input');
slider.trigger('change');
});
};

TABS.pid_tuning = pid_tuning;
export {
pid_tuning,
Expand Down
14 changes: 8 additions & 6 deletions src/tabs/pid_tuning.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@
<div class="note dMinDisabledNote">
<p i18n="pidTuningDMinDisabledNote"></p>
</div>
<div class="note slidersWarning">
<p i18n="pidTuningSliderWarning"></p>
</div>
<div class="profile_name">
<div class="number">
<label> <input type="text" name="pidProfileName" maxlength="8" style="width:100px;"/> <span
Expand Down Expand Up @@ -374,6 +371,10 @@

</table>
</div>

<div class="gui_box topspacer slidersWarning">
<p i18n="pidTuningSliderWarning"></p>
</div>

<div class="gui_box topspacer nonExpertModeSlidersNote">
<p i18n="pidTuningPidSlidersNonExpertMode"></p>
Expand Down Expand Up @@ -1183,9 +1184,6 @@
<div class="note dynamicNotchWarning">
<p i18n="pidTuningDynamicNotchFilterDisabledWarning"></p>
</div>
<div class="note slidersWarning">
<p i18n="pidTuningSliderWarning"></p>
</div>
<div class="gui_box slidersDisabled">
<table class="note-button">
<td i18n="pidTuningSlidersDisabled"></td>
Expand Down Expand Up @@ -1255,6 +1253,10 @@
</table>
</div>

<div class="gui_box topspacer slidersWarning">
<p i18n="pidTuningSliderWarning"></p>
</div>

<div class="gui_box topspacer nonExpertModeSlidersNote">
<p i18n="pidTuningFilterSlidersNonExpertMode"></p>
</div>
Expand Down

0 comments on commit 71e1240

Please sign in to comment.