In Dear ImGui, you can set the size of a drag item (or any other widget) by using ImGui::PushItemWidth() before creating the widget and after creating it. This sets the width of the item on the current window's item spacing line.
ImGui::PopItemWidth()
Here's an example of how you can set the size of a drag item:
cpp
// Set the desired width for the drag item
ImGui::PushItemWidth(200.0f); // Width in pixels
// Create the drag item
float value = 0.0f;
ImGui::DragFloat("Drag Item", &value);
// Restore the previous item width
ImGui::PopItemWidth();
In this example, the width of the drag item is set to 200 pixels. You can change 200.0f to whatever width you need.
If you need to set the size of the drag item independently of other items, ensuring only this specific widget has the desired width, make sure to wrap only this widget in ImGui::PushItemWidth() and ImGui::PopItemWidth() as shown above.
Furthermore, it's essential to call ImGui::PopItemWidth() to reset the width back to its previous value to prevent affecting other widgets that follow.
If you want to dynamically size the widget to fit different contexts (e.g., based on window size), you might also compute the width based on available dimensions:
cpp
// Example with dynamic sizing
float windowWidth = ImGui::GetWindowWidth();
ImGui::PushItemWidth(windowWidth * 0.5f); // Set width to 50% of window width
// Create drag item
float value = 0.0f;
ImGui::DragFloat("Dynamic Drag Item", &value);
// Restore previous item width
ImGui::PopItemWidth();
This sets the drag item width to half the window width, demonstrating how you can use relative sizing to make your UI adapt to different window sizes.
User
i meant the height
Assistant
In Dear ImGui, the height of standard widgets like ImGui::DragFloat is generally determined by the style settings and cannot be directly set like the width. However, you can create a custom drag item if you need to adjust the height specifically.
One way to achieve this is to use ImGui::SetNextItemWidth to set the width and then add spacing or custom styling around the widget to give the appearance of increased height. Alternatively, you can use a different approach, such as a custom button or a combination of ImGui::InvisibleButton and drawing operations for more control.
Here's an example of how you can create a custom drag item that allows you to control both the width and height:
cpp
#include "imgui.h"
// Helper function to handle dragging behavior
bool DragFloatCustom(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f")
{
ImGui::PushID(label);
// Invisible button to capture mouse dragging
ImGui::InvisibleButton("##drag", ImVec2(ImGui::GetContentRegionAvail().x, 50.0f)); // Set height to 50 pixels
bool isActive = ImGui::IsItemActive();
// Drawing background (optional)
ImGui::GetWindowDrawList()->AddRectFilled(
ImGui::GetItemRectMin(),
ImGui::GetItemRectMax(),
isActive ? IM_COL32(150, 150, 150, 255) : IM_COL32(100, 100, 100, 255)
);
bool valueChanged = false;
// Update the floating value based on mouse drag
if (isActive)
{
float mouseDelta = ImGui::GetIO().MouseDelta.x;
*v += mouseDelta * v_speed;
if (v_min != v_max)
*v = *v < v_min ? v_min : (*v > v_max ? v_max : *v);
valueChanged = true;
}
// Draw the label and value
ImVec2 pos = ImGui::GetItemRectMin();
ImGui::SetCursorScreenPos(ImVec2(pos.x + 10, pos.y + 10));
ImGui::Text("%s: ", label);
ImGui::SameLine();
ImGui::Text(format, *v);
ImGui::PopID();
return valueChanged;
}
void MyCustomUI()
{
float myValue = 0.0f;
if (DragFloatCustom("My Drag Item", &myValue))
{
// Value changed action
}
}
User
what do i have to change in reaimgui to change the sensitivity of vsliders
Assistant
To adjust the sensitivity of vertical sliders (ImGui::VSliderFloat(), ImGui::VSliderInt(), etc.) in Dear ImGui, you will need to modify the internal workings of the slider behavior, as the library doesn't directly expose a parameter for sensitivity.
Here's a step-by-step guide to achieve this:
Understanding the Sensitivity: Sensitivity in sliders is generally controlled by the amount of value change per pixel of mouse movement. To increase sensitivity, you need to make the value change more with less mouse movement, and to decrease sensitivity, you need to make the value change less with more mouse movement.
Source Code Modification:
The slider behavior in Dear ImGui is governed by the internal function called by ImGui::VSliderFloat(). If you're comfortable modifying the library's source code, you can alter this function directly. Here's a basic idea of what to do:
Locate the implementation of the ImGui::VSliderFloat function and modify the code where the value is adjusted based on mouse movement. Typically, this involves changing the sensitivity factor.
However, modifying the source code of a third-party library is generally not recommended because it can make upgrading to newer versions of the library more complex.
Using a Wrapper Function:
Instead of modifying the library, a more sustainable approach is to encapsulate the slider behavior in a custom function that introduces a sensitivity parameter.
Here's an example of how to create a custom vertical slider with adjustable sensitivity:
cpp
User
ctx = reaper.ImGui_CreateContext("ctx")
vol_image = reaper.ImGui_CreateImage("E:\Reaper Themes\Rykord Theming\ReaTools Dark V2.29 Time SelectionReaperTheme\ReaTools_Dark_unpack\mcp_volthumb.png")
v_initial = 0 -- Initial value for the slider
function main()
local _, is_open = reaper.ImGui_Begin(ctx, "ClipGain", true)
code
slider()
if is_open then
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
function slider()
local slider_width = 16
local slider_height = 200
local slider_min = -150
local slider_max = 12
code
-- Create the VSlider
local retval, v = reaper.ImGui_VSliderDouble(ctx, "##vslider", slider_width, slider_height, v_initial, slider_min, slider_max)
if v_initial ~= v then
v_initial = v
end
local image_width = 40
local image_height = 50
local y_position = -175 / 162 * v + 27.96
-- Set cursor position so that the image is centered on the slider handle
local handlepos = (162 * v - 150)
reaper.ImGui_SetCursorPos(ctx, -3, y_position)
reaper.ImGui_Image(ctx, vol_image, image_width, image_height)
end
main()
how do i make "if retval, mouse moves slower"?
Assistant
To make the mouse movement less sensitive when interacting with the vertical slider, you can adjust how much the returned value (v) changes with mouse movement. Unfortunately, the standard ImGui functions (ImGui_VSliderDouble in this case) do not provide a direct way to adjust sensitivity.
One way to achieve the desired behavior is to manually adjust the value change by introducing a sensitivity factor in your slider function. This way, you can increase or decrease how much the returned value changes when the mouse moves.
Here's how you can modify your slider function to include a sensitivity factor:
lua
ctx = reaper.ImGui_CreateContext("ctx")
vol_image = reaper.ImGui_CreateImage("E:\\Reaper Themes\\Rykord Theming\\ReaTools Dark V2.29 Time SelectionReaperTheme\\ReaTools_Dark_unpack\\mcp_volthumb.png")
v_initial = 0 -- Initial value for the slider
function main()
local _, is_open = reaper.ImGui_Begin(ctx, "ClipGain", true)
slider()
if is_open then
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
function slider()
local sensitivity = 0.5 -- Adjust this value to change sensitivity (smaller values = less sensitive)
local slider_width = 16
local slider_height = 200
local slider_min = -150
local slider_max = 12
-- Create the VSlider
local retval, tv = reaper.ImGui_VSliderDouble(ctx, "##vslider", slider_width, slider_height, v_initial, slider_min, slider_max)
if retval then
-- Apply sensitivity factor to the value change
v_initial = v_initial + (tv - v_initial) * sensitivity
end
local image_width = 40
local image_height = 50
local y_position = -175 / 162 * v_initial + 27.96
-- Set cursor position so that the image is centered on the slider handle
reaper.ImGui_SetCursorPos(ctx, -3, y_position)
reaper.ImGui_Image(ctx, vol_image, image_width, image_height)
end
main()
User
isn't there a way to move the mouse slower?
Assistant
The ImGui_VSliderDouble function in Dear ImGui (as implemented in ReaImGui for Reaper) does not provide a direct parameter to adjust the sensitivity of the mouse movement. However, you can achieve the effect of finer control by modifying how the value of the slider is updated in your script.
If your goal is to improve the user's experience by making the slider respond more granularly to mouse movement, you might simulate it by adjusting the value less drastically on each mouse movement, which is essentially what was suggested earlier with the sensitivity factor.
Unfortunately, ReaImGui itself doesn't have a built-in feature to control mouse sensitivity directly within an existing slider function. However, you can simulate slower or finer control over the slider by implementing a custom slider that leverages the mouse dragging events with your own sensitivity adjustments. This is similar to what we did earlier but with a more precise control over mouse movements.
Here’s a more refined approach:
Drag Operation: Detect the mouse drag event.
Custom Sensitivity: Adjust the amount of change based on a sensitivity factor.
Below is a more detailed version of the custom slider function with lofi mouse control:
lua
User
in reaper api, is there a function to make the mouse move slower if a condition is met?
Assistant
The Reaper API does not provide a direct function to change the actual sensitivity or velocity of the mouse movements. However, you can achieve the desired behavior indirectly in your script by interpreting the mouse movements differently based on certain conditions.
The key idea here is to adjust how you handle mouse input within your script. If a condition is met (e.g., a key is pressed or a specific mode is active), you can scale down the mouse delta values to simulate slower mouse movement.
Here’s an implementation where we use a modifier key (for example, holding down the "Shift" key) to slow down the slider’s response to mouse movements:
lua
ctx = reaper.ImGui_CreateContext("ctx")
vol_image = reaper.ImGui_CreateImage("E:\\Reaper Themes\\Rykord Theming\\ReaTools Dark V2.29 Time SelectionReaperTheme\\ReaTools_Dark_unpack\\mcp_volthumb.png")
v_initial = 0 -- Initial value for the slider
function main()
local _ = reaper.ImGui_Begin(ctx, "ClipGain", true) -- begin ImGui window
slider()
reaper.ImGui_End(ctx) -- end ImGui window
reaper.defer(main) -- keep the loop running
end
function slider()
local slider_width = 16
local slider_height = 200
local slider_min = -150
local slider_max = 12
local normal_sensitivity = 0.1
local slow_sensitivity = 0.05
local sensitivity
-- Check if the Shift key is down (or use another key/mouse button)
if reaper.ImGui_IsKeyDown(ctx, reaper.ImGui_ModKey_Shift()) then
sensitivity = slow_sensitivity
else
sensitivity = normal_sensitivity
end
-- Create the VSlider background
reaper.ImGui_SetNextItemWidth(ctx, slider_width)
reaper.ImGui_InvisibleButton(ctx, "##vslider", slider_width, slider_height)
if reaper.ImGui_IsItemActive(ctx) then
-- Calculate the mouse delta
local mouse_delta = reaper.ImGui_GetMouseDragDelta(ctx, 0, false).y
reaper.ImGui_ResetMouseDragDelta(ctx, 0)
-- Apply sensitivity and update the value
v_initial = v_initial - mouse_delta * sensitivity
v_initial = math.max(math.min(v_initial, slider_max), slider_min) -- Clamp value within slider range
end
local image_width = 40
local image_height = 50
local y_position = -175 / 162 * v_initial + 27.96
-- Set cursor position so that the image is centered on the slider handle
reaper.ImGui_SetCursorPos(ctx, -3, y_position)
reaper.ImGui_Image(ctx, vol_image, image_width, image_height)
-- Show the current value for reference (optional)
reaper.ImGui_Text(ctx, string.format("Value: %.2f", v_initial))
end
main()
User
local ctx = reaper.ImGui_CreateContext('Slider Test', 250, 250)
local default_value = 0
local value = default_value
local default_value2 = 1
local value2 = default_value2
local default_value3 = 2
local value3 = default_value3
local default_value4 = 3
local value4 = default_value4
local reset = {}
----------------------------- TEST FUNCTION ----------------------
function dosomething()
reaper.ShowConsoleMsg(test_input .. '\n')
end
if reaper.ImGui_IsCloseRequested(ctx) then
reaper.ImGui_DestroyContext(ctx)
return
end
reaper.ImGui_SetNextWindowPos(ctx, 0, 0)-- Don't really need but it makes it at 0 0 "Lock it"
reaper.ImGui_SetNextWindowSize(ctx, reaper.ImGui_GetDisplaySize(ctx))-- Don't really need but it makes it the size of the screen
reaper.ImGui_Begin(ctx, 'wnd', nil, reaper.ImGui_WindowFlags_NoDecoration())
local rv
---Vertical Slider
rv,value = myVSliderInt('##v1', 40, 160, value, -10, 10, default_value)
if rv then
test_input = value
dosomething()
end
reaper.ImGui_SameLine(ctx)
---Vertical Slider Double
rv,value2 = myVSliderDouble('##v2', 40, 160, value2, -10, 10, default_value2)
if rv then
test_input = value2
dosomething()
end
---Horizontal Slider
rv,value3 = mySliderInt('##v3', value3, -10, 10, default_value3)
if rv then
test_input = value3
dosomething()
end
---Horizontal Slider Double
rv,value4 = mySliderDouble('##v4', value4, -10, 10, default_value4)
if rv then
test_input = value4
dosomething()
end
reaper.ImGui_End(ctx)
reaper.defer(loop)
end
---------------------------- DOUBLE CLICK AND MOUSEWHEEL FUNCTIONS --------------------------------
function resetOnDoubleClick(id, value, default)
if reaper.ImGui_IsItemDeactivated(ctx) and reset[id] then
reset[id] = nil
return default
elseif reaper.ImGui_IsItemHovered(ctx) and reaper.ImGui_IsMouseDoubleClicked(ctx, 0) then
reset[id] = true
end
return value
end
function applyMousewheel(value, min, max)
local shift = reaper.ImGui_GetKeyMods(ctx) & reaper.ImGui_KeyModFlags_Shift() ~= 0
local wheel, hwheel = reaper.ImGui_GetMouseWheel(ctx)
if shift and wheel == 0 then wheel = hwheel end
if wheel ~= 0 then
speed = shift and int_mousewheel_sensitivity_fine or int_mousewheel_sensitivity
value = value + math.ceil(wheel * speed)
return true, math.max(min, math.min(value, max))
end
rv=true
return false, value
end
function applyMousewheelDouble(value, min, max)
local shift = reaper.ImGui_GetKeyMods(ctx) & reaper.ImGui_KeyModFlags_Shift() ~= 0
local wheel, hwheel = reaper.ImGui_GetMouseWheel(ctx)
if shift and wheel == 0 then wheel = hwheel end
if wheel ~= 0 then
speed = shift and double_mousewheel_sensitivity_fine or double_mousewheel_sensitivity
value = value + (wheel * speed)
return true, math.max(min, math.min(value, max))
end
rv=true
return false, value
Assistant
The given code example already includes functionality to adjust the speed of changes applied by the mouse wheel. The variables int_mousewheel_sensitivity, int_mousewheel_sensitivity_fine, double_mousewheel_sensitivity, and double_mousewheel_sensitivity_fine can be adjusted to change the sensitivity of mouse wheel input for integer and double sliders, respectively.
However, if you need to adjust the "speed" of the slider change when the slider is dragged with the mouse (as in making the dragging itself more or less sensitive), you can introduce a sensitivity factor similarly to how it's done for the mouse wheel.
Here's how you can modify the slider functions to add a sensitivity factor for mouse dragging:
Define Sensitivity Factors: Define a sensitivity factor for both integer and double sliders.
Apply Sensitivity in Slider Functions: Modify the slider functions to adjust the value based on the sensitivity factor when the slider is interacted with using the mouse.
Here’s how you can integrate those changes into your code:
Add Sensitivity Factor Definitions:
lua
local drag_sensitivity = 0.5 -- Adjust this factor to change drag sensitivity
local drag_sensitivity_double = 0.5 -- Adjust this factor for double sliders
Modify Vertical and Horizontal Slider Functions:
lua
Not yet included in a published release.
In this example, DragFloatCustom creates a custom draggable float item with a specified height set by the size of the invisible button (InvisibleButton). This approach gives you more flexibility in customizing the appearance and behavior of the drag item, including its height. The example shows how to set an item height of 50 pixels, but you can adjust this to suit your needs.
This custom widget captures mouse dragging and updates the float value myValue accordingly. It also handles the styling to visually indicate whether the item is active.
#include "imgui.h"
// Custom VSlider with adjustable sensitivity
bool CustomVSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f, float sensitivity = 1.0f)
{
ImGui::PushID(label);
ImGui::BeginGroup();
// Capture the label height to position it below the slider
ImVec2 label_size = ImGui::CalcTextSize(label);
// Create the vertical slider
bool value_changed = ImGui::VSliderFloat("##vslider", size, v, v_min, v_max, "", power);
// Adjust the value by applying sensitivity
if (ImGui::IsItemActive())
{
if (ImGui::IsMouseDragging(0))
{
float mouse_delta = ImGui::GetIO().MouseDelta.y * sensitivity;
float value_range = v_max - v_min;
float new_value = *v - (mouse_delta / size.y) * value_range;
new_value = ImClamp(new_value, v_min, v_max);
if (new_value != *v)
{
*v = new_value;
value_changed = true;
}
}
}
// Draw the label below the slider
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + size.y + ImGui::GetStyle().ItemInnerSpacing.y);
ImGui::Text(label);
ImGui::EndGroup();
ImGui::PopID();
return value_changed;
}
void MyCustomUI()
{
static float myValue = 0.0f;
ImGui::Text("Custom VSlider with Sensitivity");
CustomVSliderFloat("My VSlider", ImVec2(20, 100), &myValue, 0.0f, 1.0f, "%.3f", 1.0f, 0.5f); // Adjust `sensitivity` (last parameter) as needed
}
In the CustomVSliderFloat function:
The sensitivity parameter is used to adjust how much the value changes with mouse movement.
The slider's position is updated with the adjusted mouse delta (mouse_delta * sensitivity).
Using this custom function, you can easily create vertical sliders with different sensitivities, tailored to your requirements, without modifying the original Dear ImGui source code.
In this modified code:
A sensitivity variable is introduced, which controls how sensitive the slider value is to mouse movement.
The slider value (v_initial) is adjusted by applying the sensitivity factor to the difference between the new slider value (tv) and the initial value (v_initial).
By changing the sensitivity variable, you can control how quickly the value changes in response to mouse movement:
A value less than 1 (e.g., 0.5) makes the slider less sensitive (mouse moves slower).
A value greater than 1 makes the slider more sensitive (mouse moves faster).
Adjust the sensitivity value to achieve the desired user experience.
ctx = reaper.ImGui_CreateContext("ctx")
vol_image = reaper.ImGui_CreateImage("E:\\Reaper Themes\\Rykord Theming\\ReaTools Dark V2.29 Time SelectionReaperTheme\\ReaTools_Dark_unpack\\mcp_volthumb.png")
v_initial = 0 -- Initial value for the slider
function main()
local _, is_open = reaper.ImGui_Begin(ctx, "ClipGain", true)
slider()
if is_open then
reaper.defer(main)
end
reaper.ImGui_End(ctx)
end
function slider()
local slider_width = 16
local slider_height = 200
local slider_min = -150
local slider_max = 12
local sensitivity = 0.05 -- Adjust this value to change sensitivity (smaller values = less sensitive)
-- Create the VSlider background
reaper.ImGui_SetNextItemWidth(ctx, slider_width)
reaper.ImGui_InvisibleButton(ctx, "##vslider", slider_width, slider_height)
if reaper.ImGui_IsItemActive(ctx) then
-- Calculate the mouse delta
local mouse_delta = reaper.ImGui_GetMouseDragDelta(ctx, 0, false).y
reaper.ImGui_ResetMouseDragDelta(ctx, 0)
-- Apply sensitivity and update the value
v_initial = v_initial - mouse_delta * sensitivity
v_initial = math.max(math.min(v_initial, slider_max), slider_min) -- Clamp value within slider range
end
local image_width = 40
local image_height = 50
local y_position = -175 / 162 * v_initial + 27.96
-- Set cursor position so that the image is centered on the slider handle
local handlepos = (162 * v_initial - 150)
reaper.ImGui_SetCursorPos(ctx, -3, y_position)
reaper.ImGui_Image(ctx, vol_image, image_width, image_height)
-- Show the current value for reference (optional)
reaper.ImGui_Text(ctx, string.format("Value: %.2f", v_initial))
end
main()
Explanation:
Invisible Button: The ImGui_InvisibleButton is used to create an invisible draggable region.
Mouse Dragging: The function ImGui_GetMouseDragDelta retrieves the delta movement of the mouse since the dragging started.
Sensitivity: The mouse delta is scaled by a sensitivity factor before updating the slider value.
Clamp Value: Ensure the value stays within the specified range of the slider (slider_min to slider_max).
By adjusting the sensitivity parameter, you can make the slider respond more delicately to mouse movements, effectively creating a less sensitive or "slower" slider.
Feel free to adjust the sensitivity to match your exact needs. This approach should give you more fine-grained control over how the slider responds to the user's input.
Explanation:
Sensitivity Factor: Define two sensitivity factors, one for normal movement (normal_sensitivity), and one for slower movement (slow_sensitivity).
Shift Key Check: Use reaper.ImGui_IsKeyDown to check if the Shift key is held down. Adjust the sensitivity accordingly.
Scaling Mouse Delta: Apply the chosen sensitivity factor to the mouse delta to simulate slower or normal mouse movements.
Slider Interaction: Use the modified mouse delta to update the slider value.
By holding down the Shift key (or any other key, you can change the check to another condition easily), the slow_sensitivity will be applied, making the slider respond more slowly to mouse movements. If the Shift key is not held down, the slider responds normally.
This method does not change the actual mouse movement speed but adjusts how the script interprets mouse movements, effectively achieving the desired effect.
function myVSliderInt(id, width, height, value, min, max, default_value)
local rv
rv,value = reaper.ImGui_VSliderInt(ctx, id, width, height, value, min, max)
value = resetOnDoubleClick(id, value, default_value)
local changed
if reaper.ImGui_IsItemHovered(ctx) then
changed,value = applyMousewheel(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, value
end
function myVSliderDouble(id, width, height, value, min, max, default_value)
local rv
rv,value = reaper.ImGui_VSliderDouble(ctx, id, width, height, value, min, max)
value = resetOnDoubleClick(id, value, default_value)
local changed
if reaper.ImGui_IsItemHovered(ctx) then
changed,value = applyMousewheelDouble(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, value
end
function mySliderInt(id, value, min, max, default_value)
local rv
rv,value = reaper.ImGui_SliderInt(ctx, id, value, min, max)
value = resetOnDoubleClick(id, value, default_value)
local changed
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheel(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, value
end
function mySliderDouble(id, value, min, max, default_value)
local rv
rv,value = reaper.ImGui_SliderDouble(ctx, id, value, min, max)
value = resetOnDoubleClick(id, value, default_value)
local changed
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheelDouble(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, value
end
loop()
can any part of this code change the speed by which a slider is changed?
-- Apply drag sensitivity to the integer vertical slider
function myVSliderInt(id, width, height, value, min, max, default_value)
rv, temp_value = reaper.ImGui_VSliderInt(ctx, id, width, height, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).y
value = value + delta * drag_sensitivity
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv or reaper.ImGui_IsItemDeactivatedAfterEdit(ctx)
return changed, math.max(min, math.min(value, max))
end
-- Apply drag sensitivity to the double vertical slider
function myVSliderDouble(id, width, height, value, min, max, default_value)
rv, temp_value = reaper.ImGui_VSliderDouble(ctx, id, width, height, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).y
value = value + delta * drag_sensitivity_double
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv or reaper.ImGui_IsItemDeactivatedAfterEdit(ctx)
return changed, math.max(min, math.min(value, max))
end
The same can be applied to horizontal sliders as needed.
Here’s the full revised version including the sensitivity factors:
lua
local ctx = reaper.ImGui_CreateContext('Slider Test', 250, 250)
int_mousewheel_sensitivity = 1
int_mousewheel_sensitivity_fine = 1
double_mousewheel_sensitivity = .1
double_mousewheel_sensitivity_fine = .01
local drag_sensitivity = 0.5 -- Adjust this factor to change drag sensitivity
local drag_sensitivity_double = 0.5 -- Adjust this factor for double sliders
local default_value = 0
local value = default_value
local default_value2 = 1
local value2 = default_value2
local default_value3 = 2
local value3 = default_value3
local default_value4 = 3
local value4 = default_value4
local reset = {}
----------------------------- TEST FUNCTION ----------------------
function dosomething()
reaper.ShowConsoleMsg(test_input .. '\n')
end
-------------------------------------------------------------------
------------------------- GUI ------------------------------------
-------------------------------------------------------------------
function loop()
if reaper.ImGui_IsCloseRequested(ctx) then
reaper.ImGui_DestroyContext(ctx)
return
end
reaper.ImGui_SetNextWindowPos(ctx, 0, 0) -- Don't really need but it makes it at 0 0 "Lock it"
reaper.ImGui_SetNextWindowSize(ctx, reaper.ImGui_GetDisplaySize(ctx)) -- Don't really need but it makes it the size of the screen
reaper.ImGui_Begin(ctx, 'wnd', nil, reaper.ImGui_WindowFlags_NoDecoration())
local rv
---Vertical Slider
rv, value = myVSliderInt('##v1', 40, 160, value, -10, 10, default_value)
if rv then
test_input = value
dosomething()
end
reaper.ImGui_SameLine(ctx)
---Vertical Slider Double
rv, value2 = myVSliderDouble('##v2', 40, 160, value2, -10, 10, default_value2)
if rv then
test_input = value2
dosomething()
end
---Horizontal Slider
rv, value3 = mySliderInt('##v3', value3, -10, 10, default_value3)
if rv then
test_input = value3
dosomething()
end
---Horizontal Slider Double
rv, value4 = mySliderDouble('##v4', value4, -10, 10, default_value4)
if rv then
test_input = value4
dosomething()
end
reaper.ImGui_End(ctx)
reaper.defer(loop)
end
--------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
---------------------------- DOUBLE CLICK AND MOUSEWHEEL FUNCTIONS --------------------------------
function resetOnDoubleClick(id, value, default)
if reaper.ImGui_IsItemDeactivated(ctx) and reset[id] then
reset[id] = nil
return default
elseif reaper.ImGui_IsItemHovered(ctx) and reaper.ImGui_IsMouseDoubleClicked(ctx, 0) then
reset[id] = true
end
return value
end
function applyMousewheel(value, min, max)
local shift = reaper.ImGui_GetKeyMods(ctx) & reaper.ImGui_KeyModFlags_Shift() ~= 0
local wheel, hwheel = reaper.ImGui_GetMouseWheel(ctx)
if shift and wheel == 0 then wheel = hwheel end
if wheel ~= 0 then
speed = shift and int_mousewheel_sensitivity_fine or int_mousewheel_sensitivity
value = value + math.ceil(wheel * speed)
return true, math.max(min, math.min(value, max))
end
return false, value
end
function applyMousewheelDouble(value, min, max)
local shift = reaper.ImGui_GetKeyMods(ctx) & reaper.ImGui_KeyModFlags_Shift() ~= 0
local wheel, hwheel = reaper.ImGui_GetMouseWheel(ctx)
if shift and wheel == 0 then wheel = hwheel end
if wheel ~= 0 then
speed = shift and double_mousewheel_sensitivity_fine or double_mousewheel_sensitivity
value = value + (wheel * speed)
return true, math.max(min, math.min(value, max))
end
return false, value
end
------------------------------------ VERTICAL SLIDER INT ---------------------------------
function myVSliderInt(id, width, height, value, min, max, default_value)
local rv
rv, temp_value = reaper.ImGui_VSliderInt(ctx, id, width, height, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).y
value = value + delta * drag_sensitivity
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheel(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, math.max(min, math.min(value, max))
end
------------------------------------ VERTICAL SLIDER DOUBLE ---------------------------------
function myVSliderDouble(id, width, height, value, min, max, default_value)
local rv
rv, temp_value = reaper.ImGui_VSliderDouble(ctx, id, width, height, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).y
value = value + delta * drag_sensitivity_double
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheelDouble(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, math.max(min, math.min(value, max))
end
----------------------------------------- HORIZONTAL SLIDER INT ---------------------------
function mySliderInt(id, value, min, max, default_value)
local rv
rv, temp_value = reaper.ImGui_SliderInt(ctx, id, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).x
value = value + delta * drag_sensitivity
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheel(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, math.max(min, math.min(value, max))
end
----------------------------------------- HORIZONTAL SLIDER DOUBLE ---------------------------
function mySliderDouble(id, value, min, max, default_value)
local rv
rv, temp_value = reaper.ImGui_SliderDouble(ctx, id, value, min, max)
-- Apply sensitivity for mouse dragging
if reaper.ImGui_IsItemActive(ctx) and reaper.ImGui_IsMouseDragging(ctx) then
local delta = reaper.ImGui_GetMouseDragDelta(ctx).x
value = value + delta * drag_sensitivity_double
end
value = resetOnDoubleClick(id, value, default_value)
local changed = rv
if reaper.ImGui_IsItemHovered(ctx) then
changed, value = applyMousewheelDouble(value, min, max)
end
if not changed then changed = reaper.ImGui_IsItemDeactivatedAfterEdit(ctx) end
return changed, math.max(min, math.min(value, max))
end
----------------------------------------------------------------------------
-----------------------------------------------------------------------------
--------------------------------------------------------------------------
loop()
Explanation:
Sensitivity Factors: I added drag_sensitivity and drag_sensitivity_double to control how sensitive the slider is to mouse dragging.
Detect Mouse Dragging: Inside the slider functions, I check if the item is active (being interacted with) and if the mouse is being dragged.
Apply Sensitivity: I scale the mouse drag delta by the sensitivity factor before updating the slider value.
Clamping Values: Ensure that the updated value is within the specified min and max range.
Adjust the drag_sensitivity and drag_sensitivity_double factors to control how fine or coarse the slider movements should be in response to mouse dragging.