diff --git a/app_win_2_back.cpp b/app_win_2_back.cpp index 0ad2952..dcdb5ce 100644 --- a/app_win_2_back.cpp +++ b/app_win_2_back.cpp @@ -50,6 +50,8 @@ namespace gui_to_app { if (!app.insert_obj(obj)) { on_stack = true; switch_->set_active(false); + } else { + app.update_labels(); } } else { if (!app.erase_obj(obj)) { @@ -59,6 +61,8 @@ namespace gui_to_app { "Erasing object {} was not successful", character::BillyObjects::billy_object_to_string(obj).data() ); + } else { + app.update_labels(); } } } @@ -165,10 +169,24 @@ namespace gui_to_app { 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"); + hab_labels = CaracInterface{ + { + "habilete_label_base", + "habilete_label_carac", + "habilete_label_materiel", + "habilete_label_total" + }, + app_builder + }; + endu_labels = CaracInterface{ + { + "endurance_label_base", + "endurance_label_carac", + "endurance_label_materiel", + "endurance_label_total" + }, + app_builder + }; debug_button->signal_clicked().connect(sigc::mem_fun(*this, &AppWin2Back::debug_button_clicked), false); } @@ -182,17 +200,52 @@ namespace gui_to_app { 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 { + const json j{ { "character_sheet", sheet } }; + spdlog::info("Character sheet in json: \n{}", j.dump(4, ' ', true)); } - 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()); + static void convert_to_label(std::array &convert, + const std::string_view &err_msg, + const uint32_t data, + Gtk::Label *const __restrict label) noexcept { + if (const auto [ptr, ec] = std::to_chars(convert.data(), convert.data() + convert.size(), data); + ec != std::errc()) { + spdlog::error("An error occurred while converting {} value: {}", + err_msg, + std::make_error_code(ec).message()); + } else { + const Glib::ustring converted{ + convert.data(), static_cast(ptr - convert.data()) + }; + label->set_label(converted); } } + static void update_in_labels(const character::characteristic::Characteristic &charac, + const std::string_view short_name, + const CaracInterface &labels) noexcept { + std::array convert{}; + convert_to_label(convert, std::format("base {}", short_name), charac.get_base(), labels.base); + convert_to_label(convert, + std::format("carac {}", short_name), + charac.get_carac(), + labels.carac); + convert_to_label(convert, + std::format("materiel {}", short_name), + charac.get_materiel(), + labels.mat); + convert_to_label(convert, + std::format("total {}", short_name), + charac.get_total(), + labels.total); + } + + void AppWin2Back::update_labels() const noexcept { + update_in_labels(sheet.get_habilete(), "hab", hab_labels); + update_in_labels(sheet.get_endurance(), "endu", endu_labels); + } + bool AppWin2Back::insert_obj(const character::billyEnums &obj) noexcept { return gestionnaire.insert_object(sheet, obj); } diff --git a/app_win_2_back.hpp b/app_win_2_back.hpp index b0f30d5..2238ccf 100644 --- a/app_win_2_back.hpp +++ b/app_win_2_back.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -15,12 +16,28 @@ namespace learn_gtkmm4 { } namespace Gtk { - class Builder; class Label; class Button; } namespace gui_to_app { + template + struct CaracInterface final { + T *base{ nullptr }; + T *carac{ nullptr }; + T *mat{ nullptr }; + T *total{ nullptr }; + + CaracInterface() = default; + + explicit CaracInterface(const std::array &list, + const Glib::RefPtr &builder) : + base(builder->get_widget(list[0].data())), + carac(builder->get_widget(list[1].data())), + mat(builder->get_widget(list[2].data())), + total(builder->get_widget(list[3].data())) {} + }; + class AppWin2Back final : public Gtk::Application { public: ~AppWin2Back() noexcept final = default; @@ -58,10 +75,10 @@ namespace gui_to_app { void on_quit() noexcept; - bool switch_signal_handler(bool flag, const character::billyEnums &obj) noexcept; - void debug_button_clicked() const noexcept; + void update_labels() const noexcept; + bool insert_obj(const character::billyEnums &obj) noexcept; bool erase_obj(const character::billyEnums &obj) noexcept; @@ -83,10 +100,9 @@ namespace gui_to_app { 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 }; + CaracInterface hab_labels; + + CaracInterface endu_labels; Gtk::Button *debug_button{ nullptr }; diff --git a/window_ui.ui b/window_ui.ui index c6cf1fd..a9d7e38 100644 --- a/window_ui.ui +++ b/window_ui.ui @@ -30,6 +30,7 @@ + True start @@ -43,7 +44,8 @@ - end + center + True 1 0 @@ -62,7 +64,7 @@ - end + center 1 1 @@ -81,7 +83,7 @@ - end + center 1 2 @@ -105,6 +107,9 @@ + center + True + center 2 0 @@ -133,8 +138,11 @@ + True + True + True start @@ -148,7 +156,8 @@ - end + center + True 1 0 @@ -167,7 +176,7 @@ - end + center 1 1 @@ -186,7 +195,7 @@ - end + center 1 2 @@ -210,6 +219,8 @@ + center + True 2 0