diff --git a/app_win_2_back.cpp b/app_win_2_back.cpp index 9831f1b..e8e5271 100644 --- a/app_win_2_back.cpp +++ b/app_win_2_back.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include @@ -15,6 +15,7 @@ namespace gui_to_app { 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() { @@ -41,6 +42,107 @@ namespace gui_to_app { 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("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; + } + // 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 &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(); @@ -67,69 +169,13 @@ namespace gui_to_app { on_quit(); return; } - sword_switch = app_builder->get_widget("sword_switch"); - lance_switch = app_builder->get_widget("lance_switch"); - morgen_switch = app_builder->get_widget("morgenstern_switch"); - bow_switch = app_builder->get_widget("arc_switch"); - chainmail_switch = app_builder->get_widget("chainmail_switch"); - cookpot_switch = app_builder->get_widget("cookingpot_switch"); - pamphlet_switch = app_builder->get_widget("pamphlet_tourist_switch"); - medkit_switch = app_builder->get_widget("medkit_switch"); - fourche_switch = app_builder->get_widget("fourche_switch"); - dagger_switch = app_builder->get_widget("dagger_switch"); - rock_kit_switch = app_builder->get_widget("rock_kit_switch"); - sack_switch = app_builder->get_widget("sack_switch"); - - 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(); + if (switches_procedure()) { return; } - // 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), - [](const std::tuple &vals) { - const auto &[switch_val, signal_handle] = vals; - signal_handle.switch_ = switch_val; - switch_val->signal_state_set().connect( - sigc::mem_fun(signal_handle, &SwitchSignalHelper::signal_handler), - false); - }); + 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"); } void AppWin2Back::on_quit() noexcept { @@ -144,4 +190,12 @@ namespace gui_to_app { 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 diff --git a/app_win_2_back.hpp b/app_win_2_back.hpp index 7f950dd..2b6d61f 100644 --- a/app_win_2_back.hpp +++ b/app_win_2_back.hpp @@ -8,7 +8,8 @@ #include #include -#include +// #include +#include namespace learn_gtkmm4 { class HelloWorld; @@ -16,7 +17,7 @@ namespace learn_gtkmm4 { namespace Gtk { class Builder; - class Switch; + class Label; } namespace gui_to_app { @@ -34,39 +35,56 @@ namespace gui_to_app { void on_activate() final; private: + using selection_button = Gtk::CheckButton; + using buttons_obj_container = std::unordered_set; + + struct SwitchSignalHelper { AppWin2Back &app; const character::billyEnums obj; - Gtk::Switch *switch_{ nullptr }; + selection_button *switch_{ nullptr }; + bool on_stack{ false }; - [[nodiscard]] bool signal_handler(const bool flag) const noexcept { - switch_->set_state(app.switch_signal_handler(flag, obj)); - return true; - } + void signal_handler() noexcept; }; friend struct SwitchSignalHelper; + /*! + * + * @return true: failed, false: success + */ + bool switches_procedure(); + void on_quit() noexcept; bool switch_signal_handler(bool flag, const character::billyEnums &obj) noexcept; + bool insert_obj(const character::billyEnums &obj) noexcept; + + bool erase_obj(const character::billyEnums &obj) noexcept; + Glib::RefPtr app_builder; learn_gtkmm4::HelloWorld *main_window{ nullptr }; - Gtk::Switch *sword_switch{ nullptr }; - Gtk::Switch *lance_switch{ nullptr }; - Gtk::Switch *morgen_switch{ nullptr }; - Gtk::Switch *bow_switch{ nullptr }; + selection_button *sword_switch{ nullptr }; + selection_button *lance_switch{ nullptr }; + selection_button *morgen_switch{ nullptr }; + selection_button *bow_switch{ nullptr }; - Gtk::Switch *chainmail_switch{ nullptr }; - Gtk::Switch *cookpot_switch{ nullptr }; - Gtk::Switch *pamphlet_switch{ nullptr }; - Gtk::Switch *medkit_switch{ nullptr }; + selection_button *chainmail_switch{ nullptr }; + selection_button *cookpot_switch{ nullptr }; + selection_button *pamphlet_switch{ nullptr }; + selection_button *medkit_switch{ nullptr }; - Gtk::Switch *fourche_switch{ nullptr }; - Gtk::Switch *dagger_switch{ nullptr }; - Gtk::Switch *rock_kit_switch{ nullptr }; - Gtk::Switch *sack_switch{ nullptr }; + selection_button *fourche_switch{ nullptr }; + selection_button *dagger_switch{ nullptr }; + selection_button *rock_kit_switch{ nullptr }; + selection_button *sack_switch{ nullptr }; + + Gtk::Label *hab_base{ nullptr }; + Gtk::Label *hab_carac{ nullptr }; + Gtk::Label *hab_mat{ nullptr }; + Gtk::Label *hab_total{ nullptr }; std::array signal_handlers{ SwitchSignalHelper{ .app = *this, .obj = character::weapons::Sword }, @@ -85,6 +103,8 @@ namespace gui_to_app { SwitchSignalHelper{ .app = *this, .obj = character::tools::SackOfGrain }, }; + buttons_obj_container selection_buttons; + character::BillyObjects gestionnaire{}; character::CharacterSheet sheet{}; }; diff --git a/window_ui.ui b/window_ui.ui index dae0b80..50ac0a2 100644 --- a/window_ui.ui +++ b/window_ui.ui @@ -1,5 +1,7 @@ - + + + 500 @@ -17,7 +19,7 @@ center - HABILETÉ + HABILETÉ 0 0 @@ -51,7 +53,7 @@ start - Carac. + Carac. 0 1 @@ -70,7 +72,7 @@ start - Matériel + Matériel 0 2 @@ -122,7 +124,7 @@ center - ENDURANCE + ENDURANCE 0 0 @@ -156,7 +158,7 @@ start - Carac. + Carac. 0 1 @@ -175,7 +177,7 @@ start - Matériel + Matériel 0 2 @@ -229,7 +231,7 @@ 1 center - MATÉRIEL + MATÉRIEL 0 0 @@ -242,7 +244,7 @@ - Armes + Armes @@ -254,13 +256,13 @@ fill - Épée + Épée - + end - 1 + True @@ -273,13 +275,13 @@ - Lance + Lance - + end - 1 + True @@ -292,13 +294,13 @@ - Morgenstern + Morgenstern - + end - 1 + True @@ -311,13 +313,13 @@ - Arc + Arc - + end - 1 + True @@ -328,7 +330,7 @@ - Équipements + Équipements @@ -339,13 +341,13 @@ - Cote de maille + Cote de maille - + end - 1 + True @@ -358,13 +360,13 @@ - Faitout cuisine + Faitout cuisine - + end - 1 + True @@ -377,13 +379,13 @@ - Guide touristique + Guide touristique - + end - 1 + True @@ -396,13 +398,13 @@ - Trousse de soin + Trousse de soin - + end - 1 + True @@ -413,7 +415,7 @@ - Outils + Outils @@ -424,13 +426,13 @@ - Fourche + Fourche - + end - 1 + True @@ -443,13 +445,13 @@ - Dague + Dague - + end - 1 + True @@ -462,13 +464,13 @@ - Kit d'escalade + Kit d'escalade - + end - 1 + True @@ -481,13 +483,13 @@ - Bourse de graines + Bourse de graines - + end - 1 + True @@ -512,7 +514,7 @@ center - ADRESSE + ADRESSE 0 0 @@ -546,7 +548,7 @@ start - Carac. + Carac. 0 1 @@ -565,7 +567,7 @@ start - Matériel + Matériel 0 2 @@ -617,7 +619,7 @@ center - CHANCE + CHANCE 0 0 @@ -651,7 +653,7 @@ start - Carac. + Carac. 0 1 @@ -670,7 +672,7 @@ start - Matériel + Matériel 0 2 @@ -727,7 +729,7 @@ 1 center - STAT. SECONDAIRES + STAT. SECONDAIRES center 0 @@ -747,7 +749,7 @@ center - Dégâts + Dégâts 0 0 @@ -777,7 +779,7 @@ center - Armure + Armure 0 0 @@ -805,7 +807,7 @@ - Critique + Critique 0 0