From b3b7109bab95c3a6b1227f66705585a92a690594 Mon Sep 17 00:00:00 2001 From: Ryan Shepherd Date: Tue, 25 Sep 2018 12:35:34 -0400 Subject: [PATCH] Add key modifier support, mouse control support --- PerlRPG/Controls.pm | 34 ++++++++++++++++++++-------------- PerlRPG/Game.pm | 18 ++++++++++++++++-- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/PerlRPG/Controls.pm b/PerlRPG/Controls.pm index 79817c6..815bb17 100644 --- a/PerlRPG/Controls.pm +++ b/PerlRPG/Controls.pm @@ -14,13 +14,20 @@ use PerlRPG::Script; use vars qw/$app @ISA @EXPORT @EXPORT_OK %keymap/; @ISA = qw/Exporter/; -@EXPORT = qw/KeyDown KeyUp MouseClick/; +@EXPORT = qw/KeyPressed MouseClick/; @EXPORT_OK = @EXPORT; %keymap = ( 'space-down' => \&PerlRPG::Script::SkipText, ); +my %keymods=( + SDL::Event::KMOD_CTRL => "ctrl", + SDL::Event::KMOD_SHIFT => "shift", + SDL::Event::KMOD_ALT => "alt", + SDL::Event::KMOD_META => "meta", +); + sub KeyPress { foreach my $key (@_) { @@ -30,23 +37,22 @@ sub KeyPress { } } -sub KeyDown { - my($key)=@_; - my $key2="$key-down"; - LogData(DEVALL, "KeyDown($key)"); - KeyPress($key, $key2); +sub get_keymod { + my $mods = shift(@_); + return map { $keymods{$_} } grep { $mods & $_ } (keys %keymods); } -sub KeyUp { - my($key)=@_; - my $key2="$key-up"; - LogData(DEVALL, "KeyUp($key)"); - KeyPress($key, $key2); +sub KeyPressed { + my($key, $updown, $mods)=@_; + my @mods = get_keymod($mods); + + LogData(DEBUG, "Key %s(%s)", $updown, join(',',@mods, $key)); + KeyPress($key, "$key-$updown"); } sub MouseClick { - my($x, $y, $btn)=@_; - - LogData(DEBUG, "MouseClick($x, $y, $btn"); + my($x, $y, $btn, $updown, $mods)=@_; + my @mods = get_keymod($mods); + LogData(DEBUG, "MouseClick %s %i,%i (%s)", $updown, $x, $y, join(',',@mods, $btn)); } diff --git a/PerlRPG/Game.pm b/PerlRPG/Game.pm index 2832018..2757590 100644 --- a/PerlRPG/Game.pm +++ b/PerlRPG/Game.pm @@ -67,10 +67,24 @@ sub EventDispatcher { while(SDL::Events::poll_event($event)) { if($event->type == SDL_KEYDOWN) { my $key = SDL::Events::get_key_name($event->key_sym); - KeyDown($key); + KeyPressed($key, 'down', SDL::Events::get_mod_state()); } elsif($event->type == SDL_KEYUP) { my $key = SDL::Events::get_key_name($event->key_sym); - KeyUp($key); + KeyPressed($key, 'up', SDL::Events::get_mod_state()); + } elsif($event->type == SDL_MOUSEBUTTONDOWN) { + MouseClick( $event->button_x, + $event->button_y, + $event->button_button, + 'down', + SDL::Events::get_mod_state() + ); + } elsif($event->type == SDL_MOUSEBUTTONUP) { + MouseClick( $event->button_x, + $event->button_y, + $event->button_button, + 'up', + SDL::Events::get_mod_state() + ); } elsif($event->type == SDL_QUIT) { $opt{'Running'} = 0; }