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

Comparing kgsueme/bin/kgsueme (file contents):
Revision 1.11 by pcg, Thu May 29 12:19:10 2003 UTC vs.
Revision 1.18 by pcg, Fri May 30 10:40:21 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;
7#use Gtk::Gdk::Pixbuf; 5use Gtk::Gdk::Pixbuf;
8use Gtk::Gdk::ImlibImage; 6#use Gtk::Gdk::ImlibImage;
9
10use Gnome;
11 7
12use KGS::Protocol; 8use KGS::Protocol;
13use KGS::Listener::Debug; 9use KGS::Listener::Debug;
14 10
11use Audio::Data;
12use Audio::Play;
13
15use IO::Socket::INET; 14use IO::Socket::INET;
16 15
17use Errno; 16use Errno;
18 17
19init Gnome "kgsueme"; 18init Gtk;
20 19
21$HACK = 1; # do NEVER enable. ;) 20$HACK = 1; # do NEVER enable. ;)
22 21
23our $config; 22our $config;
23our $LIBDIR = ".";
24our $IMGDIR = "images"; 24our $IMGDIR = "$LIBDIR/images";
25our $SNDDIR = "$LIBDIR/sounds";
25 26
26sub load_img { 27sub load_img {
27# new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]" 28 new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]"
28 load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]" 29# load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]"
29 or die "$IMGDIR/$_[0]: $!"; 30 or die "$IMGDIR/$_[0]: $!";
30} 31}
31 32
33my @fontchars = ('A' .. 'Z', 0 .. 9);
34
32our @black_img = load_img "b-01.png"; 35our @black_img = load_img "b-01.png";
33our @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;
37our @triangle_img = map +(load_img "triangle-$_.png"), qw(b w);
38our @square_img = map +(load_img "square-$_.png"), qw(b w);
39our @circle_img = map +(load_img "circle-$_.png"), qw(b w);
34our $board_img = load_img "woodgrain-01.jpg"; 40our $board_img = load_img "woodgrain-01.jpg";
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}
35 73
36{ 74{
37 use Storable (); 75 use Storable ();
38 use Scalar::Util (); 76 use Scalar::Util ();
39 77
47 # grr... more gtk+ brokenness 85 # grr... more gtk+ brokenness
48 my %get = ( 86 my %get = (
49 hpane_position => sub { ($_[0]->children)[0]->allocation->[2] }, 87 hpane_position => sub { ($_[0]->children)[0]->allocation->[2] },
50 vpane_position => sub { ($_[0]->children)[0]->allocation->[3] }, 88 vpane_position => sub { ($_[0]->children)[0]->allocation->[3] },
51 window_size => sub { [ @{$_[0]->allocation}[2,3] ] }, 89 window_size => sub { [ @{$_[0]->allocation}[2,3] ] },
52 #window_pos => sub { die PApp::Util::dumpval [ $_[0]->get_root_origin ] }, 90 #window_pos => sub { die KGS::Listener::Debug::dumpval [ $_[0]->get_root_origin ] },
53 clist_column_widths => sub { 91 clist_column_widths => sub {
54 $_[0]{column_widths}; 92 $_[0]{column_widths};
55 }, 93 },
56 ); 94 );
57 95
137 $main->{status}->pop($context_id{$type}) if $context_id{$type}; 175 $main->{status}->pop($context_id{$type}) if $context_id{$type};
138 $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;
139 } 177 }
140} 178}
141 179
180if (0) {
181 use KGS::Constants;
182
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;
188 $board->{window}->show_all;
189}
190
142main Gtk; 191main Gtk;
143 192
144############################################################################# 193#############################################################################
145 194
146package kgsueme; 195package kgsueme;
240 if ($msg->{success}) { 289 if ($msg->{success}) {
241 for (keys %{$::config->{rooms}}) { 290 for (keys %{$::config->{rooms}}) {
242 $self->{roomlist}->join_room($_); 291 $self->{roomlist}->join_room($_);
243 } 292 }
244 } 293 }
245
246 warn PApp::Util::dumpval($::config);
247} 294}
248 295
249sub event_disconnect { } 296sub event_disconnect { }
250 297
251############################################################################# 298#############################################################################
446 my ($self) = @_; 493 my ($self) = @_;
447 $self->SUPER::part; 494 $self->SUPER::part;
448 495
449 delete $::config->{rooms}{$self->{channel}}; 496 delete $::config->{rooms}{$self->{channel}};
450 $self->{window}->hide_all; 497 $self->{window}->hide_all;
451 $self->event_update; 498 $self->event_update_users;
452 $self->event_update_games; 499 $self->event_update_games;
453} 500}
454 501
455sub event_join { 502sub event_join {
456 my ($self) = @_; 503 my ($self) = @_;
487 $self = $self->SUPER::new(@_); 534 $self = $self->SUPER::new(@_);
488 535
489 $self->listen($self->{conn}); 536 $self->listen($self->{conn});
490 537
491 $self->{window} = new Gtk::Window 'toplevel'; 538 $self->{window} = new Gtk::Window 'toplevel';
492 $self->{window}->set_title("KGS Game ".$self->user0." ".$self->user1); 539 $self->{window}->set_title("KGS Game ".$self->user0." ".$self->user1) if $self->{channel};#d#
493 ::state $self->{window}, "game::window", undef, window_size => [600, 500]; 540 ::state $self->{window}, "game::window", undef, window_size => [600, 500];
494 541
495 $self->{window}->signal_connect(delete_event => sub { $self->part; 1 }); 542 $self->{window}->signal_connect(delete_event => sub { $self->part; 1 });
496 543
497 $self->{window}->add(my $hpane = new Gtk::HPaned); 544 $self->{window}->add(my $hpane = new Gtk::HPaned);
498 ::state $hpane, "game::hpane", undef, hpane_position => 500; 545 ::state $hpane, "game::hpane", undef, hpane_position => 500;
499 546
500 $::config{aa} = 0; 547 Gtk::Widget->push_visual (Gtk::Gdk::Rgb->get_visual);
501 $self->{canvas} = $::config{aa} ? new_aa Gnome::Canvas : new Gnome::Canvas; 548 Gtk::Widget->push_colormap (Gtk::Gdk::Rgb->get_cmap);
502 $hpane->pack1($self->{canvas}, 1, 1); 549 $hpane->pack1(($self->{canvas} = new Gtk::DrawingArea), 1, 1);
550 Gtk::Widget->pop_colormap;
551 Gtk::Widget->pop_visual;
503 552
504 { 553 $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
505 my $line_colour = $::config{line_colour} || "darkbrown"; 554 $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
506 my $border = 0.1;
507 my $ofs = 0.5 / $self->{size};
508
509 $self->{canvas}->set_pixels_per_unit(1000);
510 $self->{canvas}->set_scroll_region(-$border,-$border,1+$border,1+$border);
511
512 $self->{canvas}->signal_connect(size_allocate => sub {
513 my ($w, $h) = @{$_[1]}[2,3];
514
515 $self->{canvas}->set_pixels_per_unit(($w > $h ? $h : $w)/(1+$border*2));
516
517 1;
518 });
519
520 my $croot = $self->{canvas}->root;
521 my $cgroup = $croot; #->new($croot, "Gnome::CanvasGroup");
522
523 if ($::board_img) {
524 $cgroup->new($cgroup, "Gnome::CanvasImage",
525 x => -$border, y => -$border,
526 width => 1+$border*2, height => 1+$border*2,
527 image => $::board_img,
528 anchor => "nw"
529 );
530 } else {
531 $cgroup->new($cgroup, "Gnome::CanvasRect",
532 x1 => -$border, x2 => 1+$border,
533 y1 => -$border, y2 => 1+$border,
534 outline_color => "black",
535 fill_color => "brown",
536 width_pixels => 2,
537 );
538 }
539
540 my $a = "A";
541 for my $i (1 .. $self->{size}) { # one more iteration for the last lines
542 my $k = $i / $self->{size} - $ofs;
543
544 $cgroup->new($cgroup, "Gnome::CanvasLine",
545 points => [ $k,$ofs, $k,1-$ofs ],
546 fill_color => $line_colour,
547 width_pixels => 1);
548
549 $cgroup->new($cgroup, "Gnome::CanvasLine",
550 points => [ $ofs,$k, 1-$ofs,$k ],
551 fill_color => $line_colour,
552 width_pixels => 1);
553
554 for ( [$k,-$border*0.5,$a], [$k,1+$border*0.5,$a],
555 [-$border*0.5,$k,$self->{size} - $i + 1], [1+$border*0.5,$k,$self->{size} - $i + 1] ) {
556 my ($x, $y, $text) = @$_;
557
558 my $text = $cgroup->new($cgroup, "Gnome::CanvasText",
559 x => 0, y => 0, text => $text,
560 justification => "center",
561 anchor => "center",
562 font => $::config{aa} ? "-*-helvetica-medium-r-*--34-*" : "-*-helvetica-bold-r-*--17-*",
563 fill_color => $line_colour);
564
565 $text->affine_relative($border*0.015,0, 0,$border*0.015, $x,$y);
566 }
567
568 $a++;
569 $a++ if $a eq "I";
570 }
571
572 my $stones = [[]];
573
574 for my $x (1 .. $self->{size}) {
575 my $xk = $x / $self->{size} - $ofs;
576 for my $y (1 .. $self->{size}) {
577 my $yk = $y / $self->{size} - $ofs;
578
579 my $col = $stones->[$x-1][$y-1] = [];
580
581 if (1) {
582 $col->[0] =
583 $cgroup->new($cgroup, "Gnome::CanvasImage",
584 x => $xk, y => $yk,
585 width => $ofs*2, height => $ofs*2,
586 image => $::black_img[int rand @::black_img],
587 anchor => "center");
588 $col->[1] =
589 $cgroup->new($cgroup, "Gnome::CanvasImage",
590 x => $xk, y => $yk,
591 width => $ofs*2, height => $ofs*2,
592 image => $::white_img[int rand @::white_img],
593 anchor => "center");
594 } else {
595 die "need pixmaps\n";
596 # too large, scale has NO effect
597 $col->[0] =
598 $cgroup->new($cgroup, "Gnome::CanvasEllipse",
599 x1 => $xk, x2 => $xk + 0.001,
600 y1 => $yk, y2 => $yk + 0.001,
601 outline_color => "black", width_pixels => 2,
602 fill_color => "black");
603 $col->[1] =
604 $cgroup->new($cgroup, "Gnome::CanvasEllipse",
605 x1 => $xk, x2 => $xk + 0.001,
606 y1 => $yk, y2 => $yk + 0.001,
607 outline_color => "black", width_pixels => 2,
608 fill_color => "white");
609 }
610 }
611 }
612
613 $self->{board_gfx}{stones} = $stones;
614 }
615 555
616 $hpane->pack2((my $vpane = new Gtk::VPaned), 0, 0); 556 $hpane->pack2((my $vpane = new Gtk::VPaned), 0, 0);
617 ::state $vpane, "game", $self->{name}, vpane_position => 80; 557 ::state $vpane, "game", $self->{name}, vpane_position => 80;
618 558
619 $vpane->add(my $sw = new Gtk::ScrolledWindow); 559 $vpane->add(my $sw = new Gtk::ScrolledWindow);
658 $self->SUPER::part; 598 $self->SUPER::part;
659 599
660 $self->{window}->hide; 600 $self->{window}->hide;
661} 601}
662 602
603sub configure_event {
604 my ($widget, $self, $event) = @_;
605 delete $self->{stack};
606 delete $self->{background};
607 $self->repaint_board;
608 1;
609}
610
611sub INTERP_NEAREST (){ 1 }
612sub INTERP_TILES (){ 1 }
613sub INTERP_BILINEAR (){ 2 }
614sub INTERP_HYPER (){ 3 }
615
616sub new_pixbuf {
617 my ($w, $h, $alpha, $clear) = @_;
618
619 my $pixbuf = new Gtk::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
620
621 if ($clear) { # damn, need to clear it ourselves
622 my $row = "\x00\x00\x00\x00" x $w;
623 $pixbuf->put_pixels ($row, $_, 0) for 0 .. $h - 1;
624 }
625
626 $pixbuf;
627}
628
629sub scale_pixbuf {
630 my ($src, $w, $h, $mode) = @_;
631
632 my $dst = new_pixbuf $w, $h, 1;
633
634 $src->scale(
635 $dst, 0, 0, $w, $h, 0, 0,
636 $w / $src->get_width, $h / $src->get_height,
637 $mode,
638 );
639
640 $dst;
641}
642
643# create a stack of stones
644sub create_stack {
645 my ($self, $mark, $size, $rand) = @_;
646
647 my $c = \$self->{stack}{$mark};
648 unless ($$c) {
649 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
650 my $base = new_pixbuf $size, $size, 1, 1;
651
652 # first the big stones
653 for ([MARK_B, 255],
654 [MARK_W, 255],
655 [MARK_GRAY_B, 128],
656 [MARK_GRAY_W, 128]) {
657 my ($mask, $alpha) = @$_;
658 if ($mark & $mask) {
659 $stone->composite (
660 $base, 0, 0, $size, $size, -0.5, -0.5,
661 $size / $stone->get_width, $size / $stone->get_height,
662 INTERP_HYPER, $alpha
663 );
664 }
665 }
666
667 # then the samll stones
668 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
669 [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
670 my ($mask, $img) = @$_;
671 if ($mark & $mask) {
672 $img->composite (
673 $base, ($size / 4) x2, (int ($size / 2 + 0.5)) x2, ($size / 4 - 0.5) x 2,
674 $size / $img->get_width / 2, $size / $img->get_height / 2,
675 INTERP_HYPER, 192
676 );
677 }
678 }
679
680 # and lastly any markers (labels NYI)
681 my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
682
683 for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
684 [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
685 [MARK_SQUARE, $::square_img[$dark_bg]]) {
686 my ($mask, $img) = @$_;
687 if ($mark & $mask) {
688 $img->composite (
689 $base, 0, 0, $size, $size, -0.5, -0.5,
690 $size / $img->get_width, $size / $img->get_height,
691 INTERP_HYPER, 255
692 );
693 }
694 }
695
696 push @$$c, $base;
697 }
698 }
699
700 $$c->[$rand % @$$c];
701}
702
703sub pixbuf_text {
704 my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
705
706 my $c = $::font[$colour][$::fontmap{substr $text, 0, 1}];
707
708 if ($c) {
709 my ($w, $h) = ($c->get_width, $c->get_height);
710 my $s = ($height-1) / ($h-1);
711
712 $x -= $w * $s * 0.5;
713 $y -= $height * 0.5;
714
715 $c->composite ($pixbuf,
716 $x, $y, $w*$s+0.5, $height+0.5, $x-0.5, $y-0.5, $s, $s,
717 INTERP_BILINEAR, 192);
718
719 } else {
720 warn "unable to render character '$text'";
721 }
722}
723
724my $black_pb;
725
726sub pixbuf_rect {
727 my ($pb, $x1, $y1, $x2, $y2) = @_;
728 # we fake lines by... an unspeakable method :/
729 unless ($black_pb) {
730 $black_pb = new_pixbuf 1, 1, 0, 0;
731 $black_pb->put_pixels ("\x44\x11\x11", 0, 0);
732 }
733
734 $black_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, 1, 1, INTERP_NEAREST, 192);
735}
736
737sub repaint_board {
738 my ($self) = @_;
739 my $canvas = $self->{canvas};
740
741 return unless $self->{board};
742
743 my ($w, $h) = @{$canvas->allocation}[2,3];
744
745 my $s = $w > $h ? $h : $w;
746
747 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
748
749 my $size = $self->{size};
750
751 my $border = int ($s / $size);
752 my $s2 = $s - $border * 2;
753 my $edge = int ($s2 / $size) | 1;
754 my $ofs = int ($edge / 2);
755
756 my @k = map int ($s2 * $_ / $size - $ofs + $border + 0.5), 0 .. $size;
757
758 unless ($self->{background}) {
759 my $pixbuf;
760
761 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
762
763 if ($s < $bw && $s < $bh) {
764 $pixbuf = new_pixbuf $s, $s, 1, 0;
765 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
766 } else {
767 $pixbuf = scale_pixbuf $::board_img, $s, $s, INTERP_TILES;
768 }
769
770 my $linew = int ($s / 300);
771
772 my $a = "A";
773 for my $i (1 .. $size) {
774 pixbuf_rect $pixbuf, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew;
775 pixbuf_rect $pixbuf, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew;
776
777 pixbuf_text $pixbuf, 0, $k[$i], ($ofs +$border) / 2, $ofs, $a;
778 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border + $ofs / 2, $ofs, $a;
779 pixbuf_text $pixbuf, 0, ($ofs + $border) / 2, $k[$i], $ofs, $size - $i + 1;
780 pixbuf_text $pixbuf, 0, $s2 + $border + $ofs / 2, $k[$i], $ofs, $size - $i + 1;
781
782 $a++;
783 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
784 }
785
786 $self->{background} = $pixbuf;
787 }
788
789 my $pixbuf = $self->{pixbuf} = $self->{background}->copy;
790
791 # hoshi-points(!)#d#
792 # caching of empty board gfx(!)#d#
793
794 for my $x (1 .. $size) {
795 for my $y (1 .. $size) {
796 my $mark = $self->{board}{board}[$x-1][$y-1];
797
798 if ($mark) {
799 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
800 my $pb = $self->create_stack($mark, $edge, $x * 17 + $y * 11 );
801
802 $pb->composite ($pixbuf, $dx, $dy, $edge, $edge, $dx, $dy, 1, 1, INTERP_NEAREST, 255);
803
804 #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 );
805
806 #$gc->set_clip_mask ($bm);
807 #$gc->set_clip_origin ($dx, $dy);
808 #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge);
809 }
810 }
811 }
812}
813
814sub expose_event {
815 my ($widget, $self, $event) = @_;
816
817 $self->{pixbuf} or return;
818
819 my ($ox, $oy, $s) = @{$self->{offsets}};
820
821 my ($x, $y, $w, $h) =
822 @{Gtk::Gdk::Rectangle->intersect(
823 $event->{area},
824 [$ox, $oy, $s, $s]
825 )};
826
827 if (defined $x) {
828 $self->{pixbuf}->render_to_drawable ($self->{canvas}->window, $self->{canvas}->style->white_gc,
829 $x - $ox, $y - $oy, $x, $y, $w, $h);
830 #$self->{canvas}->window->draw_pixmap (
831 # $self->{canvas}->style->white_gc,
832 # $self->{pixmap},
833 # $x - $ox, $y - $oy, $x, $y, $w, $h,
834 #);
835 }
836 1;
837}
838
663sub event_update_tree { 839sub event_update_tree {
664 my ($self) = @_; 840 my ($self) = @_;
665 841
666 my $board = new KGS::Game::Board $self->{size}; 842 $self->{board} = new KGS::Game::Board $self->{size};
667 $board->interpret_path ($self->get_path); 843 $self->{board}->interpret_path ($self->get_path);
668 844
669 for my $x (0 .. $self->{size} - 1) { 845 $self->repaint_board;
670 for my $y (0 .. $self->{size} - 1) {
671 my $v = $board->{board}[$x][$y];
672 846
673 $v & MARK_B 847 # force a redraw (not perfect(?))
674 ? $self->{board_gfx}->{stones}->[$x][$y][0]->show 848 expose_event $self->{canvas}, $self, { area => $self->{canvas}->allocation };
675 : $self->{board_gfx}->{stones}->[$x][$y][0]->hide;
676 $v & MARK_W
677 ? $self->{board_gfx}->{stones}->[$x][$y][1]->show
678 : $self->{board_gfx}->{stones}->[$x][$y][1]->hide;
679 }
680 }
681 849
682 $self->{text}->backward_delete($self->{text}->get_length); 850 $self->{text}->backward_delete($self->{text}->get_length);
683 $self->{text}->insert(undef, undef, undef, $board->{comment}.PApp::Util::dumpval([$board->{time},$board->{captures}])); 851 $self->{text}->insert(undef, undef, undef, KGS::Listener::Debug::dumpval([$self->{board}{time},$self->{board}{captures}]). $self->{board}{comment});
852}
853
854sub event_move {
855 ::play_sound "move";
684} 856}
685 857
6861; 8581;
687 859
688 860

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines