| 1 | package Finance::GeniusTrader::Graphics::Graphic; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 10 | use Finance::GeniusTrader::Graphics::Zone; | ||
| 11 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 12 | use Finance::GeniusTrader::Conf; | ||
| 13 | |||
| 14 | Finance::GeniusTrader::Conf::default("Graphic::BackgroundColor", "white"); | ||
| 15 | Finance::GeniusTrader::Conf::default("Graphic::ForegroundColor", "black"); | ||
| 16 | |||
| 17 | =head1 Finance::GeniusTrader::Graphics::Graphic | ||
| 18 | |||
| 19 | A graphic is composed of a layout of zones. Objects are affected to | ||
| 20 | the various zones. Those objects may be displayed. The display engine | ||
| 21 | may use an associated default scale to obtain coordinates of points to | ||
| 22 | draw. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | =head2 Finance::GeniusTrader::Graphics::Graphic->new($zone) | ||
| 27 | |||
| 28 | Create a new graphic using the specified zone layout. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | sub new { | ||
| 32 | my $type = shift; | ||
| 33 | my $class = ref($type) || $type; | ||
| 34 | my $zone = shift; | ||
| 35 | |||
| 36 | my $self = { "zone" => $zone, "objects" => [], | ||
| 37 | "bg_color" => | ||
| 38 | get_color(Finance::GeniusTrader::Conf::get("Graphic::BackgroundColor")), | ||
| 39 | "fg_color" => | ||
| 40 | get_color(Finance::GeniusTrader::Conf::get("Graphic::ForegroundColor")) | ||
| 41 | }; | ||
| 42 | |||
| 43 | return bless $self, $class; | ||
| 44 | } | ||
| 45 | |||
| 46 | =head2 $graphic->set_zone($zone) | ||
| 47 | |||
| 48 | Define the layout of the display zones. You shouldn't call this once | ||
| 49 | you added graphical objects because objects may reference zones that | ||
| 50 | are no more part of the new layout. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | sub set_zone { | ||
| 54 | my ($self, $zone) = @_; | ||
| 55 | $self->{'zone'} = $zone; | ||
| 56 | } | ||
| 57 | |||
| 58 | =head2 $graphic->set_background_color($color) | ||
| 59 | |||
| 60 | Set the background color of the graphic. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | sub set_background_color { | ||
| 64 | my ($self, $color) = @_; | ||
| 65 | $self->{'bg_color'} = get_color($color); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 $graphic->add_object($object) | ||
| 69 | |||
| 70 | Add a graphical object to the graphic. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | sub add_object { | ||
| 74 | my ($self, $object) = @_; | ||
| 75 | push @{$self->{'objects'}}, $object; | ||
| 76 | $object->set_z_level(scalar @{$self->{'objects'}}); | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 $graphic->display($driver, $picture) | ||
| 80 | |||
| 81 | Display the graphic in the picture. It will display the zones and the | ||
| 82 | graphical objects. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | sub display { | ||
| 86 | my ($self, $driver, $picture) = @_; | ||
| 87 | my $z = $self->{'zone'}; | ||
| 88 | $driver->filled_rectangle($picture, 0, 0, $z->external_width() - 1, | ||
| 89 | $z->external_height() - 1, | ||
| 90 | $self->{'bg_color'}); | ||
| 91 | $self->{'zone'}->display($driver, $picture); | ||
| 92 | foreach my $object (sort { $a->get_z_level() <=> $b->get_z_level() } | ||
| 93 | @{$self->{'objects'}}) { | ||
| 94 | $object->display($driver, $picture); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; |