#=============================================== # TkGPGL #=============================================== package TkGPGL; use MyTk::TkCommon; @ISA = qw(TkCommon); #公開したいサブルーチン #@EXPORT = qw(DelSpace Reduce01 MakePath RegExpQuote); use strict; use Utils; my $MARGINE = 0.05; my $ISCRXL = 0.; # /*描画領域*/ my $ISCRXR = 100.; my $ISCRYB = 100.; my $ISCRYT = 0.; my $X0 = 0.; my $Y0 = 0.; my $XWIDTH = 600.; my $YWIDTH = 380.; my $XL = $X0 - $XWIDTH * $MARGINE; my $XR = $X0 + $XWIDTH * (1 + $MARGINE); my $YL = $Y0 - $YWIDTH * $MARGINE; my $YR = $Y0 + $YWIDTH * (1 + $MARGINE); my $FACTRX = ($ISCRXR - $ISCRXL + 1) / ($XR - $XL); my $FACTRY = ($ISCRYB - $ISCRYT + 1) / ($YR - $YL); my $WXORG = $XL; my $WYORG = $YL; my $MAX_CHR = 80; my $LNS = 3; my $LNR = 1; my $FAX = 100.0; my $FAY = 100.0; sub FileName { return shift->{'FileName'}; } sub SetFileName { my ($this,$f)=@_; return $this->{'FileName'} = $f; } #sub SetCanvas { my($this,$can)=@_; return $this->{'Canvas'} = $can; } #sub Canvas { return shift->{'Canvas'}; } sub Canvas { my ($this, $canvas) = @_; if($canvas) { return $canvas; } return $this->mw()->Canvas(); } #sub GetCurPos { my $this=shift; return ($this->{'CurPosX'},$this->{'CurPosY'}); } #sub SetCurPos { my ($this,$x,$y)= @_;$this->{'CurPosX'} = $x;$this->{'CurPosY'} = $y; } sub GetCurVal { my $this=shift; return ($this->{'CurValX'}, $this->{'CurValY'}); } sub SetCurVal { my ($this,$x,$y) = @_;$this->{'CurValX'} = $x;$this->{'CurValY'} = $y; } sub new { my ($module, $app, $canvas) = @_; my $this = {}; bless $this; $this->SetApplication($app); # $this->SetCanvas($canvas); return $this; } sub DESTROY { my $this = shift; $this->SUPER::DESTROY(@_); } sub get_cmd { my ($this, $line) = @_; Utils::DelSpace($line); my ($command, $s) = ($line =~ /^(\D+?)\s*(.*)$/); unless(defined $command) { $command = ''; $s = $line; } my @a = split(/[\s,]+/, $s); return ($command, @a); } sub ValToPos { my ($this, $vx, $vy) = @_; my $x0 = $this->{'x0'}; my $y0 = $this->{'y0'}; my $x1 = $this->{'x1'}; my $y1 = $this->{'y1'}; my $vx0 = $this->{'vx0'}; my $vy0 = $this->{'vy0'}; my $vx1 = $this->{'vx1'}; my $vy1 = $this->{'vy1'}; #print "ViewRange: $vx0, $vy0, $vx1, $vy1\n"; my $x = $x0 + ($x1 - $x0) / ($vx1 - $vx0) * ($vx - $vx0); my $y = $y0 + ($y1 - $y0) / ($vy1 - $vy0) * ($vy - $vy0); return (int($x+0.5), int($y+0.5)); } # Axis (X) command sub X { my ($this, @a) = @_; return $this->Axis(@a); } sub Axis { my ($this, @a) = @_; return unless(defined $a[2]); my $p = $a[0]; my $d = $a[1]; my $n = $a[2]; my $dir = $p % 2; # dir = 0 Y-ax 1 X-ax my $md = $p / 2; # md = 0 d = unit length , n = iteration number # = 1 d = total length, n = division number my ($xc, $yc) = $this->GetCurVal(); $d = $d / $n if($md == 1); if($dir == 0) { my $dx = $FAX * $FACTRX; $this->M($xc+$dx, $yc); $this->D($xc-$dx, $yc); for(my $i = 0 ; $i < $n ; $i++) { my $yp = $yc + $d; $this->M($xc, $yc); $this->D($xc, $yp); $yc = $yp; $this->M($xc+$dx, $yc); $this->D($xc-$dx, $yc); } } elsif($dir == 1) { my $dy = $FAY * $FACTRY; $this->M($xc, $yc+$dy); $this->D($xc, $yc-$dy); for(my $i = 0 ; $i < $n ; $i++) { my $xp = $xc + $d; $this->M($xc, $yc); $this->D($xp, $yc); my $xc = $xp; $this->M($xc, $yc+$dy); $this->D($xc, $yc-$dy); } } } # Set color (J) command sub J { my ($this, @a) = @_; my $color = int($a[0] % 16); } # Set line style (L) command sub L {} # Pitch dot (B) command sub B {} # Speed (T) command sub T {} # Alpha (A) command sub A {} # (C) command sub C { my ($this, @a) = @_; return undef unless(defined $a[1]); my ($x, $y) = $this->ValToPos($a[0], $a[1]); $this->SetCurVal($a[0], $a[1]); #print "C Move to ($x, $y):($a[0],$a[1])\n"; return $this->M(@a); } # Print text (P) command sub P { my ($this, @a) = @_; return undef unless(defined $a[0]); my $fd = $this->{'FontDirection'}; $fd = 'Horizontal' unless($fd); my ($x0, $y0) = $this->GetCurVal(); my ($x, $y) = $this->ValToPos($x0, $y0); my $s = $a[0]; my $h = $this->Canvas()->GetTextHeight(); my $w = $this->Canvas()->GetTextWidth($s); if($fd eq 'Horizontal') { $this->Canvas()->DrawText($x, $y-$h, $s, '', 'nw'); } else { $this->Canvas()->DrawText($x-$w, $y, $s, '', 'nw'); } } # Mark (N) command sub N { my ($this, @a) = @_; return undef unless(defined $a[0]); return; } # Scale (S) command sub S { my ($this, @a) = @_; return undef unless(defined $a[0]); return; } # Change Font (Q) command sub Q { my ($this, @a) = @_; return undef unless(defined $a[0]); $this->{'FontDirection'} = 'Horizontal' if($a[0] == 0); $this->{'FontDirection'} = 'Vertical' if($a[0] == 1); } # Rel_move (R) command sub R { my ($this, @a) = @_; return undef unless(defined $a[1]); my ($x0, $y0) = $this->GetCurVal(); my $x = $x0 + $a[0]; my $y = $y0 + $a[1]; #print "R Move to ($x, $y):($a[0],$a[1])\n"; return $this->M($x, $y); } # Rel_draw (I) command sub I { my ($this, @a) = @_; return undef unless(defined $a[1]); my ($x0, $y0) = $this->GetCurVal(); for(my $i = 0 ; $i < @a ; $i += 2 ) { my $xc = $a[$i]; my $yc = $a[$i+1]; last if(!defined $yc); next if($xc eq '*' or $yc eq '*' ); my $x = $x0 + $a[0]; my $y = $y0 + $a[1]; $this->D($x, $y); #print "I Line to i=$i ($x, $y):($xc,$yc)\n"; } } # (H) command sub H { my ($this, @a) = @_; return undef unless(defined $a[1]); my ($x, $y) = $this->ValToPos($a[0], $a[1]); $this->SetCurVal($a[0], $a[1]); #print "H Move to ($x, $y):($a[0],$a[1])\n"; return $this->M(@a); } # Move (M) command sub M { my ($this, @a) = @_; return undef unless(defined $a[1]); my ($x, $y) = $this->ValToPos($a[0], $a[1]); $this->Canvas()->MoveTo($x, $y); $this->SetCurVal($a[0], $a[1]); #print "Move to ($x, $y):($a[0],$a[1])\n"; } # draw (D) command sub D { my ($this, @a) = @_; return undef unless(defined $a[1]); for(my $i = 0 ; $i < @a ; $i += 2 ) { my $xc = $a[$i]; my $yc = $a[$i+1]; last if(!defined $yc); next if($xc eq '*' or $yc eq '*' ); my ($x, $y) = $this->ValToPos($a[$i], $a[$i+1]); $this->Canvas()->LineTo($x, $y); $this->SetCurVal($a[$i], $a[$i+1]); #print "Line to i=$i ($x, $y):($xc,$yc)\n"; } } sub Draw { my ($this, $canvas) = @_; my $mw = $this->mw(); $canvas = $this->Canvas() unless($canvas); my $App = $this->App(); my $font = $App->{'GraphFrameFont'}; my @font = split(/,/, $font) if($font); $canvas->SetFont(\@font) if($font);; my $w = $canvas->width(); my $h = $canvas->height(); my $margin = 50; my $x0 = $this->{'x0'} = $margin; my $y0 = $this->{'y0'} = $h - $margin; my $x1 = $this->{'x1'} = $w - $margin; my $y1 = $this->{'y1'} = $margin; #$canvas->DrawBox($x0, $y0, $x1, $y1); my $vx0 = 1e100; my $vx1 = -1e100; my $vy0 = 1e100; my $vy1 = -1e100; my $FileName = $this->FileName(); my $infile = new JFile; my $ret = $infile->Open($FileName, "r"); unless($ret) { print "\nError: Can not open [$FileName].\n\n"; return undef; } my $textalign = "nw"; while(!$infile->eof()) { my $line = $infile->ReadLine(); Utils::DelSpace($line); my ($command, @a) = $this->get_cmd($line); if($command eq 'M' || $command eq 'D') { my $x = $a[0]; my $y = $a[1]; $vx0 = $x if($x < $vx0); $vx1 = $x if($x > $vx1); $vy0 = $y if($y < $vy0); $vy1 = $y if($y > $vy1); } } $vx0 = 0 if($vx0 > 0); $vy0 = 0 if($vy0 > 0); $infile->rewind(); $canvas->MoveTo($x0, $y0); my $rx = ($x1 - $x0) / ($y1 - $y0); my $rv = ($vx1 - $vx0) / ($vy1 - $vy0); if(abs($rx) > abs($rv)) { $x1 = $x0 + abs($rv * ($y0 - $y1)); } else { $y0 = $y1 + abs(($x1 - $x0) / $rv); } $this->{'vx0'} = $vx0; $this->{'vy0'} = $vy0; $this->{'vx1'} = $vx1; $this->{'vy1'} = $vy1; #print "ViewRange: $vx0, $vy0, $vx1, $vy1\n"; while(!$infile->eof()) { my $line = $infile->ReadLine(); Utils::DelSpace($line); my ($command, @a) = $this->get_cmd($line); #print "line: $line ($w,$h): c=[$command] a0=$a[0]\n"; if($command eq 'M') { $this->M(@a); } elsif($command eq 'D') { $this->D(@a); } elsif($command eq 'R') { $this->R(@a); } elsif($command eq 'I') { $this->I(@a); } elsif($command eq 'H') { $this->H(@a); } elsif($command eq 'P') { $this->P(@a); } elsif($command eq 'N') { $this->N(@a); } elsif($command eq 'S') { $this->S(@a); } elsif($command eq 'Q') { $this->Q(@a); } elsif($command eq 'J') { $this->J(@a); } elsif($command eq 'L') { $this->L(@a); } elsif($command eq 'B') { $this->B(@a); } elsif($command eq 'X') { $this->X(@a); } elsif($command eq 'T') { $this->T(@a); } elsif($command eq 'A') { $this->A(@a); } elsif($command eq 'C') { $this->C(@a); } } $infile->Close(); } sub CreateGraphFrame { my ($this, $canvas) = @_; } sub AssignGraphData { my ($this) = @_; } sub DeleteWidget { my ($this, $Frame) = @_; return undef unless($Frame); return 1; } sub AddWidget { my ($this, $Frame) = @_; return undef unless($Frame); return 1; } sub SetFileInfo { my ($this, $ListBox, $TextBox) = @_; return undef unless($ListBox); return undef unless($TextBox); $ListBox->ClearAll(); $TextBox->ClearText(); return 1; } 1; __END__