Permalink
Browse files

added submenu example

  • Loading branch information...
1 parent ef76ffc commit 6dcdfeba5d49d7f1176a74df0523d72824d5b15c @garu garu committed Sep 27, 2011
Showing with 73 additions and 1 deletion.
  1. +1 −0 Changes
  2. +71 −0 examples/submenu.pl
  3. +1 −1 lib/SDLx/Widget/Menu.pm
View
@@ -2,6 +2,7 @@ Revision history for Perl module SDLx::Widget
0.072 to-be-released
- updated menu example [jtpalmer, garu]
+ - added submenu example [garu]
- added mouse functionality to SDLx::Widget::Menu [jtpalmer, garu]
- menu has a default font (uses SDLx::Text) [garu]
- menu text now aligns to 'left', 'center' or 'right' [garu]
View
@@ -0,0 +1,71 @@
+use strict;
+use warnings;
+use SDLx::App;
+
+use lib 'lib';
+use SDLx::Widget::Menu;
+
+my $app = SDLx::App->new(
+ w => 800,
+ h => 600,
+ eoq => 1,
+);
+
+
+my $show_menu = 1;
+my $menu_event_id;
+my $menu = SDLx::Widget::Menu->new( topleft => [100, 230] )->items(
+ 'Some Option' => sub { },
+ 'Open Submenu' => \&open_submenu,
+ 'Some Other Option' => sub { },
+ 'Quit' => sub { $show_menu = 0; },
+);
+
+my $show_submenu = 0;
+my $submenu = SDLx::Widget::Menu->new( topleft => [100, 530] )->items(
+ 'Item 1' => sub { },
+ 'Item 2' => sub { },
+ 'Item 3' => sub { },
+ 'Back' => sub { $show_submenu = 0 },
+);
+
+open_menu();
+
+$app->add_show_handler(
+ sub {
+ $app->draw_rect( undef, undef );
+ $menu->render($app);
+ $submenu->render($app) if $show_submenu;
+ $app->update;
+ }
+);
+
+$app->run;
+exit;
+
+sub open_submenu {
+ $submenu->selected( undef );
+ $show_submenu = 1;
+
+ $app->remove_event_handler( $menu_event_id );
+
+ my $submenu_id;
+ $submenu_id = $app->add_event_handler( sub {
+ $submenu->event_hook( $_[0] );
+
+ if ($show_submenu == 0) {
+ $app->remove_event_handler( $submenu_id );
+ open_menu();
+ }
+ });
+}
+
+sub open_menu {
+ $menu->selected( undef );
+
+ $menu_event_id = $app->add_event_handler( sub {
+ $menu->event_hook( $_[0] );
+ $app->stop if $show_menu == 0;
+ });
+}
+
View
@@ -33,7 +33,7 @@ has 'active_color' => ( is => 'ro', isa => 'ArrayRef',
has 'font_size' => ( is => 'ro', isa => 'Int', default => 24 );
has 'current' => ( is => 'rw', isa => 'Int', default => 0 );
-has 'selected' => ( is => 'rw', isa => 'Int' );
+has 'selected' => ( is => 'rw', isa => 'Maybe[Int]' );
has 'mouse' => ( is => 'ro', isa => 'Bool', default => 1);

0 comments on commit 6dcdfeb

Please sign in to comment.