libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
413 stars 36 forks source link

Mouse position (dx/dy,dcx,dcy) reset to 0 with a wheel event #62

Closed FractalWire closed 5 years ago

FractalWire commented 5 years ago

Hello again,

I have a new problem, this time with the wheel event. I have a handle_events() function that could look like this :

def handle_events(events):
    key = tcod.Key()
    mouse = tcod.Mouse()
    evnt_masks = tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE
    while tcod.sys_check_for_event(evnt_masks, key, mouse):
        if key.vk == tcod.KEY_ESCAPE:
            raise SystemExit()
        print(mouse.cx,mouse.cy,mouse.x,mouse.y)

Everything is fine when I move my mouse around, but when I wheel up/down, the value printed are 0 (except the last one=1 for some reason) So it seems to reset the mouse position whenever I wheel up/down.

HexDecimal commented 5 years ago

This isn't a bug, SDL2 doesn't return the mouse position on a mouse wheel event (but it does on a wheel click.)

The tcod.sys_check_for_event returns the events type, this should be used instead of giving it a partial event mask, like in the following example:

def handle_events(events):
    key = tcod.Key()
    mouse = tcod.Mouse()
    while True:
        ev = tcod.sys_check_for_event(tcod.EVENT_ANY, key, mouse)
        if not ev:
            break
        if ev & tcod.EVENT_KEY_PRESS:
            if key.vk == tcod.KEY_ESCAPE:
                raise SystemExit()
        if ev & tcod.EVENT_MOUSE_MOVE:
            print(mouse.cx,mouse.cy,mouse.x,mouse.y)
HexDecimal commented 5 years ago

Actually, I might have been too quick to judge this. This could a regression in behavior, since mouse wheel events are put into TCOD_EVENT_MOUSE_PRESS for some reason.

HexDecimal commented 5 years ago

The regression is fixed in 8.4.1 and in libtcod. Mouse wheel events in libtcodpy are now back to their usual undefined behavior of preserving coordinates from previous mouse events.

FractalWire commented 5 years ago

Perfect, thanks !