First try to interact with buttons and CharacterSheet

This commit is contained in:
Pcornat 2026-02-06 17:26:09 +01:00
commit f4cbc2558b
Signed by: Pcornat
GPG key ID: E0326CC678A00BDD
3 changed files with 193 additions and 117 deletions

View file

@ -4,7 +4,7 @@
#include <billy_objects.hpp>
#include <glibmm/miscutils.h>
#include <gtkmm/switch.h>
#include <gtkmm/label.h>
#include <spdlog/spdlog.h>
#include <ranges>
@ -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<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();
@ -67,69 +169,13 @@ namespace gui_to_app {
on_quit();
return;
}
sword_switch = app_builder->get_widget<Gtk::Switch>("sword_switch");
lance_switch = app_builder->get_widget<Gtk::Switch>("lance_switch");
morgen_switch = app_builder->get_widget<Gtk::Switch>("morgenstern_switch");
bow_switch = app_builder->get_widget<Gtk::Switch>("arc_switch");
chainmail_switch = app_builder->get_widget<Gtk::Switch>("chainmail_switch");
cookpot_switch = app_builder->get_widget<Gtk::Switch>("cookingpot_switch");
pamphlet_switch = app_builder->get_widget<Gtk::Switch>("pamphlet_tourist_switch");
medkit_switch = app_builder->get_widget<Gtk::Switch>("medkit_switch");
fourche_switch = app_builder->get_widget<Gtk::Switch>("fourche_switch");
dagger_switch = app_builder->get_widget<Gtk::Switch>("dagger_switch");
rock_kit_switch = app_builder->get_widget<Gtk::Switch>("rock_kit_switch");
sack_switch = app_builder->get_widget<Gtk::Switch>("sack_switch");
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();
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<Gtk::Switch *, SwitchSignalHelper &> &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<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 {
@ -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

View file

@ -8,7 +8,8 @@
#include <billy_objects.hpp>
#include <character_sheet.hpp>
#include <gtkmm/switch.h>
// #include <gtkmm/label.h>
#include <gtkmm/checkbutton.h>
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<selection_button *>;
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<Gtk::Builder> 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<SwitchSignalHelper, 12> 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{};
};

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.96.3 -->
<interface>
<!-- interface-name window_ui.ui -->
<requires lib="gtk" version="4.6"/>
<object class="GtkApplicationWindow" id="main_window">
<property name="default-height">500</property>
@ -17,7 +19,7 @@
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">HABILETÉ</property>
<property name="label" translatable="yes">HABILETÉ</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -51,7 +53,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<property name="label" translatable="yes">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
@ -70,7 +72,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<property name="label" translatable="yes">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
@ -122,7 +124,7 @@
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">ENDURANCE</property>
<property name="label" translatable="yes">ENDURANCE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -156,7 +158,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<property name="label" translatable="yes">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
@ -175,7 +177,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<property name="label" translatable="yes">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
@ -229,7 +231,7 @@
<object class="GtkLabel">
<property name="hexpand">1</property>
<property name="justify">center</property>
<property name="label" translatable="1">MATÉRIEL</property>
<property name="label" translatable="yes">MATÉRIEL</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -242,7 +244,7 @@
<object class="GtkListBoxRow">
<property name="child">
<object class="GtkLabel">
<property name="label" translatable="1">Armes</property>
<property name="label" translatable="yes">Armes</property>
</object>
</property>
</object>
@ -254,13 +256,13 @@
<child>
<object class="GtkLabel">
<property name="justify">fill</property>
<property name="label" translatable="1">Épée</property>
<property name="label" translatable="yes">Épée</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="sword_switch">
<object class="GtkCheckButton" id="sword_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -273,13 +275,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Lance</property>
<property name="label" translatable="yes">Lance</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="lance_switch">
<object class="GtkCheckButton" id="lance_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -292,13 +294,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Morgenstern</property>
<property name="label" translatable="yes">Morgenstern</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="morgenstern_switch">
<object class="GtkCheckButton" id="morgenstern_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -311,13 +313,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Arc</property>
<property name="label" translatable="yes">Arc</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="arc_switch">
<object class="GtkCheckButton" id="arc_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -328,7 +330,7 @@
<object class="GtkListBoxRow">
<property name="child">
<object class="GtkLabel">
<property name="label" translatable="1">Équipements</property>
<property name="label" translatable="yes">Équipements</property>
</object>
</property>
</object>
@ -339,13 +341,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Cote de maille</property>
<property name="label" translatable="yes">Cote de maille</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="chainmail_switch">
<object class="GtkCheckButton" id="chainmail_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -358,13 +360,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Faitout cuisine</property>
<property name="label" translatable="yes">Faitout cuisine</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="cookingpot_switch">
<object class="GtkCheckButton" id="cookingpot_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -377,13 +379,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Guide touristique</property>
<property name="label" translatable="yes">Guide touristique</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="pamphlet_tourist_switch">
<object class="GtkCheckButton" id="pamphlet_tourist_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -396,13 +398,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Trousse de soin</property>
<property name="label" translatable="yes">Trousse de soin</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="medkit_switch">
<object class="GtkCheckButton" id="medkit_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -413,7 +415,7 @@
<object class="GtkListBoxRow">
<property name="child">
<object class="GtkLabel">
<property name="label" translatable="1">Outils</property>
<property name="label" translatable="yes">Outils</property>
</object>
</property>
</object>
@ -424,13 +426,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Fourche</property>
<property name="label" translatable="yes">Fourche</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="fourche_switch">
<object class="GtkCheckButton" id="fourche_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -443,13 +445,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Dague</property>
<property name="label" translatable="yes">Dague</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="dagger_switch">
<object class="GtkCheckButton" id="dagger_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -462,13 +464,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Kit d&apos;escalade</property>
<property name="label" translatable="yes">Kit d'escalade</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="rock_kit_switch">
<object class="GtkCheckButton" id="rock_kit_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -481,13 +483,13 @@
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Bourse de graines</property>
<property name="label" translatable="yes">Bourse de graines</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="sack_switch">
<object class="GtkCheckButton" id="sack_button">
<property name="halign">end</property>
<property name="hexpand">1</property>
<property name="hexpand">True</property>
</object>
</child>
</object>
@ -512,7 +514,7 @@
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">ADRESSE</property>
<property name="label" translatable="yes">ADRESSE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -546,7 +548,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<property name="label" translatable="yes">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
@ -565,7 +567,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<property name="label" translatable="yes">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
@ -617,7 +619,7 @@
<child>
<object class="GtkLabel">
<property name="justify">center</property>
<property name="label" translatable="1">CHANCE</property>
<property name="label" translatable="yes">CHANCE</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -651,7 +653,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Carac.</property>
<property name="label" translatable="yes">Carac.</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
@ -670,7 +672,7 @@
<child>
<object class="GtkLabel">
<property name="halign">start</property>
<property name="label" translatable="1">Matériel</property>
<property name="label" translatable="yes">Matériel</property>
<layout>
<property name="column">0</property>
<property name="row">2</property>
@ -727,7 +729,7 @@
<object class="GtkLabel">
<property name="hexpand">1</property>
<property name="justify">center</property>
<property name="label" translatable="1">STAT. SECONDAIRES</property>
<property name="label" translatable="yes">STAT. SECONDAIRES</property>
<property name="valign">center</property>
<layout>
<property name="column">0</property>
@ -747,7 +749,7 @@
<child>
<object class="GtkLabel">
<property name="halign">center</property>
<property name="label" translatable="1">Dégâts</property>
<property name="label" translatable="yes">Dégâts</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -777,7 +779,7 @@
<child>
<object class="GtkLabel">
<property name="halign">center</property>
<property name="label" translatable="1">Armure</property>
<property name="label" translatable="yes">Armure</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
@ -805,7 +807,7 @@
<object class="GtkGrid">
<child>
<object class="GtkLabel">
<property name="label" translatable="1">Critique</property>
<property name="label" translatable="yes">Critique</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>