ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/bin/kgsueme
(Generate patch)

Comparing kgsueme/bin/kgsueme (file contents):
Revision 1.15 by pcg, Fri May 30 04:17:25 2003 UTC vs.
Revision 1.17 by pcg, Fri May 30 09:15:37 2003 UTC

1#!/usr/bin/perl -I../lib/ 1#!/usr/bin/perl -I../lib/
2
3#use PApp::Util qw(dumpval); # debug only
4 2
5use Gtk; 3use Gtk;
6use Gtk::Gdk; 4use Gtk::Gdk;
7use Gtk::Gdk::Pixbuf; 5use Gtk::Gdk::Pixbuf;
8#use Gtk::Gdk::ImlibImage; 6#use Gtk::Gdk::ImlibImage;
9 7
10use KGS::Protocol; 8use KGS::Protocol;
11use KGS::Listener::Debug; 9use KGS::Listener::Debug;
12 10
11use Audio::Data;
12use Audio::Play;
13
13use IO::Socket::INET; 14use IO::Socket::INET;
14 15
15use Errno; 16use Errno;
16 17
17init Gtk; 18init Gtk;
18 19
19$HACK = 1; # do NEVER enable. ;) 20$HACK = 1; # do NEVER enable. ;)
20 21
21our $config; 22our $config;
23our $LIBDIR = ".";
22our $IMGDIR = "images"; 24our $IMGDIR = "$LIBDIR/images";
25our $SNDDIR = "$LIBDIR/sounds";
23 26
24sub load_img { 27sub load_img {
25 new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]" 28 new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]"
26# load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]" 29# load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]"
27 or die "$IMGDIR/$_[0]: $!"; 30 or die "$IMGDIR/$_[0]: $!";
28} 31}
32
33my @fontchars = ('A' .. 'Z', 0 .. 9);
29 34
30our @black_img = load_img "b-01.png"; 35our @black_img = load_img "b-01.png";
31our @white_img = map +(load_img "w-0$_.png"), 1,2,3,4,5; 36our @white_img = map +(load_img "w-0$_.png"), 1,2,3,4,5;
32our @triangle_img = map +(load_img "triangle-$_.png"), qw(b w); 37our @triangle_img = map +(load_img "triangle-$_.png"), qw(b w);
33our @square_img = map +(load_img "square-$_.png"), qw(b w); 38our @square_img = map +(load_img "square-$_.png"), qw(b w);
34our @circle_img = map +(load_img "circle-$_.png"), qw(b w); 39our @circle_img = map +(load_img "circle-$_.png"), qw(b w);
35our $board_img = load_img "woodgrain-01.jpg"; 40our $board_img = load_img "woodgrain-01.jpg";
36 41
42our @font = (
43 [map +(load_img "font/$_-black.png"), @fontchars],
44 [map +(load_img "font/$_-white.png"), @fontchars],
45 );
46our %fontmap;
47@fontmap{@fontchars} = (0..25 + 10);
48@fontmap{'a' .. 'z'} = (0..25);
49
50{
51 #my $audioserver = new Audio::Play(0);
52 my %sound;
53 $SIG{CHLD} = 'IGNORE';
54
55 for (qw(alarm warning move)) {
56 local $/;
57 open my $snd, "<", "$SNDDIR/$_"
58 or die "$SNDDIR: $!";
59 binmode $snd;
60
61 $sound{$_} = new Audio::Data;
62 $sound{$_}->Load($snd);
63 }
64
65 sub play_sound {
66 if (fork == 0) {
67 my $audioserver = new Audio::Play(1);
68 $audioserver->play ($sound{$_[0]});
69 Gtk->_exit(0);
70 }
71 }
72}
73
37{ 74{
38 use Storable (); 75 use Storable ();
39 use Scalar::Util (); 76 use Scalar::Util ();
40 77
41 my $staterc = "$ENV{HOME}/.kgsueme"; 78 my $staterc = "$ENV{HOME}/.kgsueme";
48 # grr... more gtk+ brokenness 85 # grr... more gtk+ brokenness
49 my %get = ( 86 my %get = (
50 hpane_position => sub { ($_[0]->children)[0]->allocation->[2] }, 87 hpane_position => sub { ($_[0]->children)[0]->allocation->[2] },
51 vpane_position => sub { ($_[0]->children)[0]->allocation->[3] }, 88 vpane_position => sub { ($_[0]->children)[0]->allocation->[3] },
52 window_size => sub { [ @{$_[0]->allocation}[2,3] ] }, 89 window_size => sub { [ @{$_[0]->allocation}[2,3] ] },
53 #window_pos => sub { die PApp::Util::dumpval [ $_[0]->get_root_origin ] }, 90 #window_pos => sub { die KGS::Listener::Debug::dumpval [ $_[0]->get_root_origin ] },
54 clist_column_widths => sub { 91 clist_column_widths => sub {
55 $_[0]{column_widths}; 92 $_[0]{column_widths};
56 }, 93 },
57 ); 94 );
58 95
139 $main->{status}->push($context_id{$type} ||= $main->{status}->get_context_id($type), $text) if $text; 176 $main->{status}->push($context_id{$type} ||= $main->{status}->get_context_id($type), $text) if $text;
140 } 177 }
141} 178}
142 179
143if (0) { 180if (0) {
181 use KGS::Constants;
182
144 my $board = new game size => 5; 183 my $board = new game size => 5;
184 $board->{board} = new KGS::Game::Board;
185 $board->{board}{board}[0][0] = MARK_B;
186 $board->{board}{board}[1][1] = MARK_GRAY_B | MARK_SMALL_W;
187 $board->{board}{board}[2][2] = MARK_W | MARK_TRIANGLE;
145 $board->{window}->show_all; 188 $board->{window}->show_all;
146} 189}
147 190
148main Gtk; 191main Gtk;
149 192
246 if ($msg->{success}) { 289 if ($msg->{success}) {
247 for (keys %{$::config->{rooms}}) { 290 for (keys %{$::config->{rooms}}) {
248 $self->{roomlist}->join_room($_); 291 $self->{roomlist}->join_room($_);
249 } 292 }
250 } 293 }
251
252 warn PApp::Util::dumpval($::config);
253} 294}
254 295
255sub event_disconnect { } 296sub event_disconnect { }
256 297
257############################################################################# 298#############################################################################
564 delete $self->{stack}; 605 delete $self->{stack};
565 $self->repaint_board; 606 $self->repaint_board;
566 1; 607 1;
567} 608}
568 609
569sub expose_event { 610sub INTERP_NEAREST (){ 1 }
570 my ($widget, $self, $event) = @_;
571
572 $self->{pixmap} or return;
573
574 my ($ox, $oy, $s) = @{$self->{offsets}};
575
576 my ($x, $y, $w, $h) =
577 @{Gtk::Gdk::Rectangle->intersect(
578 $event->{area},
579 [$ox, $oy, $s, $s]
580 )};
581
582 $self->{canvas}->window->draw_pixmap (
583 $self->{canvas}->style->white_gc,
584 $self->{pixmap},
585 $x - $ox, $y - $oy, $x, $y, $w, $h,
586 );
587 1;
588}
589
590# create new, _transparent_ pixbuf
591sub new_pixbuf {
592 my ($w, $h) = @_;
593
594 $pixbuf;
595}
596
597sub INTERP_TILES (){ 1 } 611sub INTERP_TILES (){ 1 }
598sub INTERP_BILINEAR (){ 2 } 612sub INTERP_BILINEAR (){ 2 }
599sub INTERP_HYPER (){ 3 } 613sub INTERP_HYPER (){ 3 }
600 614
601sub new_pixbuf { 615sub new_pixbuf {
602 my ($w, $h, $clear) = @_; 616 my ($w, $h, $alpha, $clear) = @_;
603 617
604 my $pixbuf = new Gtk::Gdk::Pixbuf 'rgb', 1, 8, $w, $h; 618 my $pixbuf = new Gtk::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
605 619
606 if ($clear) { # damn, need to clear it ourselves 620 if ($clear) { # damn, need to clear it ourselves
607 my $row = "\x00\x00\x00\x00" x $w; 621 my $row = "\x00\x00\x00\x00" x $w;
608 $pixbuf->put_pixels ($row, $_, 0) for 0 .. $h - 1; 622 $pixbuf->put_pixels ($row, $_, 0) for 0 .. $h - 1;
609 } 623 }
612} 626}
613 627
614sub scale_pixbuf { 628sub scale_pixbuf {
615 my ($src, $w, $h, $mode) = @_; 629 my ($src, $w, $h, $mode) = @_;
616 630
617 my $dst = new_pixbuf $w, $h; 631 my $dst = new_pixbuf $w, $h, 1;
618 632
619 $src->scale( 633 $src->scale(
620 $dst, 0, 0, $w, $h, 0, 0, 634 $dst, 0, 0, $w, $h, 0, 0,
621 $w / $src->get_width, $h / $src->get_height, 635 $w / $src->get_width, $h / $src->get_height,
622 $mode, 636 $mode,
623 ); 637 );
624 638
625 $dst; 639 $dst;
626} 640}
627 641
628sub label_font {
629 my ($size) = @_;
630
631 $size = int $size;
632 $size = 34 if $size > 34;
633
634 # I am soo incapable
635 for (8, 10, 11, 12, 14, 17, 18, 20, 24, 25, 34) {
636 next unless $size <= $_;
637 my $font = Gtk::Gdk::Font->fontset_load ("-*-helvetica-bold-r-*--$_-*");
638 return $font if $font;
639 }
640
641 return Gtk::Gdk::Font->fontset_load ("-*-helvetica-bold-r-*--8-*");
642}
643
644sub center_text {
645 my ($drawable, $font, $gc, $x, $y, $t) = @_;
646 my $w = $font->string_width ($t);
647 my $h = $font->string_height($t) - $font->descent;
648 $drawable->draw_text ($font, $gc, $x - $w*0.5, $y + $h * 0.5, $t, length $t);
649}
650
651# create a stack of stones 642# create a stack of stones
652sub create_stack { 643sub create_stack {
653 my ($self, $gc, $mark, $size, $rand) = @_; 644 my ($self, $mark, $size, $rand) = @_;
654 645
655 my $c = \$self->{stack}{$mark}; 646 my $c = \$self->{stack}{$mark};
656 unless ($$c) { 647 unless ($$c) {
657 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) { 648 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
658 my $base = 649 my $base =
659 $mark & (MARK_B | MARK_GRAY_B | MARK_W | MARK_GRAY_W) 650 $mark & (MARK_B | MARK_GRAY_B | MARK_W | MARK_GRAY_W)
660 ? scale_pixbuf $stone, $size, $size, INTERP_HYPER 651 ? scale_pixbuf $stone, $size, $size, INTERP_HYPER
661 : new_pixbuf $size, $size, 1; 652 : new_pixbuf $size, $size, 1, 1;
662 653
663 if ($mark & (MARK_GRAY_B | MARK_GRAY_W)) { 654 if ($mark & (MARK_GRAY_B | MARK_GRAY_W)) {
664 # make transparent by stippling :( 655 # make transparent by stippling :(
665 # fix this to use compositing if/when we have full compositing support 656 # fix this to use compositing if/when we have full compositing support
666 # in kgsueme 657 # in kgsueme
697 INTERP_HYPER, 255 688 INTERP_HYPER, 255
698 ); 689 );
699 } 690 }
700 } 691 }
701 692
702 push @$$c, [$base->render_pixmap_and_mask (128)]; 693 #push @$$c, [$base->render_pixmap_and_mask (128)];
694 push @$$c, $base;
703 } 695 }
704 } 696 }
705 697
706 @{$$c->[$rand % @$$c]}; 698 $$c->[$rand % @$$c];
699}
700
701sub pixbuf_text {
702 my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
703
704 my $c = $::font[$colour][$::fontmap{substr $text, 0, 1}];
705
706 if ($c) {
707 my ($w, $h) = ($c->get_width, $c->get_height);
708 my $s = $height / $h;
709
710 $x -= $w * $s *0.5;
711 $y -= $height * 0.5;
712
713 $c->composite ($pixbuf,
714 $x, $y, $w*$s+1, $height+1, $x, $y, $s, $s,
715 INTERP_HYPER, 128);
716
717 } else {
718 warn "unable to render character '$text'";
719 }
720}
721
722my $black_pb;
723
724sub pixbuf_rect {
725 my ($pb, $x1, $y1, $x2, $y2) = @_;
726 # we fake lines by... an unspeakable method :/
727 unless ($black_pb) {
728 $black_pb = new_pixbuf 1, 1, 0, 0;
729 $black_pb->put_pixels ("\x44\x11\x11", 0, 0);
730 }
731
732 $black_pb->scale ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, 10000, 10000, INTERP_NEAREST);
707} 733}
708 734
709sub repaint_board { 735sub repaint_board {
710 my ($self) = @_; 736 my ($self) = @_;
711 my $canvas = $self->{canvas}; 737 my $canvas = $self->{canvas};
738
739 return unless $self->{board};
712 740
713 %cache = (); 741 %cache = ();
714 742
715 my ($w, $h) = @{$canvas->allocation}[2,3]; 743 my ($w, $h) = @{$canvas->allocation}[2,3];
716 744
717 my $s = $w > $h ? $h : $w; 745 my $s = $w > $h ? $h : $w;
718 746
719 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s]; 747 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
720 748
721 my $pixmap = $self->{pixmap} = new Gtk::Gdk::Pixmap $self->{canvas}->window, $s, $s; 749 my $pixbuf;
722 750
723 { 751 {
724 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 752 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
725 753
726 my $bg = $s < $bw && $s < $bh ? $::board_img : scale_pixbuf $::board_img, $s, $s, INTERP_TILES; 754 if ($s < $bw && $s < $bh) {
727 $bg->render_to_drawable( 755 $pixbuf = new_pixbuf $s, $s, 0, 0;
728 $pixmap, $self->{canvas}->style->white_gc, 756 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
729 0, 0, 0, 0, $s, $s, 757 } else {
730 0, 0, 0 758 $pixbuf = scale_pixbuf $::board_img, $s, $s, INTERP_TILES;
731 ); 759 }
732 } 760 }
733 761
734 my $gc = Gtk::Gdk::GC->new ($pixmap); 762 $self->{pixbuf} = $pixbuf;
735
736 $gc->rgb_gc_set_foreground($line_colour);
737 $gc->set_line_attributes (int ($s / 300) + 1, 'solid', 'projecting', 'miter');
738 763
739 my $size = $self->{size}; 764 my $size = $self->{size};
740 my $border = int ($s / $size); 765 my $border = int ($s / $size);
741 my $s2 = $s - $border * 2; 766 my $s2 = $s - $border * 2;
742 my $edge = int ($s2 / $size) | 1; 767 my $edge = int ($s2 / $size) | 1;
743 my $ofs = int ($edge / 2); 768 my $ofs = int ($edge / 2);
744 769
745 my $font = label_font $ofs;
746
747 my @k = map int ($s2 * $_ / $size - $ofs + $border + 0.5), 0 .. $size; 770 my @k = map int ($s2 * $_ / $size - $ofs + $border + 0.5), 0 .. $size;
771
772 my $linew = int ($s / 300);
748 773
749 my $a = "A"; 774 my $a = "A";
750 for my $i (1 .. $size) { 775 for my $i (1 .. $size) {
751 $pixmap->draw_line ($gc, $k[$i], $k[1], $k[$i], $k[$size]); 776 pixbuf_rect $pixbuf, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew;
752 $pixmap->draw_line ($gc, $k[1], $k[$i], $k[$size], $k[$i]); 777 pixbuf_rect $pixbuf, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew;
753 778
754 center_text $pixmap, $font, $gc, $k[$i], ($ofs +$border) / 2, $a; 779 pixbuf_text $pixbuf, 0, $k[$i], ($ofs +$border) / 2, $ofs, $a;
755 center_text $pixmap, $font, $gc, $k[$i], $s2 + $border + $ofs / 2, $a; 780 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border + $ofs / 2, $ofs, $a;
756 center_text $pixmap, $font, $gc, ($ofs + $border) / 2, $k[$i], $i; 781 pixbuf_text $pixbuf, 0, ($ofs + $border) / 2, $k[$i], $ofs, $size - $i + 1;
757 center_text $pixmap, $font, $gc, $s2 + $border + $ofs / 2, $k[$i], $i; 782 pixbuf_text $pixbuf, 0, $s2 + $border + $ofs / 2, $k[$i], $ofs, $size - $i + 1;
758 783
759 $a++; 784 $a++;
760 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... 785 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
761 } 786 }
762 787
763 # hoshi-points(!)#d# 788 # hoshi-points(!)#d#
764 # caching of empty board gfx(!)#d# 789 # caching of empty board gfx(!)#d#
765 790
766 if ($self->{board}) { 791 for my $x (1 .. $size) {
767 for my $x (1 .. $size) { 792 for my $y (1 .. $size) {
768 for my $y (1 .. $size) {
769 my $yk = $s2 * $x / $size - $ofs + $border;
770 my $mark = $self->{board}{board}[$x-1][$y-1]; 793 my $mark = $self->{board}{board}[$x-1][$y-1];
771 794
772 if ($mark) { 795 if ($mark) {
773 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs); 796 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
797 my $pb = $self->create_stack($mark, $edge, $x * 17 + $y * 11 );
798
799 $pb->composite ($pixbuf, $dx, $dy, $edge, $edge, $dx, $dy, 1, 1, INTERP_TILES, 192);
800
774 my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 ); 801 #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 );
775 802
776 $gc->set_clip_mask ($bm); 803 #$gc->set_clip_mask ($bm);
777 $gc->set_clip_origin ($dx, $dy); 804 #$gc->set_clip_origin ($dx, $dy);
778 $pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge); 805 #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge);
779 }
780 } 806 }
781 } 807 }
782 } 808 }
809}
810
811sub expose_event {
812 my ($widget, $self, $event) = @_;
813
814 $self->{pixbuf} or return;
815
816 my ($ox, $oy, $s) = @{$self->{offsets}};
817
818 my ($x, $y, $w, $h) =
819 @{Gtk::Gdk::Rectangle->intersect(
820 $event->{area},
821 [$ox, $oy, $s, $s]
822 )};
823
824 if (defined $x) {
825 $self->{pixbuf}->render_to_drawable ($self->{canvas}->window, $self->{canvas}->style->white_gc,
826 $x - $ox, $y - $oy, $x, $y, $w, $h);
827 #$self->{canvas}->window->draw_pixmap (
828 # $self->{canvas}->style->white_gc,
829 # $self->{pixmap},
830 # $x - $ox, $y - $oy, $x, $y, $w, $h,
831 #);
832 }
833 1;
783} 834}
784 835
785sub event_update_tree { 836sub event_update_tree {
786 my ($self) = @_; 837 my ($self) = @_;
787 838
792 843
793 # force a redraw (not perfect(?)) 844 # force a redraw (not perfect(?))
794 expose_event $self->{canvas}, $self, { area => $self->{canvas}->allocation }; 845 expose_event $self->{canvas}, $self, { area => $self->{canvas}->allocation };
795 846
796 $self->{text}->backward_delete($self->{text}->get_length); 847 $self->{text}->backward_delete($self->{text}->get_length);
797 $self->{text}->insert(undef, undef, undef, $self->{board}{comment}.PApp::Util::dumpval([$self->{board}{time},$self->{board}{captures}])); 848 $self->{text}->insert(undef, undef, undef, KGS::Listener::Debug::dumpval([$self->{board}{time},$self->{board}{captures}]). $self->{board}{comment});
849}
850
851sub event_move {
852 ::play_sound "move";
798} 853}
799 854
8001; 8551;
801 856
802 857

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines