GEmeTool/lib/GEmeTool/View/MainWindow.pm

286 lines
8.0 KiB
Perl

package GEmeTool::View::MainWindow;
use v5.16.3;
use strict;
use warnings;
use utf8;
use Glib;
use Glib::IO;
use Glib::Object::Introspection;
use Data::Dumper;
use Path::Tiny;
use GEmeTool::Options;
use GEmeTool::Save;
use Moo;
Glib::Object::Introspection->setup(
basename => 'Gtk',
version => '4.0',
package => 'Gtk4',
);
Glib::Object::Introspection->setup(
basename => 'Gdk',
version => '4.0',
package => 'Gdk',
);
use namespace::clean;
has _win => (
is => 'rw',
);
has _options => (
is => 'rw',
);
has _app => (
is => 'rw',
);
has _save => (
is => 'rw',
);
has _save_as => (
is => 'rw',
);
has _file => (
is => 'rw',
);
sub start {
my $self = shift;
my $options = GEmeTool::Options->new;
$self->_options($options);
my $app = Gtk4::Application->new( 'tech.owlcode.GEmeTool', 'default-flags' );
$self->_app($app);
$app->signal_connect( activate => sub {
$self->activate;
} );
$app->run;
}
sub activate {
my $self = shift;
my $display = Gdk::Display::get_default();
my $icon_theme = Gtk4::IconTheme::get_for_display($display);
$icon_theme->set_search_path(
path(__FILE__)->parent->parent->child('resources/icons')->absolute );
Gtk4::Window::set_default_icon_name('gemetool');
my $win;
my $menu = Glib::IO::Menu->new;
my $about = Glib::IO::SimpleAction->new( 'about', undef );
my $open = Glib::IO::SimpleAction->new( 'open', undef );
my $save_as = Glib::IO::SimpleAction->new( 'save_as', undef );
$save_as->set_enabled(0);
my $app = $self->_app;
$app->add_action($about);
$app->add_action($open);
$app->add_action($save_as);
$self->_save_as($save_as);
my $save;
my $extra;
my $save_menu_item = Glib::IO::MenuItem->new( 'Save as', 'app.save_as' );
$open->signal_connect(
activate => sub {
$self->activate_open;
}
);
$save_as->signal_connect(
activate => sub {
$self->activate_save;
}
);
$about->signal_connect( activate => \&activate_about, );
my $about_menu_item = Glib::IO::MenuItem->new( 'About', 'app.about' );
my $open_menu_item = Glib::IO::MenuItem->new( 'Open', 'app.open' );
my $submenu_file = Glib::IO::Menu->new;
my $submenu_help = Glib::IO::Menu->new;
$submenu_help->append_item($about_menu_item);
$submenu_file->append_item($open_menu_item);
$submenu_file->append_item($save_menu_item);
$menu->append_submenu( 'File', $submenu_file );
$menu->append_submenu( 'Help', $submenu_help );
$app->set_menubar($menu);
my $box = Gtk4::Box->new( 'vertical', 0 );
$box->append(
Gtk4::Label->new('You have to load a Pokemon Emerald Save.') );
$win = Gtk4::ApplicationWindow->new($app);
$self->_win($win);
$win->set_title('GEmeTool');
$win->set_show_menubar(1);
$win->set_default_size( 600, 600 );
$win->set_icon_name('gemetool');
$win->set_child($box);
$win->present;
}
sub activate_open {
my $self = shift;
my $cancellable = Glib::IO::Cancellable->new;
my $dialog = Gtk4::FileDialog->new;
my $options = $self->_options;
my $win = $self->_win;
my $last_dir = $options->get_last_dir_open;
if (defined $last_dir && -d $last_dir) {
my $curdir = Glib::IO::File::new_for_path($last_dir);
$dialog->set_initial_folder($curdir);
}
$dialog->open(
$win, $cancellable,
sub {
my ( $self_dialog, $res ) = @_;
if ($res->had_error) {
return;
}
my $file = $dialog->open_finish($res);
return if !defined $file;
$file = path( $file->get_path );
$self->_file($file);
$options->set_last_dir_open($file->parent.'');
$self->start_editing_file;
}
);
}
sub activate_save {
my $self = shift;
my $win = $self->_win;
my $dialog = Gtk4::FileDialog->new;
my $options = $self->_options;
my $last_dir = $options->get_last_dir_save;
if (defined $last_dir && -d $last_dir) {
my $curdir = Glib::IO::File::new_for_path($last_dir);
$dialog->set_initial_folder($curdir);
}
$dialog->save(
$win, undef,
sub {
my ( $self_dialog, $res ) = @_;
if ($res->had_error) {
return;
}
my $file = $dialog->save_finish($res);
return if !defined $file;
$file = path( $file->get_path );
$options->set_last_dir_save($file->parent.'');
$self->_save->save( $file);
}
);
}
sub save {
my $self = shift;
my $file = shift;
$self->_save->save($file);
}
sub start_editing_file {
my $self = shift;
my $file = $self->_file;
my $save_as = $self->_save_as;
my (@saves_raw);
my $box_app = Gtk4::Box->new( 'vertical', 0 );
$self->_save(GEmeTool::Save->instance($file));
my $save_obj = $self->_save;
my $search = Gtk4::SearchBar->new;
my $entry = Gtk4::Entry->new;
$search->set_child($entry);
$search->set_search_mode(1);
$box_app->append($search);
$box_app->set_property( 'vexpand', 1 );
my $scroll = Gtk4::ScrolledWindow->new;
$scroll->set_property( 'vexpand', 1 );
my $populate_func = sub {
my $box_flags = Gtk4::Box->new( 'vertical', 0 );
for my $rematch (@{$self->_save->get_rematches_save})
{
my $name = $rematch->{name};
my $value = $rematch->{value};
my $id = $rematch->{id};
my $buffer = $entry->get_buffer;
my $search_text = uc( $buffer->get_text );
next unless index( $name, $search_text ) >= 0;
my $toggle = Gtk4::ToggleButton->new_with_label($name);
$toggle->set_active($value);
$toggle->signal_connect(
toggled => sub {
my $active = $toggle->get_active;
$self->_save->set_rematch($id, $active);
}
);
$box_flags->append($toggle);
}
for my $flag (@{$self->_save->get_flags_save}) {
my $name = $flag->{name};
my $value = $flag->{value};
my $id = $flag->{id};
my $buffer = $entry->get_buffer;
my $search_text = uc( $buffer->get_text );
next unless index( $name, $search_text ) >= 0;
my $toggle = Gtk4::ToggleButton->new_with_label($name);
$toggle->set_active( $value );
$toggle->signal_connect(
toggled => sub {
my $active = $toggle->get_active;
$self->_save->set_flag($active);
}
);
$box_flags->append($toggle);
}
$scroll->set_child($box_flags);
};
$populate_func->();
$entry->signal_connect(
activate => sub {
$populate_func->();
}
);
$box_app->append($scroll);
my $win = $self->_win;
$win->set_child($box_app);
$save_as->set_enabled(1);
}
sub activate_about {
my $about = Gtk4::AboutDialog->new;
$about->set_program_name('GEmeTool');
$about->set_copyright("© Sergio Iglesias (2024)");
$about->set_license_type('gpl-3-0');
$about->add_credit_section( 'Ideas:', ['SpectreSpecs'] );
$about->set_authors( ['Sergio Iglesias'] );
eval {
my $texture = Gdk::Texture->new_from_filename(
path(__FILE__)->parent->parent->parent->parent->child(
'resources/icons/hicolor/scalable/apps/gemetool.svg'),
);
$about->set_logo($texture);
};
if ($@) {
warn $@;
}
$about->set_website('https://git.owlcode.tech/sergiotarxz/GEmeTool');
$about->set_website_label('https://git.owlcode.tech/sergiotarxz/GEmeTool');
# my $header_bar = Gtk4::HeaderBar->new;
# $header_bar->set_property(
# 'title-widget' => Gtk4::Label->new('About GEmeTool') );
# $about->set_titlebar($header_bar);
$about->present;
}
1;