]> www.fi.muni.cz Git - slotcarman.git/commitdiff
More GUI work.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 7 Dec 2010 18:58:07 +0000 (19:58 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 7 Dec 2010 18:58:07 +0000 (19:58 +0100)
SCX/Car.pm
SCX/GUI.pm
gui.pl

index 8bb7bb9a00e8c5a1f15f596b19e0587dace11eeb..75aa503c49167763e89eca986f2691f87ee1ca31 100644 (file)
@@ -10,8 +10,13 @@ sub new {
 
        my $self = {
                gui      => $args->{gui},
-               throttle => -1,
-               fuel     => -1,
+               driver   => $args->{driver} || '--',    
+               throttle => undef,
+               fuel     => 0,
+               lap      => 0,
+               laptime  => 0,
+               avg_lap  => 0,
+               car_img  => $args->{car_img},
                id       => $args->{id},
                track    => $args->{track},
        };
@@ -25,14 +30,26 @@ sub gui { return shift->{gui}; }
 
 sub track { return shift->{track}; }
 
+sub same {
+       my ($self, $key, $val) = @_;
+
+       return 1 if !defined $self->{$key} && !defined $val
+               || (defined $self->{$key} && defined $val
+                       && $self->{$key} eq $val);
+
+       $self->{$key} = $val;
+       return undef;
+}
+
 sub set_throttle {
        my ($self, $val) = @_;
 
-       return if (!defined $self->{throttle} && !defined $val)
-               || (defined $self->{throttle} && defined $val
-                       && $self->{throttle} == $val);
+       return if $self->same('throttle', $val);
 
-       $self->{throttle} = $val;
+       if ($self->track->{start_in_progress} && $val) {
+               $self->{early_start} = 1;
+               $self->gui->set_driver('Early start!');
+       }
        $self->gui->set_throttle($self->{order}, $val);
 }
 
@@ -40,11 +57,7 @@ sub set_throttle {
 sub set_fuel {
        my ($self, $val) = @_;
 
-       return if (!defined $self->{fuel} && !defined $val)
-               || (defined $self->{fuel} && defined $val
-                       && $self->{fuel} == $val);
-
-       $self->{fuel} = $val;
+       return if $self->same('fuel', $val);
        $self->gui->set_fuel($self->{order}, $val);
 }
 
@@ -59,22 +72,14 @@ sub set_backbutton {
 sub set_model {
        my ($self, $name) = @_;
 
-       return if (!defined $self->{car_icon} && !defined $name)
-               || (defined $self->{car_icon} && defined $name
-                       && $self->{car_icon} eq $name);
-
-       $self->{car_icon} = $name;
+       return if $self->same('car_img', $name);
        $self->gui->set_car_icon($self->{order}, $name);
 }
 
 sub set_lap {
        my ($self, $lap_nr) = @_;
 
-       return if (!defined $self->{lap} && !defined $lap_nr)
-               || (defined $self->{lap} && defined $lap_nr
-                       && $self->{lap} == $lap_nr);
-
-       $self->{lap} = $lap_nr;
+       return if $self->same('lap', $lap_nr);
        $self->gui->set_lap($self->{order}, defined $lap_nr ? $lap_nr : '--');
        if ($self->track->{race_running} && $self->{lap} > 1) {
                my $now = gettimeofday;
@@ -122,12 +127,8 @@ sub set_laptime {
 sub set_driver {
        my ($self, $driver_name) = @_;
 
-       return if (!defined $self->{driver} && !defined $driver_name)
-               || (defined $self->{driver} && defined $driver_name
-                       && $self->{driver} == $driver_name);
-
-       $self->{driver} = $driver_name;
-       $self->gui->set_name($self->{order}, $driver_name || '--');
+       return if $self->same('driver', $driver_name);
+       $self->gui->set_name($self->{order}, $driver_name);
 }
 
 sub enter_pit_lane {
@@ -153,8 +154,7 @@ sub set_order {
 
        $self->{order} = $pos;
        
-       $self->gui->set_name($self->{order}, $self->{driver}
-               || '--');
+       $self->gui->set_name($self->{order}, $self->{driver});
        $self->gui->set_car_icon($self->{order}, $self->{car_icon});
        $self->gui->set_throttle($self->{order}, $self->{throttle});
        $self->gui->set_lap($self->{order}, defined $self->{lap}
index 3d500f695f311902be9363ed6e21f498068b1296..83783ae92b47df1ca82f68e5640ec3eb76787061 100755 (executable)
@@ -175,6 +175,8 @@ sub set_label {
 sub set_image {
        my ($self, $prefix, $row, $imgset, $value) = @_;
 
+       return if !defined $value;
+
        ++$row if defined $row && $row =~ /\A\d+\z/xms;
 
        my $name = defined $row ? $prefix.$row : $prefix;
diff --git a/gui.pl b/gui.pl
index 0ea57b38ef24417d6d01fde9349631be306f16ea..7fcfc7f740fc400e74bf14b4b77b60ddff47b108 100755 (executable)
--- a/gui.pl
+++ b/gui.pl
@@ -10,20 +10,17 @@ use SCX::GUI;
 use SCX::Track;
 use SCX::Reader;
 
-my $no_reader = 0;
-
-my $gui = SCX::GUI->new;
+my $gui = SCX::GUI->new({ img_height => 80 });
 my $track = SCX::Track->new({ gui => $gui });
 
-if (!$no_reader) {
-       my $reader = SCX::Reader->new({
+my $reader;
+eval {
+       $reader = SCX::Reader->new({
                portname   => '/dev/ttyUSB0',
                logfile    => 'log',
                track      => $track,
        });
-
-       Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader);
-}
+};
 
 $track->car(0)->set_model('F1 Ferrari');
 $track->car(1)->set_model('F1 McLaren');
@@ -32,21 +29,38 @@ $track->car(3)->set_model('Citroen Xsara');
 $track->car(4)->set_model('Citroen C4');
 $track->car(5)->set_model('Ford Focus');
 
-$track->car(0)->set_lap(13);
-$track->car(1)->set_lap(11);
-$track->car(2)->set_lap(10);
-$track->car(3)->set_lap(undef);
-$track->car(4)->set_lap(undef);
-$track->car(5)->set_lap(undef);
-
-$track->car(0)->set_laptime(9.12322);
-$track->car(1)->set_laptime(15.313);
-$track->car(2)->set_laptime(8.193);
-
 $track->car(0)->set_driver('Ivi');
 $track->car(1)->set_driver('Filípek');
 $track->car(2)->set_driver('Táta');
 
+if ($reader) {
+       Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader);
+} else {
+       # Just display some random values
+       $gui->rounds('5/13');
+       $gui->time('53.4');
+       $gui->best_lap('12.3');
+
+       $track->car(0)->set_lap(13);
+       $track->car(1)->set_lap(11);
+       $track->car(2)->set_lap(10);
+       $track->car(3)->set_lap(undef);
+       $track->car(4)->set_lap(undef);
+       $track->car(5)->set_lap(undef);
+
+       $track->car(0)->set_laptime(9.12322);
+       $track->car(1)->set_laptime(15.313);
+       $track->car(2)->set_laptime(5.989);
+
+       $track->car(0)->set_fuel(6);
+       $track->car(1)->set_fuel(2);
+       $track->car(2)->set_fuel(8);
+
+       $track->car(0)->set_throttle(0);
+       $track->car(1)->set_throttle(8);
+       $track->car(2)->set_throttle(12);
+}
+
 $gui->show;
 
 Gtk2->main();