#include "app_win_2_back.hpp" #include "hello_world.hpp" #include #include #include #include #include #include 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 { try { const auto builder = Gtk::Builder::create_from_file("menu_bar.ui"); return builder->get_object("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", "q"); } } Glib::RefPtr AppWin2Back::create() { return Glib::make_refptr_for_instance(new AppWin2Back()); } void AppWin2Back::SwitchSignalHelper::signal_handler() noexcept { if (switch_ != nullptr && !on_stack) { if (switch_->get_active()) { if (!app.insert_obj(obj)) { on_stack = true; switch_->set_active(false); } } else { if (!app.erase_obj(obj)) { on_stack = true; switch_->set_active(true); spdlog::warn( "Erasing object {} was not successful", character::BillyObjects::billy_object_to_string(obj).data() ); } } } if (on_stack) { on_stack = false; } } bool AppWin2Back::switches_procedure() { sword_switch = app_builder->get_widget("sword_button"); lance_switch = app_builder->get_widget("lance_button"); morgen_switch = app_builder->get_widget("morgenstern_button"); bow_switch = app_builder->get_widget("arc_button"); chainmail_switch = app_builder->get_widget("chainmail_button"); cookpot_switch = app_builder->get_widget("cookingpot_button"); pamphlet_switch = app_builder->get_widget("pamphlet_tourist_button"); medkit_switch = app_builder->get_widget("medkit_button"); fourche_switch = app_builder->get_widget("fourche_button"); dagger_switch = app_builder->get_widget("dagger_button"); rock_kit_switch = app_builder->get_widget("rock_kit_button"); sack_switch = app_builder->get_widget("sack_button"); const auto test_result = ranges::to >( 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; } 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 &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 { 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(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; } debug_button = app_builder->get_widget("debug_button"); hab_base = app_builder->get_widget("habilete_label_base"); hab_carac = app_builder->get_widget("habilete_label_carac"); hab_mat = app_builder->get_widget("habilete_label_materiel"); hab_total = app_builder->get_widget("habilete_label_total"); debug_button->signal_clicked().connect(sigc::mem_fun(*this, &AppWin2Back::debug_button_clicked), false); } 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); } void AppWin2Back::debug_button_clicked() const noexcept { spdlog::info("Display objects"); for (const auto &obj: sheet.get_objects() | views::values) { spdlog::info("Object: {}", obj->to_string()); } } 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