201 lines
8.9 KiB
C++
201 lines
8.9 KiB
C++
#include "app_win_2_back.hpp"
|
|
#include "hello_world.hpp"
|
|
|
|
#include <billy_objects.hpp>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
#include <gtkmm/label.h>
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <ranges>
|
|
|
|
namespace gui_to_app {
|
|
namespace ranges = std::ranges;
|
|
namespace views = std::views;
|
|
|
|
AppWin2Back::AppWin2Back() : Gtk::Application("org.billy_adventures.character_sheet", Flags::HANDLES_OPEN) {
|
|
Glib::set_application_name("Billy's character sheet");
|
|
selection_buttons.reserve(signal_handlers.size());
|
|
}
|
|
|
|
void AppWin2Back::on_startup() {
|
|
Application::on_startup();
|
|
const auto menu_bar = []() -> Glib::RefPtr<Gio::Menu> {
|
|
try {
|
|
const auto builder = Gtk::Builder::create_from_file("menu_bar.ui");
|
|
return builder->get_object<Gio::Menu>("menu");
|
|
} catch (const std::exception &e) {
|
|
spdlog::error("Error occurred while loader menu bar: {}", e.what());
|
|
return nullptr;
|
|
}
|
|
}();
|
|
if (!menu_bar) {
|
|
spdlog::error("Error occured while loading menu bar's.");
|
|
} else {
|
|
set_menubar(menu_bar);
|
|
add_action("quit", sigc::mem_fun(*this, &AppWin2Back::on_quit));
|
|
set_accel_for_action("app.quit", "<Ctrl>q");
|
|
}
|
|
}
|
|
|
|
Glib::RefPtr<AppWin2Back> AppWin2Back::create() {
|
|
return Glib::make_refptr_for_instance(new AppWin2Back());
|
|
}
|
|
|
|
void AppWin2Back::SwitchSignalHelper::signal_handler() noexcept {
|
|
if (switch_ != nullptr && !on_stack) {
|
|
if (!switch_->get_inconsistent()) {
|
|
if (switch_->get_active()) {
|
|
if (!app.insert_obj(obj)) {
|
|
on_stack = true;
|
|
switch_->set_active(false);
|
|
ranges::for_each(
|
|
app.selection_buttons | views::filter([](const buttons_obj_container::value_type a) {
|
|
return !a->get_active() && !a->get_inconsistent();
|
|
}),
|
|
[](const buttons_obj_container::value_type a) { a->set_inconsistent(); });
|
|
}
|
|
} else {
|
|
const bool was_full = character::BillyObjects::is_full(app.sheet);
|
|
if (!app.erase_obj(obj)) {
|
|
on_stack = true;
|
|
switch_->set_active(true);
|
|
} else if (!character::BillyObjects::is_full(app.sheet) && was_full) {
|
|
ranges::for_each(
|
|
app.selection_buttons | views::filter([](const buttons_obj_container::value_type a) {
|
|
return a->get_inconsistent();
|
|
}),
|
|
[](const buttons_obj_container::value_type a) { a->set_inconsistent(false); });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (on_stack) {
|
|
on_stack = false;
|
|
}
|
|
}
|
|
|
|
bool AppWin2Back::switches_procedure() {
|
|
sword_switch = app_builder->get_widget<selection_button>("sword_button");
|
|
lance_switch = app_builder->get_widget<selection_button>("lance_button");
|
|
morgen_switch = app_builder->get_widget<selection_button>("morgenstern_button");
|
|
bow_switch = app_builder->get_widget<selection_button>("arc_button");
|
|
chainmail_switch = app_builder->get_widget<selection_button>("chainmail_button");
|
|
cookpot_switch = app_builder->get_widget<selection_button>("cookingpot_button");
|
|
pamphlet_switch = app_builder->get_widget<selection_button>("pamphlet_tourist_button");
|
|
medkit_switch = app_builder->get_widget<selection_button>("medkit_button");
|
|
fourche_switch = app_builder->get_widget<selection_button>("fourche_button");
|
|
dagger_switch = app_builder->get_widget<selection_button>("dagger_button");
|
|
rock_kit_switch = app_builder->get_widget<selection_button>("rock_kit_button");
|
|
sack_switch = app_builder->get_widget<selection_button>("sack_button");
|
|
|
|
const auto test_result = ranges::to<std::vector<const char *> >(
|
|
std::array{
|
|
std::pair{ "sword_switch", sword_switch == nullptr },
|
|
std::pair{ "lance_switch", lance_switch == nullptr },
|
|
std::pair{ "morgen_switch", morgen_switch == nullptr },
|
|
std::pair{ "bow_switch", bow_switch == nullptr },
|
|
std::pair{ "chainmail_switch", chainmail_switch == nullptr },
|
|
std::pair{ "cookpot_switch", cookpot_switch == nullptr },
|
|
std::pair{ "pamphlet_switch", pamphlet_switch == nullptr },
|
|
std::pair{ "medkit_switch", medkit_switch == nullptr },
|
|
std::pair{ "fourche_switch", fourche_switch == nullptr },
|
|
std::pair{ "dagger_switch", dagger_switch == nullptr },
|
|
std::pair{ "rock_kit_switch", rock_kit_switch == nullptr },
|
|
std::pair{ "sack_switch", sack_switch == nullptr },
|
|
}
|
|
| views::filter([](const auto &a) { return a.second; })
|
|
| views::keys
|
|
);
|
|
|
|
if (!test_result.empty()) {
|
|
spdlog::critical("Error occurred, at least one switch is not available. See logs below");
|
|
for (const auto result: test_result) {
|
|
spdlog::critical(result);
|
|
}
|
|
on_quit();
|
|
return true;
|
|
}
|
|
// TODO: interaction avec la CharacterSheet
|
|
ranges::for_each(views::zip(std::array{
|
|
sword_switch,
|
|
lance_switch,
|
|
morgen_switch,
|
|
bow_switch,
|
|
chainmail_switch,
|
|
cookpot_switch,
|
|
pamphlet_switch,
|
|
medkit_switch,
|
|
fourche_switch,
|
|
dagger_switch,
|
|
rock_kit_switch,
|
|
sack_switch,
|
|
},
|
|
signal_handlers),
|
|
[this](const std::tuple<selection_button *, SwitchSignalHelper &> &vals) {
|
|
const auto &[switch_val, signal_handle] = vals;
|
|
signal_handle.switch_ = switch_val;
|
|
selection_buttons.emplace(switch_val);
|
|
switch_val->signal_toggled().connect(
|
|
sigc::mem_fun(signal_handle, &SwitchSignalHelper::signal_handler),
|
|
false);
|
|
});
|
|
return false;
|
|
}
|
|
|
|
void AppWin2Back::on_activate() {
|
|
Application::on_activate();
|
|
|
|
app_builder = [] -> Glib::RefPtr<Gtk::Builder> {
|
|
try {
|
|
return Gtk::Builder::create_from_file("window_ui.ui");
|
|
} catch (const std::exception &e) {
|
|
spdlog::critical("Error occurred while loading Window's UI: {}", e.what());
|
|
return nullptr;
|
|
}
|
|
}();
|
|
if (!app_builder) {
|
|
spdlog::critical("Error while loading UI");
|
|
quit();
|
|
return;
|
|
}
|
|
main_window = Gtk::Builder::get_widget_derived<learn_gtkmm4::HelloWorld>(app_builder, "main_window");
|
|
if (main_window != nullptr) {
|
|
add_window(*main_window);
|
|
main_window->set_show_menubar(true);
|
|
main_window->set_visible(true);
|
|
} else {
|
|
spdlog::critical("Error while getting window from builder");
|
|
on_quit();
|
|
return;
|
|
}
|
|
if (switches_procedure()) {
|
|
return;
|
|
}
|
|
hab_base = app_builder->get_widget<Gtk::Label>("habilete_label_base");
|
|
hab_carac = app_builder->get_widget<Gtk::Label>("habilete_label_carac");
|
|
hab_mat = app_builder->get_widget<Gtk::Label>("habilete_label_materiel");
|
|
hab_total = app_builder->get_widget<Gtk::Label>("habilete_label_total");
|
|
}
|
|
|
|
void AppWin2Back::on_quit() noexcept {
|
|
ranges::for_each(get_windows() | views::filter([](const auto windows) { return windows != nullptr; }),
|
|
[](auto *window) {
|
|
window->set_visible(false);
|
|
delete window;
|
|
});
|
|
quit();
|
|
}
|
|
|
|
bool AppWin2Back::switch_signal_handler(const bool flag, const character::billyEnums &obj) noexcept {
|
|
return flag ? gestionnaire.insert_object(sheet, obj) : !gestionnaire.erase_object(sheet, obj);
|
|
}
|
|
|
|
bool AppWin2Back::insert_obj(const character::billyEnums &obj) noexcept {
|
|
return gestionnaire.insert_object(sheet, obj);
|
|
}
|
|
|
|
bool AppWin2Back::erase_obj(const character::billyEnums &obj) noexcept {
|
|
return gestionnaire.erase_object(sheet, obj);
|
|
}
|
|
} // gui_to_app
|