From 6352e8e3196f78388b6c771073f9e03eaa612673 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:33:09 +1000 Subject: everything everywhere all at once --- source/frontend/StarCodexInterface.cpp | 159 +++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 source/frontend/StarCodexInterface.cpp (limited to 'source/frontend/StarCodexInterface.cpp') diff --git a/source/frontend/StarCodexInterface.cpp b/source/frontend/StarCodexInterface.cpp new file mode 100644 index 0000000..f841c5d --- /dev/null +++ b/source/frontend/StarCodexInterface.cpp @@ -0,0 +1,159 @@ +#include "StarCodexInterface.hpp" +#include "StarCodex.hpp" +#include "StarGuiReader.hpp" +#include "StarRoot.hpp" +#include "StarPlayer.hpp" +#include "StarLabelWidget.hpp" +#include "StarListWidget.hpp" +#include "StarStackWidget.hpp" +#include "StarImageWidget.hpp" +#include "StarButtonWidget.hpp" +#include "StarButtonGroup.hpp" +#include "StarAssets.hpp" + +namespace Star { + +CodexInterface::CodexInterface(PlayerPtr player) { + m_player = player; + + auto assets = Root::singleton().assets(); + + GuiReader reader; + + reader.registerCallback("close", [=](Widget*) { dismiss(); }); + reader.registerCallback("prevButton", [=](Widget*) { backwardPage(); }); + reader.registerCallback("nextButton", [=](Widget*) { forwardPage(); }); + reader.registerCallback("selectCodex", [=](Widget*) { showSelectedContents(); }); + reader.registerCallback("updateSpecies", [=](Widget*) { updateSpecies(); }); + + reader.construct(assets->json("/interface/windowconfig/codex.config:paneLayout"), this); + + m_speciesTabs = fetchChild("speciesTabs"); + m_selectLabel = fetchChild("selectLabel"); + m_titleLabel = fetchChild("titleLabel"); + m_bookList = fetchChild("scrollArea.bookList"); + m_pageContent = fetchChild("pageText"); + m_pageLabelWidget = fetchChild("pageLabel"); + m_pageNumberWidget = fetchChild("pageNum"); + m_prevPageButton = fetchChild("prevButton"); + m_nextPageButton = fetchChild("nextButton"); + + m_selectText = assets->json("/interface/windowconfig/codex.config:selectText").toString(); + + m_currentPage = 0; + updateSpecies(); + setupPageText(); +} + +void CodexInterface::show() { + Pane::show(); + updateCodexList(); +} + +void CodexInterface::tick() { + updateCodexList(); +} + +void CodexInterface::showSelectedContents() { + if (m_bookList->selectedItem() == NPos || m_bookList->selectedItem() >= m_codexList.size()) + return; + + showContents(m_codexList[m_bookList->selectedItem()].first); +} + +void CodexInterface::showContents(String const& codexId) { + CodexConstPtr result; + for (auto entry : m_codexList) + if (entry.first->id() == codexId) { + result = entry.first; + break; + } + if (result) + showContents(result); +} + +void CodexInterface::showContents(CodexConstPtr codex) { + if (m_player->codexes()->markCodexRead(codex->id())) + updateCodexList(); + m_currentCodex = codex; + m_currentPage = 0; + setupPageText(); +} + +void CodexInterface::forwardPage() { + if (m_currentCodex && m_currentPage < m_currentCodex->pageCount() - 1) { + ++m_currentPage; + setupPageText(); + } +} + +void CodexInterface::backwardPage() { + if (m_currentCodex && m_currentPage > 0) { + --m_currentPage; + setupPageText(); + } +} + +bool CodexInterface::showNewCodex() { + if (auto newCodex = m_player->codexes()->firstNewCodex()) { + for (auto button : m_speciesTabs->buttons()) { + if (button->data().getString("species") == newCodex->species()) { + m_speciesTabs->select(m_speciesTabs->id(button)); + break; + } + } + showContents(newCodex); + return true; + } + + return false; +} + +void CodexInterface::updateSpecies() { + String newSpecies = "other"; + if (auto speciesButton = m_speciesTabs->checkedButton()) + newSpecies = speciesButton->data().getString("species"); + if (newSpecies != m_currentSpecies) { + m_currentCodex = {}; + m_currentSpecies = newSpecies; + m_bookList->clearSelected(); + setupPageText(); + } + m_selectLabel->setText(m_selectText.replaceTags(StringMap{{"species", m_currentSpecies}}).titleCase()); +} + +void CodexInterface::setupPageText() { + if (m_currentCodex) { + m_pageContent->setText(m_currentCodex->page(m_currentPage)); + m_pageLabelWidget->show(); + m_pageNumberWidget->setText(strf("%d of %d", m_currentPage + 1, m_currentCodex->pageCount())); + m_titleLabel->setText(m_currentCodex->title()); + m_nextPageButton->setEnabled(m_currentPage < m_currentCodex->pageCount() - 1); + m_prevPageButton->setEnabled(m_currentPage > 0); + } else { + m_pageContent->setText(""); + m_pageLabelWidget->hide(); + m_pageNumberWidget->setText(""); + m_titleLabel->setText(""); + m_nextPageButton->disable(); + m_prevPageButton->disable(); + } +} + +void CodexInterface::updateCodexList() { + auto newCodexList = m_player->codexes()->codexes(); + filter(newCodexList, [&](auto const& p) { + return p.first->species() == m_currentSpecies; + }); + if (m_codexList != newCodexList) { + m_bookList->removeAllChildren(); + m_codexList = newCodexList; + for (auto entry : m_codexList) { + auto newEntry = m_bookList->addItem(); + newEntry->fetchChild("bookName")->setText(entry.first->title()); + newEntry->fetchChild("bookIcon")->setImage(entry.first->icon()); + } + } +} + +} -- cgit v1.2.3