]> www.fi.muni.cz Git - slotcarman.git/commitdiff
Refactorized GUI layer.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 7 Dec 2010 18:32:59 +0000 (19:32 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 7 Dec 2010 18:32:59 +0000 (19:32 +0100)
SCX/Car.pm
SCX/GUI.pm
slotcarman.glade

index 7dffb7a5361df8d184e1a303884e07f8aa5575c8..8bb7bb9a00e8c5a1f15f596b19e0587dace11eeb 100644 (file)
@@ -18,9 +18,6 @@ sub new {
 
        bless $self, $class;
 
-       $self->set_throttle(undef);
-       $self->set_fuel(undef);
-
        return $self;
 }
 
@@ -130,7 +127,7 @@ sub set_driver {
                        && $self->{driver} == $driver_name);
 
        $self->{driver} = $driver_name;
-       $self->gui->set_driver($self->{order}, $driver_name || '--');
+       $self->gui->set_name($self->{order}, $driver_name || '--');
 }
 
 sub enter_pit_lane {
@@ -156,7 +153,7 @@ sub set_order {
 
        $self->{order} = $pos;
        
-       $self->gui->set_driver($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});
index 4f02edc74a4c682c1d4173d7f687195a268549b7..3d500f695f311902be9363ed6e21f498068b1296 100755 (executable)
@@ -5,6 +5,7 @@ package SCX::GUI;
 use strict;
 use utf8;
 
+use Carp;
 use Encode;
 use Gtk2 '-init';
 use Glib qw(TRUE FALSE);
@@ -79,25 +80,25 @@ sub set_font_sizes {
        my ($self) = @_;
 
        if ($self->{img_height} <= 80) {
-               $self->{label_font} = 10;
-               $self->{value_font} = 18;
-               $self->{lap_font} = 24;
+               $self->{label_size} = 10;
+               $self->{value_size} = 18;
+               $self->{lap_size} = 24;
        } elsif ($self->{img_height} <= 100) {
-               $self->{label_font} = 12;
-               $self->{value_font} = 20;
-               $self->{lap_font} = 28;
+               $self->{label_size} = 12;
+               $self->{value_size} = 20;
+               $self->{lap_size} = 28;
        } elsif ($self->{img_height} <= 120) {
-               $self->{label_font} = 14;
-               $self->{value_font} = 24;
-               $self->{lap_font} = 32;
+               $self->{label_size} = 14;
+               $self->{value_size} = 24;
+               $self->{lap_size} = 32;
        } elsif ($self->{img_height} <= 150) {
-               $self->{label_font} = 16;
-               $self->{value_font} = 28;
-               $self->{lap_font} = 36;
+               $self->{label_size} = 16;
+               $self->{value_size} = 28;
+               $self->{lap_size} = 36;
        } else {
-               $self->{label_font} = 18;
-               $self->{value_font} = 32;
-               $self->{lap_font} = 40;
+               $self->{label_size} = 18;
+               $self->{value_size} = 32;
+               $self->{lap_size} = 40;
        }
 }
 
@@ -158,91 +159,84 @@ sub load_image_dir {
        return \%rv;
 }
 
+sub set_label {
+       my ($self, $prefix, $row, $text, $size) = @_;
+
+       ++$row if defined $row && $row =~ /\A\d+\z/xms;
+       $size //= 'value';
+
+       my $name = defined $row ? $prefix.$row : $prefix;
+       my $label = $self->get_object($name)
+               or croak "Can't get label named $name";
+       $label->set_markup('<span font_desc="' . $self->{$size.'_size'} . '"'
+               . ' weight="bold">' . $text . '</span>');
+}
+
+sub set_image {
+       my ($self, $prefix, $row, $imgset, $value) = @_;
+
+       ++$row if defined $row && $row =~ /\A\d+\z/xms;
+
+       my $name = defined $row ? $prefix.$row : $prefix;
+
+       my $image = $self->get_object($name)
+               or croak "Can't get image named $name";
+
+       my $set = $self->{$imgset.'_images'};
+       my $pixbuf = (ref($set) eq 'ARRAY')
+               ? $set->[$value]
+               : $set->{$value};
+               
+       $image->set_from_pixbuf($pixbuf);
+}
+
 sub set_throttle {
        my ($self, $row, $val) = @_;
 
-       $row++;
        $val = 13 if !defined $val;
-
-       my $image = $self->get_object("image_throttle$row");
-       $image->set_from_pixbuf($self->{throttle_images}->[$val]);
+       $self->set_image('image_throttle', $row, 'throttle', $val);
 }
 
 sub set_fuel {
        my ($self, $row, $val) = @_;
 
-       $row++;
-       $val = 0 if !defined $val;
-
-       my $image = $self->get_object("image_fuel$row");
-       $image->set_from_pixbuf($self->{fuel_images}->[$val]);
+       $self->set_image('image_fuel', $row, 'fuel', $val || 0);
 }
 
 sub set_car_icon {
        my ($self, $row, $name) = @_;
 
-       $row++;
-       my $pixbuf = $self->{car_images}->{$name};
-       if (!$pixbuf) {
-               print "No car image for $name.\n";
-               return;
-       }
-
-       my $image = $self->get_object("image_car$row");
-       $image->set_from_pixbuf($pixbuf);
+       $self->set_image('image_car', $row, 'car', $name);
 }
 
 sub set_lap {
        my ($self, $row, $text) = @_;
 
-       ++$row;
-       my $label = $self->get_object("label_lap$row")
-               or die "Can't get label_lap$row";
-       $label->set_markup('<span font_desc="24" weight="bold">'.$text.'</span>');
+       $self->set_label('label_lap', $row, $text, 'lap');
 }
 
 sub set_laptime {
        my ($self, $row, $text) = @_;
 
-       ++$row;
-       my $label = $self->get_object("label_laptime$row")
-               or die "Can't get label_laptime$row";
-       $label->set_markup('<span font_desc="18" weight="bold">'.$text.'</span>');
+       $self->set_label('label_laptime', $row, $text);
 }
 
 sub set_avg_lap {
        my ($self, $row, $text) = @_;
 
-       ++$row;
-       my $label = $self->get_object("label_avg_lap$row")
-               or die "Can't get label_avg_lap$row";
-       $label->set_markup('<span font_desc="22" weight="bold">'.$text.'</span>');
+       $self->set_label('label_avg_lap', $row, $text);
 }
 
 sub set_best_lap {
-       my ($self, $row, $text, $global) = @_;
-
-       ++$row;
-       my $label = $self->get_object("label_best_lap$row")
-               or die "Can't get label_best_lap$row";
-
-       if ($global) {
-               $global = ' foreground="#e00000"';
-       } else {
-               $global = '';
-       }
+       my ($self, $row, $text) = @_;
 
-       $label->set_markup('<span font_desc="18" weight="bold"'.$global.'>'
-               .$text.'</span>');
+       $self->set_label('label_best_lap', $row, $text);
 }
 
-sub set_driver {
+sub set_name {
        my ($self, $row, $text) = @_;
 
-       ++$row;
-       my $label = $self->get_object("label_name$row")
-               or die "Can't get label_lap$row";
-       $label->set_markup('<span font_desc="18" weight="bold">'.$text.'</span>');
+       $self->set_label('label_name', $row, $text);
 }
 
 sub enter_pit_lane {
@@ -272,17 +266,13 @@ sub show_semaphore {
 sub rounds {
        my ($self, $text) = @_;
 
-       my $label = $self->get_object("label_lap_global")
-               or die "Can't get label_lap_global";
-       $label->set_markup('<span font_desc="18" weight="bold">'.$text.'</span>');
+       $self->set_label('label_lap_global', undef, $text);
 }
 
 sub time {
        my ($self, $text) = @_;
 
-       my $label = $self->get_object("label_time_global")
-               or die "Can't get label_time_global";
-       $label->set_markup('<span font_desc="18" weight="bold">'.$text.'</span>');
+       $self->set_label('label_time_global', undef, $text);
 }
 
 sub best_lap {
@@ -291,10 +281,7 @@ sub best_lap {
        my $text = $time;
        $text .= ' by ' . $who
                if defined $who;
-
-       my $label = $self->get_object("label_best_lap_global")
-               or die "Can't get label_best_lap_global";
-       $label->set_markup('<span font_desc="18" weight="bold">'.$text.'</span>');
+       $self->set_label('label_best_lap_global', undef, $text);
 }
 
 1;
index ce10796173d122317bf43cf8e78f6489dfe9ab2d..cccbcf67256bf873b292d53e7e4435df3b0d1f59 100644 (file)
             <child>
               <object class="GtkFrame" id="frame1">
                 <property name="visible">True</property>
-                <property name="label_xalign">0.5</property>
+                <property name="label_xalign">0</property>
                 <child>
                   <object class="GtkAlignment" id="alignment1">
                     <property name="visible">True</property>
                 </child>
               </object>
               <packing>
+                <property name="padding">5</property>
                 <property name="position">0</property>
               </packing>
             </child>
             <child>
               <object class="GtkFrame" id="frame2">
                 <property name="visible">True</property>
-                <property name="label_xalign">0.5</property>
+                <property name="label_xalign">0</property>
                 <child>
                   <object class="GtkAlignment" id="alignment2">
                     <property name="visible">True</property>
                 </child>
               </object>
               <packing>
+                <property name="padding">5</property>
                 <property name="position">1</property>
               </packing>
             </child>
             <child>
               <object class="GtkFrame" id="frame3">
                 <property name="visible">True</property>
-                <property name="label_xalign">0.5</property>
+                <property name="label_xalign">0</property>
                 <child>
                   <object class="GtkAlignment" id="alignment3">
                     <property name="visible">True</property>
                 </child>
               </object>
               <packing>
+                <property name="padding">5</property>
                 <property name="position">2</property>
               </packing>
             </child>
           </object>
           <packing>
             <property name="expand">False</property>
+            <property name="padding">10</property>
             <property name="position">1</property>
           </packing>
         </child>