The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

TCOD::Event::Dispatch - A role to dispatch TCOD events

SYNOPSIS

    use TCOD;

    package My::Dispatch {
        use Role::Tiny::With;
        with 'TCOD::Event::Dispatch';

        sub ev_quit { exit }

        sub ev_keydown {
            my ( $event, @extra ) = @_;

            for ( $event->sym ) {
                return { move => [  0, -1 ] } if $_ == TCOD::Event::K_UP;
                return { move => [  0,  1 ] } if $_ == TCOD::Event::K_DOWN;
                return { move => [ -1,  0 ] } if $_ == TCOD::Event::K_LEFT;
                return { move => [  1,  0 ] } if $_ == TCOD::Event::K_RIGHT;
            }

            return;
        }
    }

    my $iter = TCOD::Event::wait;
    while ( my $event = $iter->() ) {
        my $action = My::Dispatch->dispatch( $event, @extra );

        if ( my $delta = $action->{move} ) {
            print 'Movement delta is ' . join ', ', @{ $delta };
        }
    }

DESCRIPTION

This is a Role::Tiny role that an be used to write your own dispatch code that can translate between one of the event types defined in TCOD::Event.

METHODS

dispatch

    ... = $dispatcher->dispatch( $event, @other );

Dispatch to one of this packages event handlers depending on the TCOD::Event passed as an argument.

The event will be passed as the first argument to one of the event handler methods mentioned below. Any other parameters passed to this method will be passed down to the dispatched method as well.

The method to dispatch to will be selected by taking the result of calling type on the event, converting it to lowercase, and adding the ev_ prefix. For example, if the event is of type KEYUP, this method will dispatch to ev_keyup. For more details on what these types are and under what circumstances these events may trigger, please see the TCOD::Event documentation.

The event handler methods do nothing by default. The user is expected to override them in the package that consumes this role. See the synopsis for an example of how this can be done.

The pre-defined event handler methods are the following:

  • ev_keydown

  • ev_keyup

  • ev_mousebuttondown

  • ev_mousebuttonup

  • ev_mousemotion

  • ev_mousewheel

  • ev_quit

  • ev_textinput

  • ev_undefined

  • ev_windowclose

  • ev_windowenter

  • ev_windowexposed

  • ev_windowfocusgained

  • ev_windowfocuslost

  • ev_windowhidden

  • ev_windowhittest

  • ev_windowleave

  • ev_windowmaximized

  • ev_windowminimized

  • ev_windowmoved

  • ev_windowresized

  • ev_windowrestored

  • ev_windowshown

  • ev_windowsizechanged

  • ev_windowtakefocus

SEE ALSO

TCOD
TCOD::Event

COPYRIGHT AND LICENSE

Copyright 2021 José Joaquín Atria

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.