line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Term::Graille - Graphical Display in the terminal using UTF8 Braille characters |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my $canvas = Term::Graille->new( |
8
|
|
|
|
|
|
|
width => 72, # pixel width |
9
|
|
|
|
|
|
|
height => 64, # pixel height |
10
|
|
|
|
|
|
|
top=>3, # row position in terminal (optional,defaults to 1) |
11
|
|
|
|
|
|
|
left=>10, # column position (optional,defaults to 1) |
12
|
|
|
|
|
|
|
borderStyle => "double", # |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Inspired by Drawille by asciimoo, which has many variants (including |
18
|
|
|
|
|
|
|
a perl variant Term::Drawille by RHOELZ), this is a clone with a few |
19
|
|
|
|
|
|
|
extras. The goal is to achieve performance and features. with built-in |
20
|
|
|
|
|
|
|
turtle-like graphics, line and curve drawing (Algorithm::Line::Bresenham), |
21
|
|
|
|
|
|
|
scrolling, border setting, and more in development. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=begin html |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=end html |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 FUNCTIONS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
package Term::Graille; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
60951
|
use strict;use warnings; |
|
1
|
|
|
1
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
37
|
|
|
|
|
|
|
our $VERSION="0.09"; |
38
|
1
|
|
|
1
|
|
535
|
use utf8; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
4
|
|
39
|
1
|
|
|
1
|
|
450
|
use open ":std", ":encoding(UTF-8)"; |
|
1
|
|
|
|
|
979
|
|
|
1
|
|
|
|
|
5
|
|
40
|
1
|
|
|
1
|
|
12345
|
use base 'Exporter'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
95
|
|
41
|
|
|
|
|
|
|
our @EXPORT_OK = qw/colour paint printAt clearScreen border blockBlit block2braille pixelAt cursorAt/; |
42
|
1
|
|
|
1
|
|
6
|
use Algorithm::Line::Bresenham 0.15; |
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
31
|
|
43
|
1
|
|
|
1
|
|
432
|
use Time::HiRes "sleep"; |
|
1
|
|
|
|
|
1148
|
|
|
1
|
|
|
|
|
3
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
BEGIN { |
46
|
1
|
50
|
|
1
|
|
717
|
if ($^O eq "MSWin32") { |
47
|
0
|
|
|
|
|
0
|
system("chcp 65001 >nul"); |
48
|
0
|
|
|
|
|
0
|
system("echo 'Please set console font to one that can handle utf8 fonts'") |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head3 Cnew(%params)> |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Creates a new canavas; params are |
55
|
|
|
|
|
|
|
C The pixel width, required C the pixel height, required, |
56
|
|
|
|
|
|
|
C terminal row (optional,default 1) C terminal column ( |
57
|
|
|
|
|
|
|
optional,default 1) C border type (optional,one of 'simple', |
58
|
|
|
|
|
|
|
'double', 'thin', 'thick' or 'shadow') C border colour ( |
59
|
|
|
|
|
|
|
optional), C Title text for top border,(optional) C |
60
|
|
|
|
|
|
|
Title colour (optional) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new{ |
66
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; # params are width and height in pixels |
67
|
0
|
|
|
|
|
|
my $self={width=>$params{width},height=>$params{height}}; |
68
|
0
|
|
|
|
|
|
for my $key (qw/borderStyle borderColour title titleColour top left cartesian/){ |
69
|
0
|
0
|
|
|
|
|
$self->{ $key}=$params{$key} if exists $params{$key} |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
0
|
|
|
|
$self->{top}//=1; |
72
|
0
|
|
0
|
|
|
|
$self->{left}//=1; |
73
|
0
|
|
0
|
|
|
|
$self->{cartesian}//=1; |
74
|
0
|
|
|
|
|
|
$self->{setPix}=[['⡀','⠄','⠂','⠁'],['⢀','⠠','⠐','⠈']]; |
75
|
0
|
|
|
|
|
|
$self->{unsetPix}=[['⢿','⣻','⣽','⣾'],['⡿','⣟','⣯','⣷']]; |
76
|
|
|
|
|
|
|
$self->{logoVars}={x=>$self->{width}/2, # integrated Turtle Graphics |
77
|
0
|
|
|
|
|
|
y=>$self->{height}/2, # initial variables x, y and direction |
78
|
|
|
|
|
|
|
d=>0, |
79
|
|
|
|
|
|
|
p=>1, |
80
|
|
|
|
|
|
|
GV=>{}, |
81
|
|
|
|
|
|
|
PC=>0, |
82
|
|
|
|
|
|
|
Stk=>[],}; |
83
|
0
|
|
|
|
|
|
bless $self,$class; |
84
|
0
|
|
|
|
|
|
$self->clear; # initiallises canvas to blank |
85
|
0
|
|
|
|
|
|
return $self; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head3 C<$canvas-Edraw()> , C<$canvas-Edraw($row, $column)> |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Draws the canvas to the terminal window. Optional row and column |
92
|
|
|
|
|
|
|
parameters may be passed to position the displayed canvas. If |
93
|
|
|
|
|
|
|
borderStyle is specified, the border is drawn, If title is specified |
94
|
|
|
|
|
|
|
this is added to the top border |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub draw{ |
99
|
0
|
|
|
0
|
1
|
|
my ($self,$top,$left)=@_; # the location on the screen can be overridden by passing row coloum position |
100
|
0
|
|
0
|
|
|
|
$top//=$self->{top};$left//=$self->{left}; |
|
0
|
|
0
|
|
|
|
|
101
|
0
|
|
|
|
|
|
border($top-1,$left-1,$top+@{$self->{grid}}-1,$left+@{$self->{grid}->[0]}, |
|
0
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$self->{borderStyle},$self->{borderColour}, |
103
|
|
|
|
|
|
|
$self->{title},$self->{titleColour}) |
104
|
0
|
0
|
0
|
|
|
|
if ((defined $self->{borderStyle})&&(ref $self->{grid} eq "ARRAY")); |
105
|
0
|
|
|
|
|
|
printAt($top,$left, [reverse @{$self->{grid}}]); |
|
0
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
print colour("reset"); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head3 C<$canvas-Eas_string()> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Returns the string containing the canvas of utf8 braille symbols, rows |
113
|
|
|
|
|
|
|
being separated by newline. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub as_string{ |
118
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
119
|
0
|
|
|
|
|
|
my $str=""; |
120
|
0
|
|
|
|
|
|
$str.=join("",@$_)."\n" foreach (reverse @{$self->{grid}}); |
|
0
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return $str; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head3 C<$canvas-Eset($x,$y,$pixelValue)> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Sets a particular pixel on (default if C<$pixelValue> not sent) or off. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# this function creates a callback for plotting pixels directly |
131
|
|
|
|
|
|
|
sub BresenhamPlot{ |
132
|
0
|
|
|
0
|
0
|
|
my($x,$y,$args)=@_; |
133
|
0
|
|
|
|
|
|
my ($canvas,$value)=(@$args); |
134
|
0
|
|
|
|
|
|
set($canvas,$x,$y,$value); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub set{ |
138
|
1
|
|
|
1
|
|
7
|
use integer; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
139
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 3; |
140
|
0
|
|
|
|
|
|
my ($self,$x,$y,$value)=@_; |
141
|
|
|
|
|
|
|
#exit if out of bounds |
142
|
0
|
0
|
0
|
|
|
|
return unless(($x<$self->{width})&&($x>=0)&&($y<$self->{height})&&($y>=0)); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
143
|
|
|
|
|
|
|
#convert coordinates to character / pixel offset position |
144
|
0
|
|
|
|
|
|
my ($chrX,$chrY,$xOffset,$yOffset)=$self->charOffset($x,$y); |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $bChr=chop($self->{grid}->[$chrY]->[$chrX]); |
147
|
0
|
0
|
|
|
|
|
if ($value=~/^[a-z]/){$self->{grid}->[$chrY]->[$chrX]=colour($value);} |
|
0
|
0
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
elsif ($value=~/^\033\[/){$self->{grid}->[$chrY]->[$chrX]=$value;} |
149
|
|
|
|
|
|
|
# ensure character is a braille character to start with |
150
|
0
|
0
|
|
|
|
|
$bChr='⠀' if (ord($bChr)&0x2800 !=0x2800); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$self->{grid}->[$chrY]->[$chrX].=$value? # if $value is false, unset, or else set pixel |
153
|
|
|
|
|
|
|
(chr( ord($self->{setPix} -> [$xOffset]->[$yOffset]) | ord($bChr) ) ): |
154
|
0
|
0
|
|
|
|
|
(chr( ord($self->{unsetPix}-> [$xOffset]->[$yOffset]) & ord($bChr))); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head3 C<$canvas-Eunset($x,$y)> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Sets the pixel value at C<$x,$y> to blank |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
sub unset{ |
163
|
0
|
|
|
0
|
1
|
|
my ($self,$x,$y)=@_; |
164
|
0
|
|
|
|
|
|
$self->set($x,$y,0); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub charOffset{ # gets the character grid position and offset within that character |
168
|
|
|
|
|
|
|
# give the pixel position |
169
|
1
|
|
|
1
|
|
304
|
use integer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
170
|
0
|
|
|
0
|
0
|
|
my ($self,$x,$y)=@_; |
171
|
0
|
0
|
0
|
|
|
|
return -1 unless(($x<$self->{width})&&($x>=0)&&($y<$self->{height})&&($y>=0)); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $chrX=$x/2;my $xOffset=$x- $chrX*2; |
|
0
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
my $chrY=$y/4;my $yOffset=$y- $chrY*4; |
|
0
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
return ($chrX,$chrY,$xOffset,$yOffset); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head3 C<$canvas-Epixel($x,$y)> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Gets the pixel value at C<$x,$y> |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
sub pixel{ #get pixel value at coordinates |
184
|
0
|
|
|
0
|
1
|
|
my ($self,$x,$y,$value)=@_; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
#exit if out of bounds |
187
|
0
|
0
|
0
|
|
|
|
return unless(($x<($self->{width}-1))&&($x>=0)&&($y<($self->{height}-1))&&($x>=0)); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
#convert coordinates to character / pixel offset position |
190
|
0
|
|
|
|
|
|
my $chrX=$x/2;my $xOffset=$x- $chrX*2; |
|
0
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
my $chrY=$y/4;my $yOffset=$y- $chrY*4; |
|
0
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
my $orOp=ord($self->{setPix}-> [$xOffset]->[$yOffset]) & ord($self->{grid}->[$chrY]->[$chrX]); |
193
|
0
|
0
|
|
|
|
|
return $orOp == ord('⠀')?0:1; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head3 C<$canvas-Eclear()> |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Re-initialises the canvas with blank braille characters |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
201
|
|
|
|
|
|
|
sub clear{ |
202
|
0
|
|
|
0
|
1
|
|
my ($self,$x1,$y1,$x2,$y2)=@_; |
203
|
0
|
0
|
|
|
|
|
if(@_<2){ |
204
|
0
|
0
|
|
|
|
|
$self->{grid}=[map {[( '⠀')x ($self->{width}/2+($self->{width}%2?1:0))]}(0..($self->{height}/4+($self->{height}%4?1:0)))] |
|
0
|
0
|
|
|
|
|
|
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
else{ |
207
|
0
|
0
|
|
|
|
|
my @xr=$x1>$x2?($x2..$x1):($x1..$x2); |
208
|
0
|
0
|
|
|
|
|
my @yr=$y1>$y2?($y2..$y1):($y1..$y2); |
209
|
0
|
|
|
|
|
|
foreach my $y(@yr){ |
210
|
0
|
|
|
|
|
|
foreach my $x(@xr){ |
211
|
0
|
|
|
|
|
|
$self->{grid}->[$y]->[$x]='⠀'; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Pixel plotting primitives for shapes using Bresenham (Algorithm::Line::Bresenham) |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head3 C<$canvas-Eline($x1,$y1,$x2,$y2,$value)> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a line from C<$x1,$y1> to C<$x2,$y2>. |
224
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
225
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub line{ |
230
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 5; # final optional parameter is set to one if not given |
231
|
0
|
|
|
|
|
|
my ($self,$x1,$y1,$x2,$y2,$value)=@_; |
232
|
0
|
|
|
|
|
|
my $setRef=\&Term::Graille::BresenhamPlot; |
233
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::line($x1,$y1,$x2,$y2,$setRef,[$self,$value]); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head3 C<$canvas-Ecircle($x1,$y1,$radius,$value)> |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a circle centered at C<$x1,$y1> |
240
|
|
|
|
|
|
|
with radius C<$radius> to C<$x2,$y2>. |
241
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
242
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=cut |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub circle{ |
247
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 4; |
248
|
0
|
|
|
|
|
|
my ($self,$x1,$y1,$radius,$value)=@_; |
249
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::circle($x1,$y1,$radius); |
250
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head3 C<$canvas-Eellipse_rect($x1,$y1,$x2,$y2,$value)> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a rectangular ellipse, (an |
257
|
|
|
|
|
|
|
ellipse bounded by a rectangle defined by C<$x1,$y1,$x2,$y2>). |
258
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
259
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub ellipse_rect{ |
264
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 5; |
265
|
0
|
|
|
|
|
|
my ($self,$x1,$y1,$x2,$y2,$value)=@_; |
266
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::ellipse_rect($x1,$y1,$x2,$y2); |
267
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head3 C<$canvas-Equad_bezier($x1,$y1,$x2,$y2,$x3,$y3,$value)> |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a quadratic bezier, defined by |
273
|
|
|
|
|
|
|
end points C<$x1,$y1,$x3,$y3>) and control point C<$x2,$y2>. |
274
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
275
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub quad_bezier{ |
281
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 7; |
282
|
0
|
|
|
|
|
|
my ($self,$x1,$y1,$x2,$y2,$x3,$y3,$value)=@_; |
283
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::quad_bezier($x1,$y1,$x2,$y2,$x3,$y3); |
284
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head3 C<$canvas-Ethick_line($x1,$y1,$x2,$y2,$thickness,$value)> |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a thick line, defined by |
291
|
|
|
|
|
|
|
end points C<$x1,$y1,$x2,$y2>) and thickness C<$thickness>. |
292
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
293
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=cut |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub thick_line{ |
299
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 6; |
300
|
0
|
|
|
|
|
|
my ($self,$x0,$y0,$x1,$y1,$thickness,$value)=@_; |
301
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::thick_line($x0,$y0,$x1,$y1,$thickness); |
302
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head3 C<$canvas-Evarthick_line($x0,$y0,$x1,$y1, |
307
|
|
|
|
|
|
|
$left,$argL, |
308
|
|
|
|
|
|
|
$right,$argR, $value)=@_; |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a variable thickness, defined by |
311
|
|
|
|
|
|
|
end points C<$x0,$y0,$x1,$y1>) and thickness defined by two user defined |
312
|
|
|
|
|
|
|
functions, each function taking as arguments CL|RE, $pos, $len> |
313
|
|
|
|
|
|
|
and returning thickness of the left and right sides |
314
|
|
|
|
|
|
|
of the line. The optional value C<$value> sets or unsets the pixels. |
315
|
|
|
|
|
|
|
If C<$value> is a valid colour (see below) the line will be drawn with that colour. |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=cut |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
sub varthick_line{ |
321
|
0
|
0
|
|
0
|
1
|
|
push @_, 1 if @_ == 9; |
322
|
0
|
|
|
|
|
|
my ($self,$x0,$y0,$x1,$y1,$left,$argL,$right,$argR,$value)=@_; |
323
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::varthick_line($x0,$y0,$x1,$y1,$left,$argL,$right,$argR); |
324
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head3 C<$canvas-Epolyline($x1,$y1,....,$xn,$yn,$value)> |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a poly line, form a |
333
|
|
|
|
|
|
|
sequences of points. |
334
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
335
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
sub polyline{ |
341
|
0
|
|
|
0
|
1
|
|
my $self=shift; |
342
|
0
|
|
|
|
|
|
my @vertices=@_; |
343
|
0
|
0
|
|
|
|
|
my $value= pop @vertices if (scalar @vertices & 1); |
344
|
0
|
|
0
|
|
|
|
$value//=1; |
345
|
0
|
|
|
|
|
|
my @points=Algorithm::Line::Bresenham::polyline(@vertices); |
346
|
0
|
|
|
|
|
|
$self->set(@$_,$value) foreach (@points); |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
sub degToRad{ |
350
|
0
|
0
|
|
0
|
0
|
|
return $_[0]?3.14159267*$_[0]/180:0; ; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=head2 Character Level Functions; |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=head3 C<$canvas-Escroll($direction,$wrap)> |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
Scrolls in C<$direction>. $direction may be |
358
|
|
|
|
|
|
|
"l", "left", "r","right", "u","up","d", "down". |
359
|
|
|
|
|
|
|
Beacuse of the use of Braille characters, up/down scrolling is 4 pixels |
360
|
|
|
|
|
|
|
at a time, whereas left right scrolling is 2 pixels at a time. If |
361
|
|
|
|
|
|
|
C<$wrap> is a true value, the screen wraps around. |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
=cut |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub scroll{ |
366
|
0
|
|
|
0
|
1
|
|
my ($self,$direction,$wrap,$numberOfChar)=@_; |
367
|
0
|
|
0
|
|
|
|
$numberOfChar//=1;$wrap//=0; |
|
0
|
|
0
|
|
|
|
|
368
|
0
|
|
|
|
|
|
for($direction){ |
369
|
0
|
0
|
|
|
|
|
/^r/i && do{ |
370
|
0
|
|
|
|
|
|
foreach my $row (0..$#{$self->{grid}}){ |
|
0
|
|
|
|
|
|
|
371
|
0
|
|
|
|
|
|
my @r=@{$self->{grid}->[$row]}; |
|
0
|
|
|
|
|
|
|
372
|
0
|
|
|
|
|
|
my $end=pop(@r); |
373
|
0
|
0
|
|
|
|
|
unshift(@r,$wrap?$end:'⠀'); |
374
|
0
|
|
|
|
|
|
$self->{grid}->[$row]=\@r; |
375
|
|
|
|
|
|
|
} |
376
|
0
|
|
|
|
|
|
last; |
377
|
|
|
|
|
|
|
}; |
378
|
|
|
|
|
|
|
|
379
|
0
|
0
|
|
|
|
|
/^l/i && do{ |
380
|
0
|
|
|
|
|
|
foreach my $row (0..$#{$self->{grid}}){ |
|
0
|
|
|
|
|
|
|
381
|
0
|
|
|
|
|
|
my @r=@{$self->{grid}->[$row]}; |
|
0
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
my $end=shift(@r); |
383
|
0
|
0
|
|
|
|
|
push(@r,$wrap?$end:'⠀'); |
384
|
0
|
|
|
|
|
|
$self->{grid}->[$row]=\@r; |
385
|
|
|
|
|
|
|
} |
386
|
0
|
|
|
|
|
|
last; |
387
|
|
|
|
|
|
|
}; |
388
|
|
|
|
|
|
|
|
389
|
0
|
0
|
|
|
|
|
/^d/i && do{ |
390
|
0
|
|
|
|
|
|
my @r=@{$self->{grid}}; |
|
0
|
|
|
|
|
|
|
391
|
0
|
|
|
|
|
|
my $end=shift(@r); |
392
|
0
|
0
|
|
|
|
|
push(@r,$wrap?$end:[('⠀')x ($self->{width}/2+($self->{width}%2?1:0))]); |
|
|
0
|
|
|
|
|
|
393
|
0
|
|
|
|
|
|
$self->{grid}=\@r; |
394
|
0
|
|
|
|
|
|
last; |
395
|
|
|
|
|
|
|
}; |
396
|
|
|
|
|
|
|
|
397
|
0
|
0
|
|
|
|
|
/^u/i && do{ |
398
|
0
|
|
|
|
|
|
my @r=@{$self->{grid}}; |
|
0
|
|
|
|
|
|
|
399
|
0
|
|
|
|
|
|
my $end=pop(@r); |
400
|
0
|
0
|
|
|
|
|
unshift(@r,$wrap?$end:[('⠀')x ($self->{width}/2+($self->{width}%2?1:0))]); |
|
|
0
|
|
|
|
|
|
401
|
0
|
|
|
|
|
|
$self->{grid}=\@r; |
402
|
0
|
|
|
|
|
|
last; |
403
|
|
|
|
|
|
|
}; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
} |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
=head3 C<$canvas-EblockBlit($block,$gridX, $gridY)> |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
Allows blitting a 2d arrays to a grid location in the canvas. Useful for |
411
|
|
|
|
|
|
|
printing using a Graille font. |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
=cut |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
sub blockBlit{ # needs protection |
416
|
0
|
|
|
0
|
1
|
|
my ($self, $blk, $gridX, $gridY)=@_; |
417
|
0
|
|
|
|
|
|
for my $x(0..$#{$blk->[0]}){ |
|
0
|
|
|
|
|
|
|
418
|
0
|
|
|
|
|
|
for my $y(0..$#$blk){ |
419
|
0
|
|
|
|
|
|
$self->{grid}->[$gridY+$y]->[$gridX+$x]=$blk->[$#$blk-$y]->[$x] |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
=head3 C<$canvas-EexportCanvas($filename)> , C<$canvas-EimportCanvas($filename,[$toBuffer])> |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
This allows the loading and unloading of a canvas from a file. There is |
428
|
|
|
|
|
|
|
no checking of the dimension of the canvas being imported at the moment. |
429
|
|
|
|
|
|
|
Import can be direct to the canvas, the function return the loaded data |
430
|
|
|
|
|
|
|
as an ArrayRef, the optional c<$toBuffer> parameter, if true prevents |
431
|
|
|
|
|
|
|
loading the data onto the canvas. |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=cut |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
sub exportCanvas{ |
437
|
0
|
|
|
0
|
1
|
|
my ($self,$file)=@_; |
438
|
0
|
0
|
|
|
|
|
open (my $fh, ">$file") or die "can not open $file for writing $!"; |
439
|
0
|
|
|
|
|
|
binmode($fh, ":utf8"); |
440
|
0
|
|
|
|
|
|
print $fh $self->as_string(); |
441
|
0
|
|
|
|
|
|
close $fh; |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
sub importCanvas{ |
446
|
0
|
|
|
0
|
1
|
|
my ($self,$file,$toBuffer)=@_; |
447
|
0
|
0
|
|
|
|
|
open (my $fh,'<', $file) or die "can not open $file for reading $!"; |
448
|
0
|
|
|
|
|
|
my @grd; |
449
|
0
|
|
|
|
|
|
while (<$fh>){ |
450
|
0
|
0
|
|
|
|
|
last if (@grd > ($self->{height}/4)); # stop if too big for canvas |
451
|
|
|
|
|
|
|
# extend if too narrow for canvas, trucate if too wide |
452
|
0
|
|
|
|
|
|
unshift @grd, [split(//,substr($_.('⠀')x($self->{width}/2),0,$self->{width}/2))]; |
453
|
|
|
|
|
|
|
} |
454
|
0
|
|
|
|
|
|
close $fh; |
455
|
0
|
0
|
|
|
|
|
$self->{grid}=[@grd] unless $toBuffer; |
456
|
0
|
|
|
|
|
|
return [@grd]; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=head3 C<$canvas-EtextAt($x,$y,$text,$fmt)> |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
Printing text on the C<$canvas>. This is different fromthe exported |
462
|
|
|
|
|
|
|
C function. the characters are printed on the C<$canvas> |
463
|
|
|
|
|
|
|
and may be scrolled with the canvas and will overwrite or be over written |
464
|
|
|
|
|
|
|
othe $canvas drawing actions. The optional C<$fmt> allows the setting of colour; |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
=cut |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
sub textAt{ |
470
|
0
|
|
|
0
|
1
|
|
my ($self,$x,$y,$text,$fmt)=@_; |
471
|
0
|
|
|
|
|
|
my ($chrX,$chrY,$xOffset,$yOffset)=charOffset($self,$x,$y); |
472
|
0
|
0
|
|
|
|
|
if ($chrX!=-1){ |
473
|
0
|
|
|
|
|
|
my @chrs=split(//,$text); |
474
|
0
|
|
|
|
|
|
my $lastChar=$chrX+(length $text)-1; |
475
|
0
|
0
|
|
|
|
|
$lastChar = $self->{width}/2 if ($lastChar>$self->{width}/2); |
476
|
0
|
0
|
|
|
|
|
$chrs[0]=colour($fmt).$chrs[0] if $fmt; |
477
|
0
|
|
|
|
|
|
for my $tc ($chrX..$lastChar){ |
478
|
0
|
|
|
|
|
|
$self->{grid}->[$chrY]->[$tc]=shift @chrs; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
} |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
sub resetUnpainted{ |
485
|
0
|
|
|
0
|
0
|
|
my ($self,$chX,$chY)=@_; |
486
|
0
|
0
|
0
|
|
|
|
return if (($chX>$self->{width}/2)||($chX<0)||($chY>$self->{height}/4)||($chY<0)); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
487
|
0
|
0
|
|
|
|
|
$self->{grid}->[$chY]->[$chX]=colour("reset").$self->{grid}->[$chY]->[$chX] if (length $self->{grid}->[$chY]->[$chX] ==1); |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
sub stripColour{ |
491
|
0
|
|
|
0
|
0
|
|
my $ch=shift; |
492
|
0
|
|
|
|
|
|
return chop $ch; |
493
|
|
|
|
|
|
|
} |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
sub axis{ |
496
|
0
|
|
|
0
|
0
|
|
my ($self,$xOrigin,$yOrigin,$xPos,$xNeg,$yPos,$yNeg,$xTics,$yTics)=@_; |
497
|
0
|
|
|
|
|
|
for my $y ($yOrigin-$yNeg..$yOrigin+$yPos) { |
498
|
0
|
|
|
|
|
|
$self->textAt($xOrigin,$y,'│'); |
499
|
|
|
|
|
|
|
} |
500
|
0
|
|
|
|
|
|
$self->textAt($xOrigin-$xNeg,$yOrigin,'─' x (($xNeg+$xPos)/2)); |
501
|
0
|
|
|
|
|
|
$self->textAt($xOrigin,$yOrigin,'┼' ); |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
sub axis2{ |
507
|
0
|
|
|
0
|
0
|
|
my ($self,$xOrigin,$yOrigin,$xPos,$xNeg,$yPos,$yNeg,$xTics,$yTics)=@_; |
508
|
0
|
|
|
|
|
|
$self->line($xOrigin-$xNeg,$yOrigin,$xOrigin+$xPos,$yOrigin); |
509
|
0
|
|
|
|
|
|
$self->line($xOrigin,$yOrigin-$yNeg,$xOrigin,$yOrigin+$yPos); |
510
|
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
=head2 Enhancements |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
=head3 C<$canvas-Elogo($script)> |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
Interface to Graille's built in Turtle interpreter. |
518
|
|
|
|
|
|
|
A string is taken and split by senicolons or newlines into instructions. |
519
|
|
|
|
|
|
|
The instructions are trimmed, and split by the first space character into |
520
|
|
|
|
|
|
|
command and parameters. Very simple in other words. No syntax checking |
521
|
|
|
|
|
|
|
is done |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
C<"fd distance"> pen moves forward a certain distance. |
524
|
|
|
|
|
|
|
C<"lt angle">, C<"rt angle"> turns left or right. |
525
|
|
|
|
|
|
|
C<"bk distance"> pen moves back a certain distance. |
526
|
|
|
|
|
|
|
C<"pu">, C<"pd"> Pen is up or down, up means no drawing takes place, |
527
|
|
|
|
|
|
|
and down means the turtle draws as it moves. |
528
|
|
|
|
|
|
|
C<"dir"> set the direction at a specific angle in dgrees, |
529
|
|
|
|
|
|
|
with 0 being directly left. |
530
|
|
|
|
|
|
|
C<"mv">moves pen to specific coordinates without drawing. |
531
|
|
|
|
|
|
|
C<"ce"> centers the turtle in the middle of a canvas |
532
|
|
|
|
|
|
|
C<"sp"> allows animated drawing by specifiying the the number |
533
|
|
|
|
|
|
|
of centiseconds between instructions |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=cut |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
sub logo{ |
538
|
0
|
|
|
0
|
1
|
|
my ($self,$script)=@_; |
539
|
0
|
|
|
|
|
|
my @commands= map{s/^\s+|\s+$//g; $_}split(/[\n;]+/,$script); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
540
|
0
|
|
|
|
|
|
foreach my $instr (@commands){ |
541
|
0
|
0
|
|
|
|
|
next unless $instr; |
542
|
0
|
0
|
|
|
|
|
next if ($instr=~/#/); |
543
|
0
|
|
|
|
|
|
my ($c,$p)=split(/[\s]+/,$instr,2); |
544
|
0
|
0
|
|
|
|
|
my @pars=split(/,/,$p) if $p; |
545
|
0
|
|
|
|
|
|
for ($c){ |
546
|
0
|
0
|
|
|
|
|
/^(fd|forward)/ && do{ |
547
|
0
|
0
|
0
|
|
|
|
last unless ($pars[0] && (0+$pars[0])); |
548
|
0
|
|
|
|
|
|
my $x=$self->{logoVars}->{x}+$pars[0]*cos(degToRad($self->{logoVars}->{d})); |
549
|
0
|
|
|
|
|
|
my $y=$self->{logoVars}->{y}+$pars[0]*sin(degToRad($self->{logoVars}->{d})); |
550
|
0
|
0
|
|
|
|
|
if ($self->{logoVars}->{p}){ |
551
|
0
|
|
0
|
|
|
|
$self->line($self->{logoVars}->{x},$self->{logoVars}->{y},$x,$y,($self->{logoVars}->{c}//1)) |
552
|
|
|
|
|
|
|
} |
553
|
0
|
|
|
|
|
|
$self->{logoVars}->{y}=$y; |
554
|
0
|
|
|
|
|
|
$self->{logoVars}->{x}=$x; |
555
|
0
|
|
|
|
|
|
last; |
556
|
|
|
|
|
|
|
}; |
557
|
0
|
0
|
|
|
|
|
/^(lt|left)/ && do{ |
558
|
0
|
0
|
0
|
|
|
|
last unless ($pars[0] && (0+$pars[0])); |
559
|
0
|
|
|
|
|
|
$self->{logoVars}->{d}+=$pars[0]; |
560
|
0
|
|
|
|
|
|
$self->{logoVars}->{d}-=360 while($self->{logoVars}->{d}>360); |
561
|
0
|
|
|
|
|
|
last; |
562
|
|
|
|
|
|
|
}; |
563
|
0
|
0
|
|
|
|
|
/^(rt|right)/ && do{ |
564
|
0
|
0
|
0
|
|
|
|
last unless ($pars[0] && (0+$pars[0])); |
565
|
0
|
|
|
|
|
|
$self->{logoVars}->{d}-=$pars[0]; |
566
|
0
|
|
|
|
|
|
$self->{logoVars}->{d}+=360 while($self->{logoVars}->{d}<360); |
567
|
0
|
|
|
|
|
|
last; |
568
|
|
|
|
|
|
|
}; |
569
|
0
|
0
|
|
|
|
|
/^(bk|back)/ && do{ |
570
|
0
|
|
|
|
|
|
$pars[0]=-$pars[0]; |
571
|
0
|
|
|
|
|
|
$_="fd"; |
572
|
0
|
|
|
|
|
|
redo; |
573
|
|
|
|
|
|
|
}; |
574
|
0
|
0
|
|
|
|
|
/^pu/ && do{ |
575
|
0
|
|
|
|
|
|
$self->{logoVars}->{p}=0; |
576
|
0
|
|
|
|
|
|
last; |
577
|
|
|
|
|
|
|
}; |
578
|
0
|
0
|
|
|
|
|
/^pd/ && do{ |
579
|
0
|
|
|
|
|
|
$self->{logoVars}->{p}=1; |
580
|
0
|
|
|
|
|
|
last; |
581
|
|
|
|
|
|
|
}; |
582
|
0
|
0
|
|
|
|
|
/^pc/ && do{ |
583
|
0
|
|
|
|
|
|
$self->{logoVars}->{c}=colour($pars[0]); |
584
|
0
|
|
|
|
|
|
last; |
585
|
|
|
|
|
|
|
}; |
586
|
0
|
0
|
|
|
|
|
/^dir/ && do{ |
587
|
0
|
|
|
|
|
|
$self->{logoVars}->{d}=$pars[0]; |
588
|
0
|
|
|
|
|
|
last; |
589
|
|
|
|
|
|
|
}; |
590
|
0
|
0
|
|
|
|
|
/^mv/ && do{ |
591
|
0
|
|
|
|
|
|
$self->{logoVars}->{x}=$pars[0]; |
592
|
0
|
|
|
|
|
|
$self->{logoVars}->{y}=$pars[1 ]; |
593
|
0
|
|
|
|
|
|
last; |
594
|
|
|
|
|
|
|
}; |
595
|
0
|
0
|
|
|
|
|
/^ce/ && do{ |
596
|
0
|
|
|
|
|
|
$self->{logoVars}->{x}=$self->{width}/2; |
597
|
0
|
|
|
|
|
|
$self->{logoVars}->{y}=$self->{height}/2; |
598
|
0
|
|
|
|
|
|
last; |
599
|
|
|
|
|
|
|
}; |
600
|
0
|
0
|
|
|
|
|
/^sp/ && do{ |
601
|
0
|
|
|
|
|
|
$self->{logoVars}->{sp}=$pars[0]; |
602
|
|
|
|
|
|
|
last |
603
|
0
|
|
|
|
|
|
} |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
} |
607
|
0
|
0
|
|
|
|
|
if (defined $self->{logoVars}->{sp}){ |
608
|
0
|
|
|
|
|
|
sleep $self->{logoVars}->{sp}/100; |
609
|
0
|
|
|
|
|
|
$self->draw(); |
610
|
|
|
|
|
|
|
} |
611
|
|
|
|
|
|
|
} |
612
|
|
|
|
|
|
|
} |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head3 Exported Routines |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
Graille exports some functions for additional console graphical manipulation |
618
|
|
|
|
|
|
|
This includes drawing of borders, printing characters at specific locations |
619
|
|
|
|
|
|
|
in the terminal window, and colouring the characters, clearing the screen, etc. |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
printAt($row,$column,@textRows); |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
Prints text sent as a scalar, a list of scalars or a reference to list |
624
|
|
|
|
|
|
|
of scalars, at a specific location on the screen. Lists are printed |
625
|
|
|
|
|
|
|
from the same column but increasing row positions. |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
border($top,$left,$bottom,$right,$style,$colour); |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
Draws a border box. |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
paint($txt,$fmt) |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
Paints text the colour and background specified, $text may be a string, or ref |
634
|
|
|
|
|
|
|
to a list of strings. This is combined with C abouve |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
clearScreen() |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
Guess what? clears the enire screen. This is different from C<$canvas-Eclear()> |
639
|
|
|
|
|
|
|
which clears the Graille canvas. |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
block2braille($block) |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
Given a block of binary data (a 2D Array ref of 8-bit data), return a |
645
|
|
|
|
|
|
|
corresponding 2d Array ref of braille blocks. This is handy to convert, |
646
|
|
|
|
|
|
|
say, binary font data tinto Braille blocks for blittting into the canvas; |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
pixelAt($block,$px,$py) |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
Given a binary block of data e.g. a font or a sprite offered as a 2D Array ref |
651
|
|
|
|
|
|
|
find the pixel value at a certain coordinate in that block. |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
=cut |
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
our %borders=( |
658
|
|
|
|
|
|
|
simple=>{tl=>"+", t=>"-", tr=>"+", l=>"|", r=>"|", bl=>"+", b=>"-", br=>"+",ts=>"|",te=>"|",}, |
659
|
|
|
|
|
|
|
double=>{tl=>"╔", t=>"═", tr=>"╗", l=>"║", r=>"║", bl=>"╚", b=>"═", br=>"╝",ts=>"╣",te=>"╠",}, |
660
|
|
|
|
|
|
|
shadow=>{tl=>"┌", t=>"─", tr=>"╖", l=>"│", r=>"║", bl=>"╘", b=>"═", br=>"╝",ts=>"┨",te=>"┠",}, |
661
|
|
|
|
|
|
|
thin =>{tl=>"┌", t=>"─", tr=>"┐", l=>"│", r=>"│", bl=>"└", b=>"─", br=>"┘",ts=>"┤",te=>"├",}, |
662
|
|
|
|
|
|
|
thick =>{tl=>"┏", t=>"━", tr=>"┓", l=>"┃", r=>"┃", bl=>"┗", b=>"━", br=>"┛",ts=>"┫",te=>"┣",}, |
663
|
|
|
|
|
|
|
); |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
our %colours=(black =>30,red =>31,green =>32,yellow =>33,blue =>34,magenta =>35,cyan =>36,white =>37, |
666
|
|
|
|
|
|
|
on_black=>40,on_red=>41,on_green=>42,on_yellow=>43,on_blue=>44,on_magenta=>4,on_cyan=>46,on_white=>47, |
667
|
|
|
|
|
|
|
reset=>0, bold=>1, italic=>3, underline=>4, strikethrough=>9,); |
668
|
|
|
|
|
|
|
sub printAt{ |
669
|
0
|
|
|
0
|
0
|
|
my ($row,$column,@textRows)=@_; |
670
|
0
|
0
|
|
|
|
|
@textRows = @{$textRows[0]} if ref $textRows[0]; |
|
0
|
|
|
|
|
|
|
671
|
0
|
|
|
|
|
|
my $blit="\033[?25l"; |
672
|
0
|
0
|
|
|
|
|
$blit.= defined $_?("\033[".$row++.";".$column."H".(ref $_?join("",@$_):$_)):"" foreach (@textRows) ; |
|
|
0
|
|
|
|
|
|
673
|
0
|
|
|
|
|
|
print $blit; |
674
|
0
|
|
|
|
|
|
print "\n"; # seems to flush the STDOUT buffer...if not then set $| to 1 |
675
|
|
|
|
|
|
|
}; |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
sub cursorAt{ |
678
|
0
|
|
|
0
|
0
|
|
my ($r,$c)=@_; |
679
|
0
|
|
|
|
|
|
return "\033[?25l\033[".$r.";".$c."H"; |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
} |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
sub border{ |
685
|
0
|
|
|
0
|
0
|
|
my ($top,$left,$bottom,$right,$style,$colour,$title,$titleColour)=@_; |
686
|
0
|
|
0
|
|
|
|
$style//="simple"; |
687
|
0
|
0
|
|
|
|
|
return unless exists $borders{$style}; |
688
|
0
|
|
|
|
|
|
my @box=(colour($colour).$borders{$style}{tl}.($borders{$style}{t}x($right-$left)).$borders{$style}{tr}); |
689
|
0
|
0
|
|
|
|
|
if ($title){ |
690
|
0
|
|
|
|
|
|
my $titleSize=4+length $title; |
691
|
0
|
|
0
|
|
|
|
$title=$borders{$style}{ts}.colour($titleColour||"reset")." ".$title.colour($colour)." ".$borders{$style}{te}; |
692
|
0
|
|
|
|
|
|
substr ($box[0],6,$titleSize)=$title; |
693
|
|
|
|
|
|
|
}; |
694
|
0
|
|
|
|
|
|
push @box,($borders{$style}{l}.(" "x($right-$left)).$borders{$style}{r})x($bottom-$top);; |
695
|
0
|
|
|
|
|
|
push @box,($borders{$style}{bl}.($borders{$style}{b}x($right-$left)).$borders{$style}{br}.colour("reset")); |
696
|
0
|
|
|
|
|
|
printAt($top,$left,\@box); |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
} |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
sub paint{ |
701
|
0
|
|
|
0
|
0
|
|
my ($txt,$fmt)=@_; |
702
|
0
|
0
|
|
|
|
|
return $txt unless $fmt; |
703
|
0
|
0
|
|
|
|
|
return colour($fmt).$txt.colour("reset") unless ref $txt; |
704
|
0
|
|
|
|
|
|
return [map {colour($fmt).$_.colour("reset");} @$txt] |
|
0
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
} |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
sub clearScreen{ |
708
|
0
|
0
|
|
0
|
0
|
|
system($^O eq 'MSWin32'?'cls':'clear'); |
709
|
|
|
|
|
|
|
} |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
sub colour{ |
712
|
0
|
|
|
0
|
0
|
|
my ($fmts)=@_; |
713
|
0
|
0
|
|
|
|
|
return "" unless $fmts; |
714
|
0
|
|
|
|
|
|
my @formats=map {lc $_} split / +/,$fmts; |
|
0
|
|
|
|
|
|
|
715
|
0
|
0
|
|
|
|
|
return join "",map {defined $colours{$_}?"\033[$colours{$_}m":""} @formats; |
|
0
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
} |
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
# given an 8 bit block of data, produce a braille block |
720
|
|
|
|
|
|
|
sub block2braille{ |
721
|
1
|
|
|
1
|
|
3793
|
use integer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
722
|
0
|
|
|
0
|
0
|
|
my ($block)=@_; |
723
|
0
|
|
|
|
|
|
my $pixelHeight=@$block; |
724
|
0
|
|
|
|
|
|
my $pixelWidth=@{$block->[0]}*8; |
|
0
|
|
|
|
|
|
|
725
|
0
|
0
|
|
|
|
|
my $brCharWidth=$pixelWidth/2 +($pixelWidth & 1?1:0); |
726
|
0
|
0
|
|
|
|
|
my $brCharHeight=$pixelHeight/4 + ($pixelHeight & 1?1:0); |
727
|
0
|
|
|
|
|
|
my $brBlk=[]; |
728
|
|
|
|
|
|
|
|
729
|
0
|
|
|
|
|
|
foreach my $chX(0..($brCharWidth-1)){ |
730
|
0
|
|
|
|
|
|
foreach my $chY(0..($brCharHeight-1)){ |
731
|
0
|
|
|
|
|
|
my $b=ord('⠀'); |
732
|
0
|
0
|
|
|
|
|
$b|=ord('⠁') if (pixelAt($block,$chX*2,$chY*4)); |
733
|
0
|
0
|
|
|
|
|
$b|=ord('⠂') if (pixelAt($block,$chX*2,$chY*4+1)); |
734
|
0
|
0
|
|
|
|
|
$b|=ord('⠄') if (pixelAt($block,$chX*2,$chY*4+2)); |
735
|
0
|
0
|
|
|
|
|
$b|=ord('⡀') if (pixelAt($block,$chX*2,$chY*4+3)); |
736
|
0
|
0
|
|
|
|
|
$b|=ord('⠈') if (pixelAt($block,$chX*2+1,$chY*4)); |
737
|
0
|
0
|
|
|
|
|
$b|=ord('⠐') if (pixelAt($block,$chX*2+1,$chY*4+1)); |
738
|
0
|
0
|
|
|
|
|
$b|=ord('⠠') if (pixelAt($block,$chX*2+1,$chY*4+2)); |
739
|
0
|
0
|
|
|
|
|
$b|=ord('⢀') if (pixelAt($block,$chX*2+1,$chY*4+3)); |
740
|
0
|
|
|
|
|
|
$brBlk->[$chY]->[$chX]=chr($b); |
741
|
|
|
|
|
|
|
} |
742
|
|
|
|
|
|
|
} |
743
|
0
|
|
|
|
|
|
return $brBlk; |
744
|
|
|
|
|
|
|
} |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
# given a block of binary data identify pixel value at a |
747
|
|
|
|
|
|
|
# particular position |
748
|
|
|
|
|
|
|
sub pixelAt{ |
749
|
1
|
|
|
1
|
|
301
|
use integer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
750
|
0
|
|
|
0
|
0
|
|
my ($blk,$px,$py)=@_; |
751
|
0
|
|
|
|
|
|
return (($blk->[$py]->[$px/8]) & 2**(7-($px%8))); |
752
|
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
sub DESTROY{ |
756
|
0
|
0
|
|
0
|
|
|
if ($^O eq "MSWin32") { |
757
|
0
|
|
|
|
|
|
system("chcp 850"); |
758
|
|
|
|
|
|
|
} |
759
|
|
|
|
|
|
|
} |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
1; |
762
|
|
|
|
|
|
|
__END__ |