Skip to content

Commit

Permalink
Enable mouse scroll wheel for sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
stoneman committed Sep 21, 2023
1 parent cc32aaf commit e4bc56d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 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
42 changes: 35 additions & 7 deletions src/js/tabs/motors.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,12 +898,13 @@ motors.initialize = async function (callback) {
setSlidersDefault();

const ignoreKeys = [
'PageUp',
'PageDown',
'End',
'Home',
'ArrowUp',
'ArrowDown',
33, // Page Up
34, // Page Down
35, // End
36, // Home
38, // Arrow Up
40, // Arrow Down
18, // Alt / Opt
];

motorsEnableTestModeElement.on('change', function () {
Expand All @@ -921,7 +922,7 @@ motors.initialize = async function (callback) {

function disableMotorTest(e) {
if (motorsEnableTestModeElement.is(':checked')) {
if (!ignoreKeys.includes(e.code)) {
if (!ignoreKeys.includes(e.which)) {
motorsEnableTestModeElement.prop('checked', false).trigger('change');
document.removeEventListener('keydown', evt => disableMotorTest(evt));
}
Expand Down Expand Up @@ -978,6 +979,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 +991,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 +1325,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 e4bc56d

Please sign in to comment.