|
| 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 (jarle@astro.ox.ac.uk) 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::State; |
| 58 | +use strict; |
| 59 | +use warnings; |
| 60 | + |
| 61 | +# |
| 62 | +# This is a very simple package to deal with the graphics state. |
| 63 | +# |
| 64 | + |
| 65 | +sub new { |
| 66 | + my $type = shift; |
| 67 | + |
| 68 | + my $self = { |
| 69 | + 'Commands' => [], |
| 70 | + }; |
| 71 | + |
| 72 | + bless $self, ref($type) || $type; |
| 73 | + return $self; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +sub DESTROY { |
| 78 | + my $self = shift; |
| 79 | + $self->clear(); |
| 80 | +} |
| 81 | + |
| 82 | +sub add { |
| 83 | + my $self = 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) = @_; |
| 88 | + |
| 89 | + # Compact and not user-friendly storage. |
| 90 | + push @{$self->{Commands}}, [$command, $command_name, $data, $opt]; |
| 91 | + |
| 92 | +# return $#{$self->{Commands}}+1; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +sub remove { |
| 97 | + my $self = shift; |
| 98 | + my $num = shift; |
| 99 | + |
| 100 | + # Remove entry #1 |
| 101 | + splice @{$self->{Commands}}, $num, 1; |
| 102 | +} |
| 103 | + |
| 104 | +sub get { |
| 105 | + my $self = shift; |
| 106 | + return @{$self->{Commands}}; |
| 107 | +} |
| 108 | + |
| 109 | +sub info { |
| 110 | + my $self = shift; |
| 111 | + print "The state has ".($#{$self->{Commands}}+1)." commands in the stack\n"; |
| 112 | +} |
| 113 | + |
| 114 | +sub show { |
| 115 | + my $self = shift; |
| 116 | + my $count=0; |
| 117 | + foreach my $arg (@{$self->{Commands}}) { |
| 118 | + print "$count - Func=$$arg[1]\n"; |
| 119 | + $count++; |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +sub clear { |
| 125 | + my $self = shift; |
| 126 | + # Do I need to do more? |
| 127 | + $self->{Commands}=[]; |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +sub copy { |
| 132 | + my $self = shift; |
| 133 | + my $new = PDL::Graphics::State->new(); |
| 134 | + foreach my $arg (@{$self->{Commands}}) { |
| 135 | + $new->add(@$arg); |
| 136 | + } |
| 137 | + return $new; |
| 138 | +} |
| 139 | + |
| 140 | +1; |
0 commit comments