Juliagraph/Frame/Panel/Board.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Juliagraph/Dialog/About.pm ↑ Summary Juliagraph/Frame/Panel/ColorBrowser.pm »
1  
2 # drawing board
3  
4 package App::GUI::Juliagraph::Frame::Panel::Board;
5 1 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Wx;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use base qw/Wx::Panel/;
9 use App::GUI::Juliagraph::Compute::Image;
10  
11 sub new {
12 my ( $class, $parent, $x, $y ) = @_;
13 my $self = $class->SUPER::new( $parent, -1, [-1,-1], [$x, $y] );
14 $self->{'menu_size'} = 28;
15 $self->{'size'}{'x'} = $x;
16 $self->{'size'}{'y'} = $y;
17 $self->{'center'}{'x'} = $x / 2;
18 $self->{'center'}{'y'} = $y / 2;
19 $self->{'dc'} = Wx::MemoryDC->new( );
20 $self->{'bmp'} = Wx::Bitmap->new( $self->{'size'}{'x'} + 10, $self->{'size'}{'y'} +10 + $self->{'menu_size'}, 24);
21 $self->{'dc'}->SelectObject( $self->{'bmp'} );
22 $self->{'tab'}{'constraint'} = '';
23  
24 Wx::Event::EVT_PAINT( $self, sub {
25 my( $self, $event ) = @_;
26 return unless ref $self->{'settings'};
27  
28 $self->{'x_pos'} = $self->GetPosition->x;
29 $self->{'y_pos'} = $self->GetPosition->y;
30  
31 if (exists $self->{'flag'}{'new'}) {
32 $self->{'dc'}->Blit (0, 0, $self->{'size'}{'x'} + $self->{'x_pos'},
33 $self->{'size'}{'y'} + $self->{'y_pos'} + $self->{'menu_size'},
34 $self->paint( Wx::PaintDC->new( $self ), $self->{'size'}{'x'}, $self->{'size'}{'y'} ), 0, 0);
35 } else {
36 Wx::PaintDC->new( $self )->Blit (0, 0, $self->{'size'}{'x'},
37 $self->{'size'}{'y'}-1,
38 $self->{'dc'}, 5, $self->{'menu_size'} + 6);
39 }
40 1;
41 });
42 # Wx::Event::EVT_ENTER_WINDOW( $self, sub { });
43 # Wx::Event::EVT_LEFT_DOWN( $self, sub { });
44 # Wx::Event::EVT_MOTION( $self, sub { });
45 Wx::Event::EVT_LEFT_DOWN( $self, sub {
46 if (ref $self->{'tab'}{'constraint'}){
47 my $pos = $_[1]->GetLogicalPosition( $self->{'dc'} );
48 my $dx = ($pos->x / $self->{'center'}{'x'} ) - 1;
49 my $dy = ($pos->y / $self->{'center'}{'y'} ) - 1;
50 $self->{'tab'}{'constraint'}->move_center_position( $dx, $dy, 0);
51 }
52 });
53 Wx::Event::EVT_LEFT_DCLICK( $self, sub {
54 if (ref $self->{'tab'}{'constraint'}){
55 my $pos = $_[1]->GetLogicalPosition($self->{'dc'});
56 my $dx = ($pos->x / $self->{'center'}{'x'} ) - 1;
57 my $dy = ($pos->y / $self->{'center'}{'y'} ) - 1;
58 $self->{'tab'}{'constraint'}->move_center_position( $dx, $dy, 1);
59 }
60 });
61 Wx::Event::EVT_RIGHT_DOWN( $self, sub {
62 if (ref $self->{'tab'}{'constraint'}){
63 my $pos = $_[1]->GetLogicalPosition($self->{'dc'});
64 my $dx = ($pos->x / $self->{'center'}{'x'} ) - 1;
65 my $dy = ($pos->y / $self->{'center'}{'y'} ) - 1;
66 $self->{'tab'}{'constraint'}->move_center_position( $dx, $dy, -1);
67 }
68 });
69 Wx::Event::EVT_MIDDLE_DOWN( $self, sub { $self->GetParent->draw });
70  
71 return $self;
72 }
73  
74 sub connect_constrains_tab {
75 my ($self, $ref) = @_;
76 return unless ref $ref eq 'App::GUI::Juliagraph::Frame::Tab::Constraints';
77 $self->{'tab'}{'constraint'} = $ref;
78 }
79  
80 sub draw {
81 my( $self, $settings ) = @_;
82 return unless $self->set_settings( $settings );
83 $self->{'flag'}{'draw'} = 1;
84 $self->Refresh;
85 }
86  
87 sub sketch {
88 my( $self, $settings ) = @_;
89 return unless $self->set_settings( $settings );
90 $self->{'flag'}{'sketch'} = 1;
91 $self->Refresh;
92 }
93  
94 sub set_settings {
95 my( $self, $settings ) = @_;
96 return unless ref $settings eq 'HASH';
97 $self->{'settings'} = $settings;
98 $self->{'flag'}{'new'} = 1;
99 }
100  
101  
102 sub paint {
103 my( $self, $dc, $width, $height ) = @_;
104 my $img = App::GUI::Juliagraph::Compute::Image::from_settings(
105 $self->{'settings'}, $self->{'size'}, $self->{'flag'}{'sketch'},
106 );
107 $dc->DrawBitmap( Wx::Bitmap->new( $img ), 0, 0, 0 ); # at point (0, 0) with no mask
108 $self->{'image'} = $img unless $self->{'flag'}{'sketch'};
109 delete $self->{'flag'};
110 $dc;
111 }
112  
113 sub save_file {
114 my( $self, $file_name, $width, $height ) = @_;
115 my $file_end = lc substr( $file_name, -3 );
116 if ($file_end eq 'svg') { $self->save_svg_file( $file_name, $width, $height ) }
117 elsif ($file_end eq 'png' or $file_end eq 'jpg') { $self->save_bmp_file( $file_name, $file_end, $width, $height ) }
118 else { return "unknown file ending: '$file_end'" }
119 }
120  
121 sub save_svg_file {
122 my( $self, $file_name, $width, $height ) = @_;
123 $width //= $self->GetParent->{'config'}->get_value('image_size');
124 $height //= $self->GetParent->{'config'}->get_value('image_size');
125 $width //= $self->{'size'}{'x'};
126 $height //= $self->{'size'}{'y'};
127 my $dc = Wx::SVGFileDC->new( $file_name, $width, $height, 250 ); # 250 dpi
128 $self->paint( $dc, $width, $height );
129 }
130  
131 sub save_bmp_file {
132 my( $self, $file_name, $file_end, $width, $height ) = @_;
133 $width //= $self->GetParent->{'config'}->get_value('image_size');
134 $height //= $self->GetParent->{'config'}->get_value('image_size');
135 $width //= $self->{'size'}{'x'};
136 $height //= $self->{'size'}{'y'};
137 # reuse $set->{'image'}
138 my $bmp = Wx::Bitmap->new( $width, $height, 24); # bit depth
139 my $dc = Wx::MemoryDC->new( );
140 $dc->SelectObject( $bmp );
141 $self->paint( $dc, $width, $height);
142 # $dc->Blit (0, 0, $width, $height, $self->{'dc'}, 10, 10 + $self->{'menu_size'});
143 $dc->SelectObject( &Wx::wxNullBitmap );
144 $bmp->SaveFile( $file_name, $file_end eq 'png' ? &Wx::wxBITMAP_TYPE_PNG : &Wx::wxBITMAP_TYPE_JPEG );
145 }
146  
147 1;
« Juliagraph/Dialog/About.pm ↑ Summary Juliagraph/Frame/Panel/ColorBrowser.pm »