Adding logic to change box.

This commit is contained in:
Sergiotarxz 2024-03-08 02:56:51 +01:00
parent 3a9326d909
commit 90a36b9547
3 changed files with 86 additions and 29 deletions

View File

@ -24,9 +24,9 @@ has _box => (
required => 1,
);
has _pokemons => (
is => 'rw',
);
has number => ( is => 'ro', );
has _pokemons => ( is => 'rw', );
sub wallpaper {
my $self = shift;
@ -47,20 +47,22 @@ sub name {
}
sub get_pokemon {
my $self = shift;
my $number = shift;
my $self = shift;
my $number = shift;
my $pokemons = $self->_pokemons;
if (!defined $pokemons) {
if ( !defined $pokemons ) {
$pokemons = [];
$self->_pokemons($pokemons);
}
if ($number < 0 || $number > 29) {
die "Pokemon boxes can only hold pokemon from 0 to 29, index $number invalid.";
if ( $number < 0 || $number > 29 ) {
die
"Pokemon boxes can only hold pokemon from 0 to 29, index $number invalid.";
}
if (defined $pokemons->[$number]) {
if ( defined $pokemons->[$number] ) {
return $pokemons->[$number];
}
my $return = GEmeTool::Save::Pokemon->new( _pokemon => $self->_box->[$number] );
my $return =
GEmeTool::Save::Pokemon->new( _pokemon => $self->_box->[$number] );
$pokemons->[$number] = $return;
return $return;
}

View File

@ -20,9 +20,7 @@ has _save => (
required => 1,
);
has _boxes => (
is => 'rw',
);
has _boxes => ( is => 'rw', );
sub boxes {
my $self = shift;
@ -34,29 +32,29 @@ sub boxes {
return \@boxes;
}
sub get_box {
my $self = shift;
my $self = shift;
my $number = shift;
my $boxes = $self->_boxes;
if (!defined $boxes) {
$self->_boxes([]);
my $boxes = $self->_boxes;
if ( !defined $boxes ) {
$self->_boxes( [] );
$boxes = $self->_boxes;
}
if ($number < 0 || $number > 13) {
if ( $number < 0 || $number > 13 ) {
die "Bad box number $number.";
}
if (defined $boxes->[$number]) {
if ( defined $boxes->[$number] ) {
return $boxes->[$number];
}
my $pc = $self->_pc;
my $pc = $self->_pc;
my $wallpaper = \$pc->{wallpapers}[$number];
my $name = \$pc->{boxes_names}[$number];
my $box = $pc->{boxes}[$number];
my $return = GEmeTool::Save::PokemonBox->new(
my $return = GEmeTool::Save::PokemonBox->new(
_wallpaper => $wallpaper,
_name => $name,
_box => $box
_box => $box,
number => $number,
);
$boxes->[$number] = $return;
return $return;
@ -64,6 +62,6 @@ sub get_box {
sub save {
my $self = shift;
$self->_save->($self->_pc);
$self->_save->( $self->_pc );
}
1;

View File

@ -30,12 +30,21 @@ has _save => (
required => 1,
);
has _current_box => (
is => 'rw',
);
my $BOX_X = 275;
my $BOX_Y = 50;
my $BOX_WIDTH = 312;
my $BOX_HEIGHT = 340;
my $ARROW_X = 28;
my $ARROW_Y = 6;
my $ARROW_WIDTH = 16;
my $ARROW_HEIGHT = 32;
has _cursor_position => ( is => 'rw' );
my $root = path(__FILE__)->parent->parent->parent->parent;
@ -44,6 +53,17 @@ sub start {
my $self = shift;
my $gtk_window = Gtk4::Window->new;
my $controller_motion = Gtk4::EventControllerMotion->new;
my $controller_gesture = Gtk4::GestureClick->new;
my $save = $self->_save;
my $pc = $save->get_pc_system;
my $box = $pc->get_box(0);
$controller_gesture->set_button(1);
$self->_current_box($box);
$controller_gesture->signal_connect(
pressed => sub {
$self->_on_primary_click(@_);
}
);
$controller_motion->signal_connect(
motion => sub {
my ( $x, $y ) = @_[ 1, 2 ];
@ -52,6 +72,7 @@ sub start {
);
my $canvas = Gtk4::DrawingArea->new;
$canvas->add_controller($controller_motion);
$canvas->add_controller($controller_gesture);
my $scale_factor_pc = 2.5;
my ($width, $height) = map { $_ * $scale_factor_pc } (256, 158);
$gtk_window->set_default_size( $width, $height );
@ -89,6 +110,42 @@ sub start {
);
}
sub _on_primary_click {
my $self = shift;
my ($gesture, $n_press, $x, $y) = @_;
return if $self->_check_click_next_box($gesture, $n_press, $x, $y);
}
sub _check_click_next_box {
my $self = shift;
my ($gesture, $n_press, $x, $y) = @_;
return if $x < $BOX_X;
return if $y < $BOX_Y;
$x -= $BOX_X;
$y -= $BOX_Y;
return if $x >= $BOX_WIDTH;
return if $y >= $BOX_HEIGHT;
my ($x_arrow, $width_arrow) = ($BOX_WIDTH - $ARROW_X, $BOX_WIDTH - $ARROW_X + $ARROW_WIDTH);
my ($y_arrow, $height_arrow) = ($ARROW_Y, $ARROW_Y + $ARROW_HEIGHT);
my $x_matches = $x >= $BOX_WIDTH - $ARROW_X && $x <= $BOX_WIDTH - $ARROW_X + $ARROW_WIDTH;
my $y_matches = $y >= $ARROW_Y && $y <= $ARROW_Y + $ARROW_HEIGHT;
if ($x_matches && $y_matches) {
my $box = $self->_current_box;
my $save = $self->_save;
my $pc = $save->get_pc_system;
if ($box->number == 13) {
$self->_current_box($pc->get_box(0));
return 1;
}
$self->_current_box($pc->get_box($box->number+1));
return 1;
}
}
sub draw_pokemon_details {
my $self = shift;
my $cairo = shift;
@ -145,7 +202,7 @@ sub draw_box_name {
my $cairo = shift;
my $save = $self->_save;
my $pc = $save->get_pc_system;
my $box = $pc->get_box(0);
my $box = $self->_current_box;
my $name = Rsaves::translate_3rd_encoding( $box->name );
my $scale_factor = 0.95 * 2;
$cairo->scale( $scale_factor, $scale_factor );
@ -163,7 +220,7 @@ sub draw_pokemon {
my $selected_pokemon = shift;
my $save = $self->_save;
my $pc = $save->get_pc_system;
my $box = $pc->get_box(0);
my $box = $self->_current_box;
my $scale_factor = 0.95 * 2;
$cairo->scale( $scale_factor, $scale_factor );
@ -226,7 +283,7 @@ sub get_selected_pokemon {
my $j = int( $x / ( $p + 1 ) );
my $save = $self->_save;
my $pc = $save->get_pc_system;
my $box = $pc->get_box(0);
my $box = $self->_current_box;
my $number = $i * 6 + $j;
return if $number > 29;
my $pokemon = $box->get_pokemon($number);
@ -246,11 +303,11 @@ sub draw_box {
$cairo->paint;
$surface = Cairo::ImageSurface->create_from_png(
$root->child('resources/right_arrow_pc.png') );
$cairo->set_source_surface( $surface, $BOX_WIDTH / 2 - 14, 3);
$cairo->set_source_surface( $surface, $BOX_WIDTH / 2 - $ARROW_X / 2, $ARROW_Y / 2);
$cairo->get_source()->set_filter('nearest');
$cairo->paint;
$cairo->scale(-1, 1);
$cairo->set_source_surface( $surface, - 14, 3);
$cairo->set_source_surface( $surface, - $ARROW_X / 2, $ARROW_Y / 2);
$cairo->get_source()->set_filter('nearest');
$cairo->paint;
$cairo->scale(-1, 1);