#!/usr/bin/perl -w
package SCX::GUI;
use strict;
use utf8;
use Carp;
use Encode;
use Gtk2 '-init';
use Gtk2::Gdk::Keysyms;
use Glib qw(TRUE FALSE);
our $BASE_SIZE;
sub new {
my ($class, $args) = @_;
my $self = {
img_height => $args->{img_height} || 100,
builder => Gtk2::Builder->new,
semaphore_win => Gtk2::Window->new('popup'),
semaphore_img => Gtk2::Image->new,
};
bless $self, $class;
$self->{builder}->add_from_file('slotcarman.glade');
$self->{builder}->connect_signals($self);
$self->load_all_images;
$self->set_font_sizes;
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->get_object('slotcarman')
);
$self->get_object('image_car1')->signal_connect(
'size_allocate' => \&watch_resize,
$self,
);
return $self;
}
sub show {
my ($self) = @_;
my $window = $self->{builder}->get_object('slotcarman');
$window->signal_connect(delete_event => \&delete_event);
$window->show();
}
sub delete_event { Gtk2->main_quit; }
sub quit { Gtk2->main_quit; }
use Data::Dumper;
sub hotkey {
my ($window, $key, $self) = @_;
if ($key->keyval == $Gtk2::Gdk::Keysyms{F5}) {
if ($self->{fullscreen}) {
leave_fullscreen($window, $self);
} else {
fullscreen($window, $self);
}
return 1;
}
return 0;
}
sub fullscreen {
my ($widget, $self) = @_;
return if $self->{fullscreen};
$self->get_object('menubar1')->hide;
$self->get_object('statusbar1')->hide;
$self->get_object('slotcarman')->fullscreen;
$self->{fullscreen} = 1;
my $h = $self->get_object('image_car1')->allocation->height;
my $h1 = $self->get_object('slotcarman')->allocation->height;
print "h=$h, h1=$h1\n";
}
sub leave_fullscreen {
my ($widget, $self) = @_;
return if !$self->{fullscreen};
$self->get_object('menubar1')->show;
$self->get_object('statusbar1')->show;
$self->get_object('slotcarman')->unfullscreen;
$self->{fullscreen} = undef;;
my $h = $self->get_object('image_car1')->allocation->height;
my $h1 = $self->get_object('slotcarman')->allocation->height;
print "h=$h, h1=$h1\n";
}
sub watch_resize {
my ($window, $rectangle, $self) = @_;
print "w=", $rectangle->width, ", h=", $rectangle->height, "\n";
# TODO
}
sub get_object { return shift->{builder}->get_object(@_); }
sub set_font_sizes {
my ($self) = @_;
if ($self->{img_height} <= 80) {
$self->{label_size} = 10;
$self->{value_size} = 16;
$self->{lap_size} = 24;
} elsif ($self->{img_height} <= 100) {
$self->{label_size} = 12;
$self->{value_size} = 20;
$self->{lap_size} = 28;
} elsif ($self->{img_height} <= 120) {
$self->{label_size} = 14;
$self->{value_size} = 24;
$self->{lap_size} = 32;
} elsif ($self->{img_height} <= 150) {
$self->{label_size} = 16;
$self->{value_size} = 28;
$self->{lap_size} = 36;
} else {
$self->{label_size} = 18;
$self->{value_size} = 32;
$self->{lap_size} = 40;
}
}
sub load_all_images {
my ($self) = @_;
my $h = $self->{img_height};
$self->{throttle_images} = [
load_image_set('img/throttle%d.svg', 13, $h)
];
$self->{throttle_button_images} = [
load_image_set('img/throttle%dbutton.svg', 12, $h)
];
$self->{fuel_images} = [
load_image_set('img/fuel%d.svg', 8, int(0.6 * $h))
];
$self->{car_images} = load_images_glob('img/SCXCars/*', $h);
$self->{state_images} = load_images_glob('img/state*.svg', $h);
$self->{semaphore_images} = [
load_image_set('img/semaphore%d.svg', 5, 1.5 * $h)
];
};
sub load_image_set {
my ($pattern, $limit, $height) = @_;
my @rv;
for my $i (0..$limit) {
my $file = sprintf($pattern, $i);
my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file);
my $width = $dummy->get_width * $height / $dummy->get_height;
push @rv, Gtk2::Gdk::Pixbuf->new_from_file_at_scale(
$file, $width, $height, TRUE
);
}
return @rv;
}
sub load_images_glob {
my ($glob, $height) = @_;
my %rv;
for my $file (glob($glob)) {
$file = Encode::decode('utf-8', $file);
my $name = $file;
$name =~ s/.*\///;
$name =~ s/\..*?$//;
$name =~ s/[-_]/ /g;
$name =~ s/\s+/ /g;
my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file)
or next;
my $width = $dummy->get_width * $height / $dummy->get_height;
$rv{$name} = Gtk2::Gdk::Pixbuf->new_from_file_at_scale(
$file, $width, $height, TRUE
);
}
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) = @_;
return if !defined $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, $button) = @_;
$val = 13 if !defined $val;
$self->set_image('image_throttle', $row,
$button ? 'throttle_button' : 'throttle', $val);
}
sub set_fuel {
my ($self, $row, $val) = @_;
return if $val > 8;
$self->set_image('image_fuel', $row, 'fuel', $val || 0);
}
sub set_car_icon {
my ($self, $row, $name) = @_;
$self->set_image('image_car', $row, 'car', $name);
}
sub set_lap {
my ($self, $row, $text) = @_;
$text = '--' if !defined $text;
$self->set_label('label_lap', $row, $text, 'lap');
}
sub set_distance {
my ($self, $row, $time, $lap_diff, $time_diff, $greyout) = @_;
my $text = '--';
if (defined $time) {
$text = format_race_time($time);
} elsif (defined $time_diff) {
$text = '+' . format_lap_time($time_diff);
} elsif (defined $lap_diff) {
$text = '+' . $lap_diff
. ($lap_diff == 1 ? ' lap' : ' laps');
}
if ($greyout) {
$text = '' . $text . '';
}
$self->set_label('label_distance', $row, $text);
}
sub set_laptime {
my ($self, $row, $text) = @_;
$self->set_label('label_laptime', $row, format_lap_time($text));
}
sub set_best_lap {
my ($self, $row, $text, $overall_best) = @_;
$text = format_lap_time($text);
if ($overall_best) {
$text = '' . $text . '';
}
$self->set_label('label_best_lap', $row, $text);
}
sub set_name {
my ($self, $row, $text) = @_;
$self->set_label('label_name', $row, $text);
}
sub set_car_id {
my ($self, $row, $text) = @_;
$self->set_label('label_car_id', $row, $text);
}
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;
}
}
sub rounds {
my ($self, $text) = @_;
$self->set_label('label_lap_global', undef, $text);
}
sub time {
my ($self, $text) = @_;
$self->set_label('label_time_global', undef, format_race_time($text));
}
sub best_lap {
my ($self, $time, $who) = @_;
my $text = format_lap_time($time);
$text .= ' by ' . $who
if defined $who;
$self->set_label('label_best_lap_global', undef, $text);
}
sub format_race_time {
my ($seconds) = @_;
return '--' if !defined $seconds;
my $hours = int($seconds / 3600);
$seconds -= 3600 * $hours;
my $mins = int($seconds / 60);
$seconds -= 60 * $mins;
return $hours
? sprintf('%d:%02d:%04.1f', $hours, $mins, $seconds)
: sprintf('%d:%04.1f', $mins, $seconds);
}
sub format_lap_time {
my ($seconds) = @_;
return defined $seconds
? sprintf('%.2f', $seconds)
: '--';
}
sub set_state {
my ($self, $num, $name) = @_;
$self->set_image('image_status', $num, 'state', 'state '.$name);
}
1;