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_avg_lap', $row, format_lap_time($text));
302 my ($self, $row, $text) = @_;
304 $self->set_label('label_best_lap', $row, format_lap_time($text));
308 my ($self, $row, $text) = @_;
310 $self->set_label('label_name', $row, $text);
314 my ($self, $row, $text) = @_;
316 $self->set_label('label_car_id', $row, $text);
320 my ($self, $num) = @_;
322 my $win = $self->{semaphore_win};
324 my $img = $self->{semaphore_img};
325 my $pixbuf = $self->{semaphore_images}->[$num];
326 $img->set_from_pixbuf($pixbuf);
334 my ($self, $text) = @_;
336 $self->set_label('label_lap_global', undef, $text);
340 my ($self, $text) = @_;
342 $self->set_label('label_time_global', undef, format_race_time($text));
346 my ($self, $time, $who) = @_;
348 my $text = format_lap_time($time);
349 $text .= ' by ' . $who
351 $self->set_label('label_best_lap_global', undef, $text);
354 sub format_race_time {
357 return '--' if !defined $seconds;
359 my $hours = int($seconds / 3600);
360 $seconds -= 3600 * $hours;
362 my $mins = int($seconds / 60);
363 $seconds -= 60 * $mins;
366 ? sprintf('%d:%02d:%04.1f', $hours, $mins, $seconds)
367 : sprintf('%d:%04.1f', $mins, $seconds);
370 sub format_lap_time {
373 return defined $seconds
374 ? sprintf('%.2f', $seconds)
379 my ($self, $num, $name) = @_;
381 $self->set_image('image_status', $num, 'state', 'state '.$name);