| 1 | # | ||
| 2 | # This file is part of Games-RailRoad | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2008 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | package Games::RailRoad::Window::Trains; | ||
| 14 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | $Games::RailRoad::Window::Trains::VERSION = '1.101330'; | |
| 16 | } | ||
| 17 | # ABSTRACT: an opaque vector class. | ||
| 18 | |||
| 19 | 0 | ▶ | use Tk; # should come before POE |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use POE; | ||
| 21 | |||
| 22 | |||
| 23 | #-- | ||
| 24 | # constructor | ||
| 25 | |||
| 26 | # | ||
| 27 | # my $id = Games::RailRoad::Window::Trains->spawn( %opts ); | ||
| 28 | # | ||
| 29 | # pod for an explanation of the supported options. | ||
| 30 | # | ||
| 31 | sub spawn { | ||
| 32 | my ($class, %opts) = @_; | ||
| 33 | |||
| 34 | my $session = POE::Session->create( | ||
| 35 | inline_states => { | ||
| 36 | _start => \&_on_start, | ||
| 37 | _stop => sub { print "ouch!\n" }, | ||
| 38 | # public events | ||
| 39 | visibility_toggle => \&_do_visibility_toggle, | ||
| 40 | # private events | ||
| 41 | # gui events | ||
| 42 | }, | ||
| 43 | args => \%opts, | ||
| 44 | ); | ||
| 45 | return $session->ID; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | #-- | ||
| 50 | # public events | ||
| 51 | |||
| 52 | # | ||
| 53 | # visibility_toggle(); | ||
| 54 | # | ||
| 55 | # request window to be hidden / shown depending on its previous state. | ||
| 56 | # | ||
| 57 | sub _do_visibility_toggle { | ||
| 58 | my ($h) = $_[HEAP]; | ||
| 59 | |||
| 60 | my $method = $h->{mw}->state eq 'normal' ? 'withdraw' : 'deiconify'; | ||
| 61 | $h->{mw}->$method; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | #-- | ||
| 66 | # private events | ||
| 67 | |||
| 68 | # | ||
| 69 | # _on_start( \%opts ); | ||
| 70 | # | ||
| 71 | # session initialization. %opts is received from spawn(); | ||
| 72 | # | ||
| 73 | sub _on_start { | ||
| 74 | my ($k, $h, $from, $s, $opts) = @_[KERNEL, HEAP, SENDER, SESSION, ARG0]; | ||
| 75 | |||
| 76 | #-- create gui | ||
| 77 | |||
| 78 | my $top = $opts->{parent}->Toplevel(-title => 'Trains'); | ||
| 79 | $h->{mw} = $top; | ||
| 80 | #$h->{list} = $top->Listbox->pack; | ||
| 81 | # $h->{but_remove} = $top->Button( | ||
| 82 | # -text => 'Remove', | ||
| 83 | # -state => 'disabled', | ||
| 84 | # -width => 6, | ||
| 85 | # -command => $s->postback('_b_breakpoint_remove') | ||
| 86 | # )->pack(-side=>'left',-fill=>'x',-expand=>1); | ||
| 87 | # $top->Button( | ||
| 88 | # -text => 'Close', | ||
| 89 | # -width => 6, | ||
| 90 | # -command => $s->postback('visibility_toggle') | ||
| 91 | # )->pack(-side=>'left',-fill=>'x',-expand=>1); | ||
| 92 | |||
| 93 | # trap some events | ||
| 94 | $top->protocol( WM_DELETE_WINDOW => $s->postback('visibility_toggle') ); | ||
| 95 | $top->bind( '<F5>', $s->postback('visibility_toggle') ); | ||
| 96 | |||
| 97 | |||
| 98 | $top->update; # force redraw | ||
| 99 | $top->resizable(0,0); | ||
| 100 | my ($maxw,$maxh) = $top->geometry =~ /^(\d+)x(\d+)/; | ||
| 101 | $top->maxsize($maxw,$maxh); # bug in resizable: minsize in effet but not maxsize | ||
| 102 | $top->iconimage($Games::RailRoad::img{train}); | ||
| 103 | |||
| 104 | |||
| 105 | # -- other inits | ||
| 106 | $h->{parent_session} = $from->ID; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | #-- | ||
| 111 | # gui events | ||
| 112 | |||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | |||
| 117 | =pod | ||
| 118 | |||
| 119 | =head1 NAME | ||
| 120 | |||
| 121 | Games::RailRoad::Window::Trains - an opaque vector class. | ||
| 122 | |||
| 123 | =head1 VERSION | ||
| 124 | |||
| 125 | version 1.101330 | ||
| 126 | |||
| 127 | =head1 DESCRIPTION | ||
| 128 | |||
| 129 | GRW::Trains implements a POE session, creating a Tk window listing the | ||
| 130 | trains existing in the simulation. The window can be hidden at will. | ||
| 131 | |||
| 132 | =head1 SYNOPSYS | ||
| 133 | |||
| 134 | my $id = Games::RailRoad::Window::Train->spawn(%opts); | ||
| 135 | $kernel->post( $id, 'visibility_toggle' ); | ||
| 136 | |||
| 137 | =head1 CLASS METHODS | ||
| 138 | |||
| 139 | =head2 my $id = Games::RailRoad::Window::Train->spawn( %opts ); | ||
| 140 | |||
| 141 | Create a window listing trains, and return the associated POE session | ||
| 142 | ID. One can pass the following options: | ||
| 143 | |||
| 144 | =over 4 | ||
| 145 | |||
| 146 | =item parent => $mw | ||
| 147 | |||
| 148 | A Tk window that will be the parent of the toplevel window created. This | ||
| 149 | parameter is mandatory. | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | =head1 PUBLIC EVENTS | ||
| 154 | |||
| 155 | The newly created POE session accepts the following events: | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item visibility_toggle() | ||
| 160 | |||
| 161 | Request the window to be hidden or restaured, depending on its previous | ||
| 162 | state. Note that closing the window is actually interpreted as hiding | ||
| 163 | the window. | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =head1 AUTHOR | ||
| 168 | |||
| 169 | Jerome Quelin | ||
| 170 | |||
| 171 | =head1 COPYRIGHT AND LICENSE | ||
| 172 | |||
| 173 | This software is copyright (c) 2008 by Jerome Quelin. | ||
| 174 | |||
| 175 | This is free software; you can redistribute it and/or modify it under | ||
| 176 | the same terms as the Perl 5 programming language system itself. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | |||
| 181 | __END__ | ||