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) = @_;

        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);

    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 );

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

The event will be passed as the only argument to one of the event handler methods mentioned below. The method 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_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.