Skip to content

Commit

Permalink
[examples] update clock and sierpinski with CLR interop improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mberends committed Sep 24, 2011
1 parent 3896bb2 commit 9fccf74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
33 changes: 15 additions & 18 deletions examples/gtk-clock.pl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Main documentation: http://docs.go-mono.com, particularly Gnome
# (for Gdk and Gtk) and Mono (for Cairo) libraries.
# Main documentation: http://docs.go-mono.com, particularly
# Gnome (for Gdk and Gtk) and Mono (for Cairo) libraries.
# See also: The X-Windows Disaster at http://www.art.net/~hopkins/Don/unix-haters/handbook.html

constant $GTK = "gtk-sharp,Version=2.12.0.0,Culture=neutral,PublicKeyToken=35e10195dab3c99f";
constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GLIB = "glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
# use 'gacutil -l' to look up similar module details

Expand All @@ -21,25 +21,25 @@
$drawingarea.add_ExposeEvent: sub ($obj, $args) {
$args; # suppress 'declared but not used' warnings
my $cc = GdkCairoHelper.Create($obj.GdkWindow); # Cairo Context
my $windowX; my $windowY; my $windowWidth; my $windowHeight; my $windowDepth;
# TODO: the following line seems to pass parameters by value, it needs to pass by reference to integers
# TODO: the following two lines pass parameters by value, need to pass by references to integers
# my $windowX; my $windowY; my $windowWidth; my $windowHeight; my $windowDepth;
# $obj.GdkWindow.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
# Tracked as https://github.com/sorear/niecza/issues/57
$obj.GdkWindow.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
# TODO: remove the following cheat that works around the above problem
$windowWidth = $windowSizeX; $windowHeight = $windowSizeY;
my $xCenter = $windowWidth / 2; my $yCenter = $windowHeight / 2;
my $maxRadius = ($windowWidth min $windowHeight)/2;
$cc.SetSourceRGB(0, 1, 1); $cc.Paint(); # Make a cyan background
# TODO: remove the following one line cheat that works around the above problem
my $windowWidth = $windowSizeX; my $windowHeight = $windowSizeY;
$cc.SetSourceRGB(0.95.Num, 0.90.Num, 0.85.Num); $cc.Paint; # background color
ClockFace($cc, $windowWidth, $windowHeight);
# Calculate the parameters needed to draw the clock
my $hour = ((now.to-posix[0] % (12*3600))/3600); # there must be a more elegant way...
my $minute = ((now.to-posix[0] / 60) % 60);
my $maxRadius = ($windowWidth min $windowHeight)/2;
my $linewidth = ($maxRadius * 0.03).Int max 1;
my $radiusHour = $maxRadius * 0.5;
my $radiusMinute = $maxRadius * 0.7;
# Draw the hands of the clock
my $radiansHour = $hour / 12 * pi * 2;
my $radiansMinute = $minute / 60 * pi * 2;
my $xCenter = $windowWidth / 2; my $yCenter = $windowHeight / 2;
my $xHour = $xCenter + $radiusHour * sin($radiansHour);
my $yHour = $yCenter + $radiusHour * sin($radiansHour - pi / 2);
my $xMinute = $xCenter + $radiusMinute * sin($radiansMinute);
Expand All @@ -51,10 +51,7 @@
$cc.Arc($xCenter.Int,$yCenter.Int,$linewidth.Int,0,7); # from 0 radians to more than 2*pi
$cc.Stroke;
$cc.Target.Dispose;
# TODO: Currently the following error appears at exit:
# Cairo.Context: called from finalization thread, programmer is missing a call to Dispose
# The following line should fix the above, but gives this error: Unable to resolve method Dispose
# $cc.Dispose(); # Should do: ((IDisposable) cc).Dispose();
$cc.dispose-hack; # Should be $cc.Dispose but CLR interop cannot call that
# Tracked as https://github.com/sorear/niecza/issues/56
};
GLibTimeout.Add: 60000, sub () { # Update once per minute
Expand All @@ -77,13 +74,13 @@ ($cc, $width, $height)
my $centerY = $height / 2;
my $lineWidth = ($maxRadius * 0.025).Int max 1;
# Draw the outer circle
$cc.SetSourceRGB(0, 0, 0); # Black
$cc.SetSourceRGB(0.7.Num, 0, 0); # dark red
$cc.LineWidth = $lineWidth;
$cc.Arc($centerX.Int, $centerY.Int, ($maxRadius - $lineWidth).Int, 0, 7);
$cc.Stroke();
# Write the clock name on its face
my $clockName = "it's Niecza time!";
$cc.SetSourceRGB(0, 0, 0); # Black
$cc.SetSourceRGB(0.7.Num, 0, 0); # dark red
$cc.SetFontSize(($maxRadius * 0.15).Int);
my $textWidth = $cc.TextExtents($clockName).Width;
$cc.MoveTo(($width/2 - $textWidth/2).Int, ($height * 0.85).Int);
Expand Down
30 changes: 14 additions & 16 deletions examples/gtk-sierpinski.pl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Main documentation: http://docs.go-mono.com, particularly Gnome
# (for Gdk and Gtk) and Mono (for Cairo) libraries.
# Main documentation: http://docs.go-mono.com, particularly
# Gnome (for Gdk and Gtk) and Mono (for Cairo) libraries.
# See also: The X-Windows Disaster at http://www.art.net/~hopkins/Don/unix-haters/handbook.html

constant $GTK = "gtk-sharp,Version=2.12.0.0,Culture=neutral,PublicKeyToken=35e10195dab3c99f";
constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GTK = "gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GDK = "gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
constant $GLIB = "glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f";
# use 'gacutil -l' to look up similar module details

Expand All @@ -21,24 +21,21 @@
$drawingarea.add_ExposeEvent: sub ($obj, $args) {
$args; # suppress 'declared but not used' warnings
my $cc = GdkCairoHelper.Create($obj.GdkWindow); # Cairo Context
my $windowX; my $windowY; my $windowWidth; my $windowHeight; my $windowDepth;
# TODO: the following line seems to pass parameters by value, it needs to pass by reference to integers
# TODO: the following two lines pass parameters by value, need to pass by references to integers
# my $windowX; my $windowY; my $windowWidth; my $windowHeight; my $windowDepth;
# $obj.GdkWindow.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
# Tracked as https://github.com/sorear/niecza/issues/57
$obj.GdkWindow.GetGeometry($windowX, $windowY, $windowWidth, $windowHeight, $windowDepth);
# TODO: remove the following cheat that works around the above problem
$windowWidth = $windowSizeX; $windowHeight = $windowSizeY;
$cc.SetSourceRGB(0, 1, 0); $cc.Paint(); # Make a green background
# TODO: remove the following one line cheat that works around the above problem
my $windowWidth = $windowSizeX; my $windowHeight = $windowSizeY;
$cc.SetSourceRGB(0.6.Num, 1, 0.6.Num); $cc.Paint(); # light green background
# Start the recursive drawing process
my $x0=0; my $y0=0; my $x1=$windowWidth-1; my $y1=$windowHeight/2;
my $x2=0; my $y2=$windowHeight-1;
my $depth = Sierpinski($cc, $x0, $y0, $x1, $y1, $x2, $y2, True, 1);
my $label = sprintf("%d x %d, %d levels", $windowWidth, $windowHeight, $depth);
say $label;
$cc.Target.Dispose;
# TODO: Currently the following error appears at exit:
# Cairo.Context: called from finalization thread, programmer is missing a call to Dispose
# The following line should fix the above, but gives this error: Unable to resolve method Dispose
# $cc.Dispose(); # Should do: ((IDisposable) cc).Dispose();
$cc.dispose-hack; # Should be $cc.Dispose but CLR interop cannot call that
# Tracked as https://github.com/sorear/niecza/issues/56
};
$window.add_DeleteEvent: sub ($obj, $args) {
Expand All @@ -51,8 +48,9 @@

sub Sierpinski($cc, $x0, $y0, $x1, $y1, $x2, $y2, $colorflag, $depth is copy)
{
if $colorflag { $cc.SetSourceRGB(0, 0, 1); }
else { $cc.SetSourceRGB(1, 1, 0); }
if $colorflag { $cc.SetSourceRGB(0.6.Num, 0, 0.8.Num); } # indigo
else { $cc.SetSourceRGB(1, 1, 0.8.Num); } # light yellow
# First draw the entire main triangle in one color
$cc.MoveTo($x0.Int, $y0.Int); $cc.LineTo($x1.Int, $y1.Int);
$cc.LineTo($x2.Int, $y2.Int); $cc.LineTo($x0.Int, $y0.Int);
$cc.Fill;
Expand Down

0 comments on commit 9fccf74

Please sign in to comment.