1=pod
2
3=head1 NAME
4
5examples/triangle.pl - Escher's Impossible Triangle
6
7=head1 FEATURES
8
9Demonstrates the basic usage of the Prima toolkit.
10
11=cut
12
13use strict;
14use warnings;
15use Prima qw(Buttons);
16use constant PI => 4.0 * atan2 1, 1;
17use constant D2R => PI / 180;
18use constant Cos_120 => cos(D2R*(-120));
19use constant Sin_120 => sin(D2R*(-120));
20
21use Prima::Application name => "Escher's Impossible Triangle";
22
23sub recalc_coordinates
24{
25	my $me = $_[0];
26	$me-> {angle} = 0 unless exists $me-> {angle};
27	delete $me-> {line1};
28	delete $me-> {line2};
29	delete $me-> {line3};
30	my ($w, $h) = $me-> size;
31	my $sz = ($w > $h ? $h : $w) * 0.8 * 0.5;
32	my ($x, $y) = (25*$sz/34,$sz);
33	my ($fx, $fy) = ($x,$y);
34	($w,$h) = ($w/2,$h/2);
35	my $angle = $me-> {angle};
36	for (1..3)
37	{
38		my $cos = cos(D2R*$angle);
39		my $sin = sin(D2R*$angle);
40		my @lines = ();
41		($x,$y) = ($fx, $fy);
42		push @lines, $cos*$x-$sin*$y+$w, $sin*$x+$cos*$y+$h;
43		$y = -$y;
44		push @lines, $cos*$x-$sin*$y+$w, $sin*$x+$cos*$y+$h;
45		# rotate -120
46		$x = Cos_120*$fx - Sin_120*$fy;
47		$y = Sin_120*$fx + Cos_120*$fy;
48		push @lines, $cos*$x-$sin*$y+$w, $sin*$x+$cos*$y+$h;
49		$y = -$y/2;
50		push @lines, $cos*$x-$sin*$y+$w, $sin*$x+$cos*$y+$h;
51		($x, $y) = ($x-sqrt(3)*$y,0);
52		push @lines, $cos*$x-$sin*$y+$w, $sin*$x+$cos*$y+$h;
53		$me-> {'line' . $_} = [@lines];
54		$angle += 120;
55	}
56}
57
58
59my $w = Prima::MainWindow-> create(
60#my $w = Window-> create(
61	text   => $::application-> name,
62	left   => 200,
63	bottom => 300,
64	width  => 400,
65	height => 250,
66	borderIcons => 0,
67	windowState => ws::Maximized,
68	borderStyle => bs::None,
69	backColor => cl::Black,
70	hiliteColor => cl::LightCyan,
71	lineWidth => 2,
72#   onSize    => \&recalc_coordinates,
73	onCreate  => sub {
74		$_[0]-> {r} = $_[0]-> {g} = $_[0]-> {b} = $_[0]-> {c} = 0;
75		my $t = $_[0]-> insert( Timer => timeout => 10, name => 'Timer', onTick => sub {
76			my $me = $_[0]-> owner;
77			my $back = $me-> backColor;
78#       		my $fore = $me-> hiliteColor;
79			$me-> {c} = 255 if $me-> {c} >= 1785;
80			$me-> {c}++;
81			if  ( $me-> {c}   <=  255) { $me-> {r}++; $me-> {g}++; $me-> {b}++; }
82			elsif ( $me-> {c} <=  510) { $me-> {g}--; $me-> {b}--; }
83			elsif ( $me-> {c} <=  765) { $me-> {g}++; }
84			elsif ( $me-> {c} <= 1020) { $me-> {r}--; }
85			elsif ( $me-> {c} <= 1275) { $me-> {b}++; }
86			elsif ( $me-> {c} <= 1530) { $me-> {g}--; }
87			else  { $me-> {g}++; $me-> {r}++;}
88			$me-> {c} = 255 if $me-> {c} >= 1785;
89			$me-> {c}++;
90			if  ( $me-> {c}   <=  255) { $me-> {r}++; $me-> {g}++; $me-> {b}++; }
91			elsif ( $me-> {c} <=  510) { $me-> {g}--; $me-> {b}--; }
92			elsif ( $me-> {c} <=  765) { $me-> {g}++; }
93			elsif ( $me-> {c} <= 1020) { $me-> {r}--; }
94			elsif ( $me-> {c} <= 1275) { $me-> {b}++; }
95			elsif ( $me-> {c} <= 1530) { $me-> {g}--; }
96			else  { $me-> {g}++; $me-> {r}++;}
97			$me-> {c} = 255 if $me-> {c} >= 1785;
98			$me-> {c}++;
99			if  ( $me-> {c}   <=  255) { $me-> {r}++; $me-> {g}++; $me-> {b}++; }
100			elsif ( $me-> {c} <=  510) { $me-> {g}--; $me-> {b}--; }
101			elsif ( $me-> {c} <=  765) { $me-> {g}++; }
102			elsif ( $me-> {c} <= 1020) { $me-> {r}--; }
103			elsif ( $me-> {c} <= 1275) { $me-> {b}++; }
104			elsif ( $me-> {c} <= 1530) { $me-> {g}--; }
105			else  { $me-> {g}++; $me-> {r}++;}
106			my $fore = $me-> {b} | ($me-> {g}<<8) | ($me-> {r}<<16);
107			my $l1 = $me-> {line1};
108			my $l2 = $me-> {line2};
109			my $l3 = $me-> {line3};
110#			($l1,$l2,$l3) = ();
111			$me-> {angle} += 1;
112			$me-> {angle} -= 360 if $me-> {angle} > 360;
113			&recalc_coordinates($me);
114			$me-> begin_paint;
115			#$me-> color( $back);
116			#$me-> polyline( $l1) if defined $l1;
117			$me-> color( $fore);
118			$me-> bar(0,0,30,30);
119			$me-> polyline( $me-> {line1}) if exists $me-> {line1};
120			$me-> color( $back);
121			$me-> polyline( $l2) if defined $l2;
122			$me-> color( $fore);
123			$me-> polyline( $me-> {line2}) if exists $me-> {line2};
124			#$me-> fillpoly( $me-> {line2}) if exists $me-> {line2};
125			#$me-> color( $back);
126			#$me-> polyline( $l3) if defined $l3;
127			$me-> color( $fore);
128			$me-> polyline( $me-> {line1}) if exists $me-> {line1};
129			$me-> polyline( $me-> {line2}) if exists $me-> {line2};
130			$me-> polyline( $me-> {line3}) if exists $me-> {line3};
131			$me-> end_paint;
132		});
133		$t-> start;
134		&recalc_coordinates($_[0]);
135	},
136	onPaint   => sub
137	{
138		my ($me,$canvas) = @_;
139		$canvas-> color( $me-> backColor);
140		$canvas-> bar( 0, 0, $canvas-> size);
141	},
142);
143
144$w-> insert( Button =>
145	text => "Install PRIMA now!",
146	width => $w-> get_text_width( "Install PRIMA now!") * 1.2,
147	# centered => 1,
148	growMode => gm::Center,
149	onClick => sub { $::application-> close},
150);
151
152run Prima;
153