11 use Gtk2::Gdk::Keysyms;
12 use Glib qw(TRUE FALSE);
17 my ($class, $args) = @_;
20 img_height => $args->{img_height} || 100,
21 builder => Gtk2::Builder->new,
22 semaphore_win => Gtk2::Window->new('popup'),
23 semaphore_img => Gtk2::Image->new,
28 $self->{builder}->add_from_file('slotcarman.glade');
30 $self->{builder}->connect_signals($self);
31 $self->load_all_images;
32 $self->set_font_sizes;
34 my $pixbuf = $self->{semaphore_images}->[0];
35 $self->{semaphore_img}->set_from_pixbuf($pixbuf);
36 $self->{semaphore_img}->show;
37 $self->{semaphore_win}->add($self->{semaphore_img});
38 $self->{semaphore_win}->set_type_hint('splashscreen');
39 $self->{semaphore_win}->set_position('center-on-parent');
40 $self->{semaphore_win}->set_transient_for(
41 $self->get_object('slotcarman')
44 $self->get_object('image_car1')->signal_connect(
45 'size_allocate' => \&watch_resize,
55 my $window = $self->{builder}->get_object('slotcarman');
56 $window->signal_connect(delete_event => \&delete_event);
60 sub delete_event { Gtk2->main_quit; }
62 sub quit { Gtk2->main_quit; }
67 my ($window, $key, $self) = @_;
69 if ($key->keyval == $Gtk2::Gdk::Keysyms{F5}) {
70 if ($self->{fullscreen}) {
71 leave_fullscreen($window, $self);
73 fullscreen($window, $self);
81 my ($widget, $self) = @_;
83 return if $self->{fullscreen};
85 $self->get_object('menubar1')->hide;
86 $self->get_object('statusbar1')->hide;
87 $self->get_object('slotcarman')->fullscreen;
89 $self->{fullscreen} = 1;
90 my $h = $self->get_object('image_car1')->allocation->height;
91 my $h1 = $self->get_object('slotcarman')->allocation->height;
92 print "h=$h, h1=$h1\n";
95 sub leave_fullscreen {
96 my ($widget, $self) = @_;
98 return if !$self->{fullscreen};
100 $self->get_object('menubar1')->show;
101 $self->get_object('statusbar1')->show;
102 $self->get_object('slotcarman')->unfullscreen;
104 $self->{fullscreen} = undef;;
105 my $h = $self->get_object('image_car1')->allocation->height;
106 my $h1 = $self->get_object('slotcarman')->allocation->height;
107 print "h=$h, h1=$h1\n";
111 my ($window, $rectangle, $self) = @_;
112 print "w=", $rectangle->width, ", h=", $rectangle->height, "\n";
116 sub get_object { return shift->{builder}->get_object(@_); }
121 if ($self->{img_height} <= 80) {
122 $self->{label_size} = 10;
123 $self->{value_size} = 16;
124 $self->{lap_size} = 24;
125 } elsif ($self->{img_height} <= 100) {
126 $self->{label_size} = 12;
127 $self->{value_size} = 20;
128 $self->{lap_size} = 28;
129 } elsif ($self->{img_height} <= 120) {
130 $self->{label_size} = 14;
131 $self->{value_size} = 24;
132 $self->{lap_size} = 32;
133 } elsif ($self->{img_height} <= 150) {
134 $self->{label_size} = 16;
135 $self->{value_size} = 28;
136 $self->{lap_size} = 36;
138 $self->{label_size} = 18;
139 $self->{value_size} = 32;
140 $self->{lap_size} = 40;
144 sub load_all_images {
147 my $h = $self->{img_height};
149 $self->{throttle_images} = [
150 load_image_set('img/throttle%d.svg', 13, $h)
152 $self->{throttle_button_images} = [
153 load_image_set('img/throttle%dbutton.svg', 12, $h)
155 $self->{fuel_images} = [
156 load_image_set('img/fuel%d.svg', 8, int(0.6 * $h))
158 $self->{car_images} = load_images_glob('img/SCXCars/*', $h);
159 $self->{state_images} = load_images_glob('img/state*.svg', $h);
160 $self->{semaphore_images} = [
161 load_image_set('img/semaphore%d.svg', 5, 1.5 * $h)
166 my ($pattern, $limit, $height) = @_;
169 for my $i (0..$limit) {
170 my $file = sprintf($pattern, $i);
172 my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file);
173 my $width = $dummy->get_width * $height / $dummy->get_height;
174 push @rv, Gtk2::Gdk::Pixbuf->new_from_file_at_scale(
175 $file, $width, $height, TRUE
181 sub load_images_glob {
182 my ($glob, $height) = @_;
185 for my $file (glob($glob)) {
186 $file = Encode::decode('utf-8', $file);
194 my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file)
197 my $width = $dummy->get_width * $height / $dummy->get_height;
198 $rv{$name} = Gtk2::Gdk::Pixbuf->new_from_file_at_scale(
199 $file, $width, $height, TRUE
206 my ($self, $prefix, $row, $text, $size) = @_;
208 ++$row if defined $row && $row =~ /\A\d+\z/xms;
211 my $name = defined $row ? $prefix.$row : $prefix;
212 my $label = $self->get_object($name)
213 or croak "Can't get label named $name";
214 $label->set_markup('<span font_desc="' . $self->{$size.'_size'} . '"'
215 . ' weight="bold">' . $text . '</span>');
219 my ($self, $prefix, $row, $imgset, $value) = @_;
221 return if !defined $value;
223 ++$row if defined $row && $row =~ /\A\d+\z/xms;
225 my $name = defined $row ? $prefix.$row : $prefix;
227 my $image = $self->get_object($name)
228 or croak "Can't get image named $name";
230 my $set = $self->{$imgset.'_images'};
231 my $pixbuf = (ref($set) eq 'ARRAY')
235 $image->set_from_pixbuf($pixbuf);
239 my ($self, $row, $val, $button) = @_;
241 $val = 13 if !defined $val;
242 $self->set_image('image_throttle', $row,
243 $button ? 'throttle_button' : 'throttle', $val);
247 my ($self, $row, $val) = @_;
251 $self->set_image('image_fuel', $row, 'fuel', $val || 0);
255 my ($self, $row, $name) = @_;
257 $self->set_image('image_car', $row, 'car', $name);
261 my ($self, $row, $text) = @_;
263 $text = '--' if !defined $text;
265 $self->set_label('label_lap', $row, $text, 'lap');
269 my ($self, $row, $time, $lap_diff, $time_diff, $greyout) = @_;
274 $text = format_race_time($time);
275 } elsif (defined $time_diff) {
276 $text = '+' . format_lap_time($time_diff);
277 } elsif (defined $lap_diff) {
278 $text = '+' . $lap_diff
279 . ($lap_diff == 1 ? ' lap' : ' laps');
283 $text = '<span fgcolor="grey">' . $text . '</span>';
286 $self->set_label('label_distance', $row, $text);
290 my ($self, $row, $text) = @_;
292 $self->set_label('label_laptime', $row, format_lap_time($text));
296 my ($self, $row, $text) = @_;
298 $self->set_label('label_best_lap', $row, format_lap_time($text));
302 my ($self, $row, $text) = @_;
304 $self->set_label('label_name', $row, $text);
308 my ($self, $row, $text) = @_;
310 $self->set_label('label_car_id', $row, $text);
314 my ($self, $num) = @_;
316 my $win = $self->{semaphore_win};
318 my $img = $self->{semaphore_img};
319 my $pixbuf = $self->{semaphore_images}->[$num];
320 $img->set_from_pixbuf($pixbuf);
328 my ($self, $text) = @_;
330 $self->set_label('label_lap_global', undef, $text);
334 my ($self, $text) = @_;
336 $self->set_label('label_time_global', undef, format_race_time($text));
340 my ($self, $time, $who) = @_;
342 my $text = format_lap_time($time);
343 $text .= ' by ' . $who
345 $self->set_label('label_best_lap_global', undef, $text);
348 sub format_race_time {
351 return '--' if !defined $seconds;
353 my $hours = int($seconds / 3600);
354 $seconds -= 3600 * $hours;
356 my $mins = int($seconds / 60);
357 $seconds -= 60 * $mins;
360 ? sprintf('%d:%02d:%04.1f', $hours, $mins, $seconds)
361 : sprintf('%d:%04.1f', $mins, $seconds);
364 sub format_lap_time {
367 return defined $seconds
368 ? sprintf('%.2f', $seconds)
373 my ($self, $num, $name) = @_;
375 $self->set_image('image_status', $num, 'state', 'state '.$name);