| 1 | =head1 NAME |
| 2 | |
| 3 | State - A package to keep track of plotting commands |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | use PDL::Graphics::State; |
| 8 | |
| 9 | =head1 DESCRIPTION |
| 10 | |
| 11 | This is a very simple, at present almost trivial, package to keep track |
| 12 | of the current set of plotting commands. |
| 13 | |
| 14 | =head1 USAGE |
| 15 | |
| 16 | You create a new object by calling the C<new> operator |
| 17 | |
| 18 | $state = PDL::Graphics::State->new(); |
| 19 | |
| 20 | Then for each new command you call C<add> on this object so that for a |
| 21 | call to C<line> of the form |
| 22 | |
| 23 | line $x, $y, $opt; |
| 24 | |
| 25 | the call to C<add> would be like |
| 26 | |
| 27 | $state->add(\&line, 'line', [$x, $y], $opt); |
| 28 | |
| 29 | which is stored internally as: |
| 30 | |
| 31 | [\&line, 'line', [$x, $y], $opt] |
| 32 | |
| 33 | The state can later be extracted using C<get> which returns the state |
| 34 | object which is an array of anonymous arrays like the one above where |
| 35 | the first object is a reference to the function, the second an anomymous |
| 36 | array of arguments to the function and finally an anonymous hash with |
| 37 | options to the command. |
| 38 | |
| 39 | If you know the order in which you inserted commands they can be removed |
| 40 | by calling C<remove> with the number in the stack. No further interaction |
| 41 | is implemented except C<clear> which clears the stack and C<copy> which |
| 42 | returns a "deep" copy of the state. |
| 43 | |
| 44 | =head1 AUTHOR |
| 45 | |
| 46 | Jarle Brinchmann ([email protected]) after some prodding by |
| 47 | Karl Glazebrook. |
| 48 | |
| 49 | All rights reserved. There is no warranty. You are allowed |
| 50 | to redistribute this software / documentation under certain |
| 51 | conditions. For details, see the file COPYING in the PDL |
| 52 | distribution. If this file is separated from the PDL distribution, |
| 53 | the copyright notice should be included in the file. |
| 54 | |
| 55 | =cut |
| 56 | |
| 57 | package PDL::Graphics::Statescip-perl cpan PGPLOT 2.36 PDL/Graphics/State/; |
| 58 | use strictscip-perl cpan PGPLOT 2.36 strict/; |
| 59 | use warningsscip-perl cpan PGPLOT 2.36 warnings/; |
| 60 | |
| 61 | # |
| 62 | # This is a very simple package to deal with the graphics state. |
| 63 | # |
| 64 | |
| 65 | sub newscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/new(). { |
| 66 | my $type = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 67 | |
| 68 | my $self = { |
| 69 | 'Commands' => [], |
| 70 | }; |
| 71 | |
| 72 | blessscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/bless(). $self, refscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/ref().($type) || $type; |
| 73 | return $self; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | sub DESTROYscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/DESTROY(). { |
| 78 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 79 | $self->clearscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/clear().(); |
| 80 | } |
| 81 | |
| 82 | sub addscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/add(). { |
| 83 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 84 | # The command is a reference to the subroutine, the data is an |
| 85 | # anonymous array containing the data passed to the routine and |
| 86 | # opt is the options hash PASSED TO THE ROUTINE.. |
| 87 | my ($command, $command_name, $data, $opt) = @_scip-perl cpan PGPLOT 2.36 PDL/Graphics/State/`@_`.; |
| 88 | |
| 89 | # Compact and not user-friendly storage. |
| 90 | pushscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/push(). @{$self->{Commands}}, [$command, $command_name, $data, $opt]; |
| 91 | |
| 92 | # return $#{$self->{Commands}}+1; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | sub removescip-perl cpan PGPLOT 2.36 PDL/Graphics/State/remove(). { |
| 97 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 98 | my $num = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 99 | |
| 100 | # Remove entry #1 |
| 101 | splicescip-perl cpan PGPLOT 2.36 PDL/Graphics/State/splice(). @{$self->{Commands}}, $num, 1; |
| 102 | } |
| 103 | |
| 104 | sub getscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/get(). { |
| 105 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 106 | return @{$self->{Commands}}; |
| 107 | } |
| 108 | |
| 109 | sub infoscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/info(). { |
| 110 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 111 | printscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/print(). "The state has ".($#{$self->{Commands}}+1)." commands in the stack\n"; |
| 112 | } |
| 113 | |
| 114 | sub showscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/show(). { |
| 115 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 116 | my $count=0; |
| 117 | foreach my $arg (@{$self->{Commands}}) { |
| 118 | printscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/print(). "$count - Func=$$arg[1]\n"; |
| 119 | $count++; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | sub clearscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/clear(). { |
| 125 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 126 | # Do I need to do more? |
| 127 | $self->{Commands}=[]; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | sub copyscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/copy(). { |
| 132 | my $self = shiftscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/shift().; |
| 133 | my $new = PDL::Graphics::State->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/new().(); |
| 134 | foreach my $arg (@{$self->{Commands}}) { |
| 135 | $new->addscip-perl cpan PGPLOT 2.36 PDL/Graphics/State/add().(@$arg); |
| 136 | } |
| 137 | return $new; |
| 138 | } |
| 139 | |
| 140 | 1; |