]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/GUI.pm
Time difference prefix + instead of -
[slotcarman.git] / SCX / GUI.pm
1 #!/usr/bin/perl -w
2
3 package SCX::GUI;
4
5 use strict;
6 use utf8;
7
8 use Carp;
9 use Encode;
10 use Gtk2 '-init';
11 use Gtk2::Gdk::Keysyms;
12 use Glib qw(TRUE FALSE);
13
14 our $BASE_SIZE;
15
16 sub new {
17         my ($class, $args) = @_;
18
19         my $self = {
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,
24         };
25
26         bless $self, $class;
27
28         $self->{builder}->add_from_file('slotcarman.glade');
29
30         $self->{builder}->connect_signals($self);
31         $self->load_all_images;
32         $self->set_font_sizes;
33
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')
42         );
43
44         $self->get_object('image_car1')->signal_connect(
45                 'size_allocate' => \&watch_resize,
46                 $self,
47         );
48         
49         return $self;
50 }
51
52 sub show {
53         my ($self) = @_;
54
55         my $window = $self->{builder}->get_object('slotcarman');
56         $window->signal_connect(delete_event => \&delete_event);
57         $window->show();
58 }
59
60 sub delete_event { Gtk2->main_quit; }
61
62 sub quit { Gtk2->main_quit; }
63
64 use Data::Dumper;
65
66 sub hotkey {
67         my ($window, $key, $self) = @_;
68
69         if ($key->keyval == $Gtk2::Gdk::Keysyms{F5}) {
70                 if ($self->{fullscreen}) {
71                         leave_fullscreen($window, $self);
72                 } else {
73                         fullscreen($window, $self);
74                 }
75                 return 1;
76         }
77         return 0;
78 }
79
80 sub fullscreen {
81         my ($widget, $self) = @_;
82
83         return if $self->{fullscreen};
84
85         $self->get_object('menubar1')->hide;
86         $self->get_object('statusbar1')->hide;
87         $self->get_object('slotcarman')->fullscreen;
88
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";
93 }
94
95 sub leave_fullscreen {
96         my ($widget, $self) = @_;
97
98         return if !$self->{fullscreen};
99
100         $self->get_object('menubar1')->show;
101         $self->get_object('statusbar1')->show;
102         $self->get_object('slotcarman')->unfullscreen;
103
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";
108 }
109
110 sub watch_resize {
111         my ($window, $rectangle, $self) = @_;
112         print "w=", $rectangle->width, ", h=", $rectangle->height, "\n";
113         # TODO
114 }
115
116 sub get_object { return shift->{builder}->get_object(@_); }
117
118 sub set_font_sizes {
119         my ($self) = @_;
120
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;
137         } else {
138                 $self->{label_size} = 18;
139                 $self->{value_size} = 32;
140                 $self->{lap_size} = 40;
141         }
142 }
143
144 sub load_all_images {
145         my ($self) = @_;
146
147         my $h = $self->{img_height};
148
149         $self->{throttle_images} = [
150                 load_image_set('img/throttle%d.svg', 13, $h)
151         ];
152         $self->{throttle_button_images} = [
153                 load_image_set('img/throttle%dbutton.svg', 12, $h)
154         ];
155         $self->{fuel_images} = [
156                         load_image_set('img/fuel%d.svg', 8, int(0.6 * $h))
157         ];
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)
162         ];
163 };
164
165 sub load_image_set {
166         my ($pattern, $limit, $height) = @_;
167
168         my @rv;
169         for my $i (0..$limit) {
170                 my $file = sprintf($pattern, $i);
171                 
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
176                 );
177         }
178         return @rv;
179 }
180
181 sub load_images_glob {
182         my ($glob, $height) = @_;
183
184         my %rv;
185         for my $file (glob($glob)) {
186                 $file = Encode::decode('utf-8', $file);
187                 my $name = $file;
188                 $name =~ s/.*\///;
189                 $name =~ s/\..*?$//;
190                 $name =~ s/[-_]/ /g;
191                 $name =~ s/\s+/ /g;
192
193
194                 my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file)
195                         or next;
196
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
200                 );
201         }
202         return \%rv;
203 }
204
205 sub set_label {
206         my ($self, $prefix, $row, $text, $size) = @_;
207
208         ++$row if defined $row && $row =~ /\A\d+\z/xms;
209         $size //= 'value';
210
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>');
216 }
217
218 sub set_image {
219         my ($self, $prefix, $row, $imgset, $value) = @_;
220
221         return if !defined $value;
222
223         ++$row if defined $row && $row =~ /\A\d+\z/xms;
224
225         my $name = defined $row ? $prefix.$row : $prefix;
226
227         my $image = $self->get_object($name)
228                 or croak "Can't get image named $name";
229
230         my $set = $self->{$imgset.'_images'};
231         my $pixbuf = (ref($set) eq 'ARRAY')
232                 ? $set->[$value]
233                 : $set->{$value};
234                 
235         $image->set_from_pixbuf($pixbuf);
236 }
237
238 sub set_throttle {
239         my ($self, $row, $val, $button) = @_;
240
241         $val = 13 if !defined $val;
242         $self->set_image('image_throttle', $row,
243                 $button ? 'throttle_button' : 'throttle', $val);
244 }
245
246 sub set_fuel {
247         my ($self, $row, $val) = @_;
248
249         return if $val > 8;
250
251         $self->set_image('image_fuel', $row, 'fuel', $val || 0);
252 }
253
254 sub set_car_icon {
255         my ($self, $row, $name) = @_;
256
257         $self->set_image('image_car', $row, 'car', $name);
258 }
259
260 sub set_lap {
261         my ($self, $row, $text) = @_;
262
263         $text = '--' if !defined $text;
264
265         $self->set_label('label_lap', $row, $text, 'lap');
266 }
267
268 sub set_distance {
269         my ($self, $row, $time, $lap_diff, $time_diff, $greyout) = @_;
270
271         my $text = '--';
272
273         if (defined $time) {
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');
280         }
281
282         if ($greyout) {
283                 $text = '<span fgcolor="grey">' . $text . '</span>';
284         }
285
286         $self->set_label('label_distance', $row, $text);
287 }
288
289 sub set_laptime {
290         my ($self, $row, $text) = @_;
291
292         $self->set_label('label_laptime', $row, format_lap_time($text));
293 }
294
295 sub set_best_lap {
296         my ($self, $row, $text) = @_;
297
298         $self->set_label('label_best_lap', $row, format_lap_time($text));
299 }
300
301 sub set_name {
302         my ($self, $row, $text) = @_;
303
304         $self->set_label('label_name', $row, $text);
305 }
306
307 sub set_car_id {
308         my ($self, $row, $text) = @_;
309
310         $self->set_label('label_car_id', $row, $text);
311 }
312
313 sub show_semaphore {
314         my ($self, $num) = @_;
315
316         my $win = $self->{semaphore_win};
317         if (defined $num) {
318                 my $img = $self->{semaphore_img};
319                 my $pixbuf = $self->{semaphore_images}->[$num];
320                 $img->set_from_pixbuf($pixbuf);
321                 $win->show;
322         } else {
323                 $win->hide;
324         }
325 }
326
327 sub rounds {
328         my ($self, $text) = @_;
329
330         $self->set_label('label_lap_global', undef, $text);
331 }
332
333 sub time {
334         my ($self, $text) = @_;
335
336         $self->set_label('label_time_global', undef, format_race_time($text));
337 }
338
339 sub best_lap {
340         my ($self, $time, $who) = @_;
341
342         my $text = format_lap_time($time);
343         $text .= ' by ' . $who
344                 if defined $who;
345         $self->set_label('label_best_lap_global', undef, $text);
346 }
347
348 sub format_race_time {
349         my ($seconds) = @_;
350
351         return '--' if !defined $seconds;
352
353         my $hours = int($seconds / 3600);
354         $seconds -= 3600 * $hours;
355
356         my $mins = int($seconds / 60);
357         $seconds -= 60 * $mins;
358
359         return $hours
360                 ? sprintf('%d:%02d:%04.1f', $hours, $mins, $seconds)
361                 : sprintf('%d:%04.1f', $mins, $seconds);
362 }
363
364 sub format_lap_time {
365         my ($seconds) = @_;
366
367         return defined $seconds
368                 ? sprintf('%.2f', $seconds)
369                 : '--';
370 }
371
372 sub set_state {
373         my ($self, $num, $name) = @_;
374
375         $self->set_image('image_status', $num, 'state', 'state '.$name);
376 }
377
378 1;
379