From: Jan "Yenya" Kasprzak Date: Tue, 7 Dec 2010 18:32:59 +0000 (+0100) Subject: Refactorized GUI layer. X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=d311baf05cce488606946a68ffee19980681030b;p=slotcarman.git Refactorized GUI layer. --- diff --git a/SCX/Car.pm b/SCX/Car.pm index 7dffb7a..8bb7bb9 100644 --- a/SCX/Car.pm +++ b/SCX/Car.pm @@ -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}); diff --git a/SCX/GUI.pm b/SCX/GUI.pm index 4f02edc..3d500f6 100755 --- a/SCX/GUI.pm +++ b/SCX/GUI.pm @@ -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('' . $text . ''); +} + +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(''.$text.''); + $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(''.$text.''); + $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(''.$text.''); + $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('' - .$text.''); + $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(''.$text.''); + $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(''.$text.''); + $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(''.$text.''); + $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(''.$text.''); + $self->set_label('label_best_lap_global', undef, $text); } 1; diff --git a/slotcarman.glade b/slotcarman.glade index ce10796..cccbcf6 100644 --- a/slotcarman.glade +++ b/slotcarman.glade @@ -167,7 +167,7 @@ True - 0.5 + 0 True @@ -189,13 +189,14 @@ + 5 0 True - 0.5 + 0 True @@ -217,13 +218,14 @@ + 5 1 True - 0.5 + 0 True @@ -245,12 +247,14 @@ + 5 2 False + 10 1