[Solved] Measure MOUSE_WHEEL_[UP/DOWN]

I didn’t find an answer to this in the docs or on the forum.

How to measure the value of how much scroll was applied UP or DOWN ?
action.value is always positiv (0,1) and other properties do not change (except states).

Yes, this needs to be documented. I’ll make a note about it.

As the actions can be distinguished by your id (zoom_out/zoom_in) and they would happen over many frames, you can sum them up to see how much scroll is applied by treating one of them as negative. There is a granularity problem if you have a more smooth-scrolling mouse which probably have fractions in amount for each frame, but do you require that fine granularity?

I’ll try and report back.

@Ragnar_Svensson No, I just want to zoom my world by the mouse wheel - so I’ll be fine with this solution (if I can make it work :slight_smile:)

Ok great! The reason they are both positive is that they are treated the same as e.g. the d-pad on a game controller, where each of the four directions are all positive in value. Same goes for the joysticks. You could argue the value should be a 2d vector in that case, but we went for consistency in the data types across input signals. The conversion from the signals to a 2d vector is also quite straightforward. :slight_smile:

2 Likes

@Ragnar_Svensson

As the actions can be distinguished by your id (zoom_out/zoom_in) and they would happen over many frames, you can sum them up to see how much scroll is applied by treating one of them as negative.

Ragnar, thanks for your advice! It worked, but I ended up solving it a little different. For anyone interested in this, here’s how I did my mouse-zooming:

Solution

  1. I created the key bindings

  2. Next, I created my respond function inside my InputController.script

  • The multiplier tells the render_script by how much to increase or decrease the zoom_factor.
  • The (optional) resulution is just a granularity controller, to adjust the speed of the action.
  • The limits can be omitted too. I needed them to forbid the player to zoom in/out to far.
  1. Finally I made my render_script handle the zooming action.

Goodies

The attached render_script can be used to position camera’s by a pivot. One could have a camera centered on screen pivot_x, pivot_y = .5, .5 or have it at the default origin pivot_x, pivot_y = 0, 0. Due to pivots having a range of 0-1, it’s even possible to push the camera out of the screen pivot_x, pivot_y = -4, 0

This script also handles different window sizes easily. No matter how the user resizes the game window (even fullscreen) the renderer displays as much content as it can, always centered based on the pivot. The aspect ration is always preserved too!

The last useful thing is that one can retrieve world coordinates from a screen point, simply by asking the renderer msg.post("@render:", "get_world_position", {x = action.screen_x, y = action.screen_y}). This is useful when touches or clicks should be translated into world context.

custom_render.script (3.5 KB)

6 Likes

Cool! Thanks for sharing!

2 Likes

There’s an issue on improving scrolling support. DEF-2271

1 Like

Referencing this thread here: Scroll granularity on macOS (DEF-2271)