Last active
May 17, 2022 07:28
-
-
Save Willy-JL/09e26f5e28e8d01a11c16a174cf096df to your computer and use it in GitHub Desktop.
Smooth scrolling and scroll multiplier for pyimgui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from imgui.integrations.glfw import GlfwRenderer | |
import OpenGL.GL as gl | |
import imgui | |
import glfw | |
# Example usage | |
if __name__ == "__main__": | |
imgui.create_context() | |
window = impl_glfw_init() # Get this here https://github.com/pyimgui/pyimgui/blob/24219a8d4338b6e197fa22af97f5f06d3b1fe9f7/doc/examples/integrations_glfw3.py | |
impl = GlfwRenderer(window) | |
smooth_scrolling = False | |
smooth_scroll_speed = 8.0 | |
scroll_amount = 1.0 | |
scroll_energy = 0.0 | |
while not glfw.window_should_close(window): | |
glfw.poll_events() | |
impl.process_inputs() | |
# The actual scroll modifiers | |
# These must be applied before new_frame()! | |
io = imgui.get_io() | |
io.mouse_wheel *= scroll_amount # Scroll multiplier / amount | |
if smooth_scrolling: | |
# Smooth scrolling | |
scroll_energy += io.mouse_wheel | |
if abs(scroll_energy) > 0.1: # Scrolling threshold | |
scroll_now = scroll_energy * io.delta_time * smooth_scroll_speed | |
scroll_energy -= scroll_now | |
else: | |
scroll_now = 0.0 | |
scroll_energy = 0.0 | |
io.mouse_wheel = scroll_now | |
imgui.new_frame() | |
with imgui.begin("Example scrolling"): | |
# Scroll modifiers settings | |
imgui.begin_group() | |
_, smooth_scrolling = imgui.checkbox("Enable smooth scrolling", smooth_scrolling) | |
imgui.set_next_item_width(100) | |
_, smooth_scroll_speed = imgui.input_float("Smooth scroll speed", smooth_scroll_speed, step=0.25) | |
imgui.set_next_item_width(100) | |
_, scroll_amount = imgui.input_float("Scroll amount", scroll_amount, step=0.25) | |
imgui.end_group() | |
imgui.same_line(spacing=20) | |
# Scroll example | |
with imgui.begin_child("Scroll region"): | |
for i in range(200): | |
imgui.text(f"Some text {i}") | |
imgui.button(f"A button {i}") | |
imgui.text("") | |
gl.glClearColor(0., 0., 0., 1) | |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) | |
imgui.render() | |
impl.render(imgui.get_draw_data()) | |
glfw.swap_buffers(window) | |
impl.shutdown() | |
glfw.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment