From 3043b1b70f247549f10fa5e3ce713c2a80cd2200 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 6 Dec 2010 23:41:28 +0100 Subject: [PATCH] Start semaphore tweaks. --- SCX/GUI.pm | 29 +++++++++++++++++++++++++++++ SCX/Track.pm | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/SCX/GUI.pm b/SCX/GUI.pm index 0945af8..5738431 100755 --- a/SCX/GUI.pm +++ b/SCX/GUI.pm @@ -22,7 +22,12 @@ sub new { load_image_set('img/fuel%d.svg', 8, 60) ], car_images => load_image_dir('img/SCXCars', 100), + semaphore_images => [ + load_image_set('img/semaphore%d.svg', 5, 200) + ], builder => Gtk2::Builder->new, + semaphore_win => Gtk2::Window->new('popup'), + semaphore_img => Gtk2::Image->new, }; bless $self, $class; @@ -31,6 +36,16 @@ sub new { $self->{builder}->connect_signals(undef); + my $pixbuf = $self->{semaphore_images}->[0]; + $self->{semaphore_img}->set_from_pixbuf($pixbuf); + $self->{semaphore_img}->show; + $self->{semaphore_win}->add($self->{semaphore_img}); + $self->{semaphore_win}->set_type_hint('splashscreen'); + $self->{semaphore_win}->set_position('center-on-parent'); + $self->{semaphore_win}->set_transient_for( + $self->{builder}->get_object('slotcarman') + ); + return $self; } @@ -166,5 +181,19 @@ sub leave_pit_lane { $self->set_laptime($row, '--'); } +sub show_semaphore { + my ($self, $num) = @_; + + my $win = $self->{semaphore_win}; + if (defined $num) { + my $img = $self->{semaphore_img}; + my $pixbuf = $self->{semaphore_images}->[$num]; + $img->set_from_pixbuf($pixbuf); + $win->show; + } else { + $win->hide; + } +} + 1; diff --git a/SCX/Track.pm b/SCX/Track.pm index f6200de..c756592 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -5,8 +5,11 @@ package SCX::Track; use strict; use Carp; +use Glib qw(TRUE FALSE); use SCX::Car; +our $SEMAPHORE_STEP = 1000; + sub new { my ($class, $args) = @_; @@ -31,8 +34,36 @@ sub car { return shift->{cars}->[shift]; } sub race_start { my ($self) = @_; - $self->{race_running} = 1; + return if $self->{race_running} || $self->{start_in_progress}; + $self->{race_running} = 0; + $self->{start_in_progress} = 1; + $self->{semaphore} = 0; + $self->{gui}->show_semaphore(0); + Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self); +} +sub semaphore_step { + my ($self) = @_; + + return if !$self->{start_in_progress} && !$self->{race_running}; + + $self->{semaphore}++; + if ($self->{semaphore} <= 5) { + $self->{gui}->show_semaphore($self->{semaphore}); + my $timeout = $SEMAPHORE_STEP; + $timeout += $SEMAPHORE_STEP * rand() + if $self->{semaphore} == 5; + Glib::Timeout->add($timeout, \&semaphore_step, $self); + } elsif ($self->{semaphore} == 6) { + $self->{race_running} = 1; + $self->{start_in_progress} = undef; + $self->{gui}->show_semaphore(0); + Glib::Timeout->add(3*$SEMAPHORE_STEP, \&semaphore_step, $self); + } else { + $self->{gui}->show_semaphore(undef); + $self->{semaphore} = undef; + } + return FALSE; } sub race_end { @@ -41,5 +72,6 @@ sub race_end { $self->{race_running} = 0; } + 1; -- 2.43.5