TStand90 / tcod_tutorial_v2

Creative Commons Zero v1.0 Universal
98 stars 45 forks source link

2020, part 7 - ev_mousemotion function issue #58

Open DesLandysh opened 2 months ago

DesLandysh commented 2 months ago

On this day, event.tile.x and event.tile.y is deprecated (they works fine, but...)
If tcod.console.Console instead old tcod.Console, and tcod.event.KeySym.ANYKEY instead of old tcod.event.K_ANYKEY is understandable intuitively (that's tiny Part 1-6 issues I've met), this issue requires some brains and logic actions, and I take a brave to open an issue.

Issue code, _inputhandlers.py:

...
   def handle_events(self, context: tcod.context.Context) -> None:
        for event in tcod.event.wait():
            context.convert_event(event)
            self.dispatch(event)

    def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None:
        if self.engine.game_map.in_bounds(event.tile.x, event.tile.y):
            self.engine.mouse_location = event.tile.x, event.tile.y

    def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
...

IDE (PyCharm, in my case) suggests the event.position.x and event.position.y respectively, however, it gets the pixel position instead of the tile position needed to proper functionality of this function.

Solution (hardcoded version):

        if self.engine.game_map.in_bounds(event.position.x // 10, event.position.y // 10):
            self.engine.mouse_location = event.position.x // 10, event.position.y // 10

Number 10 I took from dejavu10x10_gs_tc.png font. I decided that every letter is the tile, so 10x10 is the tile size in pixels.
Further, a learner should create a const.py file (methinks) to keep all constants, so it can be named as tile_size or TILE_SIZE, or something else to replace ... // 10 by ...// tile_size.

HexDecimal commented 2 months ago

You missed a small detail: context.convert_event(event) actually returns a new event with the .position attribute updated to tile coordinates:

tile_event = context.convert_event(event)
event.position.x  # pixel X
tile_event.position.x  # tile X
DesLandysh commented 2 months ago

You missed a small detail: context.convert_event(event) actually returns a new event with the .position attribute updated to tile coordinates:

tile_event = context.convert_event(event)
event.position.x  # pixel X
tile_event.position.x  # tile X

As I figure out this must be place in EventHandler class, isn't it? There weren't any mentions in tutorial till part 7 included (i'm on it). Methinks, we do not assign conversion in tutorial because we can use event.tile, so since 15.0 context.convert_event(event) returns event with tile coords instead of pixel coords by default. And we need divide by tile size or use event = context.convert_event(event) (however, I foresee some issues in future with this assignation)

HexDecimal commented 2 months ago

Because the tutorial still relies on event.tile.x. context.convert_event did not return a new event when the tutorial was written, it only updated the event in-place which is now deprecated by the latest versions of Python-tcod.

You only need to divide by the tileset size when you render consoles without a tcod context.