From 1beebda470d6665df30c9f165f075067a47ad97d Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 23 Jun 2024 22:18:46 +0200 Subject: Added support for text formatting and links to NPC dialog Use BrowserBox in NpcDialog to enable the use of text formatting and links in NPC texts. This change is roughly based on ManaPlus commit 94f11a223e03c6845e7ce6e9fe67c0e9fa7061f4. --- src/gui/npcdialog.cpp | 43 +++++++++++++++++++------------------------ src/gui/npcdialog.h | 20 ++++++++------------ 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index 90d3e37e..376813f7 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -28,14 +28,14 @@ #include "playerinfo.h" #include "gui/npcpostdialog.h" -#include "gui/setup.h" +#include "gui/widgets/browserbox.h" #include "gui/widgets/button.h" #include "gui/widgets/inttextfield.h" +#include "gui/widgets/itemlinkhandler.h" #include "gui/widgets/layout.h" #include "gui/widgets/listbox.h" #include "gui/widgets/scrollarea.h" -#include "gui/widgets/textbox.h" #include "gui/widgets/textfield.h" #include "net/net.h" @@ -69,9 +69,10 @@ static NpcEventListener *npcListener = nullptr; NpcDialog::DialogList NpcDialog::instances; NpcDialog::NpcDialog(int npcId) - : Window(_("NPC")), - mNpcId(npcId), - mLogInteraction(config.getBoolValue("logNpcInGui")) + : Window(_("NPC")) + , mNpcId(npcId) + , mLogInteraction(config.getBoolValue("logNpcInGui")) + , mItemLinkHandler(std::make_unique(this)) { // Basic Window Setup setWindowName("NpcText"); @@ -85,9 +86,8 @@ NpcDialog::NpcDialog(int npcId) setDefaultSize(260, 200, ImageRect::CENTER); // Setup output text box - mTextBox = new TextBox; - mTextBox->setEditable(false); - mTextBox->setOpaque(false); + mTextBox = new BrowserBox(BrowserBox::AUTO_WRAP); + mTextBox->setLinkHandler(mItemLinkHandler.get()); mScrollArea = new ScrollArea(mTextBox); mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -104,7 +104,7 @@ NpcDialog::NpcDialog(int npcId) mItemList->setVisible(true); // Setup string input box - mTextField = new TextField(std::string()); + mTextField = new TextField; mTextField->setVisible(true); // Setup int input box @@ -164,18 +164,19 @@ NpcDialog::~NpcDialog() npcListener->removeDialog(mNpcId); } -void NpcDialog::setText(const std::string &text) +void NpcDialog::setText(const std::vector &text) { - mText = text; - mTextBox->setTextWrapped(mText, mScrollArea->getWidth() - 15); + mTextBox->clearRows(); + for (const std::string &row : text) + mTextBox->addRow(row); } void NpcDialog::addText(const std::string &text, bool save) { if (save || mLogInteraction) { - mNewText += text + "\n"; - setText(mText + text + "\n"); + mNewText.push_back(text); + mTextBox->addRow(text); } mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll()); mActionState = NPC_ACTION_WAIT; @@ -235,13 +236,14 @@ void NpcDialog::action(const gcn::ActionEvent &event) Net::getNpcHandler()->integerInput(mNpcId, mIntField->getValue()); } // addText will auto remove the input layout - addText(strprintf("\n> \"%s\"\n", printText.c_str()), false); + addText(strprintf("> \"%s\"", printText.c_str()), false); + addText(std::string(), false); mNewText.clear(); } if (!mLogInteraction) - setText(std::string()); + setText({}); } else if (event.getId() == "reset") { @@ -371,13 +373,6 @@ void NpcDialog::move(int amount) } } -void NpcDialog::widgetResized(const gcn::Event &event) -{ - Window::widgetResized(event); - - setText(mText); -} - void NpcDialog::setVisible(bool visible) { Window::setVisible(visible); @@ -587,7 +582,7 @@ void NpcEventListener::event(Event::Channel channel, else if (event.getType() == Event::ClearDialog) { if (NpcDialog *dialog = getDialog(event.getInt("id"), false)) - dialog->setText(std::string()); + dialog->setText({}); } else if (event.getType() == Event::Close) { diff --git a/src/gui/npcdialog.h b/src/gui/npcdialog.h index 36fda2b1..b902f044 100644 --- a/src/gui/npcdialog.h +++ b/src/gui/npcdialog.h @@ -30,13 +30,15 @@ #include #include +#include #include #include -class TextBox; +class BrowserBox; class ListBox; class TextField; class IntTextField; +class ItemLinkHandler; class Button; /** @@ -64,7 +66,7 @@ class NpcDialog : public Window, * * @param string The new text. */ - void setText(const std::string &string); + void setText(const std::vector &string); /** * Adds the text to the text shows in the dialog. Also adds a newline @@ -139,13 +141,6 @@ class NpcDialog : public Window, void move(int amount); - /** - * Called when resizing the window. - * - * @param event The calling event - */ - void widgetResized(const gcn::Event &event) override; - void setVisible(bool visible) override; void event(Event::Channel channel, const Event &event) override; @@ -182,9 +177,10 @@ class NpcDialog : public Window, // Used for the main input area gcn::ScrollArea *mScrollArea; - TextBox *mTextBox; - std::string mText; - std::string mNewText; + BrowserBox *mTextBox; + std::vector mNewText; + + std::unique_ptr mItemLinkHandler; // Used for choice input ListBox *mItemList; -- cgit v1.2.3-70-g09d2