From 71fe1d395a3c1d94dac68469826118e357f7086d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 9 May 2025 10:48:30 +0200 Subject: core: rename turn_order to TurnOderModel --- core/src/init.cpp | 4 +- core/src/turn_order.cpp | 312 ------------------------------------------ core/src/turn_order_model.cpp | 312 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 314 insertions(+), 314 deletions(-) delete mode 100644 core/src/turn_order.cpp create mode 100644 core/src/turn_order_model.cpp (limited to 'core/src') diff --git a/core/src/init.cpp b/core/src/init.cpp index 3434f91..4f67817 100644 --- a/core/src/init.cpp +++ b/core/src/init.cpp @@ -2,7 +2,7 @@ #include "turns/core/disposition.hpp" #include "turns/core/participant.hpp" -#include "turns/core/turn_order.hpp" +#include "turns/core/turn_order_model.hpp" #include @@ -12,7 +12,7 @@ namespace turns::core auto register_types() -> void { static_cast(Participant{}); - static_cast(turn_order{}); + static_cast(TurnOderModel{}); g_type_ensure(Glib::Value::value_type()); } diff --git a/core/src/turn_order.cpp b/core/src/turn_order.cpp deleted file mode 100644 index 669f746..0000000 --- a/core/src/turn_order.cpp +++ /dev/null @@ -1,312 +0,0 @@ -#include "turns/core/turn_order.hpp" - -#include "turns/core/disposition.hpp" -#include "turns/core/json_ext.hpp" -#include "turns/core/participant.hpp" - -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -using namespace std::placeholders; - -namespace turns::core -{ - - namespace - { - auto constexpr comparator = [](auto lhs, auto rhs) { - return *lhs > *rhs; - }; - } // namespace - - /** Construction */ - - turn_order::turn_order() - : Glib::ObjectBase{typeid(turn_order)} - , Gio::ListModel{} - { - } - - turn_order::turn_order(nlohmann::json const & from) - : turn_order{} - { - load(from); - } - - auto turn_order::create() -> Glib::RefPtr - { - return Glib::make_refptr_for_instance(new turn_order{}); - } - - auto turn_order::create(nlohmann::json const & from) -> Glib::RefPtr - { - return Glib::make_refptr_for_instance(new turn_order{from}); - } - - /** Queries */ - - auto turn_order::is_empty() const -> Glib::PropertyProxy_ReadOnly - { - return m_is_empty.get_proxy(); - } - - auto turn_order::has_next() const -> Glib::PropertyProxy_ReadOnly - { - return m_has_next.get_proxy(); - } - - auto turn_order::has_previous() const -> Glib::PropertyProxy_ReadOnly - { - return m_has_previous.get_proxy(); - } - - auto turn_order::is_running() const -> Glib::PropertyProxy_ReadOnly - { - return m_is_running.get_proxy(); - } - - auto turn_order::progress() const -> Glib::PropertyProxy_ReadOnly - { - return m_progress.get_proxy(); - } - - auto turn_order::round_number() const -> Glib::PropertyProxy_ReadOnly - { - return m_round_number.get_proxy(); - } - - auto turn_order::skip_defeated() -> Glib::PropertyProxy - { - return m_skip_defeated.get_proxy(); - } - - /** Modifiers */ - - auto turn_order::add(Glib::ustring const & name, float priority, Disposition disposition) -> void - { - auto entry = Participant::create(name, priority, disposition); - entry->property_priority().signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &turn_order::handle_priority_changed), entry)); - auto position = std::distance(m_data.cbegin(), insert(entry)); - items_changed(position, 0, 1); - - if (get_n_items() == 1) - { - m_is_empty = false; - m_has_next = true; - } - } - - auto turn_order::clear() -> void - { - m_is_running = false; - m_is_empty = true; - m_has_next = false; - m_has_previous = false; - m_active.reset(); - m_round_number = invalid_round_number; - m_progress = 0; - - auto old_size = get_n_items(); - m_data.clear(); - items_changed(0, old_size, 0); - } - - auto turn_order::next() -> void - { - auto old_active = *m_active; - m_active = m_active.transform([this](auto index) { return (index + 1) % get_n_items(); }); - - m_has_previous = true; - m_data[old_active]->property_is_active() = false; - m_data[*m_active]->property_is_active() = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - - if (m_active == 0) - { - m_round_number = m_round_number + 1; - } - } - - auto turn_order::previous() -> void - { - if (!(m_has_previous && m_is_running)) - { - return; - } - - auto old_active = *m_active; - m_active = m_active.transform([this](auto index) { return index ? index - 1 : get_n_items() - 1; }); - - m_has_previous = m_round_number > 0 || m_active > 0; - m_data[old_active]->property_is_active() = false; - m_data[*m_active]->property_is_active() = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - - if (m_active == 0 && m_round_number > 0) - { - m_round_number = m_round_number - 1; - } - } - - auto turn_order::remove(unsigned index) -> void - { - if (index >= get_n_items()) - { - return; - } - - auto position = m_data.begin() + index; - m_data.erase(position); - items_changed(index, 1, 0); - if (get_n_items() == 0) - { - m_is_empty = true; - m_is_running = false; - m_has_next = false; - } - } - - auto turn_order::start() -> void - { - if (!m_active) - { - m_active = 0; - m_data[*m_active]->property_is_active() = true; - } - if (m_round_number == invalid_round_number) - { - m_round_number = 0; - } - m_is_running = true; - - m_progress = (static_cast(*m_active) + 1) / get_n_items(); - } - - auto turn_order::stop() -> void - { - m_is_running = false; - m_progress = 0; - } - - /** Serialization */ - auto turn_order::load(nlohmann::json const & from) -> void - { - auto old_size = get_n_items(); - - this->freeze_notify(); - - m_round_number = from.value("round", invalid_round_number); - - m_data.clear(); - auto participants = from.value("participants", std::vector{}); - auto factory = [](auto s) { - return Participant::create(s); - }; - auto inserter = std::bind(&turn_order::insert, this, _1); - std::ranges::for_each(participants | std::views::transform(factory), inserter); - - auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->property_is_active(); }); - if (active != std::ranges::end(m_data)) - { - m_active = std::ranges::distance(std::ranges::begin(m_data), active); - } - - m_is_empty = m_data.empty(); - m_has_next = !m_is_empty; - m_has_previous = m_round_number > 0 || m_active > 0; - - this->thaw_notify(); - - items_changed(0, old_size, get_n_items()); - } - - auto turn_order::serialize() -> nlohmann::json - { - auto serialized = nlohmann::json{}; - if (m_round_number != invalid_round_number) - { - serialized["round"] = m_round_number; - } - serialized["participants"] = m_data | std::views::transform([](auto const & p) { return p->serialize(); }) | std::ranges::to(); - ; - return serialized; - } - - /** ListModel implementation */ - - auto turn_order::get_item_type_vfunc() -> GType - { - return Participant::get_type(); - } - - auto turn_order::get_n_items_vfunc() -> unsigned - { - return m_data.size(); - } - - auto turn_order::get_item_vfunc(unsigned position) -> void * - { - if (position >= get_n_items()) - { - return nullptr; - } - auto item = m_data[position]; - item->reference(); - return Glib::unwrap(item); - } - - /** Signal handlers */ - - auto turn_order::handle_priority_changed(value_type entry) -> void - { - auto original_position = find(entry); - auto original_index = distance(m_data.cbegin(), original_position); - auto target_position = std::ranges::upper_bound(m_data, entry, comparator); - if (original_position == target_position) - { - return; - } - - m_data.erase(original_position); - auto inserted_position = insert(entry); - items_changed(0, get_n_items(), get_n_items()); - if (m_active == original_index) - { - m_active = distance(m_data.cbegin(), inserted_position); - m_has_previous = m_round_number > 0 || m_active > 0; - } - } - - /** Data management */ - - auto turn_order::find(value_type entry) const -> const_iterator - { - return std::ranges::find(m_data, entry); - } - - auto turn_order::insert(value_type entry) -> const_iterator - { - return m_data.insert(std::ranges::upper_bound(m_data, entry, comparator), entry); - } - -} // namespace turns::core \ No newline at end of file diff --git a/core/src/turn_order_model.cpp b/core/src/turn_order_model.cpp new file mode 100644 index 0000000..e430fed --- /dev/null +++ b/core/src/turn_order_model.cpp @@ -0,0 +1,312 @@ +#include "turns/core/turn_order_model.hpp" + +#include "turns/core/disposition.hpp" +#include "turns/core/json_ext.hpp" +#include "turns/core/participant.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace std::placeholders; + +namespace turns::core +{ + + namespace + { + auto constexpr comparator = [](auto lhs, auto rhs) { + return *lhs > *rhs; + }; + } // namespace + + /** Construction */ + + TurnOderModel::TurnOderModel() + : Glib::ObjectBase{typeid(TurnOderModel)} + , Gio::ListModel{} + { + } + + TurnOderModel::TurnOderModel(nlohmann::json const & from) + : TurnOderModel{} + { + load(from); + } + + auto TurnOderModel::create() -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(new TurnOderModel{}); + } + + auto TurnOderModel::create(nlohmann::json const & from) -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(new TurnOderModel{from}); + } + + /** Queries */ + + auto TurnOderModel::is_empty() const -> Glib::PropertyProxy_ReadOnly + { + return m_is_empty.get_proxy(); + } + + auto TurnOderModel::has_next() const -> Glib::PropertyProxy_ReadOnly + { + return m_has_next.get_proxy(); + } + + auto TurnOderModel::has_previous() const -> Glib::PropertyProxy_ReadOnly + { + return m_has_previous.get_proxy(); + } + + auto TurnOderModel::is_running() const -> Glib::PropertyProxy_ReadOnly + { + return m_is_running.get_proxy(); + } + + auto TurnOderModel::progress() const -> Glib::PropertyProxy_ReadOnly + { + return m_progress.get_proxy(); + } + + auto TurnOderModel::round_number() const -> Glib::PropertyProxy_ReadOnly + { + return m_round_number.get_proxy(); + } + + auto TurnOderModel::skip_defeated() -> Glib::PropertyProxy + { + return m_skip_defeated.get_proxy(); + } + + /** Modifiers */ + + auto TurnOderModel::add(Glib::ustring const & name, float priority, Disposition disposition) -> void + { + auto entry = Participant::create(name, priority, disposition); + entry->property_priority().signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &TurnOderModel::handle_priority_changed), entry)); + auto position = std::distance(m_data.cbegin(), insert(entry)); + items_changed(position, 0, 1); + + if (get_n_items() == 1) + { + m_is_empty = false; + m_has_next = true; + } + } + + auto TurnOderModel::clear() -> void + { + m_is_running = false; + m_is_empty = true; + m_has_next = false; + m_has_previous = false; + m_active.reset(); + m_round_number = invalid_round_number; + m_progress = 0; + + auto old_size = get_n_items(); + m_data.clear(); + items_changed(0, old_size, 0); + } + + auto TurnOderModel::next() -> void + { + auto old_active = *m_active; + m_active = m_active.transform([this](auto index) { return (index + 1) % get_n_items(); }); + + m_has_previous = true; + m_data[old_active]->property_is_active() = false; + m_data[*m_active]->property_is_active() = true; + + m_progress = (static_cast(*m_active) + 1) / get_n_items(); + + if (m_active == 0) + { + m_round_number = m_round_number + 1; + } + } + + auto TurnOderModel::previous() -> void + { + if (!(m_has_previous && m_is_running)) + { + return; + } + + auto old_active = *m_active; + m_active = m_active.transform([this](auto index) { return index ? index - 1 : get_n_items() - 1; }); + + m_has_previous = m_round_number > 0 || m_active > 0; + m_data[old_active]->property_is_active() = false; + m_data[*m_active]->property_is_active() = true; + + m_progress = (static_cast(*m_active) + 1) / get_n_items(); + + if (m_active == 0 && m_round_number > 0) + { + m_round_number = m_round_number - 1; + } + } + + auto TurnOderModel::remove(unsigned index) -> void + { + if (index >= get_n_items()) + { + return; + } + + auto position = m_data.begin() + index; + m_data.erase(position); + items_changed(index, 1, 0); + if (get_n_items() == 0) + { + m_is_empty = true; + m_is_running = false; + m_has_next = false; + } + } + + auto TurnOderModel::start() -> void + { + if (!m_active) + { + m_active = 0; + m_data[*m_active]->property_is_active() = true; + } + if (m_round_number == invalid_round_number) + { + m_round_number = 0; + } + m_is_running = true; + + m_progress = (static_cast(*m_active) + 1) / get_n_items(); + } + + auto TurnOderModel::stop() -> void + { + m_is_running = false; + m_progress = 0; + } + + /** Serialization */ + auto TurnOderModel::load(nlohmann::json const & from) -> void + { + auto old_size = get_n_items(); + + this->freeze_notify(); + + m_round_number = from.value("round", invalid_round_number); + + m_data.clear(); + auto participants = from.value("participants", std::vector{}); + auto factory = [](auto s) { + return Participant::create(s); + }; + auto inserter = std::bind(&TurnOderModel::insert, this, _1); + std::ranges::for_each(participants | std::views::transform(factory), inserter); + + auto active = std::ranges::find_if(m_data, [](auto participant) { return participant->property_is_active(); }); + if (active != std::ranges::end(m_data)) + { + m_active = std::ranges::distance(std::ranges::begin(m_data), active); + } + + m_is_empty = m_data.empty(); + m_has_next = !m_is_empty; + m_has_previous = m_round_number > 0 || m_active > 0; + + this->thaw_notify(); + + items_changed(0, old_size, get_n_items()); + } + + auto TurnOderModel::serialize() -> nlohmann::json + { + auto serialized = nlohmann::json{}; + if (m_round_number != invalid_round_number) + { + serialized["round"] = m_round_number; + } + serialized["participants"] = m_data | std::views::transform([](auto const & p) { return p->serialize(); }) | std::ranges::to(); + ; + return serialized; + } + + /** ListModel implementation */ + + auto TurnOderModel::get_item_type_vfunc() -> GType + { + return Participant::get_type(); + } + + auto TurnOderModel::get_n_items_vfunc() -> unsigned + { + return m_data.size(); + } + + auto TurnOderModel::get_item_vfunc(unsigned position) -> void * + { + if (position >= get_n_items()) + { + return nullptr; + } + auto item = m_data[position]; + item->reference(); + return Glib::unwrap(item); + } + + /** Signal handlers */ + + auto TurnOderModel::handle_priority_changed(value_type entry) -> void + { + auto original_position = find(entry); + auto original_index = distance(m_data.cbegin(), original_position); + auto target_position = std::ranges::upper_bound(m_data, entry, comparator); + if (original_position == target_position) + { + return; + } + + m_data.erase(original_position); + auto inserted_position = insert(entry); + items_changed(0, get_n_items(), get_n_items()); + if (m_active == original_index) + { + m_active = distance(m_data.cbegin(), inserted_position); + m_has_previous = m_round_number > 0 || m_active > 0; + } + } + + /** Data management */ + + auto TurnOderModel::find(value_type entry) const -> const_iterator + { + return std::ranges::find(m_data, entry); + } + + auto TurnOderModel::insert(value_type entry) -> const_iterator + { + return m_data.insert(std::ranges::upper_bound(m_data, entry, comparator), entry); + } + +} // namespace turns::core \ No newline at end of file -- cgit v1.2.3