Goo-Canvas

 view release on metacpan or  search on metacpan

bin/perlmine.pl  view on Meta::CPAN

    }
    $canvas->set_root_item($root);
    $canvas->set_size_request( $xpad * 2 + Mine::Table::SIZE * $cols,
                               $ypad * 2 + Mine::Table::SIZE * $rows);
}

sub setting {
    my $dia = Gtk2::Dialog->new(
        gettext('Setting'), $window,
        'modal', 'gtk-ok' => 'ok',
        'gtk-cancel' => 'cancel',
    );
    my $vbox = $dia->vbox;
    my $table = Gtk2::Table->new(2, 2);
    my ($label, $row_but, $col_but, $mines_but);

    $label = Gtk2::Label->new(gettext("Rows"));
    $row_but = Gtk2::SpinButton->new_with_range(1, MAXROWS, 1);
    $row_but->set_value($Config{rows});
    $table->attach_defaults($label, 0, 1, 0, 1);
    $table->attach_defaults($row_but, 1, 2, 0, 1);

    $label = Gtk2::Label->new(gettext("Columns"));
    $col_but = Gtk2::SpinButton->new_with_range(1, MAXROWS, 1);
    $col_but->set_value($Config{cols});
    $table->attach_defaults($label, 0, 1, 1, 2);
    $table->attach_defaults($col_but, 1, 2, 1, 2);
    
    $label = Gtk2::Label->new(gettext("Mines"));
    $mines_but = Gtk2::SpinButton->new_with_range(1, MAXMINES, 1);
    $mines_but->set_value($Config{mines});
    $table->attach_defaults($label, 0, 1, 2, 3);
    $table->attach_defaults($mines_but, 1, 2, 2, 3);

    $vbox->add($table);
    $vbox->show_all();
    my $response = $dia->run;
    if ( $response eq 'ok' ) {
        set_level(undef, [$row_but->get_value, $col_but->get_value, $mines_but->get_value]);
    }
    $dia->destroy;
}

#}}}

sub start_game {
    $game_status = WAITING;
    if ( $timer ) {
        Glib::Source->remove($timer);
        $elapse_time = 0;
    }
    setup_canvas();
    my $table = $canvas->{table};
    $table->reset;
    set_count_label( $table->flag_count );
    set_timer_label();
    $image_but->set_image(Gtk2::Image->new_from_file($image{'smile'}));
    return FALSE;
}

sub update_label {
    $elapse_time++;
    set_timer_label();
    return TRUE;
}

sub open_cell {
    return FALSE if $game_status == STOP;
    if ( $game_status == WAITING ) {
        $game_status = START;
        $elapse_time = 0;
        $timer = Glib::Timeout->add(1000, \&update_label);
    }
    my ($cell, $target, $ev ) = @_;
    if ( $DEBUG ) {
        # print "x, y: (", join(', ', $ev->x, $ev->y), ")\n";
        print "row, col: (", join(", ", $cell->coords), ")\n";
    }
    my $boom;
    my $table = $canvas->{table};
    if ( $ev->button == 1 ) {   # left button
        $boom = $table->open($cell->coords);
    }
    elsif ( $ev->button == 2) { # middle button
        $boom = $table->open_others($cell->coords);
    } elsif ( $ev->button == 3 ) { # right button
        $table->set_flag($cell->coords);
        set_count_label($table->flag_count);
    }
    if ( $boom ) {
        stop_game();
    }
    if ( $table->unopen_count == $table->mines ) {
        win();
    }
    return FALSE;
}

sub set_count_label {
    my ($cnt) = @_;
    $count_label->set_markup(sprintf("<span weight=\"bold\" size=\"large\" foreground=\"blue\">%3d/%d</span>", $cnt, $Config{mines}));
}

sub set_timer_label {
    $timer_label->set_markup(sprintf("<span weight=\"bold\" size=\"large\" foreground=\"red\">%3d</span>", $elapse_time));
}

sub set_level {
    my ($wid, $data) = @_;
    $Config{rows}  = $data->[0];
    $Config{cols} = $data->[1];
    $Config{mines} = $data->[2];
    if ( $Config{mines} > $Config{rows} * $Config{cols} ) {
        warn "Mines more than cells of the table!\n";
        $Config{mines} = 0.1 * $Config{rows} * $Config{cols};
    }
    setup_canvas();
    start_game();
    return FALSE;
}



( run in 1.302 second using v1.01-cache-2.11-cpan-9581c071862 )